Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: setup concurrent mode testing env
- Loading branch information
Showing
10 changed files
with
1,060 additions
and
490 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* eslint-disable camelcase */ | ||
|
||
// React internal mocking | ||
export type Scheduler = typeof import('scheduler') & { | ||
unstable_IdlePriority: number | ||
unstable_ImmediatePriority: number | ||
unstable_LowPriority: number | ||
unstable_NormalPriority: number | ||
unstable_Profiling: { | ||
startLoggingProfilingEvents: () => void | ||
stopLoggingProfilingEvents: () => any | ||
sharedProfilingBuffer: ArrayBuffer | ||
} | ||
unstable_UserBlockingPriority: number | ||
unstable_advanceTime: (ms: any) => void | ||
unstable_cancelCallback: (task: any) => void | ||
unstable_clearYields: () => any | ||
unstable_continueExecution: () => void | ||
unstable_flushAll: () => void | ||
unstable_flushAllWithoutAsserting: () => boolean | ||
unstable_flushExpired: () => void | ||
unstable_flushNumberOfYields: (count: any) => void | ||
unstable_flushUntilNextPaint: () => void | ||
unstable_forceFrameRate: () => void | ||
unstable_getCurrentPriorityLevel: () => number | ||
unstable_getFirstCallbackNode: () => any | ||
unstable_next: (eventHandler: any) => any | ||
unstable_now: () => number | ||
unstable_pauseExecution: () => void | ||
unstable_requestPaint: () => void | ||
unstable_runWithPriority: (priorityLevel: any, eventHandler: any) => any | ||
unstable_scheduleCallback: ( | ||
priorityLevel: any, | ||
callback: any, | ||
options: any | ||
) => { | ||
id: number | ||
callback: any | ||
priorityLevel: any | ||
startTime: number | ||
expirationTime: any | ||
sortIndex: number | ||
} | ||
unstable_shouldYield: () => boolean | ||
unstable_wrapCallback: (callback: any) => (...args: any[]) => any | ||
unstable_yieldValue: (value: any) => void | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["__tests__"] | ||
"extends": "../../tsconfig.test.json", | ||
"include": ["../../typings", "__tests__"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
expect.extend(require('./schedulerTestMatchers')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// https://github.com/facebook/react/blob/master/scripts/jest/matchers/schedulerTestMatchers.js | ||
|
||
'use strict' | ||
|
||
function captureAssertion(fn) { | ||
// Trick to use a Jest matcher inside another Jest matcher. `fn` contains an | ||
// assertion; if it throws, we capture the error and return it, so the stack | ||
// trace presented to the user points to the original assertion in the | ||
// test file. | ||
try { | ||
fn() | ||
} catch (error) { | ||
return { | ||
pass: false, | ||
message: () => error.message | ||
} | ||
} | ||
return { pass: true } | ||
} | ||
|
||
function assertYieldsWereCleared(Scheduler) { | ||
const actualYields = Scheduler.unstable_clearYields() | ||
if (actualYields.length !== 0) { | ||
throw new Error( | ||
'Log of yielded values is not empty. ' + | ||
'Call expect(Scheduler).toHaveYielded(...) first.' | ||
) | ||
} | ||
} | ||
|
||
function toFlushAndYield(Scheduler, expectedYields) { | ||
assertYieldsWereCleared(Scheduler) | ||
Scheduler.unstable_flushAllWithoutAsserting() | ||
const actualYields = Scheduler.unstable_clearYields() | ||
return captureAssertion(() => { | ||
expect(actualYields).toEqual(expectedYields) | ||
}) | ||
} | ||
|
||
function toFlushAndYieldThrough(Scheduler, expectedYields) { | ||
assertYieldsWereCleared(Scheduler) | ||
Scheduler.unstable_flushNumberOfYields(expectedYields.length) | ||
const actualYields = Scheduler.unstable_clearYields() | ||
return captureAssertion(() => { | ||
expect(actualYields).toEqual(expectedYields) | ||
}) | ||
} | ||
|
||
function toFlushUntilNextPaint(Scheduler, expectedYields) { | ||
assertYieldsWereCleared(Scheduler) | ||
Scheduler.unstable_flushUntilNextPaint() | ||
const actualYields = Scheduler.unstable_clearYields() | ||
return captureAssertion(() => { | ||
expect(actualYields).toEqual(expectedYields) | ||
}) | ||
} | ||
|
||
function toFlushWithoutYielding(Scheduler) { | ||
return toFlushAndYield(Scheduler, []) | ||
} | ||
|
||
function toFlushExpired(Scheduler, expectedYields) { | ||
assertYieldsWereCleared(Scheduler) | ||
Scheduler.unstable_flushExpired() | ||
const actualYields = Scheduler.unstable_clearYields() | ||
return captureAssertion(() => { | ||
expect(actualYields).toEqual(expectedYields) | ||
}) | ||
} | ||
|
||
function toHaveYielded(Scheduler, expectedYields) { | ||
return captureAssertion(() => { | ||
const actualYields = Scheduler.unstable_clearYields() | ||
expect(actualYields).toEqual(expectedYields) | ||
}) | ||
} | ||
|
||
function toFlushAndThrow(Scheduler, ...rest) { | ||
assertYieldsWereCleared(Scheduler) | ||
return captureAssertion(() => { | ||
expect(() => { | ||
Scheduler.unstable_flushAllWithoutAsserting() | ||
}).toThrow(...rest) | ||
}) | ||
} | ||
|
||
module.exports = { | ||
toFlushAndYield, | ||
toFlushAndYieldThrough, | ||
toFlushUntilNextPaint, | ||
toFlushWithoutYielding, | ||
toFlushExpired, | ||
toHaveYielded, | ||
toFlushAndThrow | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"include": ["packages/**/__tests__"] | ||
"compilerOptions": { | ||
"target": "ES2019" | ||
}, | ||
"include": ["packages/**/__tests__", "typings"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
export {} | ||
|
||
declare global { | ||
namespace jest { | ||
interface Matchers<R> { | ||
/** | ||
* 1. Assert Yields is initially empty. | ||
* 2. Flush all tasks. | ||
* 3. Collect Yields and compare. | ||
*/ | ||
toFlushAndYield( | ||
expectedYields: any[] | ||
): { | ||
pass: boolean | ||
message?: () => any | ||
} | ||
/** | ||
* 1. Assert Yields is initially empty. | ||
* 2. Flush tasks until `expectedYields.length` Yields are collected. | ||
* 3. Compare. | ||
*/ | ||
toFlushAndYieldThrough( | ||
expectedYields: any[] | ||
): { | ||
pass: boolean | ||
message?: () => any | ||
} | ||
/** | ||
* 1. Assert Yields is initially empty. | ||
* 2. Flush tasks until meeting a paint request. | ||
* 3. Collect Yields and compare. | ||
*/ | ||
toFlushUntilNextPaint( | ||
expectedYields: any[] | ||
): { | ||
pass: boolean | ||
message?: () => any | ||
} | ||
/** | ||
* 1. Assert Yields is initially empty. | ||
* 2. Flush all tasks. | ||
* 3. Expect empty Yield. | ||
*/ | ||
toFlushWithoutYielding(): { | ||
pass: boolean | ||
message?: () => any | ||
} | ||
/** | ||
* 1. Assert Yields is initially empty. | ||
* 2. Flush the next scheduled task. | ||
* 3. Collect Yields and compare. | ||
*/ | ||
toFlushExpired( | ||
expectedYields: any[] | ||
): { | ||
pass: boolean | ||
message?: () => any | ||
} | ||
/** | ||
* Compare Yields. | ||
*/ | ||
toHaveYielded( | ||
expectedYields: any[] | ||
): { | ||
pass: boolean | ||
message?: () => any | ||
} | ||
/** | ||
* 1. Assert Yields is initially empty. | ||
* 2. Expect exception is thrown when flushing all tasks. | ||
*/ | ||
toFlushAndThrow( | ||
...rest: any[] | ||
): { | ||
pass: boolean | ||
message?: () => any | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.