4

I'm just stuck on this one particular issue and hoping someone might be able to help, my ide is complaining about a syntax error Unexpected identifier, Im assuming I have a typo somewhere, but am at a loss to find it.

Error message:


  ● Test suite failed to run

    /Users/jake/learning/pact-testing-project/consumer/pact.spec.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import GraphQLService from "./api.service.graphql";
                                                                                                    ^^^^^^^^^^^^^^

    SyntaxError: Unexpected identifier

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)

and here is the code: organisation.js

class Organisation {
    constructor(name, id) {
        this.id = id;
        this.name = name;
    }

    static validateName(organisation) {
        if (typeof organisation.name !== 'string') {
            throw new Error(`organisation name must be a string! Invalid value: ${organisation.name}`)
        }
    }

    static validateId(organisation) {
        if (typeof organisation.id !== 'number') {
            throw new Error(`organisation id must be a number! Invalid value: ${organisation.id}`)
        }
    }
}

export default Organisation

api.service.graphql.js

import Organisation from "./organisation";
import {ApolloClient} from "apollo-client";
import {InMemoryCache} from "apollo-cache-inmemory";
import {HttpLink} from "apollo-link-http";
import gql from "graphql-tag";

class GraphQLService {

    constructor(baseUrl, port, fetch) {
        this.client = new ApolloClient({
            link: new HttpLink({
                uri: `${baseUrl}:${port}/graphql`,
                fetch: fetch
            }),
            cache: new InMemoryCache()
        });
    }

    getOrganisation(organisationId) {
        if (organisationId == null) {
            throw new Error("organisation id must not be null");
        }
        return this.client.query({
            query: gql`
            query GetOrganisation($organisationid: Int!) {
            organisation(id: $organisationid) {
                name
                }
            }
            `,
            variables: {
                organisationId: organisationId
            }
        }).then((response) => {
            return new Promise((resolve, reject) => {
                try {
                    const organisation = new Organisation(response.data.organisation.name, organisationId);
                    Organisation.validateName(organisation);
                    resolve(organisation);
                } catch (error) {
                    reject(error);
                }
            });
        });
    };
}
export default GraphQLService;

pact.spec.js (which houses the test that I'm trying to run)

import GraphQLService from "./api.service.graphql";
import Pact from "@pact-foundation/pact";
import fetch from "node-fetch";
import Organisation from "./organisation";

describe("OrganisationService GraphQL API", () => {

    const OrganisationService = new GraphQLService('http://localhost', global.port, fetch);

    // a matcher for the content type "application/json" in UTF8 charset
    // that ignores the spaces between the ";2 and "charset"
    const contentTypeJsonMatcher = Pact.Matchers.term({
        matcher: "application\\/json; *charset=utf-8",
        generate: "application/json; charset=utf-8"
    });

    describe('getOrganisation()', () => {

        beforeEach((done) => {

            global.provider.addInteraction(new Pact.GraphQLInteraction()
                .uponReceiving('a GetOrganisation Query')
                .withRequest({
                    path: '/graphql',
                    method: 'POST',
                })
                .withOperation("getOrganisation")
                .withQuery(`
                    query GetOrganisation($organisationid: Int!) {
                      organisation(id: $organisationid) {
                          name
                      }
                    }`)
                .withVariables({
                    organisationId: 1
                })
                .willRespondWith({
                    status: 200,
                    headers: {
                        'Content-Type': contentTypeJsonMatcher
                    },
                    body: {
                        data: {
                            hero: {
                                name: Pact.Matchers.somethingLike('example')
                            }
                        }
                    }
                })).then(() => done());
        });

        it('sends a request according to contract', (done) => {
            OrganisationService.getOrganisation(1)
                .then(organisation => {
                    expect(organisation.name).toEqual('example');
                })
                .then(() => {
                    global.provider.verify()
                        .then(() => done(), error => {
                            done.fail(error)
                        })
                });
        });

    });

});
Jake
  • 91
  • 5

1 Answers1

3

So this issue was fixed by refactoring the folder structure to have the tests inside the src directory, as this is what jest was expecting. I'm not 100% sure why this impacted how the import/export was working, but it's solved now.

Jake
  • 91
  • 5