test(zone.js): update jest test code after upgrade to jest 29.0 (#47486)

related to https://github.com/angular/angular/pull/47438
After jest 28, `jest-environment-node` and `jest-environment-jsdom` need
to be installed by the user themselves, and the API has some breaking
changes, so this PR fix these issues to make the zone/jest integration
test code work as expected.

PR Close #47486
This commit is contained in:
JiaLiPassion
2022-09-20 13:47:35 +09:00
committed by Pawel Kozlowski
parent d11b1d99b0
commit 59aa2c06d1
5 changed files with 1119 additions and 1768 deletions
+3 -1
View File
@@ -14,8 +14,10 @@
"@externs/nodejs": "^1.5.0",
"@types/node": "^10.9.4",
"domino": "2.1.6",
"jest": "^26.4",
"google-closure-compiler": "^20220905.0.0",
"jest": "^29.0",
"jest-environment-jsdom": "^29.0.3",
"jest-environment-node": "^29.0.3",
"mocha": "^10.0.0",
"mock-require": "3.0.3",
"promises-aplus-tests": "^2.1.2",
+150 -65
View File
@@ -18,6 +18,10 @@ describe('describe', () => {
afterAll(() => {
assertInsideProxyZone();
});
it('dummy test since jest does not allow before/after each without test', () => {
expect(true).toBe(true);
});
});
describe.each([[1, 2]])('describe.each', (arg1, arg2) => {
assertInsideSyncDescribeZone();
@@ -44,7 +48,7 @@ describe('test', () => {
it('it', () => {
assertInsideProxyZone();
});
it('it with done', done => {
it('it with done', (done) => {
assertInsideProxyZone();
done();
});
@@ -56,7 +60,7 @@ it.each([[1, 2]])('it.each', (arg1, arg2, done) => {
done();
});
it.each([2])('it.each with 1D array', arg1 => {
it.each([2])('it.each with 1D array', (arg1) => {
assertInsideProxyZone();
expect(arg1).toBe(2);
});
@@ -68,26 +72,26 @@ it.each([2])('it.each with 1D array and done', (arg1, done) => {
});
it.each`
foo | bar
${1} | ${2}
`('it.each should work with table as a tagged template literal', ({foo, bar}) => {
foo | bar
${1} | ${2}
`('it.each should work with table as a tagged template literal', ({foo, bar}) => {
expect(foo).toBe(1);
expect(bar).toBe(2);
});
it.each`
foo | bar
${1} | ${2}
`('it.each should work with table as a tagged template literal with done', ({foo, bar}, done) => {
foo | bar
${1} | ${2}
`('it.each should work with table as a tagged template literal with done', ({foo, bar}, done) => {
expect(foo).toBe(1);
expect(bar).toBe(2);
done();
});
it.each`
foo | bar
${1} | ${2}
`('(async) it.each should work with table as a tagged template literal', async ({foo, bar}) => {
foo | bar
${1} | ${2}
`('(async) it.each should work with table as a tagged template literal', async ({foo, bar}) => {
expect(foo).toBe(1);
expect(bar).toBe(2);
});
@@ -146,7 +150,7 @@ describe('jest modern fakeTimers with zone.js fakeAsync', () => {
test('runAllTicks should run all microTasks', () => {
const logs = [];
Promise.resolve(1).then(v => logs.push(v));
Promise.resolve(1).then((v) => logs.push(v));
expect(logs).toEqual([]);
jest.runAllTicks();
expect(logs).toEqual([1]);
@@ -154,9 +158,13 @@ describe('jest modern fakeTimers with zone.js fakeAsync', () => {
test('runAllTimers should run all macroTasks', () => {
const logs = [];
Promise.resolve(1).then(v => logs.push(v));
setTimeout(() => {logs.push('timeout')});
const id = setInterval(() => {logs.push('interval')}, 100);
Promise.resolve(1).then((v) => logs.push(v));
setTimeout(() => {
logs.push('timeout');
});
const id = setInterval(() => {
logs.push('interval');
}, 100);
expect(logs).toEqual([]);
jest.runAllTimers();
expect(logs).toEqual([1, 'timeout', 'interval']);
@@ -165,7 +173,9 @@ describe('jest modern fakeTimers with zone.js fakeAsync', () => {
test('advanceTimersByTime should act as tick', () => {
const logs = [];
setTimeout(() => {logs.push('timeout')}, 100);
setTimeout(() => {
logs.push('timeout');
}, 100);
expect(logs).toEqual([]);
jest.advanceTimersByTime(100);
expect(logs).toEqual(['timeout']);
@@ -173,11 +183,13 @@ describe('jest modern fakeTimers with zone.js fakeAsync', () => {
test('runOnlyPendingTimers should run all macroTasks and ignore new spawn macroTasks', () => {
const logs = [];
Promise.resolve(1).then(v => logs.push(v));
Promise.resolve(1).then((v) => logs.push(v));
let nestedTimeoutId;
setTimeout(() => {
logs.push('timeout');
nestedTimeoutId = setTimeout(() => {logs.push('new timeout')});
nestedTimeoutId = setTimeout(() => {
logs.push('new timeout');
});
});
expect(logs).toEqual([]);
jest.runOnlyPendingTimers();
@@ -187,10 +199,18 @@ describe('jest modern fakeTimers with zone.js fakeAsync', () => {
test('advanceTimersToNextTimer should trigger correctly', () => {
const logs = [];
setTimeout(() => {logs.push('timeout1')}, 100);
setTimeout(() => {logs.push('timeout11')}, 100);
setTimeout(() => {logs.push('timeout2')}, 200);
setTimeout(() => {logs.push('timeout3')}, 300);
setTimeout(() => {
logs.push('timeout1');
}, 100);
setTimeout(() => {
logs.push('timeout11');
}, 100);
setTimeout(() => {
logs.push('timeout2');
}, 200);
setTimeout(() => {
logs.push('timeout3');
}, 300);
expect(logs).toEqual([]);
jest.advanceTimersToNextTimer();
expect(logs).toEqual(['timeout1', 'timeout11']);
@@ -200,9 +220,15 @@ describe('jest modern fakeTimers with zone.js fakeAsync', () => {
test('clearAllTimers should clear all macroTasks', () => {
const logs = [];
setTimeout(() => {logs.push('timeout1')}, 100);
setTimeout(() => {logs.push('timeout2')}, 200);
setInterval(() => {logs.push('interval')}, 100);
setTimeout(() => {
logs.push('timeout1');
}, 100);
setTimeout(() => {
logs.push('timeout2');
}, 200);
setInterval(() => {
logs.push('interval');
}, 100);
expect(logs).toEqual([]);
jest.clearAllTimers();
jest.advanceTimersByTime(300);
@@ -211,9 +237,15 @@ describe('jest modern fakeTimers with zone.js fakeAsync', () => {
test('getTimerCount should get the count of macroTasks correctly', () => {
const logs = [];
setTimeout(() => {logs.push('timeout1')}, 100);
setTimeout(() => {logs.push('timeout2')}, 200);
setInterval(() => {logs.push('interval')}, 100);
setTimeout(() => {
logs.push('timeout1');
}, 100);
setTimeout(() => {
logs.push('timeout2');
}, 200);
setInterval(() => {
logs.push('interval');
}, 100);
expect(logs).toEqual([]);
expect(jest.getTimerCount()).toEqual(3);
jest.clearAllTimers();
@@ -234,14 +266,9 @@ describe('jest legacy fakeTimers with zone.js fakeAsync', () => {
expect(typeof fakeAsyncZoneSpec.tick).toEqual('function');
});
test('setSystemTime should set FakeDate.currentRealTime', () => {
const d = Date.now();
expect(() => {jest.setSystemTime(d)}).toThrow();
});
test('runAllTicks should run all microTasks', () => {
const logs = [];
Promise.resolve(1).then(v => logs.push(v));
Promise.resolve(1).then((v) => logs.push(v));
expect(logs).toEqual([]);
jest.runAllTicks();
expect(logs).toEqual([1]);
@@ -249,9 +276,13 @@ describe('jest legacy fakeTimers with zone.js fakeAsync', () => {
test('runAllTimers should run all macroTasks', () => {
const logs = [];
Promise.resolve(1).then(v => logs.push(v));
setTimeout(() => {logs.push('timeout')});
const id = setInterval(() => {logs.push('interval')}, 100);
Promise.resolve(1).then((v) => logs.push(v));
setTimeout(() => {
logs.push('timeout');
});
const id = setInterval(() => {
logs.push('interval');
}, 100);
expect(logs).toEqual([]);
jest.runAllTimers();
expect(logs).toEqual([1, 'timeout', 'interval']);
@@ -260,7 +291,9 @@ describe('jest legacy fakeTimers with zone.js fakeAsync', () => {
test('advanceTimersByTime should act as tick', () => {
const logs = [];
setTimeout(() => {logs.push('timeout')}, 100);
setTimeout(() => {
logs.push('timeout');
}, 100);
expect(logs).toEqual([]);
jest.advanceTimersByTime(100);
expect(logs).toEqual(['timeout']);
@@ -268,11 +301,13 @@ describe('jest legacy fakeTimers with zone.js fakeAsync', () => {
test('runOnlyPendingTimers should run all macroTasks and ignore new spawn macroTasks', () => {
const logs = [];
Promise.resolve(1).then(v => logs.push(v));
Promise.resolve(1).then((v) => logs.push(v));
let nestedTimeoutId;
setTimeout(() => {
logs.push('timeout');
nestedTimeoutId = setTimeout(() => {logs.push('new timeout')});
nestedTimeoutId = setTimeout(() => {
logs.push('new timeout');
});
});
expect(logs).toEqual([]);
jest.runOnlyPendingTimers();
@@ -282,10 +317,18 @@ describe('jest legacy fakeTimers with zone.js fakeAsync', () => {
test('advanceTimersToNextTimer should trigger correctly', () => {
const logs = [];
setTimeout(() => {logs.push('timeout1')}, 100);
setTimeout(() => {logs.push('timeout11')}, 100);
setTimeout(() => {logs.push('timeout2')}, 200);
setTimeout(() => {logs.push('timeout3')}, 300);
setTimeout(() => {
logs.push('timeout1');
}, 100);
setTimeout(() => {
logs.push('timeout11');
}, 100);
setTimeout(() => {
logs.push('timeout2');
}, 200);
setTimeout(() => {
logs.push('timeout3');
}, 300);
expect(logs).toEqual([]);
jest.advanceTimersToNextTimer();
expect(logs).toEqual(['timeout1', 'timeout11']);
@@ -295,9 +338,15 @@ describe('jest legacy fakeTimers with zone.js fakeAsync', () => {
test('clearAllTimers should clear all macroTasks', () => {
const logs = [];
setTimeout(() => {logs.push('timeout1')}, 100);
setTimeout(() => {logs.push('timeout2')}, 200);
setInterval(() => {logs.push('interval')}, 100);
setTimeout(() => {
logs.push('timeout1');
}, 100);
setTimeout(() => {
logs.push('timeout2');
}, 200);
setInterval(() => {
logs.push('interval');
}, 100);
expect(logs).toEqual([]);
jest.clearAllTimers();
jest.advanceTimersByTime(300);
@@ -306,9 +355,15 @@ describe('jest legacy fakeTimers with zone.js fakeAsync', () => {
test('getTimerCount should get the count of macroTasks correctly', () => {
const logs = [];
setTimeout(() => {logs.push('timeout1')}, 100);
setTimeout(() => {logs.push('timeout2')}, 200);
setInterval(() => {logs.push('interval')}, 100);
setTimeout(() => {
logs.push('timeout1');
}, 100);
setTimeout(() => {
logs.push('timeout2');
}, 200);
setInterval(() => {
logs.push('interval');
}, 100);
expect(logs).toEqual([]);
expect(jest.getTimerCount()).toEqual(3);
jest.clearAllTimers();
@@ -333,7 +388,9 @@ describe('jest fakeTimers inside test should call native delegate', () => {
test('runAllTicks should run all microTasks', () => {
jest.useFakeTimers();
const logs = [];
process.nextTick(() => {logs.push(1)});
process.nextTick(() => {
logs.push(1);
});
expect(logs).toEqual([]);
jest.runAllTicks();
expect(logs).toEqual([1]);
@@ -343,8 +400,12 @@ describe('jest fakeTimers inside test should call native delegate', () => {
test('runAllTimers should run all macroTasks', () => {
jest.useFakeTimers();
const logs = [];
process.nextTick(() => {logs.push(1)});
setTimeout(() => {logs.push('timeout')});
process.nextTick(() => {
logs.push(1);
});
setTimeout(() => {
logs.push('timeout');
});
const id = setInterval(() => {
logs.push('interval');
clearInterval(id);
@@ -358,7 +419,9 @@ describe('jest fakeTimers inside test should call native delegate', () => {
test('advanceTimersByTime should act as tick', () => {
jest.useFakeTimers();
const logs = [];
setTimeout(() => {logs.push('timeout')}, 100);
setTimeout(() => {
logs.push('timeout');
}, 100);
expect(logs).toEqual([]);
jest.advanceTimersByTime(100);
expect(logs).toEqual(['timeout']);
@@ -371,7 +434,9 @@ describe('jest fakeTimers inside test should call native delegate', () => {
let nestedTimeoutId;
setTimeout(() => {
logs.push('timeout');
nestedTimeoutId = setTimeout(() => {logs.push('new timeout')});
nestedTimeoutId = setTimeout(() => {
logs.push('new timeout');
});
});
expect(logs).toEqual([]);
jest.runOnlyPendingTimers();
@@ -383,10 +448,18 @@ describe('jest fakeTimers inside test should call native delegate', () => {
test('advanceTimersToNextTimer should trigger correctly', () => {
jest.useFakeTimers();
const logs = [];
setTimeout(() => {logs.push('timeout1')}, 100);
setTimeout(() => {logs.push('timeout11')}, 100);
setTimeout(() => {logs.push('timeout2')}, 200);
setTimeout(() => {logs.push('timeout3')}, 300);
setTimeout(() => {
logs.push('timeout1');
}, 100);
setTimeout(() => {
logs.push('timeout11');
}, 100);
setTimeout(() => {
logs.push('timeout2');
}, 200);
setTimeout(() => {
logs.push('timeout3');
}, 300);
expect(logs).toEqual([]);
jest.advanceTimersToNextTimer();
expect(logs).toEqual(['timeout1', 'timeout11']);
@@ -398,9 +471,15 @@ describe('jest fakeTimers inside test should call native delegate', () => {
test('clearAllTimers should clear all macroTasks', () => {
jest.useFakeTimers();
const logs = [];
setTimeout(() => {logs.push('timeout1')}, 100);
setTimeout(() => {logs.push('timeout2')}, 200);
setInterval(() => {logs.push('interval')}, 100);
setTimeout(() => {
logs.push('timeout1');
}, 100);
setTimeout(() => {
logs.push('timeout2');
}, 200);
setInterval(() => {
logs.push('interval');
}, 100);
expect(logs).toEqual([]);
jest.clearAllTimers();
jest.advanceTimersByTime(300);
@@ -411,9 +490,15 @@ describe('jest fakeTimers inside test should call native delegate', () => {
test('getTimerCount should get the count of macroTasks correctly', () => {
jest.useFakeTimers();
const logs = [];
setTimeout(() => {logs.push('timeout1')}, 100);
setTimeout(() => {logs.push('timeout2')}, 200);
setInterval(() => {logs.push('interval')}, 100);
setTimeout(() => {
logs.push('timeout1');
}, 100);
setTimeout(() => {
logs.push('timeout2');
}, 200);
setInterval(() => {
logs.push('interval');
}, 100);
expect(logs).toEqual([]);
expect(jest.getTimerCount()).toEqual(3);
jest.clearAllTimers();
@@ -1,9 +1,9 @@
const JsDOMEnvironment = require('jest-environment-jsdom');
const JSDOMEnvironment = require('jest-environment-jsdom').default;
const exportFakeTimersToSandboxGlobal = require('./jest-zone-patch-fake-timer');
class ZoneJsDOMEnvironment extends JsDOMEnvironment {
constructor(config) {
super(config);
class ZoneJsDOMEnvironment extends JSDOMEnvironment {
constructor(config, context) {
super(config, context);
exportFakeTimersToSandboxGlobal(this);
}
@@ -1,9 +1,9 @@
const NodeEnvironment = require('jest-environment-node');
const NodeEnvironment = require('jest-environment-node').default;
const exportFakeTimersToSandboxGlobal = require('./jest-zone-patch-fake-timer');
class ZoneNodeEnvironment extends NodeEnvironment {
constructor(config) {
super(config);
constructor(config, context) {
super(config, context);
exportFakeTimersToSandboxGlobal(this);
}
+959 -1695
View File
File diff suppressed because it is too large Load Diff