【发布时间】:2019-09-27 11:06:43
【问题描述】:
我正在使用 Jest 创建一个单元测试并运行测试我得到一个测试错误:“TypeError: db.find(...).toArray is not a function”。如果有人可以帮助我,我将不胜感激。
问候 :)
'use strict';
const DB = require('../../../../app/databases/mongo');
jest.mock('../../../../app/databases/mongo');
const noteController = require('../../../../app/controllers/note');
function functionMock(returnValue) {
const find = jest.fn(() => Promise.resolve(returnValue));
return jest.fn(() => ({
find: () => ({ find })
}));
}
describe('noteController', () => {
beforeAll(() => {
DB.getDb.mockImplementation(functionMock(null));
});
describe('note controller', () => {
describe('find', () => {
it('note list', async done => {
const ctx = jest.fn();
await noteController.find(ctx)
.then(result => {
expect(result).toBeDefined();
});
done();
})
})
})
});
我希望输出包含 promise 中的数据,但我得到:“TypeError: db.find(...).toArray is not a function”
【问题讨论】:
标签: javascript mongodb jestjs