The behavior is as the title describes it. I've tried a number of things but can't figure out why this is happening.
This error was fixed by adding a Jest timeout:
● AppController › /ping › Return "Pong!"
thrown: "Exceeded timeout of 15000 ms for a hook.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
19 | let nestApp: INestApplication;
20 |
> 21 | beforeAll(async () => {
| ^
22 | app = await Test.createTestingModule({
23 | controllers: [AppController],
24 | providers: [
at src/app/app.controller.spec.ts:21:3
at Object.<anonymous> (src/app/app.controller.spec.ts:15:1)
Then I get this error:
● AppController › / › Return "Pong!"
Instance Exited before being ready and without throwing an error!
at MongoInstance.<anonymous> (../../node_modules/mongodb-memory-server-core/src/util/MongoInstance.ts:338:13)
at MongoInstance.closeHandler (../../node_modules/mongodb-memory-server-core/src/util/MongoInstance.ts:513:10)
The problematic tests (just showing one of them for brevity's sake) are:
describe('AppController', () => {
let app: TestingModule;
let nestApp: INestApplication;
beforeAll(async () => {
app = await Test.createTestingModule({
controllers: [AppController],
providers: [
AppService,
{ provide: APP_PIPE, useValue: new ValidationPipe() },
],
imports: [
HttpModule,
AuthzModule,
BullzModule,
AgendazModule,
MongosezModule,
TestDatabaseModule,
],
}).compile();
nestApp = app.createNestApplication();
await nestApp.init();
return;
});
describe('/ping', () => {
test('Return "Pong!"', async () => {
const appServiceSpy = jest.spyOn(AppService.prototype, 'ping');
appServiceSpy.mockReturnValue({ message: 'Pong!' });
const response = await supertest(nestApp.getHttpServer()).get('/ping');
expect(response.body).toStrictEqual({
message: 'Pong!',
});
return;
});
return;
});
return;
});
The problems started to happening when I added Mongoose to the application. The TestDatabaseModule simply connects to an in memory database for testing (this package).
MongoosezModule:
@Module({
imports: [
NestMongooseModule.forFeature([
{ name: NewsCache.name, schema: NewsCacheSchema },
]),
],
providers: [NewsCacheService],
exports: [NewsCacheService],
})
export class MongoosezModule {}
TestDatabaseModule:
@Module({
imports: [
NestMongooseModule.forRootAsync({
useFactory: async () => {
const mongod = await MongoMemoryServer.create();
const uri = mongod.getUri();
return {
uri: uri,
};
},
}),
],
})
export class TestDatabaseModule {}
Some tests pass just fine, for example:
describe('AppService', () => {
let service: AppService;
let http: HttpService;
beforeAll(async () => {
const app = await Test.createTestingModule({
providers: [
AppService,
NewsCacheService,
{
provide: getModelToken(NewsCache.name),
useValue: newsCacheModelMock,
},
],
imports: [HttpModule],
}).compile();
service = app.get<AppService>(AppService);
http = app.get<HttpService>(HttpService);
return;
});
describe('When getNews is called..', () => {
test('A GET query is made to a generated URL.', () => {
const expected =
`${environment.newsApiUrl}/everything` +
`?apiKey=${environment.newsApiKey}` +
`&q=test0 AND test1` +
`&sortBy=popularity` +
`&to=${service.getTodaysDate()}` +
`&language=en` +
`&pageSize=10` +
`&page=3`;
const httpSpy = jest.spyOn(http, 'get');
service.getNews('test0,test1', 3);
expect(httpSpy).toHaveBeenCalledWith(expected);
return;
});
return;
});
The tests are run with nx test $APP --code-coverage --coverageReporters="text" --detectOpenHandles --verbose.
I've tried using three different Node.js images for the pipeline:
node:14.18.2node:16.11.1node:16.11.1-slim