Вот решение для модульного тестирования:
index.js
:
const db = require('./db');
async function create(req, res) {
try {
await db.restaurant.create(req.body);
res.sendStatus(200);
} catch (error) {
res.sendStatus(500);
}
}
async function read(req, res) {
try {
const data = await db.restaurant.findAll();
res.send(200).json(data);
} catch (error) {
res.sendStatus(500);
}
}
async function update(req, res) {
try {
await db.restaurant.update(
{
name: 'KFC',
},
{
where: {
id: req.params.id,
},
},
);
} catch (error) {
res.sendStatus(500);
}
}
module.exports = {
create,
read,
update,
};
db.js
:
const db = {
restaurant: {
async create() {},
async findAll() {},
async update() {},
},
};
module.exports = db;
index.test.js
:
const { create, read, update } = require('./');
const db = require('./db');
jest.mock('./db');
describe('60501363', () => {
describe('#create', () => {
it('should create correctly', async () => {
const mReq = { body: {} };
const mRes = { sendStatus: jest.fn() };
jest.spyOn(db.restaurant, 'create').mockResolvedValueOnce({});
await create(mReq, mRes);
expect(db.restaurant.create).toBeCalledWith({});
expect(mRes.sendStatus).toBeCalledWith(200);
});
it('should handle error if create failure', async () => {
const mReq = { body: {} };
const mRes = { sendStatus: jest.fn() };
const mError = new Error('network');
jest.spyOn(db.restaurant, 'create').mockRejectedValueOnce(mError);
await create(mReq, mRes);
expect(mRes.sendStatus).toBeCalledWith(500);
});
});
describe('#read', () => {
it('should read', async () => {
const mReq = {};
const mRes = { send: jest.fn().mockReturnThis(), json: jest.fn() };
jest.spyOn(db.restaurant, 'findAll').mockResolvedValueOnce([]);
await read(mReq, mRes);
expect(db.restaurant.findAll).toBeCalledTimes(1);
expect(mRes.send).toBeCalledWith(200);
expect(mRes.json).toBeCalledWith([]);
});
});
describe('#update', () => {
it('should update correctly', async () => {
const mReq = { params: { id: 1 } };
const mRes = {};
jest.spyOn(db.restaurant, 'update').mockResolvedValueOnce({});
await update(mReq, mRes);
expect(db.restaurant.update).toBeCalledWith({ name: 'KFC' }, { where: { id: 1 } });
});
});
});
Результаты модульных испытаний с отчетом о покрытии:
PASS stackoverflow/60501363/index.test.js (11.289s)
60501363
#create
✓ should create correctly (6ms)
✓ should handle error if create failure (2ms)
#read
✓ should read (1ms)
#update
✓ should update correctly
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 84.62 | 100 | 50 | 83.33 |
db.js | 100 | 100 | 0 | 100 |
index.js | 81.82 | 100 | 100 | 80 | 17,34
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 4 passed, 4 total
Snapshots: 0 total
Time: 13.076s
исходный код: https://github.com/mrdulin/react-apollo-graphql-starter-kit/tree/master/stackoverflow/60501363