当前位置:  开发笔记 > 前端 > 正文

使用Graphql Apollo Server进行graphql-tester(单元测试)

如何解决《使用GraphqlApolloServer进行graphql-tester(单元测试)》经验,为你挑选了1个好方法。

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

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


    { raw: '{"errors":[{"message":"Cannot read property \'definitions\' of undefined"}]}',
      data: undefined,
      errors: [ { message: 'Cannot read property \'definitions\' of undefined' } ],
      headers: 
       { 'x-powered-by': 'Express',
         'content-type': 'application/json',
         date: 'Wed, 18 Jan 2017 05:56:22 GMT',
         connection: 'close',
         'transfer-encoding': 'chunked' },
      status: 400,
      success: false }
          1) Returns success


      0 passing (35ms)
      1 failing

      1) Unittest1 Returns success:
         TypeError: Cannot read property 'success' of undefined
          at Assertion. (node_modules/chai/lib/chai/core/assertions.js:890:14)
          at Assertion.ctx.(anonymous function) (node_modules/chai/lib/chai/utils/addMethod.js:41:25)
          at Assertion.somethingMethod (node_modules/chai-things/lib/chai-things.js:97:25)
          at Assertion.ctx.(anonymous function) (node_modules/chai/lib/chai/utils/overwriteMethod.js:49:33)
          at Assertion.allMethod (node_modules/chai-things/lib/chai-things.js:165:25)
          at Assertion.ctx.(anonymous function) (node_modules/chai/lib/chai/utils/overwriteMethod.js:49:33)
          at node_modules/chai-as-promised/lib/chai-as-promised.js:305:22
          at 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.should();
    chai.use(require('chai-things'));
    chai.use(require('chai-properties'));
    chai.use(require('chai-arrays'));
    chai.use(require('chai-as-promised'));

    describe('Sites', () => {
      let sitesTest = tester({
        url: 'http://localhost:3000/graphql'
      });

      describe('Unittest1', () => {
        const response = sitesTest('{viewer {id}}').then((data) => {
          console.log(data)
        });

        it('Returns success', () => {
          return response.should.eventually.have.property('success').equal(true);
        });

      });

    });

jschr.. 5

以下是我如何使用它:

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

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

})

Apollo的graphql server希望将内容类型设置为application/json有效负载{ query: "...", variables: {}}.



1> jschr..:

以下是我如何使用它:

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

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

})

Apollo的graphql server希望将内容类型设置为application/json有效负载{ query: "...", variables: {}}.

推荐阅读
云聪京初瑞子_617
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有