Replies: 4 comments 4 replies
-
Thanks for raising this, it's a brilliant point. ResponsibilityI think this functionality should come from the MSW+data integration. The data package itself shouldn't care about resetting its state, as it's not something you usually do with a real database. The request handlers generating API (#11) may take care of that. There's currently no means to hook into MSW serverOrWorker.on('internal:reset-handlers', () => {...}) import { setupWorker } from 'msw'
import { factory, toHandlers } from '@mswjs/data'
const db = factory({...})
const { handlers, attachEvents } = toHandlers(db)
const worker = setupWorker(...handlers)
// Listens to "internal:reset-handlers" event and resets the database.
attachEvents(worker) That is an incredibly confusing API, and something we can brainstorm on. Reset valueQuestion: what should DB be reset to? When you model your DB you provide the map of models. It's only after that you can create entities (data) based on those models. In other words, what do you expect here to happen: // Creating a DB, clean.
const db = factory({...})
afterEach(() => server.resetHandlers())
it('test', () => {
// Request that creates an entity in the DB.
fetch('/user', { method: 'POST' })
})
it('another test', () => {
// What entities do you expect the DB to have here?
}) I expect your answer to be "no entities". In that case you can use an existing afterEach(() => {
server.resetHandlers()
db.clear()
})
This also comes down to the development pattern you choose:
As it's generally not a good idea for the data to persist between tests, you may want to choose the section option and call SemanticsI'm concern with the semantics of connecting What |
Beta Was this translation helpful? Give feedback.
-
I add a ittle hint here that I think is important for testing purposes. After the clear is important to have the migration feature. |
Beta Was this translation helpful? Give feedback.
-
Actually, I would be tempted to populate the initial data like following: // src/mocks/db.js
import initialUsers from "./users.csv"
const db = factory({...})
db.user.createMany(initialUsers)
export {db} or even to have some So the DB reset before each test would be equivalent to restoring the DB from a dump, not just Hmm, probably the Or maybe some |
Beta Was this translation helpful? Give feedback.
-
Suggestion: SnapshotsAs @Aprillion has suggested, we may use a concept of snapshots—images of the database through time to reset to. import { factory, snapshot } from '@mswjs/data'
const db = factory({...})
db.user.create()
db.post.create()
db.tags.createMany()
// Take a named snapshot of all the entities in the DB.
snapshot.take(db, 'my-snapshot')
// Tests happen here...
// Restores the DB to the previously taken snapshot.
snapshot.reset(db, 'my-snapshot') Behavior
|
Beta Was this translation helpful? Give feedback.
-
When used for testing, we will want to make the tests independent, so some mechanism should exist for resetting all data to initial state (probably outside of @mswjs/data implementation, just some documented recipe).
I would imagine most people who need independent tests already use:
How to hook into that?
Beta Was this translation helpful? Give feedback.
All reactions