首页 文章

如何使用Mocha和Chai对graphql查询/变异错误进行单元测试?

提问于
浏览
1

由于 graphql 错误不是标准 Error . 这是 GraphQLError

graphql 查询/突变抛出异常时,我无法弄清楚如何编写单元测试 .

这是我的尝试:

it('should throw an error when lack of "id" argument for user query', async () => {
    const body = {
      query: `
        query user{
          user {
            id
            name
          }
        }
      `
    };

    try {
      const actualValue = await rp(body);
    } catch (err) {
      logger.error(err);
    }

    // logger.info(actualValue);
    expect(1).to.be.equal(1);
    // expect(actualValue).to.throw()
  });

我在 graphql.js repo中找到了一些测试 . https://github.com/graphql/graphql-js/blob/master/src/tests/starWarsQuery-test.js#L393

但我认为他们测试 query/mutation 错误的方式不正确 .

他们只是做错了 . 但在运行测试套件之前,我如何知道错误数据结构,如 locations: [{ line: 5, column: 13 }],

1 回答

相关问题