Skip to content

Commit d79de1d

Browse files
authored
fix(signature-v4): mock Date in SigV4 tests (#2880)
* fix(signature-v4): mock Date in SigV4 tests * test(signature-v4): fix Argument is not assignable refs: https://stackoverflow.com/a/63782059
1 parent 5d638e1 commit d79de1d

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

packages/signature-v4/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"build:es": "tsc -p tsconfig.es.json",
1212
"build:types": "tsc -p tsconfig.types.json",
1313
"downlevel-dts": "downlevel-dts dist-types dist-types/ts3.4",
14-
"pretest": "yarn build",
1514
"test": "jest --coverage"
1615
},
1716
"author": {

packages/signature-v4/src/SignatureV4.spec.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -709,21 +709,26 @@ describe("SignatureV4", () => {
709709
});
710710

711711
describe("ambient Date usage", () => {
712-
const knownDate = new Date("1999-12-31T23:59:59.999Z");
712+
let dateSpy;
713+
const mockDate = new Date();
713714

714715
beforeEach(() => {
715-
Date.now = jest.fn().mockReturnValue(knownDate) as any;
716+
dateSpy = jest.spyOn(global, "Date").mockImplementation(() => mockDate as unknown as string);
717+
});
718+
719+
afterEach(() => {
720+
expect(dateSpy).toHaveBeenCalledTimes(1);
721+
jest.clearAllMocks();
716722
});
717723

718724
it("should use the current date for presigning if no signing date was supplied", async () => {
719-
const date = new Date();
720725
const { query } = await signer.presign(minimalRequest);
721-
expect((query as any)[AMZ_DATE_QUERY_PARAM]).toBe(iso8601(date).replace(/[\-:]/g, ""));
726+
expect((query as any)[AMZ_DATE_QUERY_PARAM]).toBe(iso8601(mockDate).replace(/[\-:]/g, ""));
722727
});
723728

724729
it("should use the current date for signing if no signing date supplied", async () => {
725730
const { headers } = await signer.sign(minimalRequest);
726-
expect(headers[AMZ_DATE_HEADER]).toBe(iso8601(new Date()).replace(/[\-:]/g, ""));
731+
expect(headers[AMZ_DATE_HEADER]).toBe(iso8601(mockDate).replace(/[\-:]/g, ""));
727732
});
728733
});
729734
});

0 commit comments

Comments
 (0)