Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mizdra opened this issue Feb 5, 2024 · 0 comments
Open
Labels
Type: Fix Fix bug.

Comments

@mizdra
Copy link
Owner

mizdra commented Feb 5, 2024

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 mizdra added the Type: Fix Fix bug. label Feb 5, 2024
@mizdra mizdra changed the title No type error is reported when an invalid trait name is passed to use No type error is reported when an unknown trait name is passed to use Mar 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Fix Fix bug.
Projects
None yet
Development

No branches or pull requests

1 participant