Understanding the Error
When developing a project in NestJS and using Supertest for testing, encountering an error like request.cookies is undefined
can be a stumbling block. This error typically occurs due to inadequate setup for the testing environment to properly simulate the presence of cookies. In a live server, cookies are set using the ‘Set-Cookie’ header and are available in the request.cookies
object. However, in a testing scenario using Supertest, cookies need to be manually set or handled.
Using Third-party Middleware
To remedy the ‘request.cookies is undefined’ error, you can use third-party middleware such as cookie-parser
. NestJS does not parse cookies by default; hence the need for this additional middleware. First, you’ll have to install cookie-parser
.
npm install cookie-parser
Once installed, you must configure your application to use it. In the setup of your NestJS application, typically in the main.ts
file, you would add:
import * as cookieParser from 'cookie-parser';
// ... other imports and code
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.use(cookieParser());
// ... other middleware or configurations
await app.listen(3000);
}
bootstrap();
This will allow your application to set and read cookies. However, in testing with Supertest, you would need to simulate cookie handling. This often involves directly adding cookie information to the request headers or using a Supertest agent that retains cookie information between requests.
Setting Up Test Environment
In your test, set up the NestJS testing module and provide the cookie-parser
middleware as you do in the main application. Here’s how:
import { Test, TestingModule } from '@nestjs/testing';
import * as cookieParser from 'cookie-parser';
import { AppModule } from '../src/app.module';
describe('AppController (e2e)', () => {
let app;
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
app.use(cookieParser());
await app.init();
});
// ... your tests
});
With cookie-parser now integrated into your tests, Supertest should register cookies as expected.
Example Code with Supertest
Here’s a complete code example with Supertest simulating cookie handling in a test:
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import * as cookieParser from 'cookie-parser';
import { AppModule } from '../src/app.module';
describe('CatsController (e2e)', () => {
let app: INestApplication;
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
app.use(cookieParser());
await app.init();
});
it('/GET cats', () => {
return request(app.getHttpServer())
.get('/cats')
.expect(200);
});
});
This example shows an end-to-end test for a GET
request on the /cats
route. Make sure to replace AppModule
and /cats
with the appropriate module and route relevant to your application.