Skip to content

Commit

Permalink
refactor(e2e): consolidate all end-to-end tests of fake timers (#12595)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas committed Mar 23, 2022
1 parent abca0fe commit f6219d1
Show file tree
Hide file tree
Showing 42 changed files with 215 additions and 207 deletions.
23 changes: 0 additions & 23 deletions e2e/__tests__/fakeTime.test.ts

This file was deleted.

48 changes: 48 additions & 0 deletions e2e/__tests__/fakeTimers.test.ts
@@ -0,0 +1,48 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import runJest from '../runJest';

describe('enableGlobally', () => {
test('enables fake timers from Jest config', () => {
const result = runJest('fake-timers/enable-globally');
expect(result.exitCode).toBe(0);
});
});

describe('useFakeTimers', () => {
test('enables fake timers from Jest object', () => {
const result = runJest('fake-timers/use-fake-timers');
expect(result.exitCode).toBe(0);
});
});

describe('requestAnimationFrame', () => {
test('fakes requestAnimationFrame', () => {
const result = runJest('fake-timers/request-animation-frame');

expect(result.stderr).toMatch('requestAnimationFrame test');
expect(result.exitCode).toBe(0);
});
});

describe('setImmediate', () => {
test('fakes setImmediate', () => {
const result = runJest('fake-timers/set-immediate');

expect(result.stderr).toMatch('setImmediate test');
expect(result.exitCode).toBe(0);
});
});

describe('useRealTimers', () => {
test('restores timers to the native implementation', () => {
const result = runJest('fake-timers/use-real-timers');
expect(result.stdout).toMatch('API is not mocked with fake timers.');
expect(result.exitCode).toBe(0);
});
});
60 changes: 60 additions & 0 deletions e2e/__tests__/fakeTimersLegacy.test.ts
@@ -0,0 +1,60 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import runJest from '../runJest';

describe('enableGlobally', () => {
test('enables fake timers from Jest config', () => {
const result = runJest('fake-timers-legacy/enable-globally');
expect(result.exitCode).toBe(0);
});
});

describe('useFakeTimers', () => {
test('enables fake timers from Jest object', () => {
const result = runJest('fake-timers-legacy/use-fake-timers');
expect(result.exitCode).toBe(0);
});
});

describe('requestAnimationFrame', () => {
test('fakes requestAnimationFrame', () => {
const result = runJest('fake-timers-legacy/request-animation-frame');

expect(result.stderr).toMatch('requestAnimationFrame test');
expect(result.exitCode).toBe(0);
});
});

describe('setImmediate', () => {
test('fakes setImmediate', () => {
const result = runJest('fake-timers-legacy/set-immediate');

expect(result.stderr).toMatch('setImmediate test');
expect(result.exitCode).toBe(0);
});
});

describe('useRealTimers', () => {
test('restores timers to the native implementation', () => {
const result = runJest('fake-timers-legacy/use-real-timers');
expect(result.stdout).toMatch('API is not mocked with fake timers.');
expect(result.exitCode).toBe(0);
});
});

describe('when mocks are reset', () => {
test('calling resetAllMocks does not break tests', () => {
const result = runJest('fake-timers-legacy/reset-all-mocks');
expect(result.exitCode).toBe(0);
});

test('setting resetMocks in Jest config does not break tests', () => {
const result = runJest('fake-timers-legacy/reset-mocks');
expect(result.exitCode).toBe(0);
});
});
20 changes: 0 additions & 20 deletions e2e/__tests__/modernFakeTimers.test.ts

This file was deleted.

15 changes: 0 additions & 15 deletions e2e/__tests__/setImmediate.test.ts

This file was deleted.

18 changes: 0 additions & 18 deletions e2e/__tests__/timerResetMocks.test.ts

This file was deleted.

14 changes: 0 additions & 14 deletions e2e/__tests__/timerUseRealTimers.test.ts

This file was deleted.

6 changes: 0 additions & 6 deletions e2e/fake-time/legacy/from-config/package.json

This file was deleted.

5 changes: 0 additions & 5 deletions e2e/fake-time/legacy/from-jest-object/package.json

This file was deleted.

6 changes: 0 additions & 6 deletions e2e/fake-time/modern/from-config/package.json

This file was deleted.

5 changes: 0 additions & 5 deletions e2e/fake-time/modern/from-jest-object/package.json

This file was deleted.

Expand Up @@ -7,7 +7,13 @@

'use strict';

test('fake timers', () => {
test('getRealSystemTime', () => {
expect(() => jest.getRealSystemTime()).toThrow(
'getRealSystemTime is not available when not using modern timers',
);
});

test('setSystemTime', () => {
expect(() => jest.setSystemTime(0)).toThrow(
'setSystemTime is not available when not using modern timers',
);
Expand Down
6 changes: 6 additions & 0 deletions e2e/fake-timers-legacy/enable-globally/package.json
@@ -0,0 +1,6 @@
{
"name": "enable-globally-legacy",
"jest": {
"timers": "legacy"
}
}
Expand Up @@ -9,7 +9,7 @@

'use strict';

test('requestAnimationFrame', () => {
test('requestAnimationFrame test', () => {
jest.useFakeTimers('legacy');
let frameTimestamp = -1;
requestAnimationFrame(timestamp => {
Expand Down
@@ -1,4 +1,5 @@
{
"name": "request-animation-frame-legacy",
"jest": {
"testEnvironment": "jsdom"
}
Expand Down
@@ -0,0 +1,25 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

test('works before resetAllMocks is called', () => {
jest.useFakeTimers();
const f = jest.fn();
setTimeout(f, 0);
jest.runAllTimers();
expect(f).toHaveBeenCalledTimes(1);
});

test('works after resetAllMocks is called', () => {
jest.resetAllMocks();
jest.useFakeTimers();
const f = jest.fn();
setTimeout(f, 0);
jest.runAllTimers();
expect(f).toHaveBeenCalledTimes(1);
});
7 changes: 7 additions & 0 deletions e2e/fake-timers-legacy/reset-all-mocks/package.json
@@ -0,0 +1,7 @@
{
"name": "reset-all-mocks",
"jest": {
"resetMocks": false,
"timers": "legacy"
}
}
Expand Up @@ -7,14 +7,10 @@

'use strict';

test('fake timers', () => {
test('works when resetMocks is set in Jest config', () => {
jest.useFakeTimers();

jest.setSystemTime(0);

expect(Date.now()).toBe(0);

jest.setSystemTime(1000);

expect(Date.now()).toBe(1000);
const f = jest.fn();
setTimeout(f, 0);
jest.runAllTimers();
expect(f).toHaveBeenCalledTimes(1);
});
7 changes: 7 additions & 0 deletions e2e/fake-timers-legacy/reset-mocks/package.json
@@ -0,0 +1,7 @@
{
"name": "reset-mocks",
"jest": {
"resetMocks": true,
"timers": "legacy"
}
}
6 changes: 6 additions & 0 deletions e2e/fake-timers-legacy/set-immediate/package.json
@@ -0,0 +1,6 @@
{
"name": "set-immediate-legacy",
"jest": {
"timers": "legacy"
}
}
3 changes: 3 additions & 0 deletions e2e/fake-timers-legacy/use-fake-timers/package.json
@@ -0,0 +1,3 @@
{
"name": "use-fake-timers-legacy"
}
Expand Up @@ -5,6 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

'use strict';

jest.useRealTimers();

test('bar', () => {
Expand Down
6 changes: 6 additions & 0 deletions e2e/fake-timers-legacy/use-real-timers/package.json
@@ -0,0 +1,6 @@
{
"name": "use-real-timers-legacy",
"jest": {
"timers": "legacy"
}
}
Expand Up @@ -26,3 +26,9 @@ test('fake timers with Date argument', () => {

expect(Date.now()).toBe(1000);
});

test('runAllImmediates', () => {
expect(() => jest.runAllImmediates()).toThrow(
'runAllImmediates is not available when using modern timers',
);
});
6 changes: 6 additions & 0 deletions e2e/fake-timers/enable-globally/package.json
@@ -0,0 +1,6 @@
{
"name": "enable-globally",
"jest": {
"timers": "modern"
}
}
Expand Up @@ -9,7 +9,7 @@

'use strict';

test('requestAnimationFrame', () => {
test('requestAnimationFrame test', () => {
jest.useFakeTimers('modern');
let frameTimestamp = -1;
requestAnimationFrame(timestamp => {
Expand Down
@@ -1,4 +1,5 @@
{
"name": "request-animation-frame",
"jest": {
"testEnvironment": "jsdom"
}
Expand Down

0 comments on commit f6219d1

Please sign in to comment.