问题描述
我正在使用 mocha
chai
和 supertest
来测试在我们的 Node/Express 服务器上设置的新 graphql 端点。
我已经运行并相应地通过了所有测试,但是当我运行以下脚本时:
"test-coverage": "nyc mocha tests/ --recursive",
我的 graphql 查询端点如下所示:
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',fields: () => ({
users: {
type: new GraphQLList(UserType),args: {
searchByName: { type: GraphQLString },queryNearbyUsers: { type: GraphQLBoolean },skip: { type: GraphQLInt },limit: { type: GraphQLInt }
},async resolve(parent,args,req) {
const { searchByName,queryNearbyUsers,skip = 0,limit = 20 } = args
// No search criteria was specified so just return an error
if(!searchByName && !queryNearbyUsers)
throw new Error('NO_SEARCH_CRITERIA_SPECIFIED')
...
}
},...
})
我的一个测试示例:
it('should throw an error (NO_SEARCH_CRITERIA_SPECIFIED) when no params supplied',function(done) {
request.post('spikeql')
.set({ Authorization: `Bearer ${token}`})
.send({ query: '{ users { _id firstName lastName displayName rpr distanceAway avatar { styles { thumb_square }}}}'})
.expect(200) // Todo: Setup GraphQL to match approproate HTTP res codes
.end((err,res) => {
if(err) return done(err)
let errors = res.body.errors
expect(errors).to.be.an('array')
// Check to make sure error was sent properly
expect(errors[0]).to.have.property('message','NO_SEARCH_CRITERIA_SPECIFIED')
expect(errors[0].message).to.be.a('string')
done()
})
})
我对 GET_USERS
查询使用不同的输入执行了其他 3 次测试。他们都通过了。只是没有在覆盖率报告中进行跟踪。
graphql 和单元/集成测试的新手,因此感谢任何帮助。如果需要,可以提供其他信息。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)