Answer the question
In order to leave comments, you need to log in
How to write a test for this resolver?
Hey!
I write unit tests for GraphQl resolvers , connect to the database using Sequelize , and write tests using Jest .
How to write a test for a resolver with a promise?
resolver
…
updateUser(root, { id, input }) {
return models.UserModel.findById(id) // Ищем User в БД по ID
.then(user => user.update(input)); // Добавляем объект с новыми значениями
}
…
…
it('update User', () => {
return Mutation.updateUser(null, {
currentId, // currentId = текущий ID юзера
input: {
login: 'T-Jane',
homeFloor: 2,
avatarUrl: 'http://pic.com/2',
},
})
.then((result) => {
return console.log(result);
});
});
…
TypeError: Cannot read property 'update' of null
7 | updateUser(root, { id, input }) {
8 | return models.UserModel.findById(id)
> 9 | .then(user => user.update(input));
10 | },
11 | removeUser(root, { id }) {
12 | return models.UserModel.findById(id)
at models.UserModel.findById.then.user (graphql/resolvers/mutation.js:9:23)
at tryCatcher (node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromiseCtx (node_modules/bluebird/js/release/promise.js:606:10)
at Async.Object.<anonymous>.Async._drainQueue (node_modules/bluebird/js/release/async.js:138:12)
at Async.Object.<anonymous>.Async._drainQueues (node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues [as _onImmediate] (node_modules/bluebird/js/release/async.js:17:14)
return models.UserModel.findById(id)
and returns the update null result. Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question