2020-01-31 10:08:40 -05:00
|
|
|
function assertInsideProxyZone() {
|
|
|
|
expect(Zone.current.name).toEqual('ProxyZone');
|
|
|
|
}
|
|
|
|
function assertInsideSyncDescribeZone() {
|
|
|
|
expect(Zone.current.name).toEqual('syncTestZone for jest.describe');
|
|
|
|
}
|
|
|
|
describe('describe', () => {
|
|
|
|
assertInsideSyncDescribeZone();
|
fix(zone.js): zone.js patch jest should handle done correctly (#36022)
`zone.js` supports jest `test.each()` methods, but it
introduces a bug, which is the `done()` function will not be handled correctly.
```
it('should work with done', done => {
// done will be undefined.
});
```
The reason is the logic of monkey patching `test` method is different from `jasmine` patch
// jasmine patch
```
return testBody.length === 0
? () => testProxyZone.run(testBody, null)
: done => testProxyZone.run(testBody, null, [done]);
```
// jest patch
```
return function(...args) {
return testProxyZone.run(testBody, null, args);
};
```
the purpose of this change is to handle the following cases.
```
test.each([1, 2])('test.each', (arg1, arg2) => {
expect(arg1).toBe(1);
expect(arg2).toBe(2);
});
```
so in jest, it is a little complex, because the `testBody`'s parameter may be bigger than 1, so the
logic in `jasmine`
```
return testBody.length === 0
? () => testProxyZone.run(testBody, null)
: done => testProxyZone.run(testBody, null, [done]);
```
will not work for `test.each` in jest.
So in this PR, I created a dynamic `Function` to return the correct length of paramters (which is required by jest core), to handle
1. normal `test` with or without `done`.
2. each with parameters with or without done.
PR Close #36022
2020-03-11 12:41:19 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
assertInsideProxyZone();
|
|
|
|
});
|
|
|
|
beforeAll(() => {
|
|
|
|
assertInsideProxyZone();
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
assertInsideProxyZone();
|
|
|
|
});
|
|
|
|
afterAll(() => {
|
|
|
|
assertInsideProxyZone();
|
|
|
|
});
|
2020-01-31 10:08:40 -05:00
|
|
|
});
|
|
|
|
describe.each([[1, 2]])('describe.each', (arg1, arg2) => {
|
|
|
|
assertInsideSyncDescribeZone();
|
|
|
|
expect(arg1).toBe(1);
|
|
|
|
expect(arg2).toBe(2);
|
|
|
|
});
|
|
|
|
describe('test', () => {
|
fix(zone.js): zone.js patch jest should handle done correctly (#36022)
`zone.js` supports jest `test.each()` methods, but it
introduces a bug, which is the `done()` function will not be handled correctly.
```
it('should work with done', done => {
// done will be undefined.
});
```
The reason is the logic of monkey patching `test` method is different from `jasmine` patch
// jasmine patch
```
return testBody.length === 0
? () => testProxyZone.run(testBody, null)
: done => testProxyZone.run(testBody, null, [done]);
```
// jest patch
```
return function(...args) {
return testProxyZone.run(testBody, null, args);
};
```
the purpose of this change is to handle the following cases.
```
test.each([1, 2])('test.each', (arg1, arg2) => {
expect(arg1).toBe(1);
expect(arg2).toBe(2);
});
```
so in jest, it is a little complex, because the `testBody`'s parameter may be bigger than 1, so the
logic in `jasmine`
```
return testBody.length === 0
? () => testProxyZone.run(testBody, null)
: done => testProxyZone.run(testBody, null, [done]);
```
will not work for `test.each` in jest.
So in this PR, I created a dynamic `Function` to return the correct length of paramters (which is required by jest core), to handle
1. normal `test` with or without `done`.
2. each with parameters with or without done.
PR Close #36022
2020-03-11 12:41:19 -04:00
|
|
|
it('it', () => {
|
|
|
|
assertInsideProxyZone();
|
|
|
|
});
|
2020-01-31 10:08:40 -05:00
|
|
|
it.each([[1, 2]])('it.each', (arg1, arg2) => {
|
|
|
|
assertInsideProxyZone();
|
|
|
|
expect(arg1).toBe(1);
|
|
|
|
expect(arg2).toBe(2);
|
|
|
|
});
|
fix(zone.js): zone.js patch jest should handle done correctly (#36022)
`zone.js` supports jest `test.each()` methods, but it
introduces a bug, which is the `done()` function will not be handled correctly.
```
it('should work with done', done => {
// done will be undefined.
});
```
The reason is the logic of monkey patching `test` method is different from `jasmine` patch
// jasmine patch
```
return testBody.length === 0
? () => testProxyZone.run(testBody, null)
: done => testProxyZone.run(testBody, null, [done]);
```
// jest patch
```
return function(...args) {
return testProxyZone.run(testBody, null, args);
};
```
the purpose of this change is to handle the following cases.
```
test.each([1, 2])('test.each', (arg1, arg2) => {
expect(arg1).toBe(1);
expect(arg2).toBe(2);
});
```
so in jest, it is a little complex, because the `testBody`'s parameter may be bigger than 1, so the
logic in `jasmine`
```
return testBody.length === 0
? () => testProxyZone.run(testBody, null)
: done => testProxyZone.run(testBody, null, [done]);
```
will not work for `test.each` in jest.
So in this PR, I created a dynamic `Function` to return the correct length of paramters (which is required by jest core), to handle
1. normal `test` with or without `done`.
2. each with parameters with or without done.
PR Close #36022
2020-03-11 12:41:19 -04:00
|
|
|
test('test', () => {
|
|
|
|
assertInsideProxyZone();
|
|
|
|
});
|
|
|
|
test.each([[]])('test.each', () => {
|
|
|
|
assertInsideProxyZone();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('it', () => {
|
|
|
|
assertInsideProxyZone();
|
|
|
|
});
|
|
|
|
it('it with done', done => {
|
|
|
|
assertInsideProxyZone();
|
|
|
|
done();
|
2020-01-31 10:08:40 -05:00
|
|
|
});
|
|
|
|
|
fix(zone.js): zone.js patch jest should handle done correctly (#36022)
`zone.js` supports jest `test.each()` methods, but it
introduces a bug, which is the `done()` function will not be handled correctly.
```
it('should work with done', done => {
// done will be undefined.
});
```
The reason is the logic of monkey patching `test` method is different from `jasmine` patch
// jasmine patch
```
return testBody.length === 0
? () => testProxyZone.run(testBody, null)
: done => testProxyZone.run(testBody, null, [done]);
```
// jest patch
```
return function(...args) {
return testProxyZone.run(testBody, null, args);
};
```
the purpose of this change is to handle the following cases.
```
test.each([1, 2])('test.each', (arg1, arg2) => {
expect(arg1).toBe(1);
expect(arg2).toBe(2);
});
```
so in jest, it is a little complex, because the `testBody`'s parameter may be bigger than 1, so the
logic in `jasmine`
```
return testBody.length === 0
? () => testProxyZone.run(testBody, null)
: done => testProxyZone.run(testBody, null, [done]);
```
will not work for `test.each` in jest.
So in this PR, I created a dynamic `Function` to return the correct length of paramters (which is required by jest core), to handle
1. normal `test` with or without `done`.
2. each with parameters with or without done.
PR Close #36022
2020-03-11 12:41:19 -04:00
|
|
|
it.each([[1, 2]])('it.each', (arg1, arg2, done) => {
|
2020-01-31 10:08:40 -05:00
|
|
|
assertInsideProxyZone();
|
|
|
|
expect(arg1).toBe(1);
|
|
|
|
expect(arg2).toBe(2);
|
fix(zone.js): zone.js patch jest should handle done correctly (#36022)
`zone.js` supports jest `test.each()` methods, but it
introduces a bug, which is the `done()` function will not be handled correctly.
```
it('should work with done', done => {
// done will be undefined.
});
```
The reason is the logic of monkey patching `test` method is different from `jasmine` patch
// jasmine patch
```
return testBody.length === 0
? () => testProxyZone.run(testBody, null)
: done => testProxyZone.run(testBody, null, [done]);
```
// jest patch
```
return function(...args) {
return testProxyZone.run(testBody, null, args);
};
```
the purpose of this change is to handle the following cases.
```
test.each([1, 2])('test.each', (arg1, arg2) => {
expect(arg1).toBe(1);
expect(arg2).toBe(2);
});
```
so in jest, it is a little complex, because the `testBody`'s parameter may be bigger than 1, so the
logic in `jasmine`
```
return testBody.length === 0
? () => testProxyZone.run(testBody, null)
: done => testProxyZone.run(testBody, null, [done]);
```
will not work for `test.each` in jest.
So in this PR, I created a dynamic `Function` to return the correct length of paramters (which is required by jest core), to handle
1. normal `test` with or without `done`.
2. each with parameters with or without done.
PR Close #36022
2020-03-11 12:41:19 -04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([2])('it.each with 1D array', arg1 => {
|
|
|
|
assertInsideProxyZone();
|
|
|
|
expect(arg1).toBe(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([2])('it.each with 1D array and done', (arg1, done) => {
|
|
|
|
assertInsideProxyZone();
|
|
|
|
expect(arg1).toBe(2);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
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) => {
|
|
|
|
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}) => {
|
|
|
|
expect(foo).toBe(1);
|
|
|
|
expect(bar).toBe(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('test', () => {
|
|
|
|
assertInsideProxyZone();
|
|
|
|
});
|
|
|
|
test.each([[]])('test.each', () => {
|
|
|
|
assertInsideProxyZone();
|
2020-01-31 10:08:40 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test.todo('todo');
|
2020-09-22 09:52:20 -04:00
|
|
|
|
|
|
|
function enableJestPatch() {
|
|
|
|
global[Zone.__symbol__('fakeAsyncDisablePatchingFakeTimer')] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function disableJestPatch() {
|
|
|
|
global[Zone.__symbol__('fakeAsyncDisablePatchingFakeTimer')] = false;
|
|
|
|
}
|
|
|
|
const {resetFakeAsyncZone, flushMicrotasks, discardPeriodicTasks, tick, flush, fakeAsync} =
|
|
|
|
Zone[Zone.__symbol__('fakeAsyncTest')];
|
|
|
|
|
|
|
|
describe('jest modern fakeTimers with zone.js fakeAsync', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.useFakeTimers('modern');
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should run into fakeAsync() automatically', () => {
|
|
|
|
const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
|
|
|
|
expect(fakeAsyncZoneSpec).toBeTruthy();
|
|
|
|
expect(typeof fakeAsyncZoneSpec.tick).toEqual('function');
|
|
|
|
});
|
|
|
|
|
2020-10-05 23:45:16 -04:00
|
|
|
test('setSystemTime should set FakeDate.currentFakeTime', () => {
|
2020-09-22 09:52:20 -04:00
|
|
|
const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
|
2020-10-05 23:45:16 -04:00
|
|
|
let d = fakeAsyncZoneSpec.getRealSystemTime();
|
2020-09-22 09:52:20 -04:00
|
|
|
jest.setSystemTime(d);
|
|
|
|
expect(Date.now()).toEqual(d);
|
|
|
|
for (let i = 0; i < 100000; i++) {
|
|
|
|
}
|
|
|
|
expect(fakeAsyncZoneSpec.getRealSystemTime()).not.toEqual(d);
|
2020-10-05 23:45:16 -04:00
|
|
|
d = fakeAsyncZoneSpec.getRealSystemTime();
|
|
|
|
let timeoutTriggered = false;
|
|
|
|
setTimeout(() => {
|
|
|
|
timeoutTriggered = true;
|
|
|
|
}, 100);
|
|
|
|
jest.setSystemTime(d);
|
|
|
|
tick(100);
|
|
|
|
expect(timeoutTriggered).toBe(true);
|
|
|
|
expect(Date.now()).toEqual(d + 100);
|
2020-09-22 09:52:20 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
test('runAllTicks should run all microTasks', () => {
|
|
|
|
const logs = [];
|
|
|
|
Promise.resolve(1).then(v => logs.push(v));
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
jest.runAllTicks();
|
|
|
|
expect(logs).toEqual([1]);
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(logs).toEqual([1, 'timeout', 'interval']);
|
|
|
|
clearInterval(id);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('advanceTimersByTime should act as tick', () => {
|
|
|
|
const logs = [];
|
|
|
|
setTimeout(() => {logs.push('timeout')}, 100);
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
jest.advanceTimersByTime(100);
|
|
|
|
expect(logs).toEqual(['timeout']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('runOnlyPendingTimers should run all macroTasks and ignore new spawn macroTasks', () => {
|
|
|
|
const logs = [];
|
|
|
|
Promise.resolve(1).then(v => logs.push(v));
|
|
|
|
let nestedTimeoutId;
|
|
|
|
setTimeout(() => {
|
|
|
|
logs.push('timeout');
|
|
|
|
nestedTimeoutId = setTimeout(() => {logs.push('new timeout')});
|
|
|
|
});
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
expect(logs).toEqual([1, 'timeout']);
|
|
|
|
clearTimeout(nestedTimeoutId);
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
jest.advanceTimersToNextTimer();
|
|
|
|
expect(logs).toEqual(['timeout1', 'timeout11']);
|
|
|
|
jest.advanceTimersToNextTimer(2);
|
|
|
|
expect(logs).toEqual(['timeout1', 'timeout11', 'timeout2', 'timeout3']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('clearAllTimers should clear all macroTasks', () => {
|
|
|
|
const logs = [];
|
|
|
|
setTimeout(() => {logs.push('timeout1')}, 100);
|
|
|
|
setTimeout(() => {logs.push('timeout2')}, 200);
|
|
|
|
setInterval(() => {logs.push('interval')}, 100);
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
jest.clearAllTimers();
|
|
|
|
jest.advanceTimersByTime(300);
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
expect(jest.getTimerCount()).toEqual(3);
|
|
|
|
jest.clearAllTimers();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('jest legacy fakeTimers with zone.js fakeAsync', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should run into fakeAsync() automatically', () => {
|
|
|
|
const fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
|
|
|
|
expect(fakeAsyncZoneSpec).toBeTruthy();
|
|
|
|
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));
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
jest.runAllTicks();
|
|
|
|
expect(logs).toEqual([1]);
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(logs).toEqual([1, 'timeout', 'interval']);
|
|
|
|
clearInterval(id);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('advanceTimersByTime should act as tick', () => {
|
|
|
|
const logs = [];
|
|
|
|
setTimeout(() => {logs.push('timeout')}, 100);
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
jest.advanceTimersByTime(100);
|
|
|
|
expect(logs).toEqual(['timeout']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('runOnlyPendingTimers should run all macroTasks and ignore new spawn macroTasks', () => {
|
|
|
|
const logs = [];
|
|
|
|
Promise.resolve(1).then(v => logs.push(v));
|
|
|
|
let nestedTimeoutId;
|
|
|
|
setTimeout(() => {
|
|
|
|
logs.push('timeout');
|
|
|
|
nestedTimeoutId = setTimeout(() => {logs.push('new timeout')});
|
|
|
|
});
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
expect(logs).toEqual([1, 'timeout']);
|
|
|
|
clearTimeout(nestedTimeoutId);
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
jest.advanceTimersToNextTimer();
|
|
|
|
expect(logs).toEqual(['timeout1', 'timeout11']);
|
|
|
|
jest.advanceTimersToNextTimer(2);
|
|
|
|
expect(logs).toEqual(['timeout1', 'timeout11', 'timeout2', 'timeout3']);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('clearAllTimers should clear all macroTasks', () => {
|
|
|
|
const logs = [];
|
|
|
|
setTimeout(() => {logs.push('timeout1')}, 100);
|
|
|
|
setTimeout(() => {logs.push('timeout2')}, 200);
|
|
|
|
setInterval(() => {logs.push('interval')}, 100);
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
jest.clearAllTimers();
|
|
|
|
jest.advanceTimersByTime(300);
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
expect(jest.getTimerCount()).toEqual(3);
|
|
|
|
jest.clearAllTimers();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('jest fakeTimers inside test should call native delegate', () => {
|
|
|
|
test('setSystemTime should set FakeDate.currentRealTime', () => {
|
|
|
|
let fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
|
|
|
|
expect(fakeAsyncZoneSpec).toBeFalsy();
|
|
|
|
jest.useFakeTimers('modern');
|
|
|
|
fakeAsyncZoneSpec = Zone.current.get('FakeAsyncTestZoneSpec');
|
|
|
|
expect(fakeAsyncZoneSpec).toBeFalsy();
|
|
|
|
const d = Date.now();
|
|
|
|
jest.setSystemTime(d);
|
|
|
|
for (let i = 0; i < 100000; i++) {
|
|
|
|
}
|
|
|
|
expect(jest.getRealSystemTime()).not.toEqual(d);
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('runAllTicks should run all microTasks', () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
const logs = [];
|
|
|
|
process.nextTick(() => {logs.push(1)});
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
jest.runAllTicks();
|
|
|
|
expect(logs).toEqual([1]);
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('runAllTimers should run all macroTasks', () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
const logs = [];
|
|
|
|
process.nextTick(() => {logs.push(1)});
|
|
|
|
setTimeout(() => {logs.push('timeout')});
|
|
|
|
const id = setInterval(() => {
|
|
|
|
logs.push('interval');
|
|
|
|
clearInterval(id);
|
|
|
|
}, 100);
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(logs).toEqual([1, 'timeout', 'interval']);
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('advanceTimersByTime should act as tick', () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
const logs = [];
|
|
|
|
setTimeout(() => {logs.push('timeout')}, 100);
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
jest.advanceTimersByTime(100);
|
|
|
|
expect(logs).toEqual(['timeout']);
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('runOnlyPendingTimers should run all macroTasks and ignore new spawn macroTasks', () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
const logs = [];
|
|
|
|
let nestedTimeoutId;
|
|
|
|
setTimeout(() => {
|
|
|
|
logs.push('timeout');
|
|
|
|
nestedTimeoutId = setTimeout(() => {logs.push('new timeout')});
|
|
|
|
});
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
expect(logs).toEqual(['timeout']);
|
|
|
|
clearTimeout(nestedTimeoutId);
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
jest.advanceTimersToNextTimer();
|
|
|
|
expect(logs).toEqual(['timeout1', 'timeout11']);
|
|
|
|
jest.advanceTimersToNextTimer(2);
|
|
|
|
expect(logs).toEqual(['timeout1', 'timeout11', 'timeout2', 'timeout3']);
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
jest.clearAllTimers();
|
|
|
|
jest.advanceTimersByTime(300);
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
expect(logs).toEqual([]);
|
|
|
|
expect(jest.getTimerCount()).toEqual(3);
|
|
|
|
jest.clearAllTimers();
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
});
|