graphql-codegen-typescript-fabbrica icon indicating copy to clipboard operation
graphql-codegen-typescript-fabbrica copied to clipboard

No type error is reported when an unknown trait name is passed to use

Open mizdra opened this issue 1 year ago • 0 comments
trafficstars

This problem occurs with factories where no traits are defined.

https://stackblitz.com/edit/playground-graphql-codegen-typescript-fabbrica-1ryp1f?file=src%2Findex.test.ts&view=editor

import {
  defineBookFactory,
  defineAuthorFactory,
  dynamic,
} from '../__generated__/fabbrica';
import { test, expect, expectTypeOf } from 'vitest';

// Define factories
const BookFactory = defineBookFactory({
  defaultFields: {
    __typename: 'Book',
    id: dynamic(({ seq }) => `Book-${seq}`),
    title: 'Yuyushiki',
    author: undefined,
  },
});
const AuthorFactory = defineAuthorFactory({
  defaultFields: {
    __typename: 'Author',
    id: dynamic(({ seq }) => `Author-${seq}`),
    name: 'Komata Mikami',
    books: undefined,
  },
  traits: {
    withoutBooks: {
      defaultFields: {
        books: [],
      },
    },
  },
});

test('throw a compile error when an unknown trait name is passed to `.use()`', async () => {
  // @ts-expect-error -- Should throw a compile error, but does not throw.
  await BookFactory.use('unknownTrantName').build();
  // @ts-expect-error
  await AuthorFactory.use('unknownTrantName').build();
});
$ npm start

> start
> npm run gen && npm run lint && npm run test


> gen
> graphql-codegen

✔ Parse Configuration
✔ Generate outputs

> lint
> tsc

src/index.test.ts:34:3 - error TS2578: Unused '@ts-expect-error' directive.

34   // @ts-expect-error -- Should throw a compile error, but does not throw.
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error in src/index.test.ts:34

npm ERR! code EIO
npm ERR! syscall write
npm ERR! errno -5
npm ERR! EIO: i/o error, write

npm ERR! A complete log of this run can be found in: /home/.npm/_logs/2024-03-09T05_46_36_334Z-debug-0.log
jsh: spawn npm EIO

mizdra avatar Feb 05 '24 03:02 mizdra