nest: Can't overrideProvider with any mock in e2e tests
Bug Report
Current behavior
I tried 3 librairies for mocking a dependency X: Sinon, Typemoq and Ts-Mockito. When I’m using the @nestjs/testing module to create a testing module and I want to override a provider, the @nestjs/testing doesn’t work with all 3 mocking librairies. It wont boot. Out of the 3, only sinon’s mock works.
Input Code
this is a repository that reproduce the problem. https://github.com/Christo676/nestjs-e2e-mock
import * as request from 'supertest';
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { AppModule } from '@main/app.module';
import * as sinon from 'sinon';
// import * as TypeMoq from 'typemoq';
import { SomeDependency } from '@main/provider/some-dependency';
import { SinonStubbedInstance } from 'sinon';
// import {instance, mock, when} from 'ts-mockito';
describe('Actuator', (): void => {
let app: INestApplication;
//Sinon
let mockedSomeDependency: SinonStubbedInstance<SomeDependency>;
//Typemoq
// let mockedSomeDependency: TypeMoq.IMock<SomeDependency>;
//Ts-Mockito
// let mockedSomeDependency: SomeDependency;
// let instanceSomeDependency: SomeDependency;
beforeAll(
async (): Promise<void> => {
//Sinon
mockedSomeDependency = sinon.createStubInstance(SomeDependency);
mockedSomeDependency.getHelloWorld.returns(Promise.resolve('hello nestjs'));
//Typemoq
// mockedSomeDependency = TypeMoq.Mock.ofType<SomeDependency>();
// mockedSomeDependency.setup((someDependency): Promise<string> => someDependency.getHelloWorld()).returns(async () => 'hello nestjs');
//Ts-Mockito
// mockedSomeDependency = mock(SomeDependency);
// when(mockedSomeDependency.getHelloWorld()).thenReturn(Promise.resolve('hello nestjs'));
// instanceSomeDependency = instance(mockedSomeDependency);
const module = await Test.createTestingModule({
imports: [AppModule]
})
//Sinon
.overrideProvider('dependency')
.useValue(mockedSomeDependency)
//Typemoq
// .overrideProvider('dependency')
// .useValue(mockedSomeDependency.object)
//Ts-Mockito
// .overrideProvider('dependency')
// .useValue(instanceSomeDependency)
.compile();
app = module.createNestApplication();
await app.init();
}
);
afterAll(
async (): Promise<void> => {
await app.close();
}
);
it('should return hello nestjs instead of hello world', async (): Promise<Test> => {
return await request
.agent(app.getHttpServer())
.get('/api/v1/test/greet')
.expect(200)
.expect('hello nestjs');
});
});
Expected behavior
I expect the NestJs testing module to take in all mocks and to override the provider targeted.
Possible Solution
Environment
Nest version: 6.2.4
For Tooling issues:
- Node version: v11.14.0
- Platform: Windows 10
Others:
IDE: IntelliJ Ultimate 2019
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 17 (7 by maintainers)
Hi @Christo676, It seems that there is some weird issue with Proxies & Jest & NestJS & mocking libraries & and
Promise.all()used at the same time (jest just hangs out).There is a very simple workaround for this though:
(add
Object.assign())I updated my repository with the latest version (6.3.1) and it did’t change anything. Only sinon mocks work.