朋友,我有問題!
我正在用 NestJS 測驗我的 API。我正在使用 Jest 進行測驗。不幸的是,我遇到了以下錯誤:
TypeError:無法讀取未定義的屬性(讀取“關閉”)
這個錯誤非常明確,但我看不出它可能來自哪里。
你有什么想法嗎?
我當前的代碼:
import * as pactum from 'pactum';
import { Test } from '@nestjs/testing';
import { AppModule } from '../src/app.module';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { PrismaService } from '../src/prisma/prisma.service';
import { AuthDto } from '../src/auth/dto';
describe('App e2e', () => {
let app: INestApplication;
let prisma: PrismaService;
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
}).compile();
const app = moduleRef.createNestApplication();
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
}),
);
await app.init();
await app.listen(3334);
prisma = app.get(PrismaService);
await prisma.cleanDatabase();
});
afterAll(async () => {
console.log('Closing server');
await app.close(); // <------------- THE PROBLEM ARISES HERE.
});
describe('Auth', () => {
describe('Signup', () => {
it('should signup a user', () => {
const dto: AuthDto = {
email: '[email protected]',
password: '1234',
};
return pactum
.spec()
.post('http://localhost:3333/auth/signup')
.withBody(dto)
.expectStatus(201);
});
});
describe('Signin', () => {
it.todo('should signin a user');
});
});
});
uj5u.com熱心網友回復:
您正在混合不同范圍內的兩個變數。
let app: INestApplication;是您實際使用的上層范圍,因為它沒有分配任何值undefined。內部是不同的,因為您是在另一個范圍內定義的。
一個解決方法很簡單,只需const從const app = moduleRef.createNestApplication();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/463344.html
標籤:javascript api 开玩笑的 巢穴
上一篇:陣列物件到簡單物件映射問題
