gpt4 book ai didi

javascript - Jest - 调用模拟函数不计算在内

转载 作者:行者123 更新时间:2023-12-02 22:13:52 25 4
gpt4 key购买 nike

我对调用计数 next 函数感到困惑。我正在使用 formidable 来处理表单。

这是我的代码:

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
});

我期望 mNext 被调用一次。但我得到了 0。我在 catch 语句中通过 console.log(next) 检查,我可以看到 next 函数是模拟函数。

在不同的测试中,我正在测试 next(boom.badRequest('Account type is not support'));
已计算在内。

最佳答案

这是单元测试解决方案:

例如

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

关于javascript - Jest - 调用模拟函数不计算在内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59459690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com