0

I want to test some graphql mutations. but I am not sure what to give as context while writing test. for example, I want to call createPost mutation:

it('admin should create a post', async () => {
    await createPost(
      null,
      {
        title: 'How to write a blog post',
        body: 'lorem ipsum dollar emmet',
        published: true,
      },
      dummyContext
    ).should.be.fulfilled;
  });

here as a dummyContext I am using this:

  const dummyContext = {
    request: { get: () => token },
  };

but, it is not working. Inside createPost mutation logincheker method is called:

export async function loginChecker({ request }) {
  const Authorization = request.get('Authorization');
  if (Authorization) {
    const token = Authorization.replace('Bearer ', '');
    const userInJwt = jwtValidator(token);
    const user = await prisma.user.findOne({ where: { id: userInJwt.id } });
    if (!user) {
      throw new Error('Not Authorized');
    }
    return user;
  }
}

and I am getting this error while running the test cases

AssertionError: expected promise to be fulfilled but it was rejected with 'TypeError: Authorization.replace is not a function

Ashik
  • 2,888
  • 8
  • 28
  • 53

1 Answers1

0

If you're looking for Integrations tests, you do not need to mock the Context. You can directly run the server against an actual database as I have created here. In the __tests__ folder, I have run it against the actual server and so you don't need to mock the Context and can directly call the API against the DB.

Ryan
  • 5,229
  • 1
  • 20
  • 31