在节点中使用JEST的模拟类

时间:2020-08-21 21:18:19

标签: javascript node.js unit-testing jestjs

所以,这实际上是我发生过几次的事情,我不知道如何解决。

我有这样的课程

//myClass.js
const { get } = require('../models/myModel');

class MyClass {
    constructor(id) {
        this.id = id;
    }

    async getByID() {
        return get(this.id);
    }
}

和一个控制器

//controller.js
module.exports = MyClass;

const MyClass = require('./MyClass.js');

module.exports = {
    getController: (req, res, next) => {
        try {
            const MyObject = new MyClass(1);
            const info = await MyObject.getByID();
            res.send(info)
        }
        catch (e) {
            next(e);
        }
    }
}

当我想进行E2E测试时,我需要模拟getByID来查看其解决和拒绝时发生的情况。

那么,如何模拟构造函数?我做了这样的事情

// myclass.spec.js


const MyClass = require('./MyClass.js');

jest.mock('./MyClass', () => jest.fn().mockImplementation(() => ({
  getByID: Promise.resolve({id: 1, name: 'John'}),
})));


describe('Testing MyClass', () => {
    it('Should return info', () => {
        const res = httpMocks.createResponse();
        const mReq = httpMocks.createRequest();
        const mNext = jest.fn();
        const mRes = mockResponse();
        await pipelineController(mReq, mRes, mNext);
        expect(mRes.send).toHaveBeenCalledWith(1);
        done();
    })
})

我知道此测试当前有效,但是现在我无法更改getByID模拟值以查看拒绝承诺时会发生什么。

如果我尝试将其包含在测试(it)中....它将不会模拟任何内容... 我想要类似的东西


const MyClass = require('./MyClass.js');
const {pipelineController} = require('./controllers.js')

jest.mock('./MyClass', () => jest.fn().mockImplementation(() => ({
  getInfo: jest.fn(),
})));


describe('Testing MyClass', () => {
    it('Should return info', () => {
        ProcessService.getInfo.mockImplementation(() => Promise.resolve('the value i want'))
        const res = httpMocks.createResponse();
        const mReq = httpMocks.createRequest();
        const mNext = jest.fn();
        const mRes = mockResponse();
        await pipelineController(mReq, mRes, mNext);
        expect(mRes.send).toHaveBeenCalledWith(1);
        done();
    })
    it('Should return info', () => {
        ProcessService.getInfo.mockImplementation(() => Promise.reject('the reason i want'))
        const res = httpMocks.createResponse();
        const mReq = httpMocks.createRequest();
        const mNext = jest.fn();
        const mRes = mockResponse();
        await pipelineController(mReq, mRes, mNext);
        expect(mRes.send).toHaveBeenCalledWith(1);
        done();
    })

})

1 个答案:

答案 0 :(得分:0)

由于getByID在实例化后立即被调用,因此应该在模拟类之外提供间谍以更改实现。

这可以通过works in obscure and unspecified ways类自动模拟来完成,但可能适用于这种特定情况:

jest.mock('./MyClass.js')
const MyClass = require('./MyClass.js');
...
MyClass.prototype.getByID.mockResolvedValue({id: 1, name: 'John'});
// instantiate MyClass and call getByID

这可以通过在班外暴露间谍来完成:

const mockGetByID = jest.fn();
jest.mock('../../../services/ProcessService', () => jest.fn(() => (
  { getByID: mockGetByID }
)));
const MyClass = require('./MyClass.js');
...
mockGetByID.mockResolvedValue({id: 1, name: 'John'});
// instantiate MyClass and call getByID