When executing e2e tests in Nest.js with FastifyAdapter I get the following error when executing tests:
TypeError: app.address is not a function
54 |
55 | return request(app.getHttpServer())
> 56 | .post('/authentication/register')
| ^
57 | .send(payload)
58 | .expect(400);
59 | });
The composition is as follows:
beforeAll(async () => {
const module = await Test.createTestingModule({
imports: [AuthenticationModule],
})
.overrideProvider(UserRepository)
.useValue(userRepository)
.compile();
app = module.createNestApplication(new FastifyAdapter());
await app.init();
});
it(`/POST register - should succeed for valid info`, () => {
const payload = { email: '[email protected]', password: '1234' };
return request(app.getHttpServer())
.post('/authentication/register')
.send(payload)
.expect({})
.expect(201);
});
When not using the FastifyAdapter there is no such error. Reason for using the adapter is because of fastify-cookie plugin which enables cookie manipulation through the requests.
Just to note that for this demonstration I went without the cookie plugin in beforeAll which would had been:
const fastifyAdapter = new FastifyAdapter();
fastifyAdapter.register(fastifyCookie);
I've missed the documentation regarding Nest.js testing for fastify which can be found in the source code of Nest.js but not on the site docs. When using fastify we need to use IT's testing methods from their docs. Following example is working correctly:
beforeAll(async () => {
const fastifyAdapter = new FastifyAdapter();
fastifyAdapter.register(fastifyCookie);
const module = await Test.createTestingModule({
imports: [AuthenticationModule],
})
.overrideProvider(UserRepository)
.useValue(userRepository)
.compile();
app = module.createNestApplication(fastifyAdapter);
await app.init();
});
it(`/POST register - should succeed for valid info`, () => {
return app
.inject({
method: 'POST',
url: '/authentication/register',
payload: { email: '[email protected]', password: '1234' },
})
.then(({ statusCode, payload }) => {
expect(payload).toEqual('');
expect(statusCode).toEqual(201);
});
});
afterAll(async () => {
await app.close();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With