diff --git a/CHANGELOG.md b/CHANGELOG.md index 47daaf340e1a..94c07afdff0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,6 +61,8 @@ ### Fixes +* `[jest-mock]` [**BREAKING**] Replace timestamps with `invocationCallOrder` + ([#5867](https://github.com/facebook/jest/pull/5867)) * `[jest-jasmine2]` Install `sourcemap-support` into normal runtime to catch runtime errors ([#5945](https://github.com/facebook/jest/pull/5945)) * `[jest-jasmine2]` Added assertion error handling inside `afterAll hook` diff --git a/packages/jest-mock/README.md b/packages/jest-mock/README.md index 0ab9815944a4..41d983e379d8 100644 --- a/packages/jest-mock/README.md +++ b/packages/jest-mock/README.md @@ -78,12 +78,12 @@ following members: ##### `.mock` -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. The `timestamps` list stores a number timestamp every time the mock -is called. +An object with three members, `calls`, `instances` and `invocationCallOrder`, +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. The `invocationCallOrder` lists the order in which the mock was +called in relation to all mock calls, starting at 1. ##### `.mockReturnValueOnce(value)` diff --git a/packages/jest-mock/src/__tests__/jest_mock.test.js b/packages/jest-mock/src/__tests__/jest_mock.test.js index 7c3f983aa027..6c2437f4b43a 100644 --- a/packages/jest-mock/src/__tests__/jest_mock.test.js +++ b/packages/jest-mock/src/__tests__/jest_mock.test.js @@ -508,67 +508,64 @@ describe('moduleMocker', () => { expect(fn.mock.thrownErrors).toEqual([undefined, error, undefined]); }); - describe('timestamps', () => { - const RealDate = Date; - - beforeEach(() => { - global.Date = { - now: jest - .fn() - .mockImplementationOnce(() => 978391040765) - .mockImplementationOnce(() => 1262388620765), - }; - }); + describe('invocationCallOrder', () => { + it('tracks invocationCallOrder made by mocks', () => { + const fn1 = moduleMocker.fn(); + expect(fn1.mock.invocationCallOrder).toEqual([]); - afterEach(() => { - global.Date = RealDate; - }); + fn1(1, 2, 3); + expect(fn1.mock.invocationCallOrder[0]).toBe(1); - it('tracks timestamps made by mocks', () => { - const fn = moduleMocker.fn(); - expect(fn.mock.timestamps).toEqual([]); + fn1('a', 'b', 'c'); + expect(fn1.mock.invocationCallOrder[1]).toBe(2); - fn(1, 2, 3); - expect(fn.mock.timestamps[0]).toBe(978391040765); + fn1(1, 2, 3); + expect(fn1.mock.invocationCallOrder[2]).toBe(3); - fn('a', 'b', 'c'); - expect(fn.mock.timestamps[1]).toBe(1262388620765); + const fn2 = moduleMocker.fn(); + expect(fn2.mock.invocationCallOrder).toEqual([]); + + fn2('d', 'e', 'f'); + expect(fn2.mock.invocationCallOrder[0]).toBe(4); + + fn2(4, 5, 6); + expect(fn2.mock.invocationCallOrder[1]).toBe(5); }); - it('supports clearing mock timestamps', () => { + it('supports clearing mock invocationCallOrder', () => { const fn = moduleMocker.fn(); - expect(fn.mock.timestamps).toEqual([]); + expect(fn.mock.invocationCallOrder).toEqual([]); fn(1, 2, 3); - expect(fn.mock.timestamps).toEqual([978391040765]); + expect(fn.mock.invocationCallOrder).toEqual([1]); fn.mockReturnValue('abcd'); fn.mockClear(); - expect(fn.mock.timestamps).toEqual([]); + expect(fn.mock.invocationCallOrder).toEqual([]); fn('a', 'b', 'c'); - expect(fn.mock.timestamps).toEqual([1262388620765]); + expect(fn.mock.invocationCallOrder).toEqual([2]); expect(fn()).toEqual('abcd'); }); - it('supports clearing all mocks timestamps', () => { + it('supports clearing all mocks invocationCallOrder', () => { const fn1 = moduleMocker.fn(); fn1.mockImplementation(() => 'abcd'); fn1(1, 2, 3); - expect(fn1.mock.timestamps).toEqual([978391040765]); + expect(fn1.mock.invocationCallOrder).toEqual([1]); const fn2 = moduleMocker.fn(); fn2.mockReturnValue('abcde'); fn2('a', 'b', 'c', 'd'); - expect(fn2.mock.timestamps).toEqual([1262388620765]); + expect(fn2.mock.invocationCallOrder).toEqual([2]); moduleMocker.clearAllMocks(); - expect(fn1.mock.timestamps).toEqual([]); - expect(fn2.mock.timestamps).toEqual([]); + expect(fn1.mock.invocationCallOrder).toEqual([]); + expect(fn2.mock.invocationCallOrder).toEqual([]); expect(fn1()).toEqual('abcd'); expect(fn2()).toEqual('abcde'); }); diff --git a/packages/jest-mock/src/index.js b/packages/jest-mock/src/index.js index 0f5dc8c91e2c..f99aad8c144e 100644 --- a/packages/jest-mock/src/index.js +++ b/packages/jest-mock/src/index.js @@ -26,7 +26,7 @@ type MockFunctionState = { calls: Array>, returnValues: Array, thrownErrors: Array, - timestamps: Array, + invocationCallOrder: Array, }; type MockFunctionConfig = { @@ -241,6 +241,7 @@ class ModuleMockerClass { _mockConfigRegistry: WeakMap; _spyState: Set<() => void>; ModuleMocker: Class; + _invocationCallCounter: number; /** * @see README.md @@ -253,6 +254,7 @@ class ModuleMockerClass { this._mockConfigRegistry = new WeakMap(); this._spyState = new Set(); this.ModuleMocker = ModuleMockerClass; + this._invocationCallCounter = 1; } _ensureMockConfig(f: Mock): MockFunctionConfig { @@ -288,9 +290,9 @@ class ModuleMockerClass { return { calls: [], instances: [], + invocationCallOrder: [], returnValues: [], thrownErrors: [], - timestamps: [], }; } @@ -325,7 +327,7 @@ class ModuleMockerClass { const mockConfig = mocker._ensureMockConfig(f); mockState.instances.push(this); mockState.calls.push(Array.prototype.slice.call(arguments)); - mockState.timestamps.push(Date.now()); + mockState.invocationCallOrder.push(mocker._invocationCallCounter++); // Will be set to the return value of the mock if an error is not thrown let finalReturnValue;