Any sample repo on seeding data in Cypress? #188
-
mswjs/data + RTL is a breeze to work with. I'm trying out Cypress for the first time now and a bit lost on how to mock and reset data across tests? For example, in RTL I can drop the db before each test, then just create the necessary mock data for each test: // in setupTests.js
beforeEach(() => {
drop(db);
});
// sample.test.js
test("renders a task", async () => {
db.task.create({ task: "First task", status: "today", priority: 1 });
renderWithProviders(<App />);
}); Where exactly do I call import { db } from "mocks/db";
describe("This Week Column", () => {
beforeEach(function () {
db.task.create({ task: "First task", status: "today", priority: 1 });
cy.visit("/");
});
it("renders a task", () => {
// ... some assertions here
});
}); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hey, @neldeles. Thanks for raising this. To understand how to control your In order to change // your-app.js
const db = factory({ ... })
// Set the database reference on "window"
// so that Cypress tests can access it.
window.db = db
export { db } Then, in your tests: import { drop } from '@mswjs/data'
describe('This Week Column', () => {
beforeEach(async () => {
await cy.window().then(win => {
drop(win.data)
})
})
}) Importing |
Beta Was this translation helpful? Give feedback.
-
// in setupTests.js // sample.test.js |
Beta Was this translation helpful? Give feedback.
Hey, @neldeles. Thanks for raising this.
To understand how to control your
db
between Cypress tests you need to understand how Cypress loads your app first. Cypress runner is its own process, while your running app is a different process (which youcy.visit
). That's why thedb
you import in your Cypress test and in your app are two differentdb
instances.In order to change
db
on test runtime, you have to access the same instance that your app is accessing. That is possible if you persist that instance onwindow
, since Cypress can share data between the tests and your app viawindow
.