Search code examples
nestjse2e-testingvitest

Vitest: e2e testing on NestJS fails with `TypeError: __vite_ssr_import_1__ is not a function`


I've been trying to run e2e test on NestJS project written in vitest, but I haven't been able to solve the error below for hours...

 FAIL  test/app.e2e-spec.ts > AppController (e2e) > / (GET)
TypeError: __vite_ssr_import_1__ is not a function
 ❯ test/app.e2e-spec.ts:20:12

Followings are my vitest config and e2e spec files. Could anyone tell me what is going wrong?

vitest.config.e2e.ts


import swc from 'unplugin-swc';
import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    include: ['**/*.e2e-spec.ts'],
    globals: true,
    root: './',
  },
  resolve: {
    alias: {
      '@': __dirname + '/src',
      '@prismaClient': __dirname + '/prisma/client',
    },
  },
  plugins: [swc.vite()],
});

test/app.e2e-spec.ts


import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { AppModule } from '../src/module/app/app.module';


describe('AppController (e2e)', () => {
  let app: INestApplication;

  beforeEach(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

    app = moduleFixture.createNestApplication();
    await app.init();
  });

  it('/ (GET)', () => {
    return request(app.getHttpServer()).get('/').expect(200).expect('Hello World!');
  });
});


Solution

  • I faced the same problem.

    Change import * as request from 'supertest'; to import request from 'supertest'; and this error will be fixed.