Description
Hey,
I'd like to get your opinions on introducing Jest for unit testing on the backend and then secondary to that, creating a way to create test data for the unit tests.
I would propose changing a few structures first, for our controllers we have the file paths of
Controllers
controllers/erpControllers/adminController.js
I would propose to change this to
controllers/erpControllers/adminController/index.js
controllers/erpControllers/adminController/index.test.js
This could then be extended later to include index.test.integration.ts for integration tests.
Models
Currently we have our model files like so
models/erpModels/Admin.js
I'd like to change this so that a model can also have a generator to create the test data for that model.
models/erpModels/Admin/index.js
models/erpModels/Admin/generate.js
Generate.js
The generate.js would look something like:
const faker = require('@faker-js/faker');
const Admin = mongoose.model('Admin');
const generateAdmin = (input) => {
return new Admin({
...input,
surname: faker.name.lastName(),
// More fields here
})
}
export default generateAdmin
Test Data
We would want a way to create test data on the fly for whatever purpose it is required and be able to relate the data. I'd propose something like below:
await resetDatabase({
testData: {
Admin: [{}],
Employee: [{}, {}, {}],
Expense: [{ expenseCategory: 0 }],
ExpenseCategory: [{}]
},
});
This would create:
1 Admin, 3 Employee's, 1 Expensive which has an Expense cateogry and 1 Expense Category that belongs to an Expense