Skip to content

buildSchema with glob in jest test throws duplicate type error #545

@ch1ll0ut1

Description

@ch1ll0ut1

Describe the bug
Using buildSchema with a list of imported modules works in normal app and jest tests.
Using buildSchema with a glob to read the same two modules from a directory works in normal app but not in jest tests

To Reproduce
/resolver directory has following files:

  • src/resolver/CampaignResolver.ts
  • src/resolver/DiscountResolver.ts
  • src/resolver/index.ts
export const build = () => buildSchema({ resolvers: [`${__dirname}/*Resolver.{js,ts}`] });

Test Case:


const allCampaignsTestCase = {
  id: 'All campaigns with discount',
  query: `
    query {
      campaigns {
          id 
          name
          discount {
            type
            name
            value
          }
      }
    }
    `,
  variables: { },

  expected: {
    data: {
      campaigns: [
        {
          id: '1',
          name: 'campaign1',
          discount: {
            name: 'test',
            value: null,
            type: 'SHIPPING',
          },
        },
        {
          id: '2',
          name: 'campaign2',
          discount: null,
        },
      ],
    },
  },
};

let schema: GraphQLSchema;
let connection: Connection;

beforeEach(async () => {
  connection = await createTestConnection();
  await seedDatabase();
  schema = await buildSchema();
});

afterEach(async () => {
  await connection.close();
});

describe('Campaign Page', () => {
  const cases = [allCampaignsTestCase];

  cases.forEach((object) => {
    const { id, query, variables, expected } = object;

    it(`query: ${id}`, async () => {
      const result = await graphql(schema, query, null, null, variables);

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

Expected behavior
Behave exactly same as the following code being used by the jest test:

import CampaignResolver from './CampaignResolver';
import DiscountResolver from './DiscountResolver';
export const build = () => buildSchema({ resolvers: [CampaignResolver, DiscountResolver] });

Logs

    ✕ query: All campaigns with discount (1257ms)

  ● Campaign Page › query: All campaigns with discount

    Schema must contain uniquely named types but contains multiple types named "Discount".

      22 | 
      23 | // const buildSchema = () => build({ resolvers: [CampaignResolver, DiscountResolver] });
    > 24 | const buildSchema = () => build({ resolvers: [`${__dirname}/*Resolver.{js,ts}`] });
         |                           ^
      25 | 
      26 | export default buildSchema;
      27 | 

      at typeMapReducer (node_modules/graphql/type/schema.js:262:13)
          at Array.reduce (<anonymous>)
      at new GraphQLSchema (node_modules/graphql/type/schema.js:145:28)
      at Function.generateFromMetadataSync (node_modules/type-graphql/dist/schema/schema-generator.js:30:24)
      at Function.generateFromMetadata (node_modules/type-graphql/dist/schema/schema-generator.js:15:29)
      at Object.buildSchema (node_modules/type-graphql/dist/utils/buildSchema.js:9:61)
      at Object.buildSchema [as default] (src/resolver/index.ts:24:27)
      at Object.<anonymous> (src/test/integration/CampaignPage.test.ts:53:29)

  ● Campaign Page › query: All campaigns with discount

    Expected undefined to be a GraphQL schema.

      65 | 
      66 |     it(`query: ${id}`, async () => {
    > 67 |       const result = await graphql(schema, query, null, null, variables);
         |                            ^
      68 | 
      69 |       expect(result).toEqual(expected);
      70 |     });

      at assertSchema (node_modules/graphql/type/schema.js:41:11)
      at validateSchema (node_modules/graphql/type/validate.js:44:28)
      at graphqlImpl (node_modules/graphql/graphql.js:79:62)
      at node_modules/graphql/graphql.js:28:59
      at Object.graphql (node_modules/graphql/graphql.js:26:10)
      at Object.<anonymous> (src/test/integration/CampaignPage.test.ts:67:28)

Enviorment (please complete the following information):

  • OS: MacOSX 10.12.6
  • Node: v12.14.1
  • Package: 0.18.0-beta.10
  • TypeScript: 3.7.5

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions