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

ZodError's addIssue and addIssues properties are not comparable #3950

Open
Amorim33 opened this issue Jan 21, 2025 · 0 comments
Open

ZodError's addIssue and addIssues properties are not comparable #3950

Amorim33 opened this issue Jan 21, 2025 · 0 comments

Comments

@Amorim33
Copy link

Description

Recent changes to vitest error comparison behavior have caused the following test to fail:

import { z } from 'zod';

it('throws a zod error', () => {
  expect(() => {
    throw new z.ZodError([
      {
        received: 'aa',
        code: 'invalid_enum_value',
        options: ['development', 'production', 'test'],
        path: ['NODE_ENV'],
        message:
          "Invalid enum value. Expected 'development' | 'production' | 'test', received 'aa'",
      },
    ]);
  }).toThrowError(
    new z.ZodError([
      {
        received: 'aa',
        code: 'invalid_enum_value',
        options: ['development', 'production', 'test'],
        path: ['NODE_ENV'],
        message:
          "Invalid enum value. Expected 'development' | 'production' | 'test', received 'aa'",
      },
    ]),
  );
});

toThrow and toThrowError are failing to compare a thrown ZodError.

I’ve already opened an issue on the Vitest repository vitest-dev/vitest#7315.

After a gentle and careful debugging session by @hi-ogawa, it appears that the root cause of the error is the fact that the addIssue and addIssues properties are enumerable. This behavior prevents instances from being compared effectively.

Here’s a Stackblitz showcasing the debug findings: https://stackblitz.com/edit/vitest-dev-vitest-bruhvcfu?file=test%2Frepro.test.ts.

If aligning with the library's goals, I’d be so stoked to submit a PR to make the addIssue and addIssues properties non-enumerable. ✨

Thanks in advance! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant