0

I wrote a test which creates a Venue with attachments property which is of Graphql Upload type. But when I try to run the test, it fails with error "message": "Variable \"$attachments\" got invalid value { fieldName: \"1\", filename: \"valid-image.png\", mimetype: \"image/png\", encoding: \"7bit\" } at \"attachments[0]\"; Upload value invalid." basically Upload value invalid.

In backend the attachments is structure like that

{
  fieldName: '1',
  filename: 'Screenshot 2023-09-05 193500.png',
  mimetype: 'image/png',
  encoding: '7bit',
  createReadStream: [Function: createReadStream]
}

Unit test

it("should create a venue with valid attachments", async () => {
    const imagePath = join(__dirname, "test-files", "valid-image.png");
    const variables = {
      organizationId: orgId,
      name: `Test Venue ${faker.string.alphanumeric(5)}`,
      description: "Test venue with valid attachments",
      capacity: 50,
      attachments: [
        {
          fieldName: "1",
          filename: "valid-image.png",
          mimetype: "image/png",
          encoding: "7bit",
          createReadStream: () => createReadStream(imagePath),
        },
      ],
    };

    const result = await mercuriusClient.mutate(Mutation_createVenue, {
      headers: { authorization: `bearer ${authToken}` },
      variables,
    });

    console.log('result is ', result);
   
  })

I have never tested FileUpload so I'm not sure whether I'm correctly testing it or not.

Any help would be appreciated.

1
  • "I want to unit test graphQL" the two pieces of that sentence don't match. GraphQL is a wire format. You shouldn't be unit testing over the wire. That goes double for GraphQL since it's typed. You should be mocking that out in your unit tests rather than making actual mutations. Consider whether you a. actually need this test at all and b. whether it should be an e2e test rather than a unit test. Commented Mar 27 at 12:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.