Jest – TypeGraphQL Invalid or Unexpected Token

Jest – TypeGraphQL Invalid or Unexpected Token


0

I’m trying to create unit tests on my GraphQL API.

This is the test file:

import { graphql } from "graphql";
import { createSchema } from "../src/schema";

describe("BlacklistResolver", () => {
  it("fetches blacklist", async () => {
    const schema = await createSchema();
    const query = `
  query GetBlacklist($organisationId: String!) {
    getBlacklist(organisationId: $organisationId)
  }
`;

    const variables = {
      organisationId: "ab5e2add-c209-4f4c-8d65-8144bc1e741d" // Replace with the desired ID
    };

    const expectedResult = {
      data: {
        getBlacklist: ["item1", "item2", "item3"]
      }
    };

    const result = await graphql({
      schema,
      source: query,
      variableValues: variables
    });

    expect(result).toEqual(expectedResult);
  });
});

And this is how I build my schema:

import { buildSchema } from "type-graphql";

export async function createSchema() {
  const schema = await buildSchema({
    resolvers: [`${__dirname.replace(/\/g, "/")}/resolvers/**/*.ts`],
    validate: false,
    emitSchemaFile: true
  });
  return schema;
}

But when I run my tests I have the following error:

    SyntaxError: Invalid or unexpected token

      11 | export async function createSchema() {
    > 12 |   const schema = await buildSchema({
         |                                   ^
      13 |     resolvers: [`${__dirname.replace(/\/g, "/")}/resolvers/**/*.ts`],
      14 |     validate: false,
      15 |     emitSchemaFile: true

Any ideas on how to run my test properly ?

I tried adding a .babelrc with some config as I had seen it in other topics, I also tried to use the await buildSchema (before I was just doing this for my schema:

import { buildSchemaSync } from "type-graphql";

export const schema = buildSchemaSync({
  resolvers: [`${__dirname.replace(/\/g, "/")}/resolvers/**/*.ts`],
  validate: false,
  emitSchemaFile: true
});

None of it changed a thing

Share


Load 7 more related questions


Show fewer related questions

0

Reset to default



Browse other questions tagged

or ask your own question.

Leave a Reply

Your email address will not be published. Required fields are marked *