当我通过 graphql-playground 创建我的所有者时,它工作正常,但我的测试失败并回复我“body.data.createOwner 未定义”,没有数据。
// owner.e2e.spec.ts
describe('Owner test (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [
GraphQLModule.forRoot({
autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
}),
OwnerModule,
DatabaseModule
]
}).compile();
app = moduleRef.createNestApplication();
await app.init();
});
afterAll(async () => {
await app.close();
})
const createOwnerQuery = `
mutation createOwner($OwnerInput: OwnerInput!) {
createOwner(ownerInput: $OwnerInput) {
_id
name
firstname
email
password
firstsub
expsub
createdAt
updatedAt
}
}
`;
let id: string = '';
it('createOwner', () => {
return request(app.getHttpServer())
.post('/graphql')
.send({
operationName: 'createOwner',
variables: {
OwnerInput: {
name: 'adar',
firstname: 'adar',
email: 'adar@test.com',
password: 'testing',
firstsub: '2020-08-14',
expsub: '2020-07-13'
}
},
query: createOwnerQuery,
})
.expect(({ body }) => {
const data = body.data.createOwner <-- test fail at this line
id = data._id
expect(data.name).toBe(owner.name)
expect(data.email).toBe(owner.email)
expect(data.firstsub).toBe(owner.firstsub)
})
.expect(200)
})
// Output terminal
FAIL test/owner.e2e-spec.ts (9.567 s)
Owner test (e2e)
✕ createOwner (79 ms)
● Owner test (e2e) › createOwner
TypeError: Cannot read property 'createOwner' of undefined
98 | })
99 | .expect(({ body }) => {
> 100 | const data = body.data.createOwner
| ^
101 | id = data._id
102 | expect(data.name).toBe(owner.name)
103 | expect(data.email).toBe(owner.email)
at owner.e2e-spec.ts:100:40
at Test._assertFunction (../node_modules/supertest/lib/test.js:283:11)
at Test.assert (../node_modules/supertest/lib/test.js:173:18)
at Server.localAssert (../node_modules/supertest/lib/test.js:131:12)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 9.645 s, estimated 10 s
Ran all test suites.