Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

mocha.js - Getting test coverage of GraphQL resolve function

I'm using mocha chai and supertest to test a new graphql endpoint set up on our Node/Express server.

I have all the tests running and passing accordingly but when I run the following script:

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

it is not counting the tests for users resolve function in the code coverage.

My graphql query endpoint looks as shown below:

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')

                ...
            }
        },
    ...
})

An example of one of my tests:

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

I perform 3 other tests with different inputs for the GET_USERS query. All of them pass. It just doesn't get tracked in coverage report.

New to graphql and unit/integration testing so any help is appreciated. Can supply additional info if needed.

question from:https://stackoverflow.com/questions/65650330/getting-test-coverage-of-graphql-resolve-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...