Skip to content

Commit

Permalink
chore: mock date in jest (#9896)
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored Dec 9, 2024
1 parent 4d4dbf1 commit 1092bcc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jobs:
- run: npm test
- run: tree -L 2 ./coverage -P 'lcov.info'
- uses: codecov/codecov-action@v5.1.1
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
directory: ./coverage/
flags: summary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,6 @@ describe('InputDateRangeComponent', () => {
});

describe('With items', () => {
beforeAll(() => {
jest.useFakeTimers({advanceTimers: true}).setSystemTime(
new Date('2024-01-15'),
);
});

beforeEach(() => {
testComponent.items = tuiCreateDefaultDayRangePeriods();
});
Expand Down
8 changes: 8 additions & 0 deletions projects/testing/setup-jest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ class TransferMockEvent {
global.DragEvent = TransferMockEvent as unknown as typeof DragEvent;
global.ClipboardEvent = TransferMockEvent as unknown as typeof ClipboardEvent;

// Need before initialize any static methods
global.Date = class extends Date {
constructor(...args: DateConstructor[]) {
// @ts-ignore
super(...(args.length === 0 ? ['2023-02-15T00:00:00Z'] : args));
}
} as unknown as DateConstructor;

/**
* in our jest setupFilesAfterEnv file,
* however when running with ng test those
Expand Down

0 comments on commit 1092bcc

Please sign in to comment.