带有graphql Apollo服务器的graphql测试仪(单元测试)

带有graphql Apollo服务器的graphql测试仪(单元测试),graphql,apollo-server,apollo-client,Graphql,Apollo Server,Apollo Client,如何为graphql编写单元测试。我正在使用apollo服务器、graphql测试仪和graphql 当我运行测试时,它给出以下错误 { raw: '{"errors":[{"message":"Cannot read property \'definitions\' of undefined"}]}', data: undefined, errors: [ { message: 'Cannot read property \'definitions\' of undef

如何为graphql编写单元测试。我正在使用apollo服务器、graphql测试仪和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) {raw:'{“errors”:[{“message”:“无法读取未定义“}]}”的属性'definitions', 数据:未定义, 错误:[{message:'无法读取未定义'}的属性'definitions', 标题: {'x-powered-by':'Express', “内容类型”:“应用程序/json”, 日期:“2017年1月18日星期三05:56:22 GMT”, 连接:“关闭”, “传输编码”:“分块”}, 现状:400, 成功:错误} 1) 返回成功 0通过(35毫秒) 1失败 1) Unittest1返回成功: TypeError:无法读取未定义的属性“success” 一声令下。(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) 在节点_modules/chai as promised/lib/chai as promised.js:305:22 在进程中。_tick回调(内部/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); }); }); }); 常数测试仪=需要('graphql-tester')。测试仪; const fromGlobalId=require('graphql-relay')。fromGlobalId; const chai=要求(“chai”); chai.should(); chai.使用(要求(“chai-things”); chai.使用(要求(“chai-properties”); chai.使用(要求(“chai-array”); 柴。使用(要求(“柴照承诺”); 描述('站点',()=>{ 设sitesTest=tester({ 网址:'http://localhost:3000/graphql' }); 描述('Unittest1',()=>{ const response=sitesTest({viewer{id}}')。然后((数据)=>{ console.log(数据) }); 它('返回成功',()=>{ 返回响应.should.finally.have.property('success').equal(true); }); }); });
您确定GraphQLAPI正在运行吗?通常,让测试套件为您处理测试服务器的旋转,这样您就可以在没有外部依赖关系的情况下运行测试,这是一种很好的做法。我将一个包装好的express服务器连同一个相对url传递到
graphql tester

比如说,

const expressApp = initApp();
const client = tester({
  server: createExpressWrapper(expressApp),
  url: '/graphql',
});
假设
initApp()
确实返回了一个将在/GraphQL端点运行graphqlapi的express应用程序,我相信它应该适合您


以下是
createExpressWrapper
的实现,仅供参考

以下是我如何让它工作的:

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:“…”,变量:{}

也许您可以使用这个包,根据您的模式测试查询/突变/订阅。它可用于返回模拟或作为断言包,因此在您的情况下,您可以执行以下操作:

const EasyGraphQLTester = require('easygraphql-tester')

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

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

graphql测试仪无关,但它是Apollo服务器的替代品。也许有帮助

我发现使用apollo服务器测试编写集成测试非常舒服。它将为apollo服务器实例创建一个测试客户端,该实例不需要运行服务器

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
});

阿波罗页面上更详细的示例:

相同的设置,得到相同的错误。你把它做好了吗?我换了别的任务,没时间再检查了。我会检查你下面提到的答案。谢谢你的帮助。。