获取 GraphQL 解析函数的测试覆盖率

问题描述

我正在使用 mocha chaisupertest 来测试在我们的 Node/Express 服务器上设置的新 graphql 端点。

我已经运行并相应地通过了所有测试,但是当我运行以下脚本时:

    "test-coverage": "nyc mocha tests/ --recursive",

它不计算代码覆盖率中对 users 解析函数的测试。

我的 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 (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...