Typescript NestJS在测试套件之间共享e2e服务器
我正在使用NestJS测试模块模拟nest应用程序,我想在各种测试套件之间共享此应用程序。Typescript NestJS在测试套件之间共享e2e服务器,typescript,mocha.js,nestjs,Typescript,Mocha.js,Nestjs,我正在使用NestJS测试模块模拟nest应用程序,我想在各种测试套件之间共享此应用程序。 以下是我的设置: test |_ helpers |_ testApp.ts |_ e2e |_ users.e2e-test.ts |_ beforeAll.e2e-test.ts testApp.ts import { Test } from '@nestjs/testing'; import { DatabaseModule } from '../../src/data
以下是我的设置:
test
|_ helpers
|_ testApp.ts
|_ e2e
|_ users.e2e-test.ts
|_ beforeAll.e2e-test.ts
testApp.ts
import { Test } from '@nestjs/testing';
import { DatabaseModule } from '../../src/database/database.module';
import { UserModule } from '../../src/user/user.module';
let app: any;
export async function initServer() {
const fixture = await Test.createTestingModule({
imports: [
DatabaseModule,
UserModule,
],
}).compile();
app = fixture.createNestApplication();
await app.init();
}
export default app;
import { initServer } from './helpers/testApp';
before(async () => {
await initServer();
});
import * as request from 'supertest';
import * as chai from 'chai';
const expect = chai.expect;
import { UserType } from '../../src/user/enum/user-types.enm';
import app from '../helpers/testApp';
const admin = {
email: 'admin@example.com',
password: '123',
type: UserType.ADMIN
};
describe.only('Creating User with customized permissions', async () => {
it('User should be able to sign in', async () => {
request(app.getHttpServer())
.post('/auth/signin')
.send({ email: admin.email, password: '123' })
.end((_, res) => {
expect(res.status).to.equal(200);
});
});
});
beforeAll.e2e test.ts
import { Test } from '@nestjs/testing';
import { DatabaseModule } from '../../src/database/database.module';
import { UserModule } from '../../src/user/user.module';
let app: any;
export async function initServer() {
const fixture = await Test.createTestingModule({
imports: [
DatabaseModule,
UserModule,
],
}).compile();
app = fixture.createNestApplication();
await app.init();
}
export default app;
import { initServer } from './helpers/testApp';
before(async () => {
await initServer();
});
import * as request from 'supertest';
import * as chai from 'chai';
const expect = chai.expect;
import { UserType } from '../../src/user/enum/user-types.enm';
import app from '../helpers/testApp';
const admin = {
email: 'admin@example.com',
password: '123',
type: UserType.ADMIN
};
describe.only('Creating User with customized permissions', async () => {
it('User should be able to sign in', async () => {
request(app.getHttpServer())
.post('/auth/signin')
.send({ email: admin.email, password: '123' })
.end((_, res) => {
expect(res.status).to.equal(200);
});
});
});
users.e2e test.ts
import { Test } from '@nestjs/testing';
import { DatabaseModule } from '../../src/database/database.module';
import { UserModule } from '../../src/user/user.module';
let app: any;
export async function initServer() {
const fixture = await Test.createTestingModule({
imports: [
DatabaseModule,
UserModule,
],
}).compile();
app = fixture.createNestApplication();
await app.init();
}
export default app;
import { initServer } from './helpers/testApp';
before(async () => {
await initServer();
});
import * as request from 'supertest';
import * as chai from 'chai';
const expect = chai.expect;
import { UserType } from '../../src/user/enum/user-types.enm';
import app from '../helpers/testApp';
const admin = {
email: 'admin@example.com',
password: '123',
type: UserType.ADMIN
};
describe.only('Creating User with customized permissions', async () => {
it('User should be able to sign in', async () => {
request(app.getHttpServer())
.post('/auth/signin')
.send({ email: admin.email, password: '123' })
.end((_, res) => {
expect(res.status).to.equal(200);
});
});
});
因此,基本上我想在各种测试套件之间共享NestApplication
实例,但我在测试用例中将app
设置为undefined
,它给出了以下错误:TypeError:无法读取未定义的属性“getHttpServer”
有没有办法做到这一点?或者我应该在每个测试套件中初始化一个新的NestApplication
我正在使用摩卡咖啡作为测试跑步者。试试这个
beforeAll(async () => {
const module = await Test.createTestingModule({
providers: [
MyTestsService,
{
provide: getRepositoryToken(Warehouse),
useValue: myTestsService
}
],
controllers: [MyTestsController], // <-- this will fix problem of getHttpServer
})
.overrideProvider(MyTestsService)
.useValue(myTestsService)
.compile();
app = module.createNestApplication();
await app.init();
});
beforeAll(异步()=>{
const module=await Test.createTestingModule({
供应商:[
MyTestsService,
{
提供:getRepositoryToken(仓库),
useValue:myTestsService
}
],
控制器:[MyTestsController],//试试这个
beforeAll(async () => {
const module = await Test.createTestingModule({
providers: [
MyTestsService,
{
provide: getRepositoryToken(Warehouse),
useValue: myTestsService
}
],
controllers: [MyTestsController], // <-- this will fix problem of getHttpServer
})
.overrideProvider(MyTestsService)
.useValue(myTestsService)
.compile();
app = module.createNestApplication();
await app.init();
});
beforeAll(异步()=>{
const module=await Test.createTestingModule({
供应商:[
MyTestsService,
{
提供:getRepositoryToken(仓库),
useValue:myTestsService
}
],
控制器:[MyTestsController],//您最终是找到了解决方案,还是为每个套件使用了不同的测试模块?您最终是找到了解决方案,还是为每个套件使用了不同的测试模块?