Can't overrideProvider with any mock in e2e tests
See original GitHub issueBug 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
Issue Analytics
- State:
- Created 4 years ago
- Comments:17 (7 by maintainers)
Top Results From Across the Web
Override provider in single test in nest.js e2e testing
I want to use real implementation of TestService which under the hood uses TestEntity repository which is mocked (and I don't want to...
Read more >Testing | NestJS - A progressive Node.js framework
Now the JwtAuthGuard is visible to Nest as a regular provider that can be overridden when creating the TestingModule : const moduleRef =...
Read more >End-to-end testing in NestJS with TypeORM
Mock all testing modules and then call the controllers and services; Test the endpoints with a database. Our approach enables direct requests to ......
Read more >Testing Your Application
Mocking using QuarkusMock. The io.quarkus.test.junit.QuarkusMock class can be used to temporarily mock out any normal scoped bean. If you ...
Read more >How to mock providers in Angular tests
A mock provider in Angular tests can be created by MockProvider function. ... All methods are empty dummies. MockProvider(Service), // All methods are...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.