Skip to content

Commit

Permalink
Fixed issues with mocked timestamps in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Lysne committed Jul 11, 2023
1 parent 8604bfd commit ac678ab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
17 changes: 13 additions & 4 deletions packages/core/tests/classes/AcquireLogger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,22 @@ describe("class: AcquireLogger", () => {
const OriginalDate = global.Date;

beforeAll(() => {
jest
.spyOn(global, "Date")
.mockImplementation(() => new OriginalDate("2023-07-03T11:22:33Z"));
const mockDate = new OriginalDate("2023-07-03T11:22:33Z");

function MockDate(): Date {
return mockDate;
}
MockDate.UTC = OriginalDate.UTC;
MockDate.parse = OriginalDate.parse;
MockDate.now = (): number => mockDate.getTime();
MockDate.prototype = OriginalDate.prototype;
MockDate.prototype.constructor = MockDate;

global.Date = MockDate as any;
});

afterAll(() => {
jest.restoreAllMocks();
global.Date = OriginalDate;
});

afterEach(() => {
Expand Down
32 changes: 14 additions & 18 deletions packages/core/tests/middleware/AcquireRequestLogger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,8 @@ import axios, { AxiosError } from "axios";
import MockAdapter from "axios-mock-adapter";

describe("middleware: AcquireRequestLogger", () => {
const {
blue,
green,
red,
cyan,
yellow,
brightBlack,
brightBlue,
brightGreen,
reset
} = AcquireLogger.color;
const { blue, green, red, cyan, yellow, brightBlack, brightBlue, reset } =
AcquireLogger.color;
const axiosInstance = axios.create();
const mockAxios = new MockAdapter(axiosInstance);

Expand All @@ -25,13 +16,18 @@ describe("middleware: AcquireRequestLogger", () => {
const OriginalDate = global.Date;

beforeAll(() => {
jest
.spyOn(global, "Date")
.mockImplementation(() => new OriginalDate("2023-07-03T11:22:33Z"));
});

afterAll(() => {
jest.restoreAllMocks();
const mockDate = new OriginalDate("2023-07-03T11:22:33Z");

function MockDate(): Date {
return mockDate;
}
MockDate.UTC = OriginalDate.UTC;
MockDate.parse = OriginalDate.parse;
MockDate.now = (): number => mockDate.getTime();
MockDate.prototype = OriginalDate.prototype;
MockDate.prototype.constructor = MockDate;

global.Date = MockDate as any;
});

afterEach(() => {
Expand Down

0 comments on commit ac678ab

Please sign in to comment.