Has anyone come across any examples of using jest ...
# prisma-whats-new
k
Has anyone come across any examples of using jest to test resolvers? Only examples I've seen so far are with ava but ideally we'd like to use jest
m
I've written a few tests in jest. Here's an example
Copy code
import { GraphQLClient } from "graphql-request";

const { GRAPHQL_SERVER_URL, WRONG_TOKEN, TEST_EMAIL, TEST_PASSWORD, TEST_NAME } = process.env

interface User {
  id: string
  name: string
  email: string
}

const userQuery = `
  {
    user {
      id
      name
      email
    }
  }
`;

const authenticationMutation = `
  mutation authenticateUser($email: String! $password: String!) {
    authenticateUser(email: $email, password: $password) {
      token
      user {
        id
        name
        email
      }
    }
  }
`;


test("if token is malformed, it should throw an error", () => {
  expect.assertions(1);
  const config = {
    headers: {
      Authorization: "123"
    }
  }
  const client = new GraphQLClient(GRAPHQL_SERVER_URL, config);
  return client.request(userQuery)
  .then(response =>
    // expect(response).toEqual({ user: null })
    console.log('response',response)
  )
  .catch(e => {
    // console.log('e.response',e.response)
    expect(e.response.errors[0].message).toEqual("jwt malformed")
  });
});

test("if token is invalid, it should throw an error", () => {
  expect.assertions(1);
  const config = {
    headers: {
      Authorization: `Bearer ${WRONG_TOKEN}`
    }
  }
  const client = new GraphQLClient(GRAPHQL_SERVER_URL, config);
  return client.request(userQuery)
  .then(response =>
    // expect(response).toEqual({ user: null })
    console.log('response',response)
  )
  .catch(e => {
    // console.log('e.response',e.response)
    expect(e.response.errors[0].message).toEqual("invalid signature")
  });
});
@Kyle Gammon
v
const _ = require(‘lodash’); const { graphql } = require(‘graphql’); const schema = require(‘../schema’); const articles = require(‘../data/articles.json’); describe(‘whishlist query’, () => { test(‘should return a list of articles’, async () => { const query = ` query { wishlist { id suggestion image subTitle } } `; const rootValue = {}; const context = { articles: _.clone(articles) }; const result = await graphql(schema, query, rootValue, context); const { data } = result; expect(data.wishlist).toEqual(context.articles); }); });
k
Thanks both - don't suppose you could share any related configuration for this? Falling at the first hurdle with presumably some bad config for jest (i.e. not finding 'import' or 'export' as expected tokens) as our Graphcool CLI source code is pretty barebones
m
@Kyle Gammon you'll want to get the token you receive from the authentication mutation and pass it into your headers under
Authorizations
. See the
config
var below:
Copy code
test("if token is valid, it should return user details", () => {
  expect.assertions(2);
  const config = {
    headers: {
      Authorization: `Bearer ${USER_TOKEN}`
    }
  }
  const client = new GraphQLClient(GRAPHQL_SERVER_URL, config);
  return client.request(userQuery)
  .then( (response: {user: User}) => {
    // console.log('response',response)
    expect(response.user.name).toEqual(TEST_NAME)
    expect(response.user.email).toEqual(TEST_EMAIL)
  })
  .catch(e => console.log('e',e) );
});
v
Hi @Kyle Gammon, this is an example repo where u can find a basic config, it’s not production ready hope it can help, https://github.com/adamovittorio/adidas-wishlist