Jest mock没有解析到每个注入的值



我正试图在functions/send.js模块中测试一个名为myCloudFn的Firebase云函数。我的测试在functions/test/send.spec.js:中

// send.js
const admin = require('firebase-admin');
async function myCloudFn (email) {
const authUser = await admin.auth().getUserByEmail(email);
return authUser;
}
module.exports = { myCloudFn };
// send.spec.js
const send = require('../send.js');
jest.mock('firebase-admin', () => ({
auth: () => ({
getUserByEmail: jest.fn()
.mockResolvedValueOnce({ uid: 'foo-bar' })
.mockResolvedValueOnce(null),
}),
}));
describe('send.js', () => {
it('returns authUser from myCloudFn()', async () => {
const email = 'foo@bar.com';
const responseOptions = [{ uid: 'foo-bar' }, null];
const responsePromises = responseOptions.map(() => send.myCloudFn(email));
const responses = await Promise.all(responsePromises);
expect(responses[0]).toStrictEqual(responseOptions[0]);
expect(responses[1]).toStrictEqual(responseOptions[1]);
});
});

测试在第一个断言时通过,但在第二个断言时失败。测试两次都返回相同的{ uid: 'foo-bar' }对象,但我希望第二次测试响应值为null。我错过了什么?

在每个auth调用上都会创建一个新的getUserByEmail间谍,它不会被调用多次。

相反,它应该是:

const mockGetUserByEmail = jest.fn();
jest.mock('firebase-admin', () => ({
auth: () => ({
getUserByEmail: mockGetUserByEmail
})
}));
...
mockGetUserByEmail
.mockResolvedValueOnce({ uid: 'foo-bar' })
.mockResolvedValueOnce(null);
const responsePromises = responseOptions.map(() => send.myCloudFn(email));
...

您可以为每个测试用例模拟getUserByEmail方法的解析值一次。

例如

send.js:

const admin = require('firebase-admin');
async function myCloudFn(email) {
const authUser = await admin.auth().getUserByEmail(email);
return authUser;
}
module.exports = { myCloudFn };

send.test.js:

const { myCloudFn } = require('./send');
const admin = require('firebase-admin');
jest.mock(
'firebase-admin',
() => {
const mAdmin = {
auth: jest.fn().mockReturnThis(),
getUserByEmail: jest.fn(),
};
return mAdmin;
},
{ virtual: true },
);
describe('send.js', () => {
afterAll(() => {
jest.resetAllMocks();
});
it('should returns authUser from myCloudFn()', async () => {
admin.auth().getUserByEmail.mockResolvedValueOnce({ uid: 'foo-bar' });
const actual = await myCloudFn();
expect(actual).toEqual({ uid: 'foo-bar' });
});
it('should return null', async () => {
admin.auth().getUserByEmail.mockResolvedValueOnce(null);
const actual = await myCloudFn();
expect(actual).toBeNull();
});
});

单元测试结果:

PASS  src/stackoverflow/64575307/send.test.js (14.349s)
send.js
✓ should returns authUser from myCloudFn() (11ms)
✓ should return null (3ms)
----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
send.js  |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        17.321s

相关内容

  • 没有找到相关文章

最新更新