Sling Academy
Home/Node.js/Fixing NestJS & Supertest Error: request.cookies is undefined

Fixing NestJS & Supertest Error: request.cookies is undefined

Last updated: December 31, 2023

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.

Next Article: NestJS Issue Fix: How to Inject a Service into a Subscriber

Previous Article: NestJS & TypeORM Error: Cannot Connect to MySQL Database

Series: Nest.js Tutorials: From Basics to Advanced

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates