Quramy / prisma-fabbrica

Prisma generator to define model factory

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

モデル定義に'@unique'属性が存在すると'TypeError: Cannot read properties of undefined (reading 'generate')' が発生する

h-furutani-sansan opened this issue · comments

Describe the bug
ユニークキーを持った not null の string カラムで
'TypeError: Cannot read properties of undefined (reading 'generate')'
が発生しテスト実行時エラーになる

To Reproduce
後述するschema.prismaを用意し、テストを実行で確認できます。

Expected behavior
実行時エラーが発生しないこと

Your Schema

// Paste schema.prisma file.
model Users {
  id BigInt @id @default(autoincrement())
  name String
  email String @unique

  createdAt DateTime @default(now())
  updatedAt DateTime @default(now()) @updatedAt
}

factory 定義

const usersFactory = defineUsersFactory({
  defaultData: async ({ seq }) => {
    return {
      name: 'test user',
      email: `test_${seq}@example.com`,
    };
  },
});
beforeEach(async () => {
  await usersFactory.create();
});

ログ

TypeError: Cannot read properties of undefined (reading 'generate')

  220 |  return {
  221 |    name: getScalarFieldValueGenerator().String({ modelName: "Users", fieldName: "name", isId: false, isUnique: false, seq }),
> 222 |    email: getScalarFieldValueGenerator().String({ modelName: "Users", fieldName: "email", isId: false, isUnique: true, seq })
      |                                               ^
  223 |  };
  224 | }
  225 |

  at Object.String (node_modules/.pnpm/@quramy+prisma-fabbrica@2.0.1_@prisma+client@5.2.0_encoding@0.1.13_typescript@5.2.2/node_modules/@quramy/prisma-fabbrica/lib/scalar/gen.js:12:41)
  at autoGenerateUsersScalarsOrEnums (fabbrica/index.ts:222:47)
  at build (fabbrica/index.ts:233:40)
  at Object.create (fabbrica/index.ts:253:32)

Environment (please complete the following information):

  • Database kind [mysql:8.1 ]
  • Node.js version [v18.17.1]
  • @prisma/client version [5.2.0]
  • TypeScript version [5.2.2]

Additional context

  • schema.prisma の定義で @default を設定することで回避しています。
  • prisma と prisma-fabbrica のバージョンを変更して確認したところ、発生していない様でした。
    • 確認したバージョン: "@prisma/client": "^4.12.0", "@quramy/prisma-fabbrica": "^1.0.3",

When the @unique attribute exists in the model definition, a TypeError: Cannot read properties of undefined (reading 'generate') error occurs.

Describe the bug
The error TypeError: Cannot read properties of undefined (reading 'generate') occurs when using a string column with a unique constraint and not null, causing a test execution error.

To Reproduce
You can prepare the schema.prisma mentioned below and execute the test to confirm.

Expected behavior
No runtime error should occur.

Additional context

  • The issue is being avoided by setting @default in the schema.prisma definition.
  • When testing with different versions of Prisma and prisma-fabbrica, the issue does not occur.
  • Versions tested: "@prisma/client": "^4.12.0", "@quramy/prisma-fabbrica": "^1.0.3"

@h-furutani-sansan

返信が遅くなってすいません

Sorry for my late reply 🙇

prisma-fabbrica の問題ではないと思います.
エラーログには TypeError: Cannot read properties of undefined (reading 'generate') とあり、generate 関数はshort-uuid パッケージからインポートしたものです.

I think it's not prisma-fabbrica bug.
I found TypeError: Cannot read properties of undefined (reading 'generate') message from your error log, and generate function corresponds to this line, which calls imported function from short-uuid package:

まずは node_modules に short-uuid パッケージが正しく含まれていることを確認してみてください。

Please confirm that the short-uuid package is installed into your node_modules .

@Quramy

ご確認いただきまして、ありがとうございます。

Thank you for checking.

ご教授いただきました、 short-uuid パッケージをプロジェクトに追加して
テスト実行してみたのですがエラーの回避はできませんでした。
Thanks for your guidance, add the short-uuid package to your project.
I tried running a test, but was unable to avoid the error.

変更点と致しましては、ご教授頂く前と後での違いは、 short-uuid のパッケージ有無のみになります。

Regarding the changes, the only difference between before and after your instruction is the presence or absence of the short-uuid package.

大変お手数ではあるのですが、その他試した方が良いアプローチなどありますでしょうか?
よろしくお願い致します。

I'm sorry to bother you, but are there any other approaches I should try?
Thank you for your support.

commented

Hi, While this might be specific to our project,

Initially, We had set up the following configuration in jest.config.js to resolve the uuid module issue in jest-environment-jsdom.

{
   moduleNameMapper: {
     // see: https://github.com/uuidjs/uuid/issues/451#issuecomment-1112328417
     uuid: require.resolve('uuid')
   }
}

However,

If you provide module names without boundaries ^$ it may cause hard to spot errors. E.g. relay will replace all modules which contain relay as a substring in its name: relay, react-relay and graphql-relay will all be pointed to your stub.

https://jestjs.io/ja/docs/configuration

As mentioned above, it seemed that a partial match was being made, and short-uuid also adopted the absolute path of uuid(and short was undefined).

I resolved it by doing the following:

{
   moduleNameMapper: {
     "^uuid$": require.resolve('uuid')
   }
}

@kz-d @Quramy

ご確認と情報共有ありがとうございます。

Thank you for checking and sharing information.

ご教授いただきました内容で解決致しました。
誠にありがとうございます。

I was able to solve the problem with the information you provided.
Thank you very much.

こちらはクローズさせていただきます。

This will be closed.