From 6534d53a97518dba351fa860738dcad2e8705e2d Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Wed, 8 Nov 2017 23:56:28 +0000 Subject: [PATCH 1/4] Add timestamp to mock state --- .../jest-mock/src/__tests__/jest_mock.test.js | 67 +++++++++++++++++++ packages/jest-mock/src/index.js | 3 + 2 files changed, 70 insertions(+) diff --git a/packages/jest-mock/src/__tests__/jest_mock.test.js b/packages/jest-mock/src/__tests__/jest_mock.test.js index 56106ca9d846..8b1678e4b432 100644 --- a/packages/jest-mock/src/__tests__/jest_mock.test.js +++ b/packages/jest-mock/src/__tests__/jest_mock.test.js @@ -390,6 +390,73 @@ describe('moduleMocker', () => { expect(fake(2)).toEqual(42); expect(fake(2)).toEqual(4); }); + + describe('timestamps', () => { + const RealDate = Date; + + beforeEach(() => { + const mockDates = { + getTime: jest + .fn() + .mockImplementationOnce(() => 978391040765) + .mockImplementationOnce(() => 1262388620765), + }; + global.Date = jest.fn(() => mockDates); + }); + + afterEach(() => { + global.Date = RealDate; + }); + + it('tracks timestamps made by mocks', () => { + const fn = moduleMocker.fn(); + expect(fn.mock.timestamps).toEqual([]); + + fn(1, 2, 3); + expect(fn.mock.timestamps[0]).toBe(978391040765); + + fn('a', 'b', 'c'); + expect(fn.mock.timestamps[1]).toBe(1262388620765); + }); + + it('supports clearing mock timestamps', () => { + const fn = moduleMocker.fn(); + expect(fn.mock.timestamps).toEqual([]); + + fn(1, 2, 3); + expect(fn.mock.timestamps).toEqual([978391040765]); + + fn.mockReturnValue('abcd'); + + fn.mockClear(); + expect(fn.mock.timestamps).toEqual([]); + + fn('a', 'b', 'c'); + expect(fn.mock.timestamps).toEqual([1262388620765]); + + expect(fn()).toEqual('abcd'); + }); + + it('supports clearing all mocks timestamps', () => { + const fn1 = moduleMocker.fn(); + fn1.mockImplementation(() => 'abcd'); + + fn1(1, 2, 3); + expect(fn1.mock.timestamps).toEqual([978391040765]); + + const fn2 = moduleMocker.fn(); + + fn2.mockReturnValue('abcde'); + fn2('a', 'b', 'c', 'd'); + expect(fn2.mock.timestamps).toEqual([1262388620765]); + + moduleMocker.clearAllMocks(); + expect(fn1.mock.timestamps).toEqual([]); + expect(fn2.mock.timestamps).toEqual([]); + expect(fn1()).toEqual('abcd'); + expect(fn2()).toEqual('abcde'); + }); + }); }); describe('getMockImplementation', () => { diff --git a/packages/jest-mock/src/index.js b/packages/jest-mock/src/index.js index 60776394ad41..c8b7ef0486f3 100644 --- a/packages/jest-mock/src/index.js +++ b/packages/jest-mock/src/index.js @@ -24,6 +24,7 @@ export type MockFunctionMetadata = { type MockFunctionState = { instances: Array, calls: Array>, + timestamps: Array, }; type MockFunctionConfig = { @@ -279,6 +280,7 @@ class ModuleMockerClass { return { calls: [], instances: [], + timestamps: [], }; } @@ -313,6 +315,7 @@ class ModuleMockerClass { const mockConfig = mocker._ensureMockConfig(f); mockState.instances.push(this); mockState.calls.push(Array.prototype.slice.call(arguments)); + mockState.timestamps.push(new Date().getTime()); if (this instanceof f) { // This is probably being called as a constructor prototypeSlots.forEach(slot => { From d8b9f921adb56abff72221733416e5e4db0d8d9f Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Thu, 9 Nov 2017 00:06:55 +0000 Subject: [PATCH 2/4] Add timestamps docs --- packages/jest-mock/README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/jest-mock/README.md b/packages/jest-mock/README.md index ed0f1c612526..58920979bef4 100644 --- a/packages/jest-mock/README.md +++ b/packages/jest-mock/README.md @@ -80,11 +80,12 @@ the following members: ##### `.mock` -An object with two members, `calls`, and `instances`, which are both -lists. The items in the `calls` list are the arguments with which the +An object with three members, `calls`, `instances` and `timestamps`, which are +all lists. The items in the `calls` list are the arguments with which the function was called. The "instances" list stores the value of 'this' for each call to the function. This is useful for retrieving instances from a -constructor. +constructor. The `timestamps` list stores a number timestamp every time the +mock is called. ##### `.mockReturnValueOnce(value)` From efad34e7e9a485ee260e3b9f36c961a18f4ec6dc Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Thu, 9 Nov 2017 00:16:06 +0000 Subject: [PATCH 3/4] Add feature changes to change log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25e5ef463e13..045ac10d3456 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ * `[jest-environment-jsdom]` Fix asynchronous test will fail due to timeout issue. ([#4669](https://github.com/facebook/jest/pull/4669)) ### Features +* `[jest-mock]` Add `timestamps` to mock state. ([#4866](https://github.com/facebook/jest/pull/4866)) * `[eslint-plugin-jest]` Add `prefer-to-have-length` lint rule. ([#4771](https://github.com/facebook/jest/pull/4771)) * `[jest-environment-jsdom]` [**BREAKING**] Upgrade to JSDOM@11 ([#4770](https://github.com/facebook/jest/pull/4770)) * `[jest-environment-*]` [**BREAKING**] Add Async Test Environment APIs, dispose is now teardown ([#4506](https://github.com/facebook/jest/pull/4506)) From 53b25f1aaa4c27548b92d02ab82d05bf85b68f5a Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Thu, 9 Nov 2017 07:45:41 +0000 Subject: [PATCH 4/4] Update timestamps to use Date.now --- packages/jest-mock/src/__tests__/jest_mock.test.js | 5 ++--- packages/jest-mock/src/index.js | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/jest-mock/src/__tests__/jest_mock.test.js b/packages/jest-mock/src/__tests__/jest_mock.test.js index 8b1678e4b432..e081c8ecbc7e 100644 --- a/packages/jest-mock/src/__tests__/jest_mock.test.js +++ b/packages/jest-mock/src/__tests__/jest_mock.test.js @@ -395,13 +395,12 @@ describe('moduleMocker', () => { const RealDate = Date; beforeEach(() => { - const mockDates = { - getTime: jest + global.Date = { + now: jest .fn() .mockImplementationOnce(() => 978391040765) .mockImplementationOnce(() => 1262388620765), }; - global.Date = jest.fn(() => mockDates); }); afterEach(() => { diff --git a/packages/jest-mock/src/index.js b/packages/jest-mock/src/index.js index c8b7ef0486f3..33019e3edb51 100644 --- a/packages/jest-mock/src/index.js +++ b/packages/jest-mock/src/index.js @@ -315,7 +315,7 @@ class ModuleMockerClass { const mockConfig = mocker._ensureMockConfig(f); mockState.instances.push(this); mockState.calls.push(Array.prototype.slice.call(arguments)); - mockState.timestamps.push(new Date().getTime()); + mockState.timestamps.push(Date.now()); if (this instanceof f) { // This is probably being called as a constructor prototypeSlots.forEach(slot => {