EasyGraphQL / easygraphql-tester

Test GraphQL queries, mutations and schemas on an easy way! 🚀
https://easygraphql.com/docs/easygraphql-tester/overview
MIT License
314 stars 34 forks source link

Testing async resolvers #93

Closed renegoretzka closed 5 years ago

renegoretzka commented 5 years ago

I have a problem testing async resolvers. I get an error which says "Promise returned by test never resolved".

How to get rid of it? I need async in the resolvers ofcourse.

estrada9166 commented 5 years ago

@renegoretzka can you share me your test case pls?

renegoretzka commented 5 years ago

user.resolvers.js

export default {
  Query: {
    getUser: async (root, { _id }) => {
      try {
        const user = await User.findById(_id)
        return user
      } catch (error) {
        throw new Error(error)
      }
    }
}

test.js

import test from 'ava'
import gql from 'graphql-tag'
import EasyGraphQLTester from 'easygraphql-tester'
import { getSchema } from '../utils'
import { expect } from 'chai'

import UserResolvers from '@graphql/User/user.resolvers'
const UserSchema = getSchema('User', 'user')

const tester = new EasyGraphQLTester(UserSchema, UserResolvers)

test('Query: getUser', async t => {
  const getUser = `
    query GET_USER($_id: String!) {
      getUser(_id: $_id) {
        name
        _id
      }
    }
  `
  const result = await tester.graphql(
    getUser,
    {},
    {},
    {
      _id: '5c8d1b7c1b06595010cee8cd'
    }
  )
  t.log(result)
}

The function getSchema() is just getting my right Schema. I use Ava for testing.

estrada9166 commented 5 years ago

I'm trying to reproduce but it's not possible... do you have a better trace of the error?

Also, are you initializing the db connection before the tests?

renegoretzka commented 5 years ago

Thats a great question! As the server (mongo) connection only runs when i start graphql, but in the test its not initialized. I am thinking about how to solve that it connects in the test environment.

estrada9166 commented 5 years ago

Maybe try doing something like this

estrada9166 commented 5 years ago

@renegoretzka can we close this issue?

estrada9166 commented 5 years ago

I'm going to close this issue, in case that is needed we can open again 🙂