"Cannot redefine property" when stubbing module ex...
# i-need-help
b
index.ts:
Copy code
ts
export const testConstant = "initial"
test file:
Copy code
ts
import * as testExports from "./index.ts";
it("changes the module", () => {
  cy.stub(testExports, "testConstant")
    .value("VALUE")
  expect(testExports.testConstant).to.eq("VALUE")
})
Result:
Copy code
js
TypeError: Cannot redefine property: testConstant
    at Function.defineProperty (<anonymous>)
    at Object.value (/__cypress/runner/cy…runner.js:129024:16)
    at proto.<computed> [as value] (/__cypress/runner/cy…runner.js:128601:12)
    at Function.<anonymous> (/__cypress/runner/cy…runner.js:128594:46)
    at runnable.fn (/__cypress/runner/cy…runner.js:160854:19)
    at callFn (/__cypress/runner/cy…runner.js:114387:21)
    at ../driver/node_modules/mocha/lib/runnable.js.Runnable.run (/__cypress/runner/cy…_runner.js:114374:7)
    at <unknown> (/__cypress/runner/cy…runner.js:167842:30)
    at PassThroughHandlerContext.finallyHandler (/__cypress/runner/cy…_runner.js:15298:23)
    at PassThroughHandlerContext.tryCatcher (/__cypress/runner/cy…_runner.js:18744:23)
    at Promise._settlePromiseFromHandler (/__cypress/runner/cy…_runner.js:16679:31)
    at Promise._settlePromise (/__cypress/runner/cy…_runner.js:16736:18)
    at Promise._settlePromise0 (/__cypress/runner/cy…_runner.js:16781:10)
    at Promise._settlePromises (/__cypress/runner/cy…_runner.js:16861:18)
    at Promise._fulfill (/__cypress/runner/cy…_runner.js:16805:18)
    at Promise._settlePromise (/__cypress/runner/cy…_runner.js:16749:21)
    at Promise._settlePromise0 (/__cypress/runner/cy…_runner.js:16781:10)
    at Promise._settlePromises (/__cypress/runner/cy…_runner.js:16861:18)
    at Promise._fulfill (/__cypress/runner/cy…_runner.js:16805:18)
    at Promise._resolveCallback (/__cypress/runner/cy…_runner.js:16599:57)
    at Promise._settlePromiseFromHandler (/__cypress/runner/cy…_runner.js:16691:17)
I also didn't find any info in the docs about stub().value() or anything about cases like this.
g
You can find more spying and stubbying examples at https://glebbahmutov.com/cypress-examples/commands/spies-stubs-clocks.html
b
I still don't see any examples with module dependencies.
To elaborate, I want something like
Copy code
ts
vi.mock("path", () => ({
    default: "value"
}))
Is this possible with Cypress?
I saw your example (https://glebbahmutov.com/cypress-examples/commands/spies-stubs-clocks.html#stub-a-property), but you're stubbing an object declared right in the test. What's the point of that? I need to stub module exports that are being used.
g
well, sure. But since you provided no code - like is this index.ts in the app? Component test? E2E test? Ok, you can find my stub blog posts by searching https://cypress.tips/search

https://cdn.discordapp.com/attachments/1111676475192639610/1111692496418918500/Screenshot_2023-05-26_at_12.28.44.png

b
I've included tags for this post that answer your questions. I also did provide the code in the first post.
To clarify, the first line is the whole index.ts.
If you're implying I should follow what's described in your blog post that is mentioned in your screenshot, I did try to use the provided cypress config to include a certain webpack plugin, but I still cannot stub imports, getting the same error.
2 Views