A database fixture is a fixed, known set of records loaded into a database for testing. It gives an application predictable data before a test begins. Fixtures can also make a locally running application ready for manual testing or a product demonstration. section 9.7.3 section 10.7.1
Use a fixture when the test needs a real database populated with specific records. A unit test that mocks the MongoDB library can supply fake data through the mock instead. An integration test can insert records directly from its test code; separate fixture files become more useful when loading data for an end-to-end test. section 10.7.1
For a small database-backed test:
- Start the application and its database before trying to load records. section 4.5.2
- Choose the collection and records. A Jest helper can reuse the microservice’s existing database connection, drop the test database, and insert the records with insertMany. section 9.7.3
- Run the test against the seeded state. Keep the database reset confined to testing: dropping the database is not a production operation. section 9.7.3 section 9.8.5
Two videos for a browser test
A two-videos fixture supplies records for the metadata microservice. A Playwright test loads it, visits the application’s root page, and checks that the video list contains two entries with the expected names and links. The known records make those assertions meaningful. section 9.8.5
Listing 10.2 An example database fixture for FlixTube
const { ObjectId } = require("mongodb"); #1
module.exports = [ #2
{
_id: new ObjectId("5ea234a1c34230004592eb32"), #3
name: "SampleVideo_1280x720_1mb.mp4" #4
},
{
_id: new ObjectId("5ea234a5c34230004592eb33"), #3
name: "Another video.mp4" #4
}
];
The fixture file exports two video records with fixed MongoDB ObjectIds. Its filename, videos.js, identifies the collection they are loaded into. section 10.7.1
For end-to-end tests, a fixtures REST API can load the data instead of having the browser test connect directly to MongoDB. The test requests a named fixture before checking the running application. section 10.7.1 section 9.8.5









