我一直在尝试对用 vitest 编写的 NestJS 项目运行 e2e 测试, 但我几个小时都无法解决下面的错误...
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
以下是我的 vitest 配置和 e2e 规范文件。 谁能告诉我出了什么问题吗?
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!');
});
});
我也遇到了同样的问题。
将
import * as request from 'supertest';
更改为 import request from 'supertest';
,此错误将得到修复。