-
-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to work with data if referenced from Cypress support file #221
Comments
Hey, @lavoscore. Thanks for raising this.
Yes, I believe that's the case. Regardless of how Cypress works, you are referencing two difference instances of exported
Each surface will require a "new" instance of "db", and that's why it's empty when you attempt to use it in your handlers. SolutionI recommend sharing instances globally through I'd do the following: // test/cypress/support/start-worker.ts
import { rest } from 'msw'
import { db } from 'src/mocks/db'
Cypress.on('window:before:load', win => {
// Set the "db" instance on the window.
// From now on, only reference it through the window,
// not through direct imports.
win.db = db
// You may also want to store the worker reference
// the same way so you could "win.msw.worker.use()"
// from within your test files.
win.msw = {
worker: setupWorker(rest.get('/resource', (req, res, ctx) => {
return res(ctx.text(win.db.users.count()))
})
}
}); You should read on the exact Cypress API to get access to the Then, in your test you do: // test/my.e2e.test.js
it('', () => {
const win = cy.win()
win.db.users.create({ name: 'John' })
// ...do requests.
}) |
What a nice answer! Thanks! It's a pity not to be able to use support files as "reusable behavior" in this case, but I totally understand why now. I think I'll just repeat my direct imports in my tests for now. It's not DRY, but the alternative is a little bit too much for me at the moment. Maybe I'll switch to your proposed solution in the future though. |
Sorry in advance if this is not the right place to post this issue, but I couldn't find any info about it: I'm having a hard time trying to share my db instance between my Cypress support file and my e2e test files.
(
db
is created as simple as possible withexport const db = factory({ user: ... })
)I have support/index.ts import start-worker.ts, which starts my msw worker to intercept my API calls. But unfortunately,
db
is empty inside the handlers:But, if I import start-worker.ts directly from my test file, it works.
db
inside the handlers holds the data created in the test.Am I getting two
db
s here? According to docs, Cypress loads the support file once before the tests, but I fail to see why this is enough to create two separate db instances to the same import. I'd appreciate an explanation on why this should be expected.The text was updated successfully, but these errors were encountered: