Inversifyjs: How to test node controller with jest

Created on 13 Apr 2018  路  7Comments  路  Source: inversify/InversifyJS

Hi everyone.
I have tried to test controller with jest but I got an error "TypeError: Reflect.hasOwnMetadata is not a function". I tried to add reflect-metadata as a mockModule but I got the same error. Has anyone the same trouble??

Most helpful comment

Here is how i did it recently

    let UserServiceMock: UserService;
    let container: Container = null;

    beforeEach(() => {
        UserServiceMock = mock(UserServiceImpl);
        container = new Container();
        container.bind<UserController>('controller').to(UserController);
        container.bind<UserService>('userService').toConstantValue(instance(UserServiceMock));
    });

    it('Should return a user', () => {

        let userController: UserController = container.get('controller');
        when(UserServiceMock.getUser('userId')).thenResolve(user);

        userController.getUser('userId')
            .then((data: User) => {
                expect(data).to.equal(user);
            }, (error) => {
                assert.fail(error);
            });
    });

All 7 comments

you need to import "reflect-metadata"; once in the tests

@AltekkeE I had a trouble with creating container with a few nested services. Do u know how to do it??
Something like this
controller = new UserController(new UserService(new MongoDBClientMock()));
but with container.

Here is how i did it recently

    let UserServiceMock: UserService;
    let container: Container = null;

    beforeEach(() => {
        UserServiceMock = mock(UserServiceImpl);
        container = new Container();
        container.bind<UserController>('controller').to(UserController);
        container.bind<UserService>('userService').toConstantValue(instance(UserServiceMock));
    });

    it('Should return a user', () => {

        let userController: UserController = container.get('controller');
        when(UserServiceMock.getUser('userId')).thenResolve(user);

        userController.getUser('userId')
            .then((data: User) => {
                expect(data).to.equal(user);
            }, (error) => {
                assert.fail(error);
            });
    });

@AltekkeE thanks a lot. It works. Can u help me with this question: https://github.com/inversify/inversify-express-example/issues/327 ?? I will be very thankful ??

@Bon4ik I dont see jest having a mock function on the global scope. I see jest.mock() which can be used to auto mock a file. How did you make this work?

@JoA-MoS What r u trying to do? Can u share some part of code?? If u would like just to mock some util function which not connected to inversify u can use rewire or something like this :

import { someFunction } from 'utils/someFunction';

jest.mock('utils/someFunction');

someFunction.mockReturnValue(value);

@dcavanagh Do you have an example where the controller has middleware. Or would that not be tested in the controller.test?

Was this page helpful?
0 / 5 - 0 ratings