【问题标题】:graphql-tester (unit tests) with Graphql Apollo Server带有 Graphql Apollo Server 的 graphql-tester(单元测试)
【发布时间】:2017-06-02 10:18:29
【问题描述】:

如何为 graphql 编写单元测试。我正在使用 apollo 服务器、graphql-tester 和 graphql。

当我运行测试时,它会出现以下错误

{ raw: '{"errors":[{"message":"Cannot read property \'definitions\' of undefined"}]}', 数据:未定义, 错误:[ {消息:'无法读取未定义的属性\'定义\'}], 标题: { 'x-powered-by': '快递', '内容类型':'应用程序/json', 日期:'2017 年 1 月 18 日,星期三,格林威治标准时间 05:56:22', 连接:'关闭', '传输编码':'分块'}, 状态:400, 成功:假} 1) 返回成功 0 次通过(35 毫秒) 1 次失败 1) Unittest1 返回成功: TypeError:无法读取未定义的属性“成功” 在断言。 (node_modules/chai/lib/chai/core/assertions.js:890:14) 在 Assertion.ctx.(匿名函数) (node_modules/chai/lib/chai/utils/addMethod.js:41:25) 在 Assertion.somethingMethod (node_modules/chai-things/lib/chai-things.js:97:25) 在 Assertion.ctx.(匿名函数) (node_modules/chai/lib/chai/utils/overwriteMethod.js:49:33) 在 Assertion.allMethod (node_modules/chai-things/lib/chai-things.js:165:25) 在 Assertion.ctx.(匿名函数) (node_modules/chai/lib/chai/utils/overwriteMethod.js:49:33) 在 node_modules/chai-as-promised/lib/chai-as-promised.js:305:22 在 process._tickCallback (internal/process/next_tick.js:103:7)

接下来是单元测试。

const tester = require('graphql-tester').tester; const fromGlobalId = require('graphql-relay').fromGlobalId; const chai = require('chai'); 柴.应该(); chai.use(require('chai-things')); chai.use(require('chai-properties')); chai.use(require('chai-arrays')); chai.use(require('chai-as-promised')); 描述('网站',()=> { 让站点测试=测试器({ 网址:'http://localhost:3000/graphql' }); 描述('Unittest1',()=> { const response = sitesTest('{viewer {id}}').then((data) => { 控制台日志(数据) }); it('返回成功', () => { 返回 response.should.eventually.have.property('success').equal(true); }); }); });

【问题讨论】:

  • 同样的设置,得到同样的错误。你搞定了吗?
  • 我转移到其他任务,没有时间再次检查。将检查您下面提到的答案。感谢您的帮助..

标签: graphql apollo-server apollo-client


【解决方案1】:

这是我的工作原理:

const gql = tester({
  server: createExpressWrapper(server),
  url: '/graphql',
  authorization: `Bearer ${token}`,
  contentType: 'application/json'
})

gql(JSON.stringify({ query: '{ users { id } }' })).then((data) => {

})

Apollo 的 graphql 服务器期望内容类型设置为application/json,有效负载为{ query: "...", variables: {}}

【讨论】:

    【解决方案2】:

    也许您可以使用easygraphql-tester,使用此包,您可以针对您的架构测试查询/突变/订阅。它可用于返回模拟或断言包,因此在您的情况下,您可以执行以下操作:

    const EasyGraphQLTester = require('easygraphql-tester')
    
    const tester = new EasyGraphQLTester(schema)
    const query = `
      {
        viewer {
          id
        }
      }
    `
    const mock = tester.mock({ query })
    

    然后您可以测试模拟的结果...或者,您也可以在那里设置固定装置!

    【讨论】:

      【解决方案3】:

      graphql-tester 无关,而是 Apollo-Server 的替代方案。也许有帮助。

      我发现使用apollo-server-testing 编写集成测试非常方便。它将为不需要运行服务器的 apollo-server 实例创建一个测试客户端。

      import { ApolloServer } from 'apollo-server';
      import { createTestClient } from 'apollo-server-testing';
      
      const prepareServer = async () => {
        // creating your real schema
        const schema = await Schemas.getSchemas();
        return new ApolloServer({
          schema,
          dataSources: () => ({
            data: new MockDatasource(), // provides mocked data
          }),
        });
      };
      
      it('test query', async () => {
        const server = await prepareServer();
        const { query } = createTestClient(server);
      
        const result = await query({ query: QUERY_GQL });
        // test result data
      });
      

      更详细的例子在 apollo 页面:https://www.apollographql.com/docs/apollo-server/features/testing.html

      【讨论】:

        【解决方案4】:

        您确定 GraphQL API 正在运行吗?让测试套件为您处理启动测试服务器通常是一种很好的做法,这样您就可以在没有外部依赖关系的情况下运行测试。我将一个包装好的快递服务器连同一个相对 URL 传递给 graphql-tester

        例如,

        const expressApp = initApp();
        const client = tester({
          server: createExpressWrapper(expressApp),
          url: '/graphql',
        });
        

        假设 initApp() 确实返回了一个快速应用程序,该应用程序将在 /graphql 端点运行 GraphQL API,我相信它应该适合您。

        供参考,这里是createExpressWrapper的实现。 https://github.com/sazzer/graphql-tester/blob/master/src/main/servers/express.js

        【讨论】:

          猜你喜欢
          • 2020-10-05
          • 2021-01-08
          • 2019-06-12
          • 1970-01-01
          • 2019-09-01
          • 2019-07-26
          • 2019-12-19
          • 2017-05-24
          • 2017-07-19
          相关资源
          最近更新 更多