Is the use of if (result.success) instead of expect.assert(result.success) intentional in tests?
#6341
Replies: 2 comments
|
Your proposed version is valid with the Vitest version used by Zod today. Zod currently declares const result = schema.safeParse(input);
expect.assert(result.success);
expect(result.data).toEqual(...);Both forms fail the test when The code alone does not establish whether Zod's current pattern was an intentional style choice. There is no technical reason to avoid |
|
The pattern in Zod's test suite is intentional, driven by two primary considerations: 1. The Jest → Vitest HeritageZod's test suite was originally authored in Jest before the codebase migrated to Vitest. Jest does not provide When migrating test runners, test suites generally preserve existing test code to avoid mass-churn on stable tests. 2. Mirroring Userland
|
Uh oh!
There was an error while loading. Please reload this page.
I am currently learning about writing test code, especially how TypeScript narrowing works with Vitest assertions.
While reading Zod's tests, I found several cases using the following pattern:
I understand that
expect(result.success).toBe(true)does not narrow the type ofresult, so the additionalifstatement is needed before accessingresult.data.I noticed that Vitest also exposes Chai's
assertAPI throughexpect.assert, so I wondered whether the following approach had been considered:Is the current use of
if (result.success)intentional, for example to keep the tests consistent with typicalsafeParseusage?Or is there another reason not to use
expect.assertfor narrowing?I understand that the current tests work correctly, so this is not a bug report. I was simply curious whether this pattern is intentional.
All reactions