我对通话计数createAuthenticationToken
函数感到困惑。我正在使用强大的功能来处理表单。
这是我的代码:
next
这是测试:
const ctrl = {};
ctrl.addAccount = async (req, res, next) => {
const form = new formidable.IncomingForm();
form.multiples = true;
form.parse(req, async function (err, fields, files) {
const accountType = _.get(fields, 'accountType');
if (accountTypes.indexOf(accountType) === -1) {
// next is mocked, call counted in different test
next(boom.badRequest('Account type is not supported'));
return;
}
try {
// some logic
await validator.validate(fields);
// Some logic
res.status(200).send("OK");
} catch (err) {
next(boom.badRequest(err)); // next is mocked but call didn't count
}
});
};
module.exports = ctrl;
我希望it.only('return Error on validation error', async () => {
const fields = {
accountType: "moderator",
title: "Title"
};
const files = {};
const formidableFn = {
IncomingForm: jest.fn(),
parse: jest.fn((req, callback) => callback(undefined, fields, files)),
multiples: true
};
const formidable = require('formidable');
formidable.mockImplementation(() => formidableFn);
const { addAccount } = require('./add');
const mReq = {
body: {},
dbConnection: jest.fn()
};
const mRes = {
send: jest.fn().mockReturnThis()
};
const mNext = jest.fn().mockName('next function');
await addAccount(mReq, mRes, mNext);
expect(formidableFn.parse).toHaveBeenCalledTimes(1);
expect(formidableFn.parse).toHaveBeenCalledWith(mReq, expect.any(Function));
expect(mNext).toHaveBeenCalledTimes(1); // I've got 0 instead of 1
});
被打一次。但是我有0。我在catch语句中通过mNext
进行了检查,可以看到console.log(next)
函数是模拟函数。
在另一项测试中,我正在测试next
。
答案 0 :(得分:1)
这是单元测试解决方案:
例如
add.js
:
const formidable = require('formidable');
const _ = require('lodash');
const validator = require('./validator');
const accountTypes = ['moderator'];
const boom = {
badRequest: (err) => err,
};
const ctrl = {};
ctrl.addAccount = async (req, res, next) => {
const form = new formidable.IncomingForm();
form.multiples = true;
form.parse(req, async function(err, fields, files) {
const accountType = _.get(fields, 'accountType');
if (accountTypes.indexOf(accountType) === -1) {
next(boom.badRequest('Account type is not supported'));
return;
}
try {
await validator.validate(fields);
res.status(200).send('OK');
} catch (err) {
next(boom.badRequest(err));
}
});
};
module.exports = ctrl;
validator.js
:
const validator = {
validate: async (fields) => {
console.log('validate fields');
},
};
module.exports = validator;
add.test.js
:
const formidable = require('formidable');
const { addAccount } = require('./add');
const validator = require('./validator');
jest.mock('formidable', () => {
const mForm = {
multiples: false,
parse: jest.fn(),
};
return {
IncomingForm: jest.fn(() => mForm),
};
});
describe('59459690', () => {
afterEach(() => {
jest.resetAllMocks();
jest.restoreAllMocks();
});
it('return Error on validation error', async () => {
const fields = {
accountType: 'moderator',
title: 'Title',
};
const files = {};
const form = new formidable.IncomingForm();
let originalCallback;
form.parse.mockImplementation((req, callback) => {
originalCallback = callback;
});
const mError = new Error('validation error');
jest.spyOn(validator, 'validate').mockRejectedValueOnce(mError);
const mReq = {
body: {},
dbConnection: jest.fn(),
};
const mRes = {
send: jest.fn().mockReturnThis(),
};
const mNext = jest.fn().mockName('next function');
await addAccount(mReq, mRes, mNext);
await originalCallback(undefined, fields, files);
expect(form.parse).toHaveBeenCalledTimes(1);
expect(form.parse).toHaveBeenCalledWith(mReq, expect.any(Function));
expect(mNext).toHaveBeenCalledTimes(1);
});
});
带有覆盖率报告的单元测试结果:
PASS src/stackoverflow/59459690/add.test.js
59459690
✓ return Error on validation error (8ms)
--------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
--------------|----------|----------|----------|----------|-------------------|
All files | 79.17 | 50 | 66.67 | 78.26 | |
add.js | 85 | 50 | 100 | 84.21 | 20,21,25 |
validator.js | 50 | 100 | 0 | 50 | 2,3 |
--------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 3.82s, estimated 8s
源代码:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59459690