当我运行 e2e 测试时:
这是
应用程序.Module.ts
@Module({
imports: [
GraphQLModule.forRoot({
cors: {
origin: process.env.FRONTENDURL,
credentials: true,
},
installSubscriptionHandlers: true,
autoSchemaFile: 'src/schema.gql',
context: ({ req, res, url }) => ({
headers: req.headers,
res,
url: req.protocol + '://' + req.headers.host,
}),
}),
ScheduleModule.forRoot(),
UserModule,
BusinessModule,
PrismaModule,
ReviewModule,
LikeModule,
FollowModule,
ImageModule,
LocationModule,
CategoryModule,
ReportModule,
ClaimModule,
FeedModule,
],
controllers: [UserController],
providers: [ClaimResolver, ClaimService],
})
export class AppModule {}
和 main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as cookieParser from 'cookie-parser';
import { NestExpressApplication } from '@nestjs/platform-express';
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
cors: true,
});
app.use(cookieParser('Secret'));
app.enableCors({
origin: process.env.FRONTENDURL,
credentials: true,
});
//app.setViewEngine('hbs');
await app.listen(5000);
}
bootstrap();
所以这是我的
app.e2e-spec.ts
import { Test, TestingModule } from '@nestjs/testing';
import { ExecutionContext, INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
import { GqlExecutionContext } from '@nestjs/graphql';
import { AuthGuard } from '../src/utils/auth/auth.guard';
const gql = '/graphql';
describe('AppController (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
afterAll(async () => {
await app.close();
});
it('loged in', async () => {
const result = await request(app.getHttpServer())
.post(gql)
.send({
query: `mutation login{
login(data:{
email:"alaa@gmail.com",
password: "alaa01151116980",
}){
token
user{
userName
id
}
}
}`,
})
// .expect(404)
.expect((response) => {
expect(response.body.data.login.token).toEqual(
expect.objectContaining({
login: {
token: expect.any(String),
},
}),
);
});
});
});
我得到了错误
● AppController (e2e) › loged in
TypeError: Cannot read property 'login' of undefined
108 | // .expect(404)
109 | .expect((response) => {
> 110 | expect(response.body.data.login.token).toEqual(
| ^
111 | expect.objectContaining({
112 | login: {
113 | token: expect.any(String),
at app.e2e-spec.ts:110:35
at ../node_modules/supertest/lib/test.js:80:15
at Test.Object.<anonymous>.Test._assertFunction (../node_modules/supertest/lib/test.js:311:11)
at Test.Object.<anonymous>.Test.assert (../node_modules/supertest/lib/test.js:201:21)
at Server.localAssert (../node_modules/supertest/lib/test.js:159:12)
A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks.
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 30.379 s
Ran all test suites.
Watch Usage
› Press f to run only failed tests.
› Press o to only run tests related to changed files.
› Press p to filter by a filename regex pattern.
› Press t to filter by a test name regex pattern.
› Press q to quit watch mode.
› Press Enter to trigger a test run.
当我将app.e2e-spec.ts文件中的响应更改为:
it('loged in', async () => {
const result = await request(app.getHttpServer())
.post(gql)
.send({
query: `mutation login{
login(data:{
email:"alaa@gmail.com",
password: "alaa01151116980",
}){
token
user{
userName
id
}
}
}`,
})
// .expect(404)
.expect((response) => {
expect(response).toEqual(
expect.objectContaining({
login: {
token: expect.any(String),
},
}),
);
});
});
我收到了回复
● AppController (e2e) › loged in
expect(received).toEqual(expected) // deep equality
Expected: ObjectContaining {"login": {"token": Any<String>}}
Received: {"header": {"connection": "close", "content-length": "147", "content-security-policy": "default-src 'none'", "content-type": "text/html; charset=utf-8", "date": "Fri, 07 May 2021 03:30:24 GMT", "x-content-type-options": "nosniff", "x-powered-by": "Express"}, "req": {"data": {"query": "mutation login{
login(data:{
email:\"alaa@gmail.com\",
password: \"alaa01151116980\",
}){
token
user{
userName
id
}
}
}"}, "headers": {"content-type": "application/json"}, "method": "POST", "url": "http://127.0.0.1:55679/graphql"}, "status": 404, "text": "<!DOCTYPE
html>
<html lang=\"en\">
<head>
<meta charset=\"utf-8\">
<title>Error</title>
</head>
<body>
<pre>Cannot POST /graphql</pre>
</body>
</html>
"}
108 | // .expect(404)
109 | .expect((response) => {
> 110 | expect(response).toEqual(
| ^
111 | expect.objectContaining({
112 | login: {
113 | token: expect.any(String),
at app.e2e-spec.ts:110:26
at ../node_modules/supertest/lib/test.js:80:15
at Test.Object.<anonymous>.Test._assertFunction (../node_modules/supertest/lib/test.js:311:11)
at Test.Object.<anonymous>.Test.assert (../node_modules/supertest/lib/test.js:201:21)
at Server.localAssert (../node_modules/supertest/lib/test.js:159:12)
A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks.
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 20.85 s, estimated 29 s
Ran all test suites.
Watch Usage: Press w to show more.
我注意到这里有些东西我找不到 404 状态代码,并且不同的 url 端口与我使用的后端 url 不匹配,所以我将超测试请求发送到错误的 url 并收到 {data:“null”} 但我不知道怎么做要解决这个问题 。