@dereekb/vitest 13.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/VITEST_DATE_MATCHERS.md +145 -0
- package/package.json +21 -0
- package/src/extend.d.ts +6 -0
- package/src/extend.js +6 -0
- package/src/extend.js.map +1 -0
- package/src/index.d.ts +1 -0
- package/src/index.js +2 -0
- package/src/index.js.map +1 -0
- package/src/lib/index.d.ts +1 -0
- package/src/lib/index.js +2 -0
- package/src/lib/index.js.map +1 -0
- package/src/lib/matcher.date.d.ts +116 -0
- package/src/lib/matcher.date.js +236 -0
- package/src/lib/matcher.date.js.map +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# Vitest Date Matchers - Implementation Summary
|
|
2
|
+
|
|
3
|
+
## Files Created
|
|
4
|
+
|
|
5
|
+
### 1. Implementation: [extend.date.ts](src/lib/vitest/extend.date.ts)
|
|
6
|
+
- Contains all matcher implementations using `expect.extend()`
|
|
7
|
+
- Exports `vitestDateMatchers` object with all matchers
|
|
8
|
+
- Exports `extendVitestDateMatchers()` function to register matchers
|
|
9
|
+
- Uses date-fns for all date comparisons
|
|
10
|
+
- Properly typed with `MatcherState` and `ExpectationResult` from `@vitest/expect`
|
|
11
|
+
|
|
12
|
+
### 2. Type Declarations: [extend.date.d.ts](src/lib/vitest/extend.date.d.ts)
|
|
13
|
+
- Ambient type declarations that extend Vitest's `Matchers` interface
|
|
14
|
+
- Provides TypeScript autocomplete and type safety
|
|
15
|
+
- Automatically picked up when `@dereekb/util/test` is imported
|
|
16
|
+
|
|
17
|
+
### 3. Tests: [extend.date.spec.ts](src/lib/vitest/extend.date.spec.ts)
|
|
18
|
+
- Comprehensive test suite with 37 tests
|
|
19
|
+
- Tests all matchers including edge cases and `.not` negation
|
|
20
|
+
- All tests passing ✅
|
|
21
|
+
|
|
22
|
+
### 4. Documentation: [README.md](src/lib/vitest/README.md)
|
|
23
|
+
- Complete usage guide
|
|
24
|
+
- Examples for all matchers
|
|
25
|
+
- Setup instructions
|
|
26
|
+
|
|
27
|
+
## Matchers Implemented
|
|
28
|
+
|
|
29
|
+
### Relative Matchers
|
|
30
|
+
- ✅ `toBeBefore(date: Date)` - Check if date is before another
|
|
31
|
+
- ✅ `toBeAfter(date: Date)` - Check if date is after another
|
|
32
|
+
- ✅ `toBeSameSecondAs(date: Date)` - Same second comparison
|
|
33
|
+
- ✅ `toBeSameMinuteAs(date: Date)` - Same minute comparison
|
|
34
|
+
- ✅ `toBeSameHourAs(date: Date)` - Same hour comparison
|
|
35
|
+
- ✅ `toBeSameDayAs(date: Date)` - Same day comparison
|
|
36
|
+
- ✅ `toBeSameWeekAs(date: Date)` - Same week comparison
|
|
37
|
+
- ✅ `toBeSameMonthAs(date: Date)` - Same month comparison
|
|
38
|
+
- ✅ `toBeSameQuarterAs(date: Date)` - Same quarter comparison
|
|
39
|
+
- ✅ `toBeSameYearAs(date: Date)` - Same year comparison
|
|
40
|
+
|
|
41
|
+
### Weekday Matchers
|
|
42
|
+
- ✅ `toBeMonday()` - Check if date is Monday
|
|
43
|
+
- ✅ `toBeTuesday()` - Check if date is Tuesday
|
|
44
|
+
- ✅ `toBeWednesday()` - Check if date is Wednesday
|
|
45
|
+
- ✅ `toBeThursday()` - Check if date is Thursday
|
|
46
|
+
- ✅ `toBeFriday()` - Check if date is Friday
|
|
47
|
+
- ✅ `toBeSaturday()` - Check if date is Saturday
|
|
48
|
+
- ✅ `toBeSunday()` - Check if date is Sunday
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
### Setup in Test Files
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { beforeAll } from 'vitest';
|
|
56
|
+
import { extendVitestDateMatchers } from '@dereekb/util/test';
|
|
57
|
+
|
|
58
|
+
beforeAll(() => {
|
|
59
|
+
extendVitestDateMatchers();
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Or in a Global Setup File
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
// vitest.setup.ts
|
|
67
|
+
import { extendVitestDateMatchers } from '@dereekb/util/test';
|
|
68
|
+
|
|
69
|
+
extendVitestDateMatchers();
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Then configure in `vitest.config.ts`:
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
export default defineConfig({
|
|
76
|
+
test: {
|
|
77
|
+
setupFiles: ['./vitest.setup.ts']
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Example Usage
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import { describe, it, expect } from 'vitest';
|
|
86
|
+
|
|
87
|
+
describe('My Date Tests', () => {
|
|
88
|
+
it('should validate date ordering', () => {
|
|
89
|
+
expect(new Date('2020')).toBeAfter(new Date('1970'));
|
|
90
|
+
expect(new Date('1970')).toBeBefore(new Date('2020'));
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('should check if dates are in same period', () => {
|
|
94
|
+
const date = new Date();
|
|
95
|
+
expect(startOfDay(date)).toBeSameDayAs(date);
|
|
96
|
+
expect(addDays(date, 1)).not.toBeSameDayAs(date);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('should check weekdays', () => {
|
|
100
|
+
const monday = new Date('2025-01-06T12:00:00');
|
|
101
|
+
expect(monday).toBeMonday();
|
|
102
|
+
expect(monday).not.toBeTuesday();
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## TypeScript Integration
|
|
108
|
+
|
|
109
|
+
The type declarations are automatically included when you import from `@dereekb/util/test`. The matchers will have full TypeScript autocomplete and type checking.
|
|
110
|
+
|
|
111
|
+
If you need to manually reference the types:
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
/// <reference types="@dereekb/util/test/src/lib/vitest/extend.date" />
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Testing
|
|
118
|
+
|
|
119
|
+
Run the test suite:
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
npm exec nx test util-test -- --testFile=extend.date.spec
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
All 37 tests pass successfully ✅
|
|
126
|
+
|
|
127
|
+
## Key Implementation Details
|
|
128
|
+
|
|
129
|
+
1. **Proper TypeScript Typing**: All matcher functions use `this: MatcherState` to access Vitest's context
|
|
130
|
+
2. **Return Type**: All matchers return `ExpectationResult` with `pass`, `message`, `actual`, and `expected`
|
|
131
|
+
3. **Negation Support**: All matchers properly handle `.not` using `this.isNot`
|
|
132
|
+
4. **Error Messages**: Clear, descriptive error messages using ISO date strings
|
|
133
|
+
5. **Date-fns Integration**: Uses date-fns functions for reliable date comparisons
|
|
134
|
+
6. **Timezone Awareness**: Tests use time-specific dates to avoid timezone issues
|
|
135
|
+
|
|
136
|
+
## Exports
|
|
137
|
+
|
|
138
|
+
From `@dereekb/util/test`:
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
export {
|
|
142
|
+
vitestDateMatchers, // The matcher object
|
|
143
|
+
extendVitestDateMatchers // Setup function
|
|
144
|
+
} from './lib/vitest/extend.date';
|
|
145
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dereekb/vitest",
|
|
3
|
+
"version": "13.0.0",
|
|
4
|
+
"peerDependencies": {
|
|
5
|
+
"date-fns": "^4.0.0"
|
|
6
|
+
},
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./src/index.d.ts",
|
|
10
|
+
"default": "./src/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./extend": {
|
|
13
|
+
"types": "./src/extend.d.ts",
|
|
14
|
+
"default": "./src/extend.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"types": "./src/index.d.ts",
|
|
18
|
+
"module": "./src/index.js",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"main": "./src/index.js"
|
|
21
|
+
}
|
package/src/extend.d.ts
ADDED
package/src/extend.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extend.js","sourceRoot":"","sources":["../../../../packages/vitest/src/extend.ts"],"names":[],"mappings":"AAAA,qFAAqF;AAErF,OAAO,QAAQ,CAAC;AAChB,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAGhC,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAErD,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC"}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './lib';
|
package/src/index.js
ADDED
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/vitest/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './matcher.date';
|
package/src/lib/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/vitest/src/lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { ExpectationResult, MatcherState } from '@vitest/expect';
|
|
2
|
+
export type DateMatcherTypeWithExpected<T extends (this: MatcherState, input: Date, expected: Date) => ExpectationResult> = T extends (this: MatcherState, input: any, expected: infer B) => ExpectationResult ? (expected: B) => ExpectationResult : never;
|
|
3
|
+
export type DateMatcherTypeWithoutExpected<T extends (this: MatcherState, input: Date) => ExpectationResult> = T extends (this: MatcherState, input: any) => ExpectationResult ? () => ExpectationResult : never;
|
|
4
|
+
/**
|
|
5
|
+
* This defines our matchers as they can be used in actual tests, see
|
|
6
|
+
* https://vitest.dev/guide/extending-matchers.html#extending-matchers
|
|
7
|
+
* for reference
|
|
8
|
+
*/
|
|
9
|
+
interface AllDateMatchers {
|
|
10
|
+
toBeBefore: DateMatcherTypeWithExpected<typeof toBeBefore>;
|
|
11
|
+
toBeAfter: DateMatcherTypeWithExpected<typeof toBeAfter>;
|
|
12
|
+
toBeSameSecondAs: DateMatcherTypeWithExpected<typeof toBeSameSecondAs>;
|
|
13
|
+
toBeSameMinuteAs: DateMatcherTypeWithExpected<typeof toBeSameMinuteAs>;
|
|
14
|
+
toBeSameHourAs: DateMatcherTypeWithExpected<typeof toBeSameHourAs>;
|
|
15
|
+
toBeSameDayAs: DateMatcherTypeWithExpected<typeof toBeSameDayAs>;
|
|
16
|
+
toBeSameWeekAs: DateMatcherTypeWithExpected<typeof toBeSameWeekAs>;
|
|
17
|
+
toBeSameMonthAs: DateMatcherTypeWithExpected<typeof toBeSameMonthAs>;
|
|
18
|
+
toBeSameQuarterAs: DateMatcherTypeWithExpected<typeof toBeSameQuarterAs>;
|
|
19
|
+
toBeSameYearAs: DateMatcherTypeWithExpected<typeof toBeSameYearAs>;
|
|
20
|
+
toBeMonday: DateMatcherTypeWithoutExpected<typeof toBeMonday>;
|
|
21
|
+
toBeTuesday: DateMatcherTypeWithoutExpected<typeof toBeTuesday>;
|
|
22
|
+
toBeWednesday: DateMatcherTypeWithoutExpected<typeof toBeWednesday>;
|
|
23
|
+
toBeThursday: DateMatcherTypeWithoutExpected<typeof toBeThursday>;
|
|
24
|
+
toBeFriday: DateMatcherTypeWithoutExpected<typeof toBeFriday>;
|
|
25
|
+
toBeSaturday: DateMatcherTypeWithoutExpected<typeof toBeSaturday>;
|
|
26
|
+
toBeSunday: DateMatcherTypeWithoutExpected<typeof toBeSunday>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Check if a date is before another date.
|
|
30
|
+
*/
|
|
31
|
+
declare function toBeBefore(this: MatcherState, received: Date, expected: Date): ExpectationResult;
|
|
32
|
+
/**
|
|
33
|
+
* Check if a date is after another date.
|
|
34
|
+
*/
|
|
35
|
+
declare function toBeAfter(this: MatcherState, received: Date, expected: Date): ExpectationResult;
|
|
36
|
+
/**
|
|
37
|
+
* Check if a date is in the same second as another date.
|
|
38
|
+
*/
|
|
39
|
+
declare function toBeSameSecondAs(this: MatcherState, received: Date, expected: Date): ExpectationResult;
|
|
40
|
+
/**
|
|
41
|
+
* Check if a date is in the same minute as another date.
|
|
42
|
+
*/
|
|
43
|
+
declare function toBeSameMinuteAs(this: MatcherState, received: Date, expected: Date): ExpectationResult;
|
|
44
|
+
/**
|
|
45
|
+
* Check if a date is in the same hour as another date.
|
|
46
|
+
*/
|
|
47
|
+
declare function toBeSameHourAs(this: MatcherState, received: Date, expected: Date): ExpectationResult;
|
|
48
|
+
/**
|
|
49
|
+
* Check if a date is in the same day as another date.
|
|
50
|
+
*/
|
|
51
|
+
declare function toBeSameDayAs(this: MatcherState, received: Date, expected: Date): ExpectationResult;
|
|
52
|
+
/**
|
|
53
|
+
* Check if a date is in the same week as another date.
|
|
54
|
+
*/
|
|
55
|
+
declare function toBeSameWeekAs(this: MatcherState, received: Date, expected: Date): ExpectationResult;
|
|
56
|
+
/**
|
|
57
|
+
* Check if a date is in the same month as another date.
|
|
58
|
+
*/
|
|
59
|
+
declare function toBeSameMonthAs(this: MatcherState, received: Date, expected: Date): ExpectationResult;
|
|
60
|
+
/**
|
|
61
|
+
* Check if a date is in the same quarter as another date.
|
|
62
|
+
*/
|
|
63
|
+
declare function toBeSameQuarterAs(this: MatcherState, received: Date, expected: Date): ExpectationResult;
|
|
64
|
+
/**
|
|
65
|
+
* Check if a date is in the same year as another date.
|
|
66
|
+
*/
|
|
67
|
+
declare function toBeSameYearAs(this: MatcherState, received: Date, expected: Date): ExpectationResult;
|
|
68
|
+
/**
|
|
69
|
+
* Check if a date is on a Monday.
|
|
70
|
+
*/
|
|
71
|
+
declare function toBeMonday(this: MatcherState, received: Date): ExpectationResult;
|
|
72
|
+
/**
|
|
73
|
+
* Check if a date is on a Tuesday.
|
|
74
|
+
*/
|
|
75
|
+
declare function toBeTuesday(this: MatcherState, received: Date): ExpectationResult;
|
|
76
|
+
/**
|
|
77
|
+
* Check if a date is on a Wednesday.
|
|
78
|
+
*/
|
|
79
|
+
declare function toBeWednesday(this: MatcherState, received: Date): ExpectationResult;
|
|
80
|
+
/**
|
|
81
|
+
* Check if a date is on a Thursday.
|
|
82
|
+
*/
|
|
83
|
+
declare function toBeThursday(this: MatcherState, received: Date): ExpectationResult;
|
|
84
|
+
/**
|
|
85
|
+
* Check if a date is on a Friday.
|
|
86
|
+
*/
|
|
87
|
+
declare function toBeFriday(this: MatcherState, received: Date): ExpectationResult;
|
|
88
|
+
/**
|
|
89
|
+
* Check if a date is on a Saturday.
|
|
90
|
+
*/
|
|
91
|
+
declare function toBeSaturday(this: MatcherState, received: Date): ExpectationResult;
|
|
92
|
+
/**
|
|
93
|
+
* Check if a date is on a Sunday.
|
|
94
|
+
*/
|
|
95
|
+
declare function toBeSunday(this: MatcherState, received: Date): ExpectationResult;
|
|
96
|
+
export declare const allDateMatchers: {
|
|
97
|
+
toBeBefore: typeof toBeBefore;
|
|
98
|
+
toBeAfter: typeof toBeAfter;
|
|
99
|
+
toBeSameSecondAs: typeof toBeSameSecondAs;
|
|
100
|
+
toBeSameMinuteAs: typeof toBeSameMinuteAs;
|
|
101
|
+
toBeSameHourAs: typeof toBeSameHourAs;
|
|
102
|
+
toBeSameDayAs: typeof toBeSameDayAs;
|
|
103
|
+
toBeSameWeekAs: typeof toBeSameWeekAs;
|
|
104
|
+
toBeSameMonthAs: typeof toBeSameMonthAs;
|
|
105
|
+
toBeSameQuarterAs: typeof toBeSameQuarterAs;
|
|
106
|
+
toBeSameYearAs: typeof toBeSameYearAs;
|
|
107
|
+
toBeMonday: typeof toBeMonday;
|
|
108
|
+
toBeTuesday: typeof toBeTuesday;
|
|
109
|
+
toBeWednesday: typeof toBeWednesday;
|
|
110
|
+
toBeThursday: typeof toBeThursday;
|
|
111
|
+
toBeFriday: typeof toBeFriday;
|
|
112
|
+
toBeSaturday: typeof toBeSaturday;
|
|
113
|
+
toBeSunday: typeof toBeSunday;
|
|
114
|
+
};
|
|
115
|
+
export type { AllDateMatchers };
|
|
116
|
+
export { toBeBefore, toBeAfter, toBeSameSecondAs, toBeSameMinuteAs, toBeSameHourAs, toBeSameDayAs, toBeSameWeekAs, toBeSameMonthAs, toBeSameQuarterAs, toBeSameYearAs, toBeMonday, toBeTuesday, toBeWednesday, toBeThursday, toBeFriday, toBeSaturday, toBeSunday };
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { isBefore, isAfter, isSameSecond, isSameMinute, isSameHour, isSameDay, isSameWeek, isSameMonth, isSameQuarter, isSameYear, getDay } from 'date-fns';
|
|
2
|
+
/**
|
|
3
|
+
* Check if a date is before another date.
|
|
4
|
+
*/
|
|
5
|
+
function toBeBefore(received, expected) {
|
|
6
|
+
const { isNot } = this;
|
|
7
|
+
const pass = isBefore(received, expected);
|
|
8
|
+
return {
|
|
9
|
+
pass,
|
|
10
|
+
message: () => `Expected ${received.toISOString()} ${isNot ? 'not ' : ''}to be before ${expected.toISOString()}`,
|
|
11
|
+
actual: received,
|
|
12
|
+
expected
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Check if a date is after another date.
|
|
17
|
+
*/
|
|
18
|
+
function toBeAfter(received, expected) {
|
|
19
|
+
const { isNot } = this;
|
|
20
|
+
const pass = isAfter(received, expected);
|
|
21
|
+
return {
|
|
22
|
+
pass,
|
|
23
|
+
message: () => `Expected ${received.toISOString()} ${isNot ? 'not ' : ''}to be after ${expected.toISOString()}`,
|
|
24
|
+
actual: received,
|
|
25
|
+
expected
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Check if a date is in the same second as another date.
|
|
30
|
+
*/
|
|
31
|
+
function toBeSameSecondAs(received, expected) {
|
|
32
|
+
const { isNot } = this;
|
|
33
|
+
const pass = isSameSecond(received, expected);
|
|
34
|
+
return {
|
|
35
|
+
pass,
|
|
36
|
+
message: () => `Expected ${received.toISOString()} ${isNot ? 'not ' : ''}to be in the same second as ${expected.toISOString()}`,
|
|
37
|
+
actual: received,
|
|
38
|
+
expected
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Check if a date is in the same minute as another date.
|
|
43
|
+
*/
|
|
44
|
+
function toBeSameMinuteAs(received, expected) {
|
|
45
|
+
const { isNot } = this;
|
|
46
|
+
const pass = isSameMinute(received, expected);
|
|
47
|
+
return {
|
|
48
|
+
pass,
|
|
49
|
+
message: () => `Expected ${received.toISOString()} ${isNot ? 'not ' : ''}to be in the same minute as ${expected.toISOString()}`,
|
|
50
|
+
actual: received,
|
|
51
|
+
expected
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Check if a date is in the same hour as another date.
|
|
56
|
+
*/
|
|
57
|
+
function toBeSameHourAs(received, expected) {
|
|
58
|
+
const { isNot } = this;
|
|
59
|
+
const pass = isSameHour(received, expected);
|
|
60
|
+
return {
|
|
61
|
+
pass,
|
|
62
|
+
message: () => `Expected ${received.toISOString()} ${isNot ? 'not ' : ''}to be in the same hour as ${expected.toISOString()}`,
|
|
63
|
+
actual: received,
|
|
64
|
+
expected
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Check if a date is in the same day as another date.
|
|
69
|
+
*/
|
|
70
|
+
function toBeSameDayAs(received, expected) {
|
|
71
|
+
const { isNot } = this;
|
|
72
|
+
const pass = isSameDay(received, expected);
|
|
73
|
+
return {
|
|
74
|
+
pass,
|
|
75
|
+
message: () => `Expected ${received.toISOString()} ${isNot ? 'not ' : ''}to be in the same day as ${expected.toISOString()}`,
|
|
76
|
+
actual: received,
|
|
77
|
+
expected
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Check if a date is in the same week as another date.
|
|
82
|
+
*/
|
|
83
|
+
function toBeSameWeekAs(received, expected) {
|
|
84
|
+
const { isNot } = this;
|
|
85
|
+
const pass = isSameWeek(received, expected);
|
|
86
|
+
return {
|
|
87
|
+
pass,
|
|
88
|
+
message: () => `Expected ${received.toISOString()} ${isNot ? 'not ' : ''}to be in the same week as ${expected.toISOString()}`,
|
|
89
|
+
actual: received,
|
|
90
|
+
expected
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Check if a date is in the same month as another date.
|
|
95
|
+
*/
|
|
96
|
+
function toBeSameMonthAs(received, expected) {
|
|
97
|
+
const { isNot } = this;
|
|
98
|
+
const pass = isSameMonth(received, expected);
|
|
99
|
+
return {
|
|
100
|
+
pass,
|
|
101
|
+
message: () => `Expected ${received.toISOString()} ${isNot ? 'not ' : ''}to be in the same month as ${expected.toISOString()}`,
|
|
102
|
+
actual: received,
|
|
103
|
+
expected
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Check if a date is in the same quarter as another date.
|
|
108
|
+
*/
|
|
109
|
+
function toBeSameQuarterAs(received, expected) {
|
|
110
|
+
const { isNot } = this;
|
|
111
|
+
const pass = isSameQuarter(received, expected);
|
|
112
|
+
return {
|
|
113
|
+
pass,
|
|
114
|
+
message: () => `Expected ${received.toISOString()} ${isNot ? 'not ' : ''}to be in the same quarter as ${expected.toISOString()}`,
|
|
115
|
+
actual: received,
|
|
116
|
+
expected
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Check if a date is in the same year as another date.
|
|
121
|
+
*/
|
|
122
|
+
function toBeSameYearAs(received, expected) {
|
|
123
|
+
const { isNot } = this;
|
|
124
|
+
const pass = isSameYear(received, expected);
|
|
125
|
+
return {
|
|
126
|
+
pass,
|
|
127
|
+
message: () => `Expected ${received.toISOString()} ${isNot ? 'not ' : ''}to be in the same year as ${expected.toISOString()}`,
|
|
128
|
+
actual: received,
|
|
129
|
+
expected
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Check if a date is on a Monday.
|
|
134
|
+
*/
|
|
135
|
+
function toBeMonday(received) {
|
|
136
|
+
const { isNot } = this;
|
|
137
|
+
const pass = getDay(received) === 1;
|
|
138
|
+
return {
|
|
139
|
+
pass,
|
|
140
|
+
message: () => `Expected ${received.toISOString()} ${isNot ? 'not ' : ''}to be on a Monday`,
|
|
141
|
+
actual: received
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Check if a date is on a Tuesday.
|
|
146
|
+
*/
|
|
147
|
+
function toBeTuesday(received) {
|
|
148
|
+
const { isNot } = this;
|
|
149
|
+
const pass = getDay(received) === 2;
|
|
150
|
+
return {
|
|
151
|
+
pass,
|
|
152
|
+
message: () => `Expected ${received.toISOString()} ${isNot ? 'not ' : ''}to be on a Tuesday`,
|
|
153
|
+
actual: received
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Check if a date is on a Wednesday.
|
|
158
|
+
*/
|
|
159
|
+
function toBeWednesday(received) {
|
|
160
|
+
const { isNot } = this;
|
|
161
|
+
const pass = getDay(received) === 3;
|
|
162
|
+
return {
|
|
163
|
+
pass,
|
|
164
|
+
message: () => `Expected ${received.toISOString()} ${isNot ? 'not ' : ''}to be on a Wednesday`,
|
|
165
|
+
actual: received
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Check if a date is on a Thursday.
|
|
170
|
+
*/
|
|
171
|
+
function toBeThursday(received) {
|
|
172
|
+
const { isNot } = this;
|
|
173
|
+
const pass = getDay(received) === 4;
|
|
174
|
+
return {
|
|
175
|
+
pass,
|
|
176
|
+
message: () => `Expected ${received.toISOString()} ${isNot ? 'not ' : ''}to be on a Thursday`,
|
|
177
|
+
actual: received
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Check if a date is on a Friday.
|
|
182
|
+
*/
|
|
183
|
+
function toBeFriday(received) {
|
|
184
|
+
const { isNot } = this;
|
|
185
|
+
const pass = getDay(received) === 5;
|
|
186
|
+
return {
|
|
187
|
+
pass,
|
|
188
|
+
message: () => `Expected ${received.toISOString()} ${isNot ? 'not ' : ''}to be on a Friday`,
|
|
189
|
+
actual: received
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Check if a date is on a Saturday.
|
|
194
|
+
*/
|
|
195
|
+
function toBeSaturday(received) {
|
|
196
|
+
const { isNot } = this;
|
|
197
|
+
const pass = getDay(received) === 6;
|
|
198
|
+
return {
|
|
199
|
+
pass,
|
|
200
|
+
message: () => `Expected ${received.toISOString()} ${isNot ? 'not ' : ''}to be on a Saturday`,
|
|
201
|
+
actual: received
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Check if a date is on a Sunday.
|
|
206
|
+
*/
|
|
207
|
+
function toBeSunday(received) {
|
|
208
|
+
const { isNot } = this;
|
|
209
|
+
const pass = getDay(received) === 0;
|
|
210
|
+
return {
|
|
211
|
+
pass,
|
|
212
|
+
message: () => `Expected ${received.toISOString()} ${isNot ? 'not ' : ''}to be on a Sunday`,
|
|
213
|
+
actual: received
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
export const allDateMatchers = {
|
|
217
|
+
toBeBefore,
|
|
218
|
+
toBeAfter,
|
|
219
|
+
toBeSameSecondAs,
|
|
220
|
+
toBeSameMinuteAs,
|
|
221
|
+
toBeSameHourAs,
|
|
222
|
+
toBeSameDayAs,
|
|
223
|
+
toBeSameWeekAs,
|
|
224
|
+
toBeSameMonthAs,
|
|
225
|
+
toBeSameQuarterAs,
|
|
226
|
+
toBeSameYearAs,
|
|
227
|
+
toBeMonday,
|
|
228
|
+
toBeTuesday,
|
|
229
|
+
toBeWednesday,
|
|
230
|
+
toBeThursday,
|
|
231
|
+
toBeFriday,
|
|
232
|
+
toBeSaturday,
|
|
233
|
+
toBeSunday
|
|
234
|
+
};
|
|
235
|
+
export { toBeBefore, toBeAfter, toBeSameSecondAs, toBeSameMinuteAs, toBeSameHourAs, toBeSameDayAs, toBeSameWeekAs, toBeSameMonthAs, toBeSameQuarterAs, toBeSameYearAs, toBeMonday, toBeTuesday, toBeWednesday, toBeThursday, toBeFriday, toBeSaturday, toBeSunday };
|
|
236
|
+
//# sourceMappingURL=matcher.date.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"matcher.date.js","sourceRoot":"","sources":["../../../../../packages/vitest/src/lib/matcher.date.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,WAAW,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAgC5J;;GAEG;AACH,SAAS,UAAU,CAAqB,QAAc,EAAE,QAAc;IACpE,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC;IACvB,MAAM,IAAI,GAAG,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IAE1C,OAAO;QACL,IAAI;QACJ,OAAO,EAAE,GAAG,EAAE,CAAC,YAAY,QAAQ,CAAC,WAAW,EAAE,IAAI,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,gBAAgB,QAAQ,CAAC,WAAW,EAAE,EAAE;QAChH,MAAM,EAAE,QAAQ;QAChB,QAAQ;KACT,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,SAAS,SAAS,CAAqB,QAAc,EAAE,QAAc;IACnE,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC;IACvB,MAAM,IAAI,GAAG,OAAO,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IAEzC,OAAO;QACL,IAAI;QACJ,OAAO,EAAE,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