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 timestamp #4866

Merged
merged 4 commits into from Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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))
Expand Down
7 changes: 4 additions & 3 deletions packages/jest-mock/README.md
Expand Up @@ -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)`

Expand Down
66 changes: 66 additions & 0 deletions packages/jest-mock/src/__tests__/jest_mock.test.js
Expand Up @@ -390,6 +390,72 @@ describe('moduleMocker', () => {
expect(fake(2)).toEqual(42);
expect(fake(2)).toEqual(4);
});

describe('timestamps', () => {
const RealDate = Date;

beforeEach(() => {
global.Date = {
now: jest
.fn()
.mockImplementationOnce(() => 978391040765)
.mockImplementationOnce(() => 1262388620765),
};
});

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', () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-mock/src/index.js
Expand Up @@ -24,6 +24,7 @@ export type MockFunctionMetadata = {
type MockFunctionState = {
instances: Array<any>,
calls: Array<Array<any>>,
timestamps: Array<number>,
};

type MockFunctionConfig = {
Expand Down Expand Up @@ -279,6 +280,7 @@ class ModuleMockerClass {
return {
calls: [],
instances: [],
timestamps: [],
};
}

Expand Down Expand Up @@ -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(Date.now());
if (this instanceof f) {
// This is probably being called as a constructor
prototypeSlots.forEach(slot => {
Expand Down