Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock invocation order #5867

Merged
merged 9 commits into from
Apr 15, 2018
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@

### Fixes

* `[jest-mock]` Breaking Change: 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`
Expand Down
12 changes: 6 additions & 6 deletions packages/jest-mock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`

Expand Down
59 changes: 28 additions & 31 deletions packages/jest-mock/src/__tests__/jest_mock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down
8 changes: 5 additions & 3 deletions packages/jest-mock/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type MockFunctionState = {
calls: Array<Array<any>>,
returnValues: Array<any>,
thrownErrors: Array<any>,
timestamps: Array<number>,
invocationCallOrder: Array<number>,
};

type MockFunctionConfig = {
Expand Down Expand Up @@ -241,6 +241,7 @@ class ModuleMockerClass {
_mockConfigRegistry: WeakMap<Function, MockFunctionConfig>;
_spyState: Set<() => void>;
ModuleMocker: Class<ModuleMockerClass>;
_invocationCallCounter: number;

/**
* @see README.md
Expand All @@ -253,6 +254,7 @@ class ModuleMockerClass {
this._mockConfigRegistry = new WeakMap();
this._spyState = new Set();
this.ModuleMocker = ModuleMockerClass;
this._invocationCallCounter = 1;
}

_ensureMockConfig(f: Mock): MockFunctionConfig {
Expand Down Expand Up @@ -288,9 +290,9 @@ class ModuleMockerClass {
return {
calls: [],
instances: [],
invocationCallOrder: [],
returnValues: [],
thrownErrors: [],
timestamps: [],
};
}

Expand Down Expand Up @@ -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;
Expand Down