I’m trying to do E2E tests on my NestJs application, followed some tutorials and even nestjs doc and couldn’t get to work, but i did exactly what other tutorials said.
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { AppModule } from 'src/app.module';
describe('Auth', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleRef.createNestApplication();
await app.init();
});
describe('login', () => {
const URL = 'http://localhost:3000/auth/login';
it('should return a token', () => {
return request(app.getHttpServer())
.post(URL)
.send({ email: '[email protected]', password: 'Fetin@2024' })
.expect('token');
});
});
afterAll(async () => {
await app.close();
});
});
This is the code example to test my auth route, but it keeps getting this error:
FAIL src/providers/auth/tests/auth.e2e-spec.ts
● Auth › login › should return a token
TypeError: JwtStrategy requires a secret or key
9 | export class JwtStrategy extends PassportStrategy(Strategy) {
10 | constructor(private readonly configService: ConfigService) {
> 11 | super({
| ^
12 | jwtFromRequest: ExtractJwt.fromExtractors([
13 | (request: Request) => {
14 | return request?.cookies?.Authentication;
If i’m building the testModule based on AppModule, it shouldn’t resolve it all by itself? In nestjs docs it says to change the guard provider from ‘useClass’ to ‘useExisting’, but doesn’t work too.
Rcarolino is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.