Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: add helpers to vitest (#1869)
## Describe your changes Fixes NAN-593 - Add reusable helpers to vitest
- Loading branch information
1 parent
7797512
commit 449ec1a
Showing
6 changed files
with
69 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export * from 'vitest'; | ||
|
||
/* eslint-disable @typescript-eslint/no-empty-interface */ | ||
interface CustomMatchers<TR = unknown> { | ||
toBeIsoDate: () => TR; | ||
toBeIsoDateTimezone: () => TR; | ||
toBeUUID: () => TR; | ||
} | ||
|
||
declare module 'vitest' { | ||
export interface Assertion<T = any> extends CustomMatchers<T> {} | ||
export interface AsymmetricMatchersContaining extends CustomMatchers {} | ||
export interface ExpectStatic<T = any> extends CustomMatchers<T> {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { expect } from 'vitest'; | ||
|
||
const dateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{1,6}Z$/; | ||
const dateRegexWithTZ = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{1,6}\+\d{2}:\d{2}$/; | ||
const uuidRegex = /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i; | ||
|
||
expect.extend({ | ||
toBeIsoDate: (received: any) => { | ||
if (received instanceof Date) { | ||
return { pass: true, message: () => '' }; | ||
} else if (typeof received === 'string' && dateRegex.test(received)) { | ||
return { pass: true, message: () => '' }; | ||
} | ||
|
||
return { | ||
message: () => `expected ${received} to be an ISO Date`, | ||
pass: false | ||
}; | ||
}, | ||
|
||
toBeIsoDateTimezone: (received: any) => { | ||
if (received instanceof Date) { | ||
return { pass: true, message: () => '' }; | ||
} else if (typeof received === 'string' && dateRegexWithTZ.test(received)) { | ||
return { pass: true, message: () => '' }; | ||
} | ||
|
||
return { | ||
message: () => `expected ${received} to be an ISO Date`, | ||
pass: false | ||
}; | ||
}, | ||
|
||
toBeUUID: (received: any) => { | ||
if (!uuidRegex.test(received)) { | ||
return { | ||
message: () => `expected ${received} to be a UUID v4`, | ||
pass: false | ||
}; | ||
} | ||
return { pass: true, message: () => '' }; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters