首页 > 解决方案 > 所有测试都通过时测试套件失败:Jest & Supertest with GraphQL request

问题描述

我有以下帮助函数用于在 Jest 中发送我的 GraphQL 请求:

export async function graphQLBase(requestString: string, variables?: any) {
  const sendRequest: TestGQLRequest = { query: requestString, variables }
  return request(app)
    .post('/spikeql')
    .set('Authorization', `Bearer ${mockToken}`)
    .send(sendRequest)
    .expect('Content-Type', /json/)
    .expect(200)
}

然后我有以下测试套件:

const createPostMutation = `mutation Mutation($postInfo: PostCreationInfo) {
  createPost(postInfo: $postInfo) {
    description
  }
}`

describe('Query GQL: Posts', () => {
  beforeAll(async () => {
    await mongoConnect()
  })

  describe('create post', () => {
    test('It should create a text post', async () => {
      const testTextPostDesc = 'Test post via supertest unit test'
      const createPostVariables = {
        postInfo: {
          description: testTextPostDesc
        }
      }

      const { body } = await graphQLBase(createPostMutation, createPostVariables)
      // const { description, createdBy } = body.data.createPost
      // expect(description).toBeDefined()
      // expect(createdBy).toBeDefined()
      // expect(description === testTextPostDesc)
      expect(1)
    })  
  })

  afterAll(async () => {
    await mongoDisconnect()
  })
})

我的测试套件中出现以下错误,而位于测试套件中的测试完美通过。我还添加了一个日志来查看突变是否返回任何值,并且还可以确认它正确地将资源添加到数据库中。

使用 Supertest 时是否需要额外考虑突变?到目前为止,查询工作正常。

我得到的错误如下:在此处输入图像描述

标签: node.jsmongodbgraphqlsupertestts-jest

解决方案


推荐阅读