@voidzero-dev/vite-plus-test 0.0.0-0bfcc90f.20260209-0731
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +691 -0
- package/browser/context.d.ts +4 -0
- package/browser/context.js +20 -0
- package/config.d.ts +3 -0
- package/coverage.d.ts +1 -0
- package/dist/@vitest/browser/client/.vite/manifest.json +24 -0
- package/dist/@vitest/browser/client/__vitest__/assets/index-BUCFJtth.js +57 -0
- package/dist/@vitest/browser/client/__vitest__/assets/index-DlhE0rqZ.css +1 -0
- package/dist/@vitest/browser/client/__vitest__/bg.png +0 -0
- package/dist/@vitest/browser/client/__vitest__/favicon.ico +0 -0
- package/dist/@vitest/browser/client/__vitest__/favicon.svg +5 -0
- package/dist/@vitest/browser/client/__vitest__/index.html +32 -0
- package/dist/@vitest/browser/client/__vitest_browser__/orchestrator-S_3e_uzt.js +345 -0
- package/dist/@vitest/browser/client/__vitest_browser__/tester-k74mgIRa.js +2171 -0
- package/dist/@vitest/browser/client/__vitest_browser__/utils-uxqdqUz8.js +223 -0
- package/dist/@vitest/browser/client/error-catcher.js +82 -0
- package/dist/@vitest/browser/client/esm-client-injector.js +67 -0
- package/dist/@vitest/browser/client/favicon.svg +5 -0
- package/dist/@vitest/browser/client/orchestrator.html +35 -0
- package/dist/@vitest/browser/client/tester/tester.html +13 -0
- package/dist/@vitest/browser/client.js +456 -0
- package/dist/@vitest/browser/context.d.ts +792 -0
- package/dist/@vitest/browser/context.js +541 -0
- package/dist/@vitest/browser/expect-element.js +32 -0
- package/dist/@vitest/browser/index-D6m36C6U.js +11 -0
- package/dist/@vitest/browser/index.d.ts +73 -0
- package/dist/@vitest/browser/index.js +3513 -0
- package/dist/@vitest/browser/jest-dom.d.ts +724 -0
- package/dist/@vitest/browser/locators.d.ts +354 -0
- package/dist/@vitest/browser/locators.js +1 -0
- package/dist/@vitest/browser/matchers.d.ts +29 -0
- package/dist/@vitest/browser/shared/screenshotMatcher/types.d.ts +22 -0
- package/dist/@vitest/browser/state.js +280 -0
- package/dist/@vitest/browser/types.d.ts +69 -0
- package/dist/@vitest/browser-playwright/context.d.ts +1 -0
- package/dist/@vitest/browser-playwright/index.d.ts +106 -0
- package/dist/@vitest/browser-playwright/index.js +1111 -0
- package/dist/@vitest/browser-playwright/locators.js +114 -0
- package/dist/@vitest/browser-preview/context.d.ts +1 -0
- package/dist/@vitest/browser-preview/index.d.ts +19 -0
- package/dist/@vitest/browser-preview/index.js +148 -0
- package/dist/@vitest/browser-preview/locators.js +79 -0
- package/dist/@vitest/browser-webdriverio/context.d.ts +1 -0
- package/dist/@vitest/browser-webdriverio/index.d.ts +63 -0
- package/dist/@vitest/browser-webdriverio/index.js +600 -0
- package/dist/@vitest/browser-webdriverio/locators.js +163 -0
- package/dist/@vitest/expect/index.d.ts +807 -0
- package/dist/@vitest/expect/index.js +1875 -0
- package/dist/@vitest/mocker/auto-register.d.ts +2 -0
- package/dist/@vitest/mocker/auto-register.js +9 -0
- package/dist/@vitest/mocker/automock.d.ts +12 -0
- package/dist/@vitest/mocker/automock.js +1 -0
- package/dist/@vitest/mocker/browser.d.ts +53 -0
- package/dist/@vitest/mocker/browser.js +91 -0
- package/dist/@vitest/mocker/chunk-automock.js +354 -0
- package/dist/@vitest/mocker/chunk-interceptor-native.js +15 -0
- package/dist/@vitest/mocker/chunk-mocker.js +521 -0
- package/dist/@vitest/mocker/chunk-pathe.M-eThtNZ.js +174 -0
- package/dist/@vitest/mocker/chunk-registry.js +185 -0
- package/dist/@vitest/mocker/chunk-utils.js +16 -0
- package/dist/@vitest/mocker/index.d-C-sLYZi-.d.ts +25 -0
- package/dist/@vitest/mocker/index.d.ts +2 -0
- package/dist/@vitest/mocker/index.js +185 -0
- package/dist/@vitest/mocker/mocker.d-TnKRhz7N.d.ts +81 -0
- package/dist/@vitest/mocker/node.d.ts +800 -0
- package/dist/@vitest/mocker/node.js +966 -0
- package/dist/@vitest/mocker/redirect.d.ts +3 -0
- package/dist/@vitest/mocker/redirect.js +79 -0
- package/dist/@vitest/mocker/register.d.ts +9 -0
- package/dist/@vitest/mocker/register.js +41 -0
- package/dist/@vitest/mocker/types.d-B8CCKmHt.d.ts +107 -0
- package/dist/@vitest/pretty-format/index.d.ts +124 -0
- package/dist/@vitest/pretty-format/index.js +1022 -0
- package/dist/@vitest/runner/chunk-tasks.js +340 -0
- package/dist/@vitest/runner/index.d.ts +180 -0
- package/dist/@vitest/runner/index.js +2114 -0
- package/dist/@vitest/runner/tasks.d-C7UxawJ9.d.ts +834 -0
- package/dist/@vitest/runner/types.d.ts +183 -0
- package/dist/@vitest/runner/types.js +1 -0
- package/dist/@vitest/runner/utils.d.ts +45 -0
- package/dist/@vitest/runner/utils.js +5 -0
- package/dist/@vitest/snapshot/environment.d-DHdQ1Csl.d.ts +22 -0
- package/dist/@vitest/snapshot/environment.d.ts +16 -0
- package/dist/@vitest/snapshot/environment.js +40 -0
- package/dist/@vitest/snapshot/index.d.ts +130 -0
- package/dist/@vitest/snapshot/index.js +1437 -0
- package/dist/@vitest/snapshot/manager.d.ts +18 -0
- package/dist/@vitest/snapshot/manager.js +73 -0
- package/dist/@vitest/snapshot/rawSnapshot.d-lFsMJFUd.d.ts +61 -0
- package/dist/@vitest/spy/index.d.ts +384 -0
- package/dist/@vitest/spy/index.js +433 -0
- package/dist/@vitest/utils/chunk-_commonjsHelpers.js +5 -0
- package/dist/@vitest/utils/chunk-pathe.M-eThtNZ.js +156 -0
- package/dist/@vitest/utils/constants.d.ts +21 -0
- package/dist/@vitest/utils/constants.js +49 -0
- package/dist/@vitest/utils/diff.d.ts +93 -0
- package/dist/@vitest/utils/diff.js +2199 -0
- package/dist/@vitest/utils/display.d.ts +29 -0
- package/dist/@vitest/utils/display.js +742 -0
- package/dist/@vitest/utils/error.d.ts +7 -0
- package/dist/@vitest/utils/error.js +42 -0
- package/dist/@vitest/utils/helpers.d.ts +73 -0
- package/dist/@vitest/utils/helpers.js +295 -0
- package/dist/@vitest/utils/highlight.d.ts +9 -0
- package/dist/@vitest/utils/highlight.js +538 -0
- package/dist/@vitest/utils/index.d.ts +5 -0
- package/dist/@vitest/utils/index.js +1 -0
- package/dist/@vitest/utils/offset.d.ts +5 -0
- package/dist/@vitest/utils/offset.js +32 -0
- package/dist/@vitest/utils/resolver.d.ts +7 -0
- package/dist/@vitest/utils/resolver.js +71 -0
- package/dist/@vitest/utils/serialize.d.ts +3 -0
- package/dist/@vitest/utils/serialize.js +118 -0
- package/dist/@vitest/utils/source-map.d.ts +55 -0
- package/dist/@vitest/utils/source-map.js +478 -0
- package/dist/@vitest/utils/timers.d.ts +33 -0
- package/dist/@vitest/utils/timers.js +49 -0
- package/dist/@vitest/utils/types.d-BCElaP-c.d.ts +53 -0
- package/dist/@vitest/utils/types.d.ts +34 -0
- package/dist/@vitest/utils/types.js +1 -0
- package/dist/browser-compat.js +3 -0
- package/dist/browser.d.ts +46 -0
- package/dist/browser.js +20 -0
- package/dist/chunks/_commonjsHelpers.D26ty3Ew.js +6 -0
- package/dist/chunks/base.CJ0Y4ePK.js +165 -0
- package/dist/chunks/benchmark.B3N2zMcH.js +40 -0
- package/dist/chunks/benchmark.d.DAaHLpsq.d.ts +24 -0
- package/dist/chunks/browser.d.ChKACdzH.d.ts +59 -0
- package/dist/chunks/cac.DVeoLl0M.js +1409 -0
- package/dist/chunks/cli-api.B7PN_QUv.js +13672 -0
- package/dist/chunks/config.d.Cy95HiCx.d.ts +210 -0
- package/dist/chunks/console.Cf-YriPC.js +146 -0
- package/dist/chunks/constants.D_Q9UYh-.js +36 -0
- package/dist/chunks/coverage.AVPTjMgw.js +3292 -0
- package/dist/chunks/coverage.D_JHT54q.js +25 -0
- package/dist/chunks/coverage.d.BZtK59WP.d.ts +37 -0
- package/dist/chunks/creator.DAmOKTvJ.js +673 -0
- package/dist/chunks/date.Bq6ZW5rf.js +73 -0
- package/dist/chunks/defaults.BOqNVLsY.js +74 -0
- package/dist/chunks/env.D4Lgay0q.js +8 -0
- package/dist/chunks/environment.d.CrsxCzP1.d.ts +29 -0
- package/dist/chunks/evaluatedModules.Dg1zASAC.js +17 -0
- package/dist/chunks/evaluatedModules.d.BxJ5omdx.d.ts +7 -0
- package/dist/chunks/git.Bm2pzPAa.js +71 -0
- package/dist/chunks/global.d.B15mdLcR.d.ts +99 -0
- package/dist/chunks/globals.DOayXfHP.js +30 -0
- package/dist/chunks/index.6Qv1eEA6.js +109 -0
- package/dist/chunks/index.C5r1PdPD.js +231 -0
- package/dist/chunks/index.Chj8NDwU.js +206 -0
- package/dist/chunks/index.CyBMJtT7.js +727 -0
- package/dist/chunks/index.D3XRDfWc.js +213 -0
- package/dist/chunks/index.D4KonVSU.js +6343 -0
- package/dist/chunks/index.M8mOzt4Y.js +3839 -0
- package/dist/chunks/index.Z5E_ObnR.js +37 -0
- package/dist/chunks/init-forks._y3TW739.js +41 -0
- package/dist/chunks/init-threads.DBO2kn-p.js +18 -0
- package/dist/chunks/init.B6MLFIaN.js +334 -0
- package/dist/chunks/inspector.CvyFGlXm.js +53 -0
- package/dist/chunks/modules.BJuCwlRJ.js +36 -0
- package/dist/chunks/node.Ce0vMQM7.js +14 -0
- package/dist/chunks/plugin.d.CtqpEehP.d.ts +38 -0
- package/dist/chunks/reporters.d.CWXNI2jG.d.ts +3271 -0
- package/dist/chunks/rpc.BoxB0q7B.js +76 -0
- package/dist/chunks/rpc.d.RH3apGEf.d.ts +64 -0
- package/dist/chunks/setup-common.Cm-kSBVi.js +60 -0
- package/dist/chunks/startModuleRunner.DEj0jb3e.js +861 -0
- package/dist/chunks/suite.d.BJWk38HB.d.ts +10 -0
- package/dist/chunks/test.B8ej_ZHS.js +254 -0
- package/dist/chunks/traces.CCmnQaNT.js +217 -0
- package/dist/chunks/traces.d.402V_yFI.d.ts +18 -0
- package/dist/chunks/utils.DvEY5TfP.js +52 -0
- package/dist/chunks/vi.2VT5v0um.js +3919 -0
- package/dist/chunks/vm.D3epNOPZ.js +744 -0
- package/dist/chunks/worker.d.Dyxm8DEL.d.ts +255 -0
- package/dist/cli.js +28 -0
- package/dist/client/.vite/manifest.json +24 -0
- package/dist/client/__vitest__/assets/index-BUCFJtth.js +57 -0
- package/dist/client/__vitest__/assets/index-DlhE0rqZ.css +1 -0
- package/dist/client/__vitest__/bg.png +0 -0
- package/dist/client/__vitest__/favicon.ico +0 -0
- package/dist/client/__vitest__/favicon.svg +5 -0
- package/dist/client/__vitest__/index.html +32 -0
- package/dist/client/__vitest_browser__/orchestrator-S_3e_uzt.js +345 -0
- package/dist/client/__vitest_browser__/tester-k74mgIRa.js +2171 -0
- package/dist/client/__vitest_browser__/utils-uxqdqUz8.js +223 -0
- package/dist/client/error-catcher.js +82 -0
- package/dist/client/esm-client-injector.js +67 -0
- package/dist/client/favicon.svg +5 -0
- package/dist/client/orchestrator.html +35 -0
- package/dist/client/tester/tester.html +13 -0
- package/dist/client.js +456 -0
- package/dist/config.cjs +94 -0
- package/dist/config.d.ts +104 -0
- package/dist/config.js +15 -0
- package/dist/context.js +541 -0
- package/dist/coverage.d.ts +118 -0
- package/dist/coverage.js +23 -0
- package/dist/dummy.js +2 -0
- package/dist/environments.d.ts +22 -0
- package/dist/environments.js +3 -0
- package/dist/expect-element.js +27 -0
- package/dist/index-D6m36C6U.js +6 -0
- package/dist/index-node.js +7 -0
- package/dist/index.d.ts +510 -0
- package/dist/index.js +19 -0
- package/dist/locators.d.ts +354 -0
- package/dist/locators.js +1 -0
- package/dist/mocker.d.ts +1 -0
- package/dist/mocker.js +1 -0
- package/dist/module-evaluator.d.ts +124 -0
- package/dist/module-evaluator.js +343 -0
- package/dist/module-runner-stub.js +44 -0
- package/dist/module-runner.js +17 -0
- package/dist/node.d.ts +251 -0
- package/dist/node.js +98 -0
- package/dist/path.js +7 -0
- package/dist/plugins/browser-client.mjs +2 -0
- package/dist/plugins/browser-context.mjs +2 -0
- package/dist/plugins/browser-locators.mjs +2 -0
- package/dist/plugins/browser-playwright.mjs +2 -0
- package/dist/plugins/browser-preview.mjs +2 -0
- package/dist/plugins/browser-webdriverio.mjs +2 -0
- package/dist/plugins/browser.mjs +2 -0
- package/dist/plugins/expect.mjs +2 -0
- package/dist/plugins/mocker-automock.mjs +2 -0
- package/dist/plugins/mocker-browser.mjs +2 -0
- package/dist/plugins/mocker-node.mjs +2 -0
- package/dist/plugins/mocker-redirect.mjs +2 -0
- package/dist/plugins/mocker-register.mjs +2 -0
- package/dist/plugins/mocker.mjs +2 -0
- package/dist/plugins/pretty-format.mjs +2 -0
- package/dist/plugins/runner-types.mjs +2 -0
- package/dist/plugins/runner-utils.mjs +2 -0
- package/dist/plugins/runner.mjs +2 -0
- package/dist/plugins/snapshot-environment.mjs +2 -0
- package/dist/plugins/snapshot-manager.mjs +2 -0
- package/dist/plugins/snapshot.mjs +2 -0
- package/dist/plugins/spy.mjs +2 -0
- package/dist/plugins/utils-constants.mjs +2 -0
- package/dist/plugins/utils-diff.mjs +2 -0
- package/dist/plugins/utils-display.mjs +2 -0
- package/dist/plugins/utils-error.mjs +2 -0
- package/dist/plugins/utils-helpers.mjs +2 -0
- package/dist/plugins/utils-highlight.mjs +2 -0
- package/dist/plugins/utils-offset.mjs +2 -0
- package/dist/plugins/utils-resolver.mjs +2 -0
- package/dist/plugins/utils-serialize.mjs +2 -0
- package/dist/plugins/utils-source-map.mjs +2 -0
- package/dist/plugins/utils-timers.mjs +2 -0
- package/dist/plugins/utils.mjs +2 -0
- package/dist/reporters.d.ts +27 -0
- package/dist/reporters.js +24 -0
- package/dist/runners.d.ts +50 -0
- package/dist/runners.js +19 -0
- package/dist/shared/screenshotMatcher/types.d.ts +22 -0
- package/dist/snapshot.d.ts +9 -0
- package/dist/snapshot.js +4 -0
- package/dist/spy.js +1 -0
- package/dist/state.js +280 -0
- package/dist/suite.d.ts +5 -0
- package/dist/suite.js +6 -0
- package/dist/types.d.ts +69 -0
- package/dist/vendor/chai.d.mts +1 -0
- package/dist/vendor/chai.mjs +3577 -0
- package/dist/vendor/es-module-lexer.d.mts +193 -0
- package/dist/vendor/es-module-lexer.mjs +79 -0
- package/dist/vendor/estree-walker.d.mts +583 -0
- package/dist/vendor/estree-walker.mjs +339 -0
- package/dist/vendor/expect-type.d.mts +1574 -0
- package/dist/vendor/expect-type.mjs +214 -0
- package/dist/vendor/magic-string.d.mts +261 -0
- package/dist/vendor/magic-string.mjs +1700 -0
- package/dist/vendor/obug.d.mts +56 -0
- package/dist/vendor/obug.mjs +276 -0
- package/dist/vendor/pathe.d.mts +46 -0
- package/dist/vendor/pathe.mjs +496 -0
- package/dist/vendor/picomatch.d.mts +1 -0
- package/dist/vendor/picomatch.mjs +1855 -0
- package/dist/vendor/shared-3g9mwCWP.mjs +31 -0
- package/dist/vendor/std-env.d.mts +88 -0
- package/dist/vendor/std-env.mjs +159 -0
- package/dist/vendor/tinybench.d.mts +317 -0
- package/dist/vendor/tinybench.mjs +504 -0
- package/dist/vendor/tinyexec.d.mts +72 -0
- package/dist/vendor/tinyexec.mjs +637 -0
- package/dist/vendor/tinyglobby.d.mts +157 -0
- package/dist/vendor/tinyglobby.mjs +832 -0
- package/dist/vendor/tinyrainbow.d.mts +60 -0
- package/dist/vendor/tinyrainbow.mjs +93 -0
- package/dist/vendor/vitest_browser.mjs +2 -0
- package/dist/vendor/vitest_internal_browser.mjs +2 -0
- package/dist/vendor/vitest_runner.mjs +2 -0
- package/dist/vendor/vitest_runners.mjs +2 -0
- package/dist/worker.d.ts +32 -0
- package/dist/worker.js +48 -0
- package/dist/workers/forks.js +54 -0
- package/dist/workers/runVmTests.js +95 -0
- package/dist/workers/threads.js +55 -0
- package/dist/workers/vmForks.js +36 -0
- package/dist/workers/vmThreads.js +37 -0
- package/environments.d.ts +1 -0
- package/globals.d.ts +20 -0
- package/import-meta.d.ts +5 -0
- package/importMeta.d.ts +4 -0
- package/index.cjs +5 -0
- package/index.d.cts +1 -0
- package/jsdom.d.ts +6 -0
- package/mocker.d.ts +1 -0
- package/node.d.ts +1 -0
- package/optional-types.d.ts +7 -0
- package/package.json +335 -0
- package/reporters.d.ts +1 -0
- package/runners.d.ts +1 -0
- package/snapshot.d.ts +1 -0
- package/suite.d.ts +1 -0
- package/suppress-warnings.cjs +21 -0
- package/vitest.mjs +2 -0
- package/worker.d.ts +1 -0
|
@@ -0,0 +1,3919 @@
|
|
|
1
|
+
import { chai, equals, iterableEquality, subsetEquality, JestExtend, JestChaiExpect, JestAsymmetricMatchers, GLOBAL_EXPECT, ASYMMETRIC_MATCHERS_OBJECT, getState, setState, addCustomEqualityTesters, customMatchers } from '../@vitest/expect/index.js';
|
|
2
|
+
import { getCurrentTest } from '../@vitest/runner/index.js';
|
|
3
|
+
import { g as getWorkerState, i as isChildProcess, w as waitForImportsToResolve, r as resetModules } from './utils.DvEY5TfP.js';
|
|
4
|
+
import { getSafeTimers, delay } from '../@vitest/utils/timers.js';
|
|
5
|
+
import { getNames } from '../@vitest/runner/utils.js';
|
|
6
|
+
import { stripSnapshotIndentation, addSerializer, SnapshotClient } from '../@vitest/snapshot/index.js';
|
|
7
|
+
import '../@vitest/utils/error.js';
|
|
8
|
+
import { assertTypes, createSimpleStackTrace } from '../@vitest/utils/helpers.js';
|
|
9
|
+
import { fn, spyOn, restoreAllMocks, resetAllMocks, clearAllMocks, isMockFunction } from '../@vitest/spy/index.js';
|
|
10
|
+
import '../@vitest/utils/offset.js';
|
|
11
|
+
import { parseSingleStack } from '../@vitest/utils/source-map.js';
|
|
12
|
+
import { c as commonjsGlobal } from './_commonjsHelpers.D26ty3Ew.js';
|
|
13
|
+
import { R as RealDate, r as resetDate, m as mockDate } from './date.Bq6ZW5rf.js';
|
|
14
|
+
|
|
15
|
+
// these matchers are not supported because they don't make sense with poll
|
|
16
|
+
const unsupported = [
|
|
17
|
+
"matchSnapshot",
|
|
18
|
+
"toMatchSnapshot",
|
|
19
|
+
"toMatchInlineSnapshot",
|
|
20
|
+
"toThrowErrorMatchingSnapshot",
|
|
21
|
+
"toThrowErrorMatchingInlineSnapshot",
|
|
22
|
+
"throws",
|
|
23
|
+
"Throw",
|
|
24
|
+
"throw",
|
|
25
|
+
"toThrow",
|
|
26
|
+
"toThrowError"
|
|
27
|
+
];
|
|
28
|
+
/**
|
|
29
|
+
* Attaches a `cause` property to the error if missing, copies the stack trace from the source, and throws.
|
|
30
|
+
*
|
|
31
|
+
* @param error - The error to throw
|
|
32
|
+
* @param source - Error to copy the stack trace from
|
|
33
|
+
*
|
|
34
|
+
* @throws Always throws the provided error with an amended stack trace
|
|
35
|
+
*/
|
|
36
|
+
function throwWithCause(error, source) {
|
|
37
|
+
if (error.cause == null) error.cause = /* @__PURE__ */ new Error("Matcher did not succeed in time.");
|
|
38
|
+
throw copyStackTrace$1(error, source);
|
|
39
|
+
}
|
|
40
|
+
function createExpectPoll(expect) {
|
|
41
|
+
return function poll(fn, options = {}) {
|
|
42
|
+
const defaults = getWorkerState().config.expect?.poll ?? {};
|
|
43
|
+
const { interval = defaults.interval ?? 50, timeout = defaults.timeout ?? 1e3, message } = options;
|
|
44
|
+
// @ts-expect-error private poll access
|
|
45
|
+
const assertion = expect(null, message).withContext({ poll: true });
|
|
46
|
+
fn = fn.bind(assertion);
|
|
47
|
+
const test = chai.util.flag(assertion, "vitest-test");
|
|
48
|
+
if (!test) throw new Error("expect.poll() must be called inside a test");
|
|
49
|
+
const proxy = new Proxy(assertion, { get(target, key, receiver) {
|
|
50
|
+
const assertionFunction = Reflect.get(target, key, receiver);
|
|
51
|
+
if (typeof assertionFunction !== "function") return assertionFunction instanceof chai.Assertion ? proxy : assertionFunction;
|
|
52
|
+
if (key === "assert") return assertionFunction;
|
|
53
|
+
if (typeof key === "string" && unsupported.includes(key)) throw new SyntaxError(`expect.poll() is not supported in combination with .${key}(). Use vi.waitFor() if your assertion condition is unstable.`);
|
|
54
|
+
return function(...args) {
|
|
55
|
+
const STACK_TRACE_ERROR = /* @__PURE__ */ new Error("STACK_TRACE_ERROR");
|
|
56
|
+
const promise = async () => {
|
|
57
|
+
const { setTimeout, clearTimeout } = getSafeTimers();
|
|
58
|
+
let executionPhase = "fn";
|
|
59
|
+
let hasTimedOut = false;
|
|
60
|
+
const timerId = setTimeout(() => {
|
|
61
|
+
hasTimedOut = true;
|
|
62
|
+
}, timeout);
|
|
63
|
+
chai.util.flag(assertion, "_name", key);
|
|
64
|
+
try {
|
|
65
|
+
while (true) {
|
|
66
|
+
const isLastAttempt = hasTimedOut;
|
|
67
|
+
if (isLastAttempt) chai.util.flag(assertion, "_isLastPollAttempt", true);
|
|
68
|
+
try {
|
|
69
|
+
executionPhase = "fn";
|
|
70
|
+
const obj = await fn();
|
|
71
|
+
chai.util.flag(assertion, "object", obj);
|
|
72
|
+
executionPhase = "assertion";
|
|
73
|
+
return await assertionFunction.call(assertion, ...args);
|
|
74
|
+
} catch (err) {
|
|
75
|
+
if (isLastAttempt || executionPhase === "assertion" && chai.util.flag(assertion, "_poll.assert_once")) throwWithCause(err, STACK_TRACE_ERROR);
|
|
76
|
+
await delay(interval, setTimeout);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
} finally {
|
|
80
|
+
clearTimeout(timerId);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
let awaited = false;
|
|
84
|
+
test.onFinished ??= [];
|
|
85
|
+
test.onFinished.push(() => {
|
|
86
|
+
if (!awaited) {
|
|
87
|
+
const negated = chai.util.flag(assertion, "negate") ? "not." : "";
|
|
88
|
+
const assertionString = `expect.${chai.util.flag(assertion, "_poll.element") ? "element(locator)" : "poll(assertion)"}.${negated}${String(key)}()`;
|
|
89
|
+
throw copyStackTrace$1(/* @__PURE__ */ new Error(`${assertionString} was not awaited. This assertion is asynchronous and must be awaited; otherwise, it is not executed to avoid unhandled rejections:\n\nawait ${assertionString}\n`), STACK_TRACE_ERROR);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
let resultPromise;
|
|
93
|
+
// only .then is enough to check awaited, but we type this as `Promise<void>` in global types
|
|
94
|
+
// so let's follow it
|
|
95
|
+
return {
|
|
96
|
+
then(onFulfilled, onRejected) {
|
|
97
|
+
awaited = true;
|
|
98
|
+
return (resultPromise ||= promise()).then(onFulfilled, onRejected);
|
|
99
|
+
},
|
|
100
|
+
catch(onRejected) {
|
|
101
|
+
return (resultPromise ||= promise()).catch(onRejected);
|
|
102
|
+
},
|
|
103
|
+
finally(onFinally) {
|
|
104
|
+
return (resultPromise ||= promise()).finally(onFinally);
|
|
105
|
+
},
|
|
106
|
+
[Symbol.toStringTag]: "Promise"
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
} });
|
|
110
|
+
return proxy;
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function copyStackTrace$1(target, source) {
|
|
114
|
+
if (source.stack !== void 0) target.stack = source.stack.replace(source.message, target.message);
|
|
115
|
+
return target;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function createAssertionMessage(util, assertion, hasArgs) {
|
|
119
|
+
const not = util.flag(assertion, "negate") ? "not." : "";
|
|
120
|
+
const name = `${util.flag(assertion, "_name")}(${"expected" })`;
|
|
121
|
+
const promiseName = util.flag(assertion, "promise");
|
|
122
|
+
return `expect(actual)${promiseName ? `.${promiseName}` : ""}.${not}${name}`;
|
|
123
|
+
}
|
|
124
|
+
function recordAsyncExpect(_test, promise, assertion, error) {
|
|
125
|
+
const test = _test;
|
|
126
|
+
// record promise for test, that resolves before test ends
|
|
127
|
+
if (test && promise instanceof Promise) {
|
|
128
|
+
// if promise is explicitly awaited, remove it from the list
|
|
129
|
+
promise = promise.finally(() => {
|
|
130
|
+
if (!test.promises) return;
|
|
131
|
+
const index = test.promises.indexOf(promise);
|
|
132
|
+
if (index !== -1) test.promises.splice(index, 1);
|
|
133
|
+
});
|
|
134
|
+
// record promise
|
|
135
|
+
if (!test.promises) test.promises = [];
|
|
136
|
+
test.promises.push(promise);
|
|
137
|
+
let resolved = false;
|
|
138
|
+
test.onFinished ??= [];
|
|
139
|
+
test.onFinished.push(() => {
|
|
140
|
+
if (!resolved) {
|
|
141
|
+
const stack = (globalThis.__vitest_worker__?.onFilterStackTrace || ((s) => s || ""))(error.stack);
|
|
142
|
+
console.warn([
|
|
143
|
+
`Promise returned by \`${assertion}\` was not awaited. `,
|
|
144
|
+
"Vitest currently auto-awaits hanging assertions at the end of the test, but this will cause the test to fail in Vitest 3. ",
|
|
145
|
+
"Please remember to await the assertion.\n",
|
|
146
|
+
stack
|
|
147
|
+
].join(""));
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
return {
|
|
151
|
+
then(onFulfilled, onRejected) {
|
|
152
|
+
resolved = true;
|
|
153
|
+
return promise.then(onFulfilled, onRejected);
|
|
154
|
+
},
|
|
155
|
+
catch(onRejected) {
|
|
156
|
+
return promise.catch(onRejected);
|
|
157
|
+
},
|
|
158
|
+
finally(onFinally) {
|
|
159
|
+
return promise.finally(onFinally);
|
|
160
|
+
},
|
|
161
|
+
[Symbol.toStringTag]: "Promise"
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
return promise;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
let _client;
|
|
168
|
+
function getSnapshotClient() {
|
|
169
|
+
if (!_client) _client = new SnapshotClient({ isEqual: (received, expected) => {
|
|
170
|
+
return equals(received, expected, [iterableEquality, subsetEquality]);
|
|
171
|
+
} });
|
|
172
|
+
return _client;
|
|
173
|
+
}
|
|
174
|
+
function getError(expected, promise) {
|
|
175
|
+
if (typeof expected !== "function") {
|
|
176
|
+
if (!promise) throw new Error(`expected must be a function, received ${typeof expected}`);
|
|
177
|
+
// when "promised", it receives thrown error
|
|
178
|
+
return expected;
|
|
179
|
+
}
|
|
180
|
+
try {
|
|
181
|
+
expected();
|
|
182
|
+
} catch (e) {
|
|
183
|
+
return e;
|
|
184
|
+
}
|
|
185
|
+
throw new Error("snapshot function didn't throw");
|
|
186
|
+
}
|
|
187
|
+
function getTestNames(test) {
|
|
188
|
+
return {
|
|
189
|
+
filepath: test.file.filepath,
|
|
190
|
+
name: getNames(test).slice(1).join(" > "),
|
|
191
|
+
testId: test.id
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
const SnapshotPlugin = (chai, utils) => {
|
|
195
|
+
function getTest(assertionName, obj) {
|
|
196
|
+
const test = utils.flag(obj, "vitest-test");
|
|
197
|
+
if (!test) throw new Error(`'${assertionName}' cannot be used without test context`);
|
|
198
|
+
return test;
|
|
199
|
+
}
|
|
200
|
+
for (const key of ["matchSnapshot", "toMatchSnapshot"]) utils.addMethod(chai.Assertion.prototype, key, function(properties, message) {
|
|
201
|
+
utils.flag(this, "_name", key);
|
|
202
|
+
if (utils.flag(this, "negate")) throw new Error(`${key} cannot be used with "not"`);
|
|
203
|
+
const expected = utils.flag(this, "object");
|
|
204
|
+
const test = getTest(key, this);
|
|
205
|
+
if (typeof properties === "string" && typeof message === "undefined") {
|
|
206
|
+
message = properties;
|
|
207
|
+
properties = void 0;
|
|
208
|
+
}
|
|
209
|
+
const errorMessage = utils.flag(this, "message");
|
|
210
|
+
getSnapshotClient().assert({
|
|
211
|
+
received: expected,
|
|
212
|
+
message,
|
|
213
|
+
isInline: false,
|
|
214
|
+
properties,
|
|
215
|
+
errorMessage,
|
|
216
|
+
...getTestNames(test)
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
utils.addMethod(chai.Assertion.prototype, "toMatchFileSnapshot", function(file, message) {
|
|
220
|
+
utils.flag(this, "_name", "toMatchFileSnapshot");
|
|
221
|
+
if (utils.flag(this, "negate")) throw new Error("toMatchFileSnapshot cannot be used with \"not\"");
|
|
222
|
+
const error = /* @__PURE__ */ new Error("resolves");
|
|
223
|
+
const expected = utils.flag(this, "object");
|
|
224
|
+
const test = getTest("toMatchFileSnapshot", this);
|
|
225
|
+
const errorMessage = utils.flag(this, "message");
|
|
226
|
+
return recordAsyncExpect(test, getSnapshotClient().assertRaw({
|
|
227
|
+
received: expected,
|
|
228
|
+
message,
|
|
229
|
+
isInline: false,
|
|
230
|
+
rawSnapshot: { file },
|
|
231
|
+
errorMessage,
|
|
232
|
+
...getTestNames(test)
|
|
233
|
+
}), createAssertionMessage(utils, this), error);
|
|
234
|
+
});
|
|
235
|
+
utils.addMethod(chai.Assertion.prototype, "toMatchInlineSnapshot", function __INLINE_SNAPSHOT__(properties, inlineSnapshot, message) {
|
|
236
|
+
utils.flag(this, "_name", "toMatchInlineSnapshot");
|
|
237
|
+
if (utils.flag(this, "negate")) throw new Error("toMatchInlineSnapshot cannot be used with \"not\"");
|
|
238
|
+
const test = getTest("toMatchInlineSnapshot", this);
|
|
239
|
+
if (test.each || test.suite?.each) throw new Error("InlineSnapshot cannot be used inside of test.each or describe.each");
|
|
240
|
+
const expected = utils.flag(this, "object");
|
|
241
|
+
const error = utils.flag(this, "error");
|
|
242
|
+
if (typeof properties === "string") {
|
|
243
|
+
message = inlineSnapshot;
|
|
244
|
+
inlineSnapshot = properties;
|
|
245
|
+
properties = void 0;
|
|
246
|
+
}
|
|
247
|
+
if (inlineSnapshot) inlineSnapshot = stripSnapshotIndentation(inlineSnapshot);
|
|
248
|
+
const errorMessage = utils.flag(this, "message");
|
|
249
|
+
getSnapshotClient().assert({
|
|
250
|
+
received: expected,
|
|
251
|
+
message,
|
|
252
|
+
isInline: true,
|
|
253
|
+
properties,
|
|
254
|
+
inlineSnapshot,
|
|
255
|
+
error,
|
|
256
|
+
errorMessage,
|
|
257
|
+
...getTestNames(test)
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
utils.addMethod(chai.Assertion.prototype, "toThrowErrorMatchingSnapshot", function(message) {
|
|
261
|
+
utils.flag(this, "_name", "toThrowErrorMatchingSnapshot");
|
|
262
|
+
if (utils.flag(this, "negate")) throw new Error("toThrowErrorMatchingSnapshot cannot be used with \"not\"");
|
|
263
|
+
const expected = utils.flag(this, "object");
|
|
264
|
+
const test = getTest("toThrowErrorMatchingSnapshot", this);
|
|
265
|
+
const promise = utils.flag(this, "promise");
|
|
266
|
+
const errorMessage = utils.flag(this, "message");
|
|
267
|
+
getSnapshotClient().assert({
|
|
268
|
+
received: getError(expected, promise),
|
|
269
|
+
message,
|
|
270
|
+
errorMessage,
|
|
271
|
+
...getTestNames(test)
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
utils.addMethod(chai.Assertion.prototype, "toThrowErrorMatchingInlineSnapshot", function __INLINE_SNAPSHOT__(inlineSnapshot, message) {
|
|
275
|
+
if (utils.flag(this, "negate")) throw new Error("toThrowErrorMatchingInlineSnapshot cannot be used with \"not\"");
|
|
276
|
+
const test = getTest("toThrowErrorMatchingInlineSnapshot", this);
|
|
277
|
+
if (test.each || test.suite?.each) throw new Error("InlineSnapshot cannot be used inside of test.each or describe.each");
|
|
278
|
+
const expected = utils.flag(this, "object");
|
|
279
|
+
const error = utils.flag(this, "error");
|
|
280
|
+
const promise = utils.flag(this, "promise");
|
|
281
|
+
const errorMessage = utils.flag(this, "message");
|
|
282
|
+
if (inlineSnapshot) inlineSnapshot = stripSnapshotIndentation(inlineSnapshot);
|
|
283
|
+
getSnapshotClient().assert({
|
|
284
|
+
received: getError(expected, promise),
|
|
285
|
+
message,
|
|
286
|
+
inlineSnapshot,
|
|
287
|
+
isInline: true,
|
|
288
|
+
error,
|
|
289
|
+
errorMessage,
|
|
290
|
+
...getTestNames(test)
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
utils.addMethod(chai.expect, "addSnapshotSerializer", addSerializer);
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
chai.use(JestExtend);
|
|
297
|
+
chai.use(JestChaiExpect);
|
|
298
|
+
chai.use(SnapshotPlugin);
|
|
299
|
+
chai.use(JestAsymmetricMatchers);
|
|
300
|
+
|
|
301
|
+
function createExpect(test) {
|
|
302
|
+
const expect = ((value, message) => {
|
|
303
|
+
const { assertionCalls } = getState(expect);
|
|
304
|
+
setState({ assertionCalls: assertionCalls + 1 }, expect);
|
|
305
|
+
const assert = chai.expect(value, message);
|
|
306
|
+
const _test = test || getCurrentTest();
|
|
307
|
+
if (_test)
|
|
308
|
+
// @ts-expect-error internal
|
|
309
|
+
return assert.withTest(_test);
|
|
310
|
+
else return assert;
|
|
311
|
+
});
|
|
312
|
+
Object.assign(expect, chai.expect);
|
|
313
|
+
Object.assign(expect, globalThis[ASYMMETRIC_MATCHERS_OBJECT]);
|
|
314
|
+
expect.getState = () => getState(expect);
|
|
315
|
+
expect.setState = (state) => setState(state, expect);
|
|
316
|
+
// @ts-expect-error global is not typed
|
|
317
|
+
const globalState = getState(globalThis[GLOBAL_EXPECT]) || {};
|
|
318
|
+
setState({
|
|
319
|
+
...globalState,
|
|
320
|
+
assertionCalls: 0,
|
|
321
|
+
isExpectingAssertions: false,
|
|
322
|
+
isExpectingAssertionsError: null,
|
|
323
|
+
expectedAssertionsNumber: null,
|
|
324
|
+
expectedAssertionsNumberErrorGen: null,
|
|
325
|
+
get testPath() {
|
|
326
|
+
return getWorkerState().filepath;
|
|
327
|
+
},
|
|
328
|
+
currentTestName: test ? test.fullTestName ?? "" : globalState.currentTestName
|
|
329
|
+
}, expect);
|
|
330
|
+
expect.assert = chai.assert;
|
|
331
|
+
// @ts-expect-error untyped
|
|
332
|
+
expect.extend = (matchers) => chai.expect.extend(expect, matchers);
|
|
333
|
+
expect.addEqualityTesters = (customTesters) => addCustomEqualityTesters(customTesters);
|
|
334
|
+
expect.soft = (...args) => {
|
|
335
|
+
// @ts-expect-error private soft access
|
|
336
|
+
return expect(...args).withContext({ soft: true });
|
|
337
|
+
};
|
|
338
|
+
expect.poll = createExpectPoll(expect);
|
|
339
|
+
expect.unreachable = (message) => {
|
|
340
|
+
chai.assert.fail(`expected${message ? ` "${message}" ` : " "}not to be reached`);
|
|
341
|
+
};
|
|
342
|
+
function assertions(expected) {
|
|
343
|
+
const errorGen = () => /* @__PURE__ */ new Error(`expected number of assertions to be ${expected}, but got ${expect.getState().assertionCalls}`);
|
|
344
|
+
if (Error.captureStackTrace) Error.captureStackTrace(errorGen(), assertions);
|
|
345
|
+
expect.setState({
|
|
346
|
+
expectedAssertionsNumber: expected,
|
|
347
|
+
expectedAssertionsNumberErrorGen: errorGen
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
function hasAssertions() {
|
|
351
|
+
const error = /* @__PURE__ */ new Error("expected any number of assertion, but got none");
|
|
352
|
+
if (Error.captureStackTrace) Error.captureStackTrace(error, hasAssertions);
|
|
353
|
+
expect.setState({
|
|
354
|
+
isExpectingAssertions: true,
|
|
355
|
+
isExpectingAssertionsError: error
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
chai.util.addMethod(expect, "assertions", assertions);
|
|
359
|
+
chai.util.addMethod(expect, "hasAssertions", hasAssertions);
|
|
360
|
+
expect.extend(customMatchers);
|
|
361
|
+
return expect;
|
|
362
|
+
}
|
|
363
|
+
const globalExpect = createExpect();
|
|
364
|
+
Object.defineProperty(globalThis, GLOBAL_EXPECT, {
|
|
365
|
+
value: globalExpect,
|
|
366
|
+
writable: true,
|
|
367
|
+
configurable: true
|
|
368
|
+
});
|
|
369
|
+
const assert = chai.assert;
|
|
370
|
+
const should = chai.should;
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Gives access to injected context provided from the main thread.
|
|
374
|
+
* This usually returns a value provided by `globalSetup` or an external library.
|
|
375
|
+
*/
|
|
376
|
+
function inject(key) {
|
|
377
|
+
return getWorkerState().providedContext[key];
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
var fakeTimersSrc = {};
|
|
381
|
+
|
|
382
|
+
var global;
|
|
383
|
+
var hasRequiredGlobal;
|
|
384
|
+
|
|
385
|
+
function requireGlobal () {
|
|
386
|
+
if (hasRequiredGlobal) return global;
|
|
387
|
+
hasRequiredGlobal = 1;
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* A reference to the global object
|
|
391
|
+
* @type {object} globalObject
|
|
392
|
+
*/
|
|
393
|
+
var globalObject;
|
|
394
|
+
|
|
395
|
+
/* istanbul ignore else */
|
|
396
|
+
if (typeof commonjsGlobal !== "undefined") {
|
|
397
|
+
// Node
|
|
398
|
+
globalObject = commonjsGlobal;
|
|
399
|
+
} else if (typeof window !== "undefined") {
|
|
400
|
+
// Browser
|
|
401
|
+
globalObject = window;
|
|
402
|
+
} else {
|
|
403
|
+
// WebWorker
|
|
404
|
+
globalObject = self;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
global = globalObject;
|
|
408
|
+
return global;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
var throwsOnProto_1;
|
|
412
|
+
var hasRequiredThrowsOnProto;
|
|
413
|
+
|
|
414
|
+
function requireThrowsOnProto () {
|
|
415
|
+
if (hasRequiredThrowsOnProto) return throwsOnProto_1;
|
|
416
|
+
hasRequiredThrowsOnProto = 1;
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Is true when the environment causes an error to be thrown for accessing the
|
|
420
|
+
* __proto__ property.
|
|
421
|
+
* This is necessary in order to support `node --disable-proto=throw`.
|
|
422
|
+
*
|
|
423
|
+
* See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto
|
|
424
|
+
* @type {boolean}
|
|
425
|
+
*/
|
|
426
|
+
let throwsOnProto;
|
|
427
|
+
try {
|
|
428
|
+
const object = {};
|
|
429
|
+
// eslint-disable-next-line no-proto, no-unused-expressions
|
|
430
|
+
object.__proto__;
|
|
431
|
+
throwsOnProto = false;
|
|
432
|
+
} catch (_) {
|
|
433
|
+
// This branch is covered when tests are run with `--disable-proto=throw`,
|
|
434
|
+
// however we can test both branches at the same time, so this is ignored
|
|
435
|
+
/* istanbul ignore next */
|
|
436
|
+
throwsOnProto = true;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
throwsOnProto_1 = throwsOnProto;
|
|
440
|
+
return throwsOnProto_1;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
var copyPrototypeMethods;
|
|
444
|
+
var hasRequiredCopyPrototypeMethods;
|
|
445
|
+
|
|
446
|
+
function requireCopyPrototypeMethods () {
|
|
447
|
+
if (hasRequiredCopyPrototypeMethods) return copyPrototypeMethods;
|
|
448
|
+
hasRequiredCopyPrototypeMethods = 1;
|
|
449
|
+
|
|
450
|
+
var call = Function.call;
|
|
451
|
+
var throwsOnProto = requireThrowsOnProto();
|
|
452
|
+
|
|
453
|
+
var disallowedProperties = [
|
|
454
|
+
// ignore size because it throws from Map
|
|
455
|
+
"size",
|
|
456
|
+
"caller",
|
|
457
|
+
"callee",
|
|
458
|
+
"arguments",
|
|
459
|
+
];
|
|
460
|
+
|
|
461
|
+
// This branch is covered when tests are run with `--disable-proto=throw`,
|
|
462
|
+
// however we can test both branches at the same time, so this is ignored
|
|
463
|
+
/* istanbul ignore next */
|
|
464
|
+
if (throwsOnProto) {
|
|
465
|
+
disallowedProperties.push("__proto__");
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
copyPrototypeMethods = function copyPrototypeMethods(prototype) {
|
|
469
|
+
// eslint-disable-next-line @sinonjs/no-prototype-methods/no-prototype-methods
|
|
470
|
+
return Object.getOwnPropertyNames(prototype).reduce(function (
|
|
471
|
+
result,
|
|
472
|
+
name
|
|
473
|
+
) {
|
|
474
|
+
if (disallowedProperties.includes(name)) {
|
|
475
|
+
return result;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
if (typeof prototype[name] !== "function") {
|
|
479
|
+
return result;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
result[name] = call.bind(prototype[name]);
|
|
483
|
+
|
|
484
|
+
return result;
|
|
485
|
+
},
|
|
486
|
+
Object.create(null));
|
|
487
|
+
};
|
|
488
|
+
return copyPrototypeMethods;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
var array;
|
|
492
|
+
var hasRequiredArray;
|
|
493
|
+
|
|
494
|
+
function requireArray () {
|
|
495
|
+
if (hasRequiredArray) return array;
|
|
496
|
+
hasRequiredArray = 1;
|
|
497
|
+
|
|
498
|
+
var copyPrototype = requireCopyPrototypeMethods();
|
|
499
|
+
|
|
500
|
+
array = copyPrototype(Array.prototype);
|
|
501
|
+
return array;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
var calledInOrder_1;
|
|
505
|
+
var hasRequiredCalledInOrder;
|
|
506
|
+
|
|
507
|
+
function requireCalledInOrder () {
|
|
508
|
+
if (hasRequiredCalledInOrder) return calledInOrder_1;
|
|
509
|
+
hasRequiredCalledInOrder = 1;
|
|
510
|
+
|
|
511
|
+
var every = requireArray().every;
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* @private
|
|
515
|
+
*/
|
|
516
|
+
function hasCallsLeft(callMap, spy) {
|
|
517
|
+
if (callMap[spy.id] === undefined) {
|
|
518
|
+
callMap[spy.id] = 0;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
return callMap[spy.id] < spy.callCount;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* @private
|
|
526
|
+
*/
|
|
527
|
+
function checkAdjacentCalls(callMap, spy, index, spies) {
|
|
528
|
+
var calledBeforeNext = true;
|
|
529
|
+
|
|
530
|
+
if (index !== spies.length - 1) {
|
|
531
|
+
calledBeforeNext = spy.calledBefore(spies[index + 1]);
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
if (hasCallsLeft(callMap, spy) && calledBeforeNext) {
|
|
535
|
+
callMap[spy.id] += 1;
|
|
536
|
+
return true;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
return false;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* A Sinon proxy object (fake, spy, stub)
|
|
544
|
+
* @typedef {object} SinonProxy
|
|
545
|
+
* @property {Function} calledBefore - A method that determines if this proxy was called before another one
|
|
546
|
+
* @property {string} id - Some id
|
|
547
|
+
* @property {number} callCount - Number of times this proxy has been called
|
|
548
|
+
*/
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* Returns true when the spies have been called in the order they were supplied in
|
|
552
|
+
* @param {SinonProxy[] | SinonProxy} spies An array of proxies, or several proxies as arguments
|
|
553
|
+
* @returns {boolean} true when spies are called in order, false otherwise
|
|
554
|
+
*/
|
|
555
|
+
function calledInOrder(spies) {
|
|
556
|
+
var callMap = {};
|
|
557
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
558
|
+
var _spies = arguments.length > 1 ? arguments : spies;
|
|
559
|
+
|
|
560
|
+
return every(_spies, checkAdjacentCalls.bind(null, callMap));
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
calledInOrder_1 = calledInOrder;
|
|
564
|
+
return calledInOrder_1;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
var className_1;
|
|
568
|
+
var hasRequiredClassName;
|
|
569
|
+
|
|
570
|
+
function requireClassName () {
|
|
571
|
+
if (hasRequiredClassName) return className_1;
|
|
572
|
+
hasRequiredClassName = 1;
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* Returns a display name for a value from a constructor
|
|
576
|
+
* @param {object} value A value to examine
|
|
577
|
+
* @returns {(string|null)} A string or null
|
|
578
|
+
*/
|
|
579
|
+
function className(value) {
|
|
580
|
+
const name = value.constructor && value.constructor.name;
|
|
581
|
+
return name || null;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
className_1 = className;
|
|
585
|
+
return className_1;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
var deprecated = {};
|
|
589
|
+
|
|
590
|
+
/* eslint-disable no-console */
|
|
591
|
+
|
|
592
|
+
var hasRequiredDeprecated;
|
|
593
|
+
|
|
594
|
+
function requireDeprecated () {
|
|
595
|
+
if (hasRequiredDeprecated) return deprecated;
|
|
596
|
+
hasRequiredDeprecated = 1;
|
|
597
|
+
(function (exports$1) {
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* Returns a function that will invoke the supplied function and print a
|
|
601
|
+
* deprecation warning to the console each time it is called.
|
|
602
|
+
* @param {Function} func
|
|
603
|
+
* @param {string} msg
|
|
604
|
+
* @returns {Function}
|
|
605
|
+
*/
|
|
606
|
+
exports$1.wrap = function (func, msg) {
|
|
607
|
+
var wrapped = function () {
|
|
608
|
+
exports$1.printWarning(msg);
|
|
609
|
+
return func.apply(this, arguments);
|
|
610
|
+
};
|
|
611
|
+
if (func.prototype) {
|
|
612
|
+
wrapped.prototype = func.prototype;
|
|
613
|
+
}
|
|
614
|
+
return wrapped;
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* Returns a string which can be supplied to `wrap()` to notify the user that a
|
|
619
|
+
* particular part of the sinon API has been deprecated.
|
|
620
|
+
* @param {string} packageName
|
|
621
|
+
* @param {string} funcName
|
|
622
|
+
* @returns {string}
|
|
623
|
+
*/
|
|
624
|
+
exports$1.defaultMsg = function (packageName, funcName) {
|
|
625
|
+
return `${packageName}.${funcName} is deprecated and will be removed from the public API in a future version of ${packageName}.`;
|
|
626
|
+
};
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* Prints a warning on the console, when it exists
|
|
630
|
+
* @param {string} msg
|
|
631
|
+
* @returns {undefined}
|
|
632
|
+
*/
|
|
633
|
+
exports$1.printWarning = function (msg) {
|
|
634
|
+
/* istanbul ignore next */
|
|
635
|
+
if (typeof process === "object" && process.emitWarning) {
|
|
636
|
+
// Emit Warnings in Node
|
|
637
|
+
process.emitWarning(msg);
|
|
638
|
+
} else if (console.info) {
|
|
639
|
+
console.info(msg);
|
|
640
|
+
} else {
|
|
641
|
+
console.log(msg);
|
|
642
|
+
}
|
|
643
|
+
};
|
|
644
|
+
} (deprecated));
|
|
645
|
+
return deprecated;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
var every;
|
|
649
|
+
var hasRequiredEvery;
|
|
650
|
+
|
|
651
|
+
function requireEvery () {
|
|
652
|
+
if (hasRequiredEvery) return every;
|
|
653
|
+
hasRequiredEvery = 1;
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* Returns true when fn returns true for all members of obj.
|
|
657
|
+
* This is an every implementation that works for all iterables
|
|
658
|
+
* @param {object} obj
|
|
659
|
+
* @param {Function} fn
|
|
660
|
+
* @returns {boolean}
|
|
661
|
+
*/
|
|
662
|
+
every = function every(obj, fn) {
|
|
663
|
+
var pass = true;
|
|
664
|
+
|
|
665
|
+
try {
|
|
666
|
+
// eslint-disable-next-line @sinonjs/no-prototype-methods/no-prototype-methods
|
|
667
|
+
obj.forEach(function () {
|
|
668
|
+
if (!fn.apply(this, arguments)) {
|
|
669
|
+
// Throwing an error is the only way to break `forEach`
|
|
670
|
+
throw new Error();
|
|
671
|
+
}
|
|
672
|
+
});
|
|
673
|
+
} catch (e) {
|
|
674
|
+
pass = false;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
return pass;
|
|
678
|
+
};
|
|
679
|
+
return every;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
var functionName;
|
|
683
|
+
var hasRequiredFunctionName;
|
|
684
|
+
|
|
685
|
+
function requireFunctionName () {
|
|
686
|
+
if (hasRequiredFunctionName) return functionName;
|
|
687
|
+
hasRequiredFunctionName = 1;
|
|
688
|
+
|
|
689
|
+
/**
|
|
690
|
+
* Returns a display name for a function
|
|
691
|
+
* @param {Function} func
|
|
692
|
+
* @returns {string}
|
|
693
|
+
*/
|
|
694
|
+
functionName = function functionName(func) {
|
|
695
|
+
if (!func) {
|
|
696
|
+
return "";
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
try {
|
|
700
|
+
return (
|
|
701
|
+
func.displayName ||
|
|
702
|
+
func.name ||
|
|
703
|
+
// Use function decomposition as a last resort to get function
|
|
704
|
+
// name. Does not rely on function decomposition to work - if it
|
|
705
|
+
// doesn't debugging will be slightly less informative
|
|
706
|
+
// (i.e. toString will say 'spy' rather than 'myFunc').
|
|
707
|
+
(String(func).match(/function ([^\s(]+)/) || [])[1]
|
|
708
|
+
);
|
|
709
|
+
} catch (e) {
|
|
710
|
+
// Stringify may fail and we might get an exception, as a last-last
|
|
711
|
+
// resort fall back to empty string.
|
|
712
|
+
return "";
|
|
713
|
+
}
|
|
714
|
+
};
|
|
715
|
+
return functionName;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
var orderByFirstCall_1;
|
|
719
|
+
var hasRequiredOrderByFirstCall;
|
|
720
|
+
|
|
721
|
+
function requireOrderByFirstCall () {
|
|
722
|
+
if (hasRequiredOrderByFirstCall) return orderByFirstCall_1;
|
|
723
|
+
hasRequiredOrderByFirstCall = 1;
|
|
724
|
+
|
|
725
|
+
var sort = requireArray().sort;
|
|
726
|
+
var slice = requireArray().slice;
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* @private
|
|
730
|
+
*/
|
|
731
|
+
function comparator(a, b) {
|
|
732
|
+
// uuid, won't ever be equal
|
|
733
|
+
var aCall = a.getCall(0);
|
|
734
|
+
var bCall = b.getCall(0);
|
|
735
|
+
var aId = (aCall && aCall.callId) || -1;
|
|
736
|
+
var bId = (bCall && bCall.callId) || -1;
|
|
737
|
+
|
|
738
|
+
return aId < bId ? -1 : 1;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* A Sinon proxy object (fake, spy, stub)
|
|
743
|
+
* @typedef {object} SinonProxy
|
|
744
|
+
* @property {Function} getCall - A method that can return the first call
|
|
745
|
+
*/
|
|
746
|
+
|
|
747
|
+
/**
|
|
748
|
+
* Sorts an array of SinonProxy instances (fake, spy, stub) by their first call
|
|
749
|
+
* @param {SinonProxy[] | SinonProxy} spies
|
|
750
|
+
* @returns {SinonProxy[]}
|
|
751
|
+
*/
|
|
752
|
+
function orderByFirstCall(spies) {
|
|
753
|
+
return sort(slice(spies), comparator);
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
orderByFirstCall_1 = orderByFirstCall;
|
|
757
|
+
return orderByFirstCall_1;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
var _function;
|
|
761
|
+
var hasRequired_function;
|
|
762
|
+
|
|
763
|
+
function require_function () {
|
|
764
|
+
if (hasRequired_function) return _function;
|
|
765
|
+
hasRequired_function = 1;
|
|
766
|
+
|
|
767
|
+
var copyPrototype = requireCopyPrototypeMethods();
|
|
768
|
+
|
|
769
|
+
_function = copyPrototype(Function.prototype);
|
|
770
|
+
return _function;
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
var map;
|
|
774
|
+
var hasRequiredMap;
|
|
775
|
+
|
|
776
|
+
function requireMap () {
|
|
777
|
+
if (hasRequiredMap) return map;
|
|
778
|
+
hasRequiredMap = 1;
|
|
779
|
+
|
|
780
|
+
var copyPrototype = requireCopyPrototypeMethods();
|
|
781
|
+
|
|
782
|
+
map = copyPrototype(Map.prototype);
|
|
783
|
+
return map;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
var object;
|
|
787
|
+
var hasRequiredObject;
|
|
788
|
+
|
|
789
|
+
function requireObject () {
|
|
790
|
+
if (hasRequiredObject) return object;
|
|
791
|
+
hasRequiredObject = 1;
|
|
792
|
+
|
|
793
|
+
var copyPrototype = requireCopyPrototypeMethods();
|
|
794
|
+
|
|
795
|
+
object = copyPrototype(Object.prototype);
|
|
796
|
+
return object;
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
var set;
|
|
800
|
+
var hasRequiredSet;
|
|
801
|
+
|
|
802
|
+
function requireSet () {
|
|
803
|
+
if (hasRequiredSet) return set;
|
|
804
|
+
hasRequiredSet = 1;
|
|
805
|
+
|
|
806
|
+
var copyPrototype = requireCopyPrototypeMethods();
|
|
807
|
+
|
|
808
|
+
set = copyPrototype(Set.prototype);
|
|
809
|
+
return set;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
var string;
|
|
813
|
+
var hasRequiredString;
|
|
814
|
+
|
|
815
|
+
function requireString () {
|
|
816
|
+
if (hasRequiredString) return string;
|
|
817
|
+
hasRequiredString = 1;
|
|
818
|
+
|
|
819
|
+
var copyPrototype = requireCopyPrototypeMethods();
|
|
820
|
+
|
|
821
|
+
string = copyPrototype(String.prototype);
|
|
822
|
+
return string;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
var prototypes;
|
|
826
|
+
var hasRequiredPrototypes;
|
|
827
|
+
|
|
828
|
+
function requirePrototypes () {
|
|
829
|
+
if (hasRequiredPrototypes) return prototypes;
|
|
830
|
+
hasRequiredPrototypes = 1;
|
|
831
|
+
|
|
832
|
+
prototypes = {
|
|
833
|
+
array: requireArray(),
|
|
834
|
+
function: require_function(),
|
|
835
|
+
map: requireMap(),
|
|
836
|
+
object: requireObject(),
|
|
837
|
+
set: requireSet(),
|
|
838
|
+
string: requireString(),
|
|
839
|
+
};
|
|
840
|
+
return prototypes;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
var typeDetect$1 = {exports: {}};
|
|
844
|
+
|
|
845
|
+
var typeDetect = typeDetect$1.exports;
|
|
846
|
+
|
|
847
|
+
var hasRequiredTypeDetect;
|
|
848
|
+
|
|
849
|
+
function requireTypeDetect () {
|
|
850
|
+
if (hasRequiredTypeDetect) return typeDetect$1.exports;
|
|
851
|
+
hasRequiredTypeDetect = 1;
|
|
852
|
+
(function (module, exports$1) {
|
|
853
|
+
(function (global, factory) {
|
|
854
|
+
module.exports = factory() ;
|
|
855
|
+
}(typeDetect, (function () {
|
|
856
|
+
/* !
|
|
857
|
+
* type-detect
|
|
858
|
+
* Copyright(c) 2013 jake luer <jake@alogicalparadox.com>
|
|
859
|
+
* MIT Licensed
|
|
860
|
+
*/
|
|
861
|
+
var promiseExists = typeof Promise === 'function';
|
|
862
|
+
|
|
863
|
+
/* eslint-disable no-undef */
|
|
864
|
+
var globalObject = typeof self === 'object' ? self : commonjsGlobal; // eslint-disable-line id-blacklist
|
|
865
|
+
|
|
866
|
+
var symbolExists = typeof Symbol !== 'undefined';
|
|
867
|
+
var mapExists = typeof Map !== 'undefined';
|
|
868
|
+
var setExists = typeof Set !== 'undefined';
|
|
869
|
+
var weakMapExists = typeof WeakMap !== 'undefined';
|
|
870
|
+
var weakSetExists = typeof WeakSet !== 'undefined';
|
|
871
|
+
var dataViewExists = typeof DataView !== 'undefined';
|
|
872
|
+
var symbolIteratorExists = symbolExists && typeof Symbol.iterator !== 'undefined';
|
|
873
|
+
var symbolToStringTagExists = symbolExists && typeof Symbol.toStringTag !== 'undefined';
|
|
874
|
+
var setEntriesExists = setExists && typeof Set.prototype.entries === 'function';
|
|
875
|
+
var mapEntriesExists = mapExists && typeof Map.prototype.entries === 'function';
|
|
876
|
+
var setIteratorPrototype = setEntriesExists && Object.getPrototypeOf(new Set().entries());
|
|
877
|
+
var mapIteratorPrototype = mapEntriesExists && Object.getPrototypeOf(new Map().entries());
|
|
878
|
+
var arrayIteratorExists = symbolIteratorExists && typeof Array.prototype[Symbol.iterator] === 'function';
|
|
879
|
+
var arrayIteratorPrototype = arrayIteratorExists && Object.getPrototypeOf([][Symbol.iterator]());
|
|
880
|
+
var stringIteratorExists = symbolIteratorExists && typeof String.prototype[Symbol.iterator] === 'function';
|
|
881
|
+
var stringIteratorPrototype = stringIteratorExists && Object.getPrototypeOf(''[Symbol.iterator]());
|
|
882
|
+
var toStringLeftSliceLength = 8;
|
|
883
|
+
var toStringRightSliceLength = -1;
|
|
884
|
+
/**
|
|
885
|
+
* ### typeOf (obj)
|
|
886
|
+
*
|
|
887
|
+
* Uses `Object.prototype.toString` to determine the type of an object,
|
|
888
|
+
* normalising behaviour across engine versions & well optimised.
|
|
889
|
+
*
|
|
890
|
+
* @param {Mixed} object
|
|
891
|
+
* @return {String} object type
|
|
892
|
+
* @api public
|
|
893
|
+
*/
|
|
894
|
+
function typeDetect(obj) {
|
|
895
|
+
/* ! Speed optimisation
|
|
896
|
+
* Pre:
|
|
897
|
+
* string literal x 3,039,035 ops/sec ±1.62% (78 runs sampled)
|
|
898
|
+
* boolean literal x 1,424,138 ops/sec ±4.54% (75 runs sampled)
|
|
899
|
+
* number literal x 1,653,153 ops/sec ±1.91% (82 runs sampled)
|
|
900
|
+
* undefined x 9,978,660 ops/sec ±1.92% (75 runs sampled)
|
|
901
|
+
* function x 2,556,769 ops/sec ±1.73% (77 runs sampled)
|
|
902
|
+
* Post:
|
|
903
|
+
* string literal x 38,564,796 ops/sec ±1.15% (79 runs sampled)
|
|
904
|
+
* boolean literal x 31,148,940 ops/sec ±1.10% (79 runs sampled)
|
|
905
|
+
* number literal x 32,679,330 ops/sec ±1.90% (78 runs sampled)
|
|
906
|
+
* undefined x 32,363,368 ops/sec ±1.07% (82 runs sampled)
|
|
907
|
+
* function x 31,296,870 ops/sec ±0.96% (83 runs sampled)
|
|
908
|
+
*/
|
|
909
|
+
var typeofObj = typeof obj;
|
|
910
|
+
if (typeofObj !== 'object') {
|
|
911
|
+
return typeofObj;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
/* ! Speed optimisation
|
|
915
|
+
* Pre:
|
|
916
|
+
* null x 28,645,765 ops/sec ±1.17% (82 runs sampled)
|
|
917
|
+
* Post:
|
|
918
|
+
* null x 36,428,962 ops/sec ±1.37% (84 runs sampled)
|
|
919
|
+
*/
|
|
920
|
+
if (obj === null) {
|
|
921
|
+
return 'null';
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
/* ! Spec Conformance
|
|
925
|
+
* Test: `Object.prototype.toString.call(window)``
|
|
926
|
+
* - Node === "[object global]"
|
|
927
|
+
* - Chrome === "[object global]"
|
|
928
|
+
* - Firefox === "[object Window]"
|
|
929
|
+
* - PhantomJS === "[object Window]"
|
|
930
|
+
* - Safari === "[object Window]"
|
|
931
|
+
* - IE 11 === "[object Window]"
|
|
932
|
+
* - IE Edge === "[object Window]"
|
|
933
|
+
* Test: `Object.prototype.toString.call(this)``
|
|
934
|
+
* - Chrome Worker === "[object global]"
|
|
935
|
+
* - Firefox Worker === "[object DedicatedWorkerGlobalScope]"
|
|
936
|
+
* - Safari Worker === "[object DedicatedWorkerGlobalScope]"
|
|
937
|
+
* - IE 11 Worker === "[object WorkerGlobalScope]"
|
|
938
|
+
* - IE Edge Worker === "[object WorkerGlobalScope]"
|
|
939
|
+
*/
|
|
940
|
+
if (obj === globalObject) {
|
|
941
|
+
return 'global';
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
/* ! Speed optimisation
|
|
945
|
+
* Pre:
|
|
946
|
+
* array literal x 2,888,352 ops/sec ±0.67% (82 runs sampled)
|
|
947
|
+
* Post:
|
|
948
|
+
* array literal x 22,479,650 ops/sec ±0.96% (81 runs sampled)
|
|
949
|
+
*/
|
|
950
|
+
if (
|
|
951
|
+
Array.isArray(obj) &&
|
|
952
|
+
(symbolToStringTagExists === false || !(Symbol.toStringTag in obj))
|
|
953
|
+
) {
|
|
954
|
+
return 'Array';
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
// Not caching existence of `window` and related properties due to potential
|
|
958
|
+
// for `window` to be unset before tests in quasi-browser environments.
|
|
959
|
+
if (typeof window === 'object' && window !== null) {
|
|
960
|
+
/* ! Spec Conformance
|
|
961
|
+
* (https://html.spec.whatwg.org/multipage/browsers.html#location)
|
|
962
|
+
* WhatWG HTML$7.7.3 - The `Location` interface
|
|
963
|
+
* Test: `Object.prototype.toString.call(window.location)``
|
|
964
|
+
* - IE <=11 === "[object Object]"
|
|
965
|
+
* - IE Edge <=13 === "[object Object]"
|
|
966
|
+
*/
|
|
967
|
+
if (typeof window.location === 'object' && obj === window.location) {
|
|
968
|
+
return 'Location';
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
/* ! Spec Conformance
|
|
972
|
+
* (https://html.spec.whatwg.org/#document)
|
|
973
|
+
* WhatWG HTML$3.1.1 - The `Document` object
|
|
974
|
+
* Note: Most browsers currently adher to the W3C DOM Level 2 spec
|
|
975
|
+
* (https://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-26809268)
|
|
976
|
+
* which suggests that browsers should use HTMLTableCellElement for
|
|
977
|
+
* both TD and TH elements. WhatWG separates these.
|
|
978
|
+
* WhatWG HTML states:
|
|
979
|
+
* > For historical reasons, Window objects must also have a
|
|
980
|
+
* > writable, configurable, non-enumerable property named
|
|
981
|
+
* > HTMLDocument whose value is the Document interface object.
|
|
982
|
+
* Test: `Object.prototype.toString.call(document)``
|
|
983
|
+
* - Chrome === "[object HTMLDocument]"
|
|
984
|
+
* - Firefox === "[object HTMLDocument]"
|
|
985
|
+
* - Safari === "[object HTMLDocument]"
|
|
986
|
+
* - IE <=10 === "[object Document]"
|
|
987
|
+
* - IE 11 === "[object HTMLDocument]"
|
|
988
|
+
* - IE Edge <=13 === "[object HTMLDocument]"
|
|
989
|
+
*/
|
|
990
|
+
if (typeof window.document === 'object' && obj === window.document) {
|
|
991
|
+
return 'Document';
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
if (typeof window.navigator === 'object') {
|
|
995
|
+
/* ! Spec Conformance
|
|
996
|
+
* (https://html.spec.whatwg.org/multipage/webappapis.html#mimetypearray)
|
|
997
|
+
* WhatWG HTML$8.6.1.5 - Plugins - Interface MimeTypeArray
|
|
998
|
+
* Test: `Object.prototype.toString.call(navigator.mimeTypes)``
|
|
999
|
+
* - IE <=10 === "[object MSMimeTypesCollection]"
|
|
1000
|
+
*/
|
|
1001
|
+
if (typeof window.navigator.mimeTypes === 'object' &&
|
|
1002
|
+
obj === window.navigator.mimeTypes) {
|
|
1003
|
+
return 'MimeTypeArray';
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
/* ! Spec Conformance
|
|
1007
|
+
* (https://html.spec.whatwg.org/multipage/webappapis.html#pluginarray)
|
|
1008
|
+
* WhatWG HTML$8.6.1.5 - Plugins - Interface PluginArray
|
|
1009
|
+
* Test: `Object.prototype.toString.call(navigator.plugins)``
|
|
1010
|
+
* - IE <=10 === "[object MSPluginsCollection]"
|
|
1011
|
+
*/
|
|
1012
|
+
if (typeof window.navigator.plugins === 'object' &&
|
|
1013
|
+
obj === window.navigator.plugins) {
|
|
1014
|
+
return 'PluginArray';
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
if ((typeof window.HTMLElement === 'function' ||
|
|
1019
|
+
typeof window.HTMLElement === 'object') &&
|
|
1020
|
+
obj instanceof window.HTMLElement) {
|
|
1021
|
+
/* ! Spec Conformance
|
|
1022
|
+
* (https://html.spec.whatwg.org/multipage/webappapis.html#pluginarray)
|
|
1023
|
+
* WhatWG HTML$4.4.4 - The `blockquote` element - Interface `HTMLQuoteElement`
|
|
1024
|
+
* Test: `Object.prototype.toString.call(document.createElement('blockquote'))``
|
|
1025
|
+
* - IE <=10 === "[object HTMLBlockElement]"
|
|
1026
|
+
*/
|
|
1027
|
+
if (obj.tagName === 'BLOCKQUOTE') {
|
|
1028
|
+
return 'HTMLQuoteElement';
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
/* ! Spec Conformance
|
|
1032
|
+
* (https://html.spec.whatwg.org/#htmltabledatacellelement)
|
|
1033
|
+
* WhatWG HTML$4.9.9 - The `td` element - Interface `HTMLTableDataCellElement`
|
|
1034
|
+
* Note: Most browsers currently adher to the W3C DOM Level 2 spec
|
|
1035
|
+
* (https://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-82915075)
|
|
1036
|
+
* which suggests that browsers should use HTMLTableCellElement for
|
|
1037
|
+
* both TD and TH elements. WhatWG separates these.
|
|
1038
|
+
* Test: Object.prototype.toString.call(document.createElement('td'))
|
|
1039
|
+
* - Chrome === "[object HTMLTableCellElement]"
|
|
1040
|
+
* - Firefox === "[object HTMLTableCellElement]"
|
|
1041
|
+
* - Safari === "[object HTMLTableCellElement]"
|
|
1042
|
+
*/
|
|
1043
|
+
if (obj.tagName === 'TD') {
|
|
1044
|
+
return 'HTMLTableDataCellElement';
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
/* ! Spec Conformance
|
|
1048
|
+
* (https://html.spec.whatwg.org/#htmltableheadercellelement)
|
|
1049
|
+
* WhatWG HTML$4.9.9 - The `td` element - Interface `HTMLTableHeaderCellElement`
|
|
1050
|
+
* Note: Most browsers currently adher to the W3C DOM Level 2 spec
|
|
1051
|
+
* (https://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-82915075)
|
|
1052
|
+
* which suggests that browsers should use HTMLTableCellElement for
|
|
1053
|
+
* both TD and TH elements. WhatWG separates these.
|
|
1054
|
+
* Test: Object.prototype.toString.call(document.createElement('th'))
|
|
1055
|
+
* - Chrome === "[object HTMLTableCellElement]"
|
|
1056
|
+
* - Firefox === "[object HTMLTableCellElement]"
|
|
1057
|
+
* - Safari === "[object HTMLTableCellElement]"
|
|
1058
|
+
*/
|
|
1059
|
+
if (obj.tagName === 'TH') {
|
|
1060
|
+
return 'HTMLTableHeaderCellElement';
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
/* ! Speed optimisation
|
|
1066
|
+
* Pre:
|
|
1067
|
+
* Float64Array x 625,644 ops/sec ±1.58% (80 runs sampled)
|
|
1068
|
+
* Float32Array x 1,279,852 ops/sec ±2.91% (77 runs sampled)
|
|
1069
|
+
* Uint32Array x 1,178,185 ops/sec ±1.95% (83 runs sampled)
|
|
1070
|
+
* Uint16Array x 1,008,380 ops/sec ±2.25% (80 runs sampled)
|
|
1071
|
+
* Uint8Array x 1,128,040 ops/sec ±2.11% (81 runs sampled)
|
|
1072
|
+
* Int32Array x 1,170,119 ops/sec ±2.88% (80 runs sampled)
|
|
1073
|
+
* Int16Array x 1,176,348 ops/sec ±5.79% (86 runs sampled)
|
|
1074
|
+
* Int8Array x 1,058,707 ops/sec ±4.94% (77 runs sampled)
|
|
1075
|
+
* Uint8ClampedArray x 1,110,633 ops/sec ±4.20% (80 runs sampled)
|
|
1076
|
+
* Post:
|
|
1077
|
+
* Float64Array x 7,105,671 ops/sec ±13.47% (64 runs sampled)
|
|
1078
|
+
* Float32Array x 5,887,912 ops/sec ±1.46% (82 runs sampled)
|
|
1079
|
+
* Uint32Array x 6,491,661 ops/sec ±1.76% (79 runs sampled)
|
|
1080
|
+
* Uint16Array x 6,559,795 ops/sec ±1.67% (82 runs sampled)
|
|
1081
|
+
* Uint8Array x 6,463,966 ops/sec ±1.43% (85 runs sampled)
|
|
1082
|
+
* Int32Array x 5,641,841 ops/sec ±3.49% (81 runs sampled)
|
|
1083
|
+
* Int16Array x 6,583,511 ops/sec ±1.98% (80 runs sampled)
|
|
1084
|
+
* Int8Array x 6,606,078 ops/sec ±1.74% (81 runs sampled)
|
|
1085
|
+
* Uint8ClampedArray x 6,602,224 ops/sec ±1.77% (83 runs sampled)
|
|
1086
|
+
*/
|
|
1087
|
+
var stringTag = (symbolToStringTagExists && obj[Symbol.toStringTag]);
|
|
1088
|
+
if (typeof stringTag === 'string') {
|
|
1089
|
+
return stringTag;
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
var objPrototype = Object.getPrototypeOf(obj);
|
|
1093
|
+
/* ! Speed optimisation
|
|
1094
|
+
* Pre:
|
|
1095
|
+
* regex literal x 1,772,385 ops/sec ±1.85% (77 runs sampled)
|
|
1096
|
+
* regex constructor x 2,143,634 ops/sec ±2.46% (78 runs sampled)
|
|
1097
|
+
* Post:
|
|
1098
|
+
* regex literal x 3,928,009 ops/sec ±0.65% (78 runs sampled)
|
|
1099
|
+
* regex constructor x 3,931,108 ops/sec ±0.58% (84 runs sampled)
|
|
1100
|
+
*/
|
|
1101
|
+
if (objPrototype === RegExp.prototype) {
|
|
1102
|
+
return 'RegExp';
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
/* ! Speed optimisation
|
|
1106
|
+
* Pre:
|
|
1107
|
+
* date x 2,130,074 ops/sec ±4.42% (68 runs sampled)
|
|
1108
|
+
* Post:
|
|
1109
|
+
* date x 3,953,779 ops/sec ±1.35% (77 runs sampled)
|
|
1110
|
+
*/
|
|
1111
|
+
if (objPrototype === Date.prototype) {
|
|
1112
|
+
return 'Date';
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
/* ! Spec Conformance
|
|
1116
|
+
* (http://www.ecma-international.org/ecma-262/6.0/index.html#sec-promise.prototype-@@tostringtag)
|
|
1117
|
+
* ES6$25.4.5.4 - Promise.prototype[@@toStringTag] should be "Promise":
|
|
1118
|
+
* Test: `Object.prototype.toString.call(Promise.resolve())``
|
|
1119
|
+
* - Chrome <=47 === "[object Object]"
|
|
1120
|
+
* - Edge <=20 === "[object Object]"
|
|
1121
|
+
* - Firefox 29-Latest === "[object Promise]"
|
|
1122
|
+
* - Safari 7.1-Latest === "[object Promise]"
|
|
1123
|
+
*/
|
|
1124
|
+
if (promiseExists && objPrototype === Promise.prototype) {
|
|
1125
|
+
return 'Promise';
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
/* ! Speed optimisation
|
|
1129
|
+
* Pre:
|
|
1130
|
+
* set x 2,222,186 ops/sec ±1.31% (82 runs sampled)
|
|
1131
|
+
* Post:
|
|
1132
|
+
* set x 4,545,879 ops/sec ±1.13% (83 runs sampled)
|
|
1133
|
+
*/
|
|
1134
|
+
if (setExists && objPrototype === Set.prototype) {
|
|
1135
|
+
return 'Set';
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
/* ! Speed optimisation
|
|
1139
|
+
* Pre:
|
|
1140
|
+
* map x 2,396,842 ops/sec ±1.59% (81 runs sampled)
|
|
1141
|
+
* Post:
|
|
1142
|
+
* map x 4,183,945 ops/sec ±6.59% (82 runs sampled)
|
|
1143
|
+
*/
|
|
1144
|
+
if (mapExists && objPrototype === Map.prototype) {
|
|
1145
|
+
return 'Map';
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
/* ! Speed optimisation
|
|
1149
|
+
* Pre:
|
|
1150
|
+
* weakset x 1,323,220 ops/sec ±2.17% (76 runs sampled)
|
|
1151
|
+
* Post:
|
|
1152
|
+
* weakset x 4,237,510 ops/sec ±2.01% (77 runs sampled)
|
|
1153
|
+
*/
|
|
1154
|
+
if (weakSetExists && objPrototype === WeakSet.prototype) {
|
|
1155
|
+
return 'WeakSet';
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
/* ! Speed optimisation
|
|
1159
|
+
* Pre:
|
|
1160
|
+
* weakmap x 1,500,260 ops/sec ±2.02% (78 runs sampled)
|
|
1161
|
+
* Post:
|
|
1162
|
+
* weakmap x 3,881,384 ops/sec ±1.45% (82 runs sampled)
|
|
1163
|
+
*/
|
|
1164
|
+
if (weakMapExists && objPrototype === WeakMap.prototype) {
|
|
1165
|
+
return 'WeakMap';
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
/* ! Spec Conformance
|
|
1169
|
+
* (http://www.ecma-international.org/ecma-262/6.0/index.html#sec-dataview.prototype-@@tostringtag)
|
|
1170
|
+
* ES6$24.2.4.21 - DataView.prototype[@@toStringTag] should be "DataView":
|
|
1171
|
+
* Test: `Object.prototype.toString.call(new DataView(new ArrayBuffer(1)))``
|
|
1172
|
+
* - Edge <=13 === "[object Object]"
|
|
1173
|
+
*/
|
|
1174
|
+
if (dataViewExists && objPrototype === DataView.prototype) {
|
|
1175
|
+
return 'DataView';
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
/* ! Spec Conformance
|
|
1179
|
+
* (http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%mapiteratorprototype%-@@tostringtag)
|
|
1180
|
+
* ES6$23.1.5.2.2 - %MapIteratorPrototype%[@@toStringTag] should be "Map Iterator":
|
|
1181
|
+
* Test: `Object.prototype.toString.call(new Map().entries())``
|
|
1182
|
+
* - Edge <=13 === "[object Object]"
|
|
1183
|
+
*/
|
|
1184
|
+
if (mapExists && objPrototype === mapIteratorPrototype) {
|
|
1185
|
+
return 'Map Iterator';
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
/* ! Spec Conformance
|
|
1189
|
+
* (http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%setiteratorprototype%-@@tostringtag)
|
|
1190
|
+
* ES6$23.2.5.2.2 - %SetIteratorPrototype%[@@toStringTag] should be "Set Iterator":
|
|
1191
|
+
* Test: `Object.prototype.toString.call(new Set().entries())``
|
|
1192
|
+
* - Edge <=13 === "[object Object]"
|
|
1193
|
+
*/
|
|
1194
|
+
if (setExists && objPrototype === setIteratorPrototype) {
|
|
1195
|
+
return 'Set Iterator';
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
/* ! Spec Conformance
|
|
1199
|
+
* (http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%arrayiteratorprototype%-@@tostringtag)
|
|
1200
|
+
* ES6$22.1.5.2.2 - %ArrayIteratorPrototype%[@@toStringTag] should be "Array Iterator":
|
|
1201
|
+
* Test: `Object.prototype.toString.call([][Symbol.iterator]())``
|
|
1202
|
+
* - Edge <=13 === "[object Object]"
|
|
1203
|
+
*/
|
|
1204
|
+
if (arrayIteratorExists && objPrototype === arrayIteratorPrototype) {
|
|
1205
|
+
return 'Array Iterator';
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
/* ! Spec Conformance
|
|
1209
|
+
* (http://www.ecma-international.org/ecma-262/6.0/index.html#sec-%stringiteratorprototype%-@@tostringtag)
|
|
1210
|
+
* ES6$21.1.5.2.2 - %StringIteratorPrototype%[@@toStringTag] should be "String Iterator":
|
|
1211
|
+
* Test: `Object.prototype.toString.call(''[Symbol.iterator]())``
|
|
1212
|
+
* - Edge <=13 === "[object Object]"
|
|
1213
|
+
*/
|
|
1214
|
+
if (stringIteratorExists && objPrototype === stringIteratorPrototype) {
|
|
1215
|
+
return 'String Iterator';
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
/* ! Speed optimisation
|
|
1219
|
+
* Pre:
|
|
1220
|
+
* object from null x 2,424,320 ops/sec ±1.67% (76 runs sampled)
|
|
1221
|
+
* Post:
|
|
1222
|
+
* object from null x 5,838,000 ops/sec ±0.99% (84 runs sampled)
|
|
1223
|
+
*/
|
|
1224
|
+
if (objPrototype === null) {
|
|
1225
|
+
return 'Object';
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
return Object
|
|
1229
|
+
.prototype
|
|
1230
|
+
.toString
|
|
1231
|
+
.call(obj)
|
|
1232
|
+
.slice(toStringLeftSliceLength, toStringRightSliceLength);
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
return typeDetect;
|
|
1236
|
+
|
|
1237
|
+
})));
|
|
1238
|
+
} (typeDetect$1));
|
|
1239
|
+
return typeDetect$1.exports;
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
var typeOf;
|
|
1243
|
+
var hasRequiredTypeOf;
|
|
1244
|
+
|
|
1245
|
+
function requireTypeOf () {
|
|
1246
|
+
if (hasRequiredTypeOf) return typeOf;
|
|
1247
|
+
hasRequiredTypeOf = 1;
|
|
1248
|
+
|
|
1249
|
+
var type = requireTypeDetect();
|
|
1250
|
+
|
|
1251
|
+
/**
|
|
1252
|
+
* Returns the lower-case result of running type from type-detect on the value
|
|
1253
|
+
* @param {*} value
|
|
1254
|
+
* @returns {string}
|
|
1255
|
+
*/
|
|
1256
|
+
typeOf = function typeOf(value) {
|
|
1257
|
+
return type(value).toLowerCase();
|
|
1258
|
+
};
|
|
1259
|
+
return typeOf;
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
var valueToString_1;
|
|
1263
|
+
var hasRequiredValueToString;
|
|
1264
|
+
|
|
1265
|
+
function requireValueToString () {
|
|
1266
|
+
if (hasRequiredValueToString) return valueToString_1;
|
|
1267
|
+
hasRequiredValueToString = 1;
|
|
1268
|
+
|
|
1269
|
+
/**
|
|
1270
|
+
* Returns a string representation of the value
|
|
1271
|
+
* @param {*} value
|
|
1272
|
+
* @returns {string}
|
|
1273
|
+
*/
|
|
1274
|
+
function valueToString(value) {
|
|
1275
|
+
if (value && value.toString) {
|
|
1276
|
+
// eslint-disable-next-line @sinonjs/no-prototype-methods/no-prototype-methods
|
|
1277
|
+
return value.toString();
|
|
1278
|
+
}
|
|
1279
|
+
return String(value);
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
valueToString_1 = valueToString;
|
|
1283
|
+
return valueToString_1;
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
var lib;
|
|
1287
|
+
var hasRequiredLib;
|
|
1288
|
+
|
|
1289
|
+
function requireLib () {
|
|
1290
|
+
if (hasRequiredLib) return lib;
|
|
1291
|
+
hasRequiredLib = 1;
|
|
1292
|
+
|
|
1293
|
+
lib = {
|
|
1294
|
+
global: requireGlobal(),
|
|
1295
|
+
calledInOrder: requireCalledInOrder(),
|
|
1296
|
+
className: requireClassName(),
|
|
1297
|
+
deprecated: requireDeprecated(),
|
|
1298
|
+
every: requireEvery(),
|
|
1299
|
+
functionName: requireFunctionName(),
|
|
1300
|
+
orderByFirstCall: requireOrderByFirstCall(),
|
|
1301
|
+
prototypes: requirePrototypes(),
|
|
1302
|
+
typeOf: requireTypeOf(),
|
|
1303
|
+
valueToString: requireValueToString(),
|
|
1304
|
+
};
|
|
1305
|
+
return lib;
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
var hasRequiredFakeTimersSrc;
|
|
1309
|
+
|
|
1310
|
+
function requireFakeTimersSrc () {
|
|
1311
|
+
if (hasRequiredFakeTimersSrc) return fakeTimersSrc;
|
|
1312
|
+
hasRequiredFakeTimersSrc = 1;
|
|
1313
|
+
|
|
1314
|
+
const globalObject = requireLib().global;
|
|
1315
|
+
let timersModule, timersPromisesModule;
|
|
1316
|
+
if (typeof __vitest_required__ !== 'undefined') {
|
|
1317
|
+
try {
|
|
1318
|
+
timersModule = __vitest_required__.timers;
|
|
1319
|
+
} catch (e) {
|
|
1320
|
+
// ignored
|
|
1321
|
+
}
|
|
1322
|
+
try {
|
|
1323
|
+
timersPromisesModule = __vitest_required__.timersPromises;
|
|
1324
|
+
} catch (e) {
|
|
1325
|
+
// ignored
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
/**
|
|
1330
|
+
* @typedef {object} IdleDeadline
|
|
1331
|
+
* @property {boolean} didTimeout - whether or not the callback was called before reaching the optional timeout
|
|
1332
|
+
* @property {function():number} timeRemaining - a floating-point value providing an estimate of the number of milliseconds remaining in the current idle period
|
|
1333
|
+
*/
|
|
1334
|
+
|
|
1335
|
+
/**
|
|
1336
|
+
* Queues a function to be called during a browser's idle periods
|
|
1337
|
+
* @callback RequestIdleCallback
|
|
1338
|
+
* @param {function(IdleDeadline)} callback
|
|
1339
|
+
* @param {{timeout: number}} options - an options object
|
|
1340
|
+
* @returns {number} the id
|
|
1341
|
+
*/
|
|
1342
|
+
|
|
1343
|
+
/**
|
|
1344
|
+
* @callback NextTick
|
|
1345
|
+
* @param {VoidVarArgsFunc} callback - the callback to run
|
|
1346
|
+
* @param {...*} args - optional arguments to call the callback with
|
|
1347
|
+
* @returns {void}
|
|
1348
|
+
*/
|
|
1349
|
+
|
|
1350
|
+
/**
|
|
1351
|
+
* @callback SetImmediate
|
|
1352
|
+
* @param {VoidVarArgsFunc} callback - the callback to run
|
|
1353
|
+
* @param {...*} args - optional arguments to call the callback with
|
|
1354
|
+
* @returns {NodeImmediate}
|
|
1355
|
+
*/
|
|
1356
|
+
|
|
1357
|
+
/**
|
|
1358
|
+
* @callback VoidVarArgsFunc
|
|
1359
|
+
* @param {...*} callback - the callback to run
|
|
1360
|
+
* @returns {void}
|
|
1361
|
+
*/
|
|
1362
|
+
|
|
1363
|
+
/**
|
|
1364
|
+
* @typedef RequestAnimationFrame
|
|
1365
|
+
* @property {function(number):void} requestAnimationFrame
|
|
1366
|
+
* @returns {number} - the id
|
|
1367
|
+
*/
|
|
1368
|
+
|
|
1369
|
+
/**
|
|
1370
|
+
* @typedef Performance
|
|
1371
|
+
* @property {function(): number} now
|
|
1372
|
+
*/
|
|
1373
|
+
|
|
1374
|
+
/* eslint-disable jsdoc/require-property-description */
|
|
1375
|
+
/**
|
|
1376
|
+
* @typedef {object} Clock
|
|
1377
|
+
* @property {number} now - the current time
|
|
1378
|
+
* @property {Date} Date - the Date constructor
|
|
1379
|
+
* @property {number} loopLimit - the maximum number of timers before assuming an infinite loop
|
|
1380
|
+
* @property {RequestIdleCallback} requestIdleCallback
|
|
1381
|
+
* @property {function(number):void} cancelIdleCallback
|
|
1382
|
+
* @property {setTimeout} setTimeout
|
|
1383
|
+
* @property {clearTimeout} clearTimeout
|
|
1384
|
+
* @property {NextTick} nextTick
|
|
1385
|
+
* @property {queueMicrotask} queueMicrotask
|
|
1386
|
+
* @property {setInterval} setInterval
|
|
1387
|
+
* @property {clearInterval} clearInterval
|
|
1388
|
+
* @property {SetImmediate} setImmediate
|
|
1389
|
+
* @property {function(NodeImmediate):void} clearImmediate
|
|
1390
|
+
* @property {function():number} countTimers
|
|
1391
|
+
* @property {RequestAnimationFrame} requestAnimationFrame
|
|
1392
|
+
* @property {function(number):void} cancelAnimationFrame
|
|
1393
|
+
* @property {function():void} runMicrotasks
|
|
1394
|
+
* @property {function(string | number): number} tick
|
|
1395
|
+
* @property {function(string | number): Promise<number>} tickAsync
|
|
1396
|
+
* @property {function(): number} next
|
|
1397
|
+
* @property {function(): Promise<number>} nextAsync
|
|
1398
|
+
* @property {function(): number} runAll
|
|
1399
|
+
* @property {function(): number} runToFrame
|
|
1400
|
+
* @property {function(): Promise<number>} runAllAsync
|
|
1401
|
+
* @property {function(): number} runToLast
|
|
1402
|
+
* @property {function(): Promise<number>} runToLastAsync
|
|
1403
|
+
* @property {function(): void} reset
|
|
1404
|
+
* @property {function(number | Date): void} setSystemTime
|
|
1405
|
+
* @property {function(number): void} jump
|
|
1406
|
+
* @property {Performance} performance
|
|
1407
|
+
* @property {function(number[]): number[]} hrtime - process.hrtime (legacy)
|
|
1408
|
+
* @property {function(): void} uninstall Uninstall the clock.
|
|
1409
|
+
* @property {Function[]} methods - the methods that are faked
|
|
1410
|
+
* @property {boolean} [shouldClearNativeTimers] inherited from config
|
|
1411
|
+
* @property {{methodName:string, original:any}[] | undefined} timersModuleMethods
|
|
1412
|
+
* @property {{methodName:string, original:any}[] | undefined} timersPromisesModuleMethods
|
|
1413
|
+
* @property {Map<function(): void, AbortSignal>} abortListenerMap
|
|
1414
|
+
*/
|
|
1415
|
+
/* eslint-enable jsdoc/require-property-description */
|
|
1416
|
+
|
|
1417
|
+
/**
|
|
1418
|
+
* Configuration object for the `install` method.
|
|
1419
|
+
* @typedef {object} Config
|
|
1420
|
+
* @property {number|Date} [now] a number (in milliseconds) or a Date object (default epoch)
|
|
1421
|
+
* @property {string[]} [toFake] names of the methods that should be faked.
|
|
1422
|
+
* @property {number} [loopLimit] the maximum number of timers that will be run when calling runAll()
|
|
1423
|
+
* @property {boolean} [shouldAdvanceTime] tells FakeTimers to increment mocked time automatically (default false)
|
|
1424
|
+
* @property {number} [advanceTimeDelta] increment mocked time every <<advanceTimeDelta>> ms (default: 20ms)
|
|
1425
|
+
* @property {boolean} [shouldClearNativeTimers] forwards clear timer calls to native functions if they are not fakes (default: false)
|
|
1426
|
+
* @property {boolean} [ignoreMissingTimers] default is false, meaning asking to fake timers that are not present will throw an error
|
|
1427
|
+
*/
|
|
1428
|
+
|
|
1429
|
+
/* eslint-disable jsdoc/require-property-description */
|
|
1430
|
+
/**
|
|
1431
|
+
* The internal structure to describe a scheduled fake timer
|
|
1432
|
+
* @typedef {object} Timer
|
|
1433
|
+
* @property {Function} func
|
|
1434
|
+
* @property {*[]} args
|
|
1435
|
+
* @property {number} delay
|
|
1436
|
+
* @property {number} callAt
|
|
1437
|
+
* @property {number} createdAt
|
|
1438
|
+
* @property {boolean} immediate
|
|
1439
|
+
* @property {number} id
|
|
1440
|
+
* @property {Error} [error]
|
|
1441
|
+
*/
|
|
1442
|
+
|
|
1443
|
+
/**
|
|
1444
|
+
* A Node timer
|
|
1445
|
+
* @typedef {object} NodeImmediate
|
|
1446
|
+
* @property {function(): boolean} hasRef
|
|
1447
|
+
* @property {function(): NodeImmediate} ref
|
|
1448
|
+
* @property {function(): NodeImmediate} unref
|
|
1449
|
+
*/
|
|
1450
|
+
/* eslint-enable jsdoc/require-property-description */
|
|
1451
|
+
|
|
1452
|
+
/* eslint-disable complexity */
|
|
1453
|
+
|
|
1454
|
+
/**
|
|
1455
|
+
* Mocks available features in the specified global namespace.
|
|
1456
|
+
* @param {*} _global Namespace to mock (e.g. `window`)
|
|
1457
|
+
* @returns {FakeTimers}
|
|
1458
|
+
*/
|
|
1459
|
+
function withGlobal(_global) {
|
|
1460
|
+
const maxTimeout = Math.pow(2, 31) - 1; //see https://heycam.github.io/webidl/#abstract-opdef-converttoint
|
|
1461
|
+
const idCounterStart = 1e12; // arbitrarily large number to avoid collisions with native timer IDs
|
|
1462
|
+
const NOOP = function () {
|
|
1463
|
+
return undefined;
|
|
1464
|
+
};
|
|
1465
|
+
const NOOP_ARRAY = function () {
|
|
1466
|
+
return [];
|
|
1467
|
+
};
|
|
1468
|
+
const isPresent = {};
|
|
1469
|
+
let timeoutResult,
|
|
1470
|
+
addTimerReturnsObject = false;
|
|
1471
|
+
|
|
1472
|
+
if (_global.setTimeout) {
|
|
1473
|
+
isPresent.setTimeout = true;
|
|
1474
|
+
timeoutResult = _global.setTimeout(NOOP, 0);
|
|
1475
|
+
addTimerReturnsObject = typeof timeoutResult === "object";
|
|
1476
|
+
}
|
|
1477
|
+
isPresent.clearTimeout = Boolean(_global.clearTimeout);
|
|
1478
|
+
isPresent.setInterval = Boolean(_global.setInterval);
|
|
1479
|
+
isPresent.clearInterval = Boolean(_global.clearInterval);
|
|
1480
|
+
isPresent.hrtime =
|
|
1481
|
+
_global.process && typeof _global.process.hrtime === "function";
|
|
1482
|
+
isPresent.hrtimeBigint =
|
|
1483
|
+
isPresent.hrtime && typeof _global.process.hrtime.bigint === "function";
|
|
1484
|
+
isPresent.nextTick =
|
|
1485
|
+
_global.process && typeof _global.process.nextTick === "function";
|
|
1486
|
+
const utilPromisify = _global.process && _global.__vitest_required__ && _global.__vitest_required__.util.promisify;
|
|
1487
|
+
isPresent.performance =
|
|
1488
|
+
_global.performance && typeof _global.performance.now === "function";
|
|
1489
|
+
const hasPerformancePrototype =
|
|
1490
|
+
_global.Performance &&
|
|
1491
|
+
(typeof _global.Performance).match(/^(function|object)$/);
|
|
1492
|
+
const hasPerformanceConstructorPrototype =
|
|
1493
|
+
_global.performance &&
|
|
1494
|
+
_global.performance.constructor &&
|
|
1495
|
+
_global.performance.constructor.prototype;
|
|
1496
|
+
isPresent.queueMicrotask = _global.hasOwnProperty("queueMicrotask");
|
|
1497
|
+
isPresent.requestAnimationFrame =
|
|
1498
|
+
_global.requestAnimationFrame &&
|
|
1499
|
+
typeof _global.requestAnimationFrame === "function";
|
|
1500
|
+
isPresent.cancelAnimationFrame =
|
|
1501
|
+
_global.cancelAnimationFrame &&
|
|
1502
|
+
typeof _global.cancelAnimationFrame === "function";
|
|
1503
|
+
isPresent.requestIdleCallback =
|
|
1504
|
+
_global.requestIdleCallback &&
|
|
1505
|
+
typeof _global.requestIdleCallback === "function";
|
|
1506
|
+
isPresent.cancelIdleCallbackPresent =
|
|
1507
|
+
_global.cancelIdleCallback &&
|
|
1508
|
+
typeof _global.cancelIdleCallback === "function";
|
|
1509
|
+
isPresent.setImmediate =
|
|
1510
|
+
_global.setImmediate && typeof _global.setImmediate === "function";
|
|
1511
|
+
isPresent.clearImmediate =
|
|
1512
|
+
_global.clearImmediate && typeof _global.clearImmediate === "function";
|
|
1513
|
+
isPresent.Intl = _global.Intl && typeof _global.Intl === "object";
|
|
1514
|
+
|
|
1515
|
+
if (_global.clearTimeout) {
|
|
1516
|
+
_global.clearTimeout(timeoutResult);
|
|
1517
|
+
}
|
|
1518
|
+
|
|
1519
|
+
const NativeDate = _global.Date;
|
|
1520
|
+
const NativeIntl = isPresent.Intl
|
|
1521
|
+
? Object.defineProperties(
|
|
1522
|
+
Object.create(null),
|
|
1523
|
+
Object.getOwnPropertyDescriptors(_global.Intl),
|
|
1524
|
+
)
|
|
1525
|
+
: undefined;
|
|
1526
|
+
let uniqueTimerId = idCounterStart;
|
|
1527
|
+
|
|
1528
|
+
if (NativeDate === undefined) {
|
|
1529
|
+
throw new Error(
|
|
1530
|
+
"The global scope doesn't have a `Date` object" +
|
|
1531
|
+
" (see https://github.com/sinonjs/sinon/issues/1852#issuecomment-419622780)",
|
|
1532
|
+
);
|
|
1533
|
+
}
|
|
1534
|
+
isPresent.Date = true;
|
|
1535
|
+
|
|
1536
|
+
/**
|
|
1537
|
+
* The PerformanceEntry object encapsulates a single performance metric
|
|
1538
|
+
* that is part of the browser's performance timeline.
|
|
1539
|
+
*
|
|
1540
|
+
* This is an object returned by the `mark` and `measure` methods on the Performance prototype
|
|
1541
|
+
*/
|
|
1542
|
+
class FakePerformanceEntry {
|
|
1543
|
+
constructor(name, entryType, startTime, duration) {
|
|
1544
|
+
this.name = name;
|
|
1545
|
+
this.entryType = entryType;
|
|
1546
|
+
this.startTime = startTime;
|
|
1547
|
+
this.duration = duration;
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1550
|
+
toJSON() {
|
|
1551
|
+
return JSON.stringify({ ...this });
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
|
|
1555
|
+
/**
|
|
1556
|
+
* @param {number} num
|
|
1557
|
+
* @returns {boolean}
|
|
1558
|
+
*/
|
|
1559
|
+
function isNumberFinite(num) {
|
|
1560
|
+
if (Number.isFinite) {
|
|
1561
|
+
return Number.isFinite(num);
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1564
|
+
return isFinite(num);
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
let isNearInfiniteLimit = false;
|
|
1568
|
+
|
|
1569
|
+
/**
|
|
1570
|
+
* @param {Clock} clock
|
|
1571
|
+
* @param {number} i
|
|
1572
|
+
*/
|
|
1573
|
+
function checkIsNearInfiniteLimit(clock, i) {
|
|
1574
|
+
if (clock.loopLimit && i === clock.loopLimit - 1) {
|
|
1575
|
+
isNearInfiniteLimit = true;
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
|
|
1579
|
+
/**
|
|
1580
|
+
*
|
|
1581
|
+
*/
|
|
1582
|
+
function resetIsNearInfiniteLimit() {
|
|
1583
|
+
isNearInfiniteLimit = false;
|
|
1584
|
+
}
|
|
1585
|
+
|
|
1586
|
+
/**
|
|
1587
|
+
* Parse strings like "01:10:00" (meaning 1 hour, 10 minutes, 0 seconds) into
|
|
1588
|
+
* number of milliseconds. This is used to support human-readable strings passed
|
|
1589
|
+
* to clock.tick()
|
|
1590
|
+
* @param {string} str
|
|
1591
|
+
* @returns {number}
|
|
1592
|
+
*/
|
|
1593
|
+
function parseTime(str) {
|
|
1594
|
+
if (!str) {
|
|
1595
|
+
return 0;
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1598
|
+
const strings = str.split(":");
|
|
1599
|
+
const l = strings.length;
|
|
1600
|
+
let i = l;
|
|
1601
|
+
let ms = 0;
|
|
1602
|
+
let parsed;
|
|
1603
|
+
|
|
1604
|
+
if (l > 3 || !/^(\d\d:){0,2}\d\d?$/.test(str)) {
|
|
1605
|
+
throw new Error(
|
|
1606
|
+
"tick only understands numbers, 'm:s' and 'h:m:s'. Each part must be two digits",
|
|
1607
|
+
);
|
|
1608
|
+
}
|
|
1609
|
+
|
|
1610
|
+
while (i--) {
|
|
1611
|
+
parsed = parseInt(strings[i], 10);
|
|
1612
|
+
|
|
1613
|
+
if (parsed >= 60) {
|
|
1614
|
+
throw new Error(`Invalid time ${str}`);
|
|
1615
|
+
}
|
|
1616
|
+
|
|
1617
|
+
ms += parsed * Math.pow(60, l - i - 1);
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
return ms * 1000;
|
|
1621
|
+
}
|
|
1622
|
+
|
|
1623
|
+
/**
|
|
1624
|
+
* Get the decimal part of the millisecond value as nanoseconds
|
|
1625
|
+
* @param {number} msFloat the number of milliseconds
|
|
1626
|
+
* @returns {number} an integer number of nanoseconds in the range [0,1e6)
|
|
1627
|
+
*
|
|
1628
|
+
* Example: nanoRemainer(123.456789) -> 456789
|
|
1629
|
+
*/
|
|
1630
|
+
function nanoRemainder(msFloat) {
|
|
1631
|
+
const modulo = 1e6;
|
|
1632
|
+
const remainder = (msFloat * 1e6) % modulo;
|
|
1633
|
+
const positiveRemainder =
|
|
1634
|
+
remainder < 0 ? remainder + modulo : remainder;
|
|
1635
|
+
|
|
1636
|
+
return Math.floor(positiveRemainder);
|
|
1637
|
+
}
|
|
1638
|
+
|
|
1639
|
+
/**
|
|
1640
|
+
* Used to grok the `now` parameter to createClock.
|
|
1641
|
+
* @param {Date|number} epoch the system time
|
|
1642
|
+
* @returns {number}
|
|
1643
|
+
*/
|
|
1644
|
+
function getEpoch(epoch) {
|
|
1645
|
+
if (!epoch) {
|
|
1646
|
+
return 0;
|
|
1647
|
+
}
|
|
1648
|
+
if (typeof epoch.getTime === "function") {
|
|
1649
|
+
return epoch.getTime();
|
|
1650
|
+
}
|
|
1651
|
+
if (typeof epoch === "number") {
|
|
1652
|
+
return epoch;
|
|
1653
|
+
}
|
|
1654
|
+
throw new TypeError("now should be milliseconds since UNIX epoch");
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
/**
|
|
1658
|
+
* @param {number} from
|
|
1659
|
+
* @param {number} to
|
|
1660
|
+
* @param {Timer} timer
|
|
1661
|
+
* @returns {boolean}
|
|
1662
|
+
*/
|
|
1663
|
+
function inRange(from, to, timer) {
|
|
1664
|
+
return timer && timer.callAt >= from && timer.callAt <= to;
|
|
1665
|
+
}
|
|
1666
|
+
|
|
1667
|
+
/**
|
|
1668
|
+
* @param {Clock} clock
|
|
1669
|
+
* @param {Timer} job
|
|
1670
|
+
*/
|
|
1671
|
+
function getInfiniteLoopError(clock, job) {
|
|
1672
|
+
const infiniteLoopError = new Error(
|
|
1673
|
+
`Aborting after running ${clock.loopLimit} timers, assuming an infinite loop!`,
|
|
1674
|
+
);
|
|
1675
|
+
|
|
1676
|
+
if (!job.error) {
|
|
1677
|
+
return infiniteLoopError;
|
|
1678
|
+
}
|
|
1679
|
+
|
|
1680
|
+
// pattern never matched in Node
|
|
1681
|
+
const computedTargetPattern = /target\.*[<|(|[].*?[>|\]|)]\s*/;
|
|
1682
|
+
let clockMethodPattern = new RegExp(
|
|
1683
|
+
String(Object.keys(clock).join("|")),
|
|
1684
|
+
);
|
|
1685
|
+
|
|
1686
|
+
if (addTimerReturnsObject) {
|
|
1687
|
+
// node.js environment
|
|
1688
|
+
clockMethodPattern = new RegExp(
|
|
1689
|
+
`\\s+at (Object\\.)?(?:${Object.keys(clock).join("|")})\\s+`,
|
|
1690
|
+
);
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
let matchedLineIndex = -1;
|
|
1694
|
+
job.error.stack.split("\n").some(function (line, i) {
|
|
1695
|
+
// If we've matched a computed target line (e.g. setTimeout) then we
|
|
1696
|
+
// don't need to look any further. Return true to stop iterating.
|
|
1697
|
+
const matchedComputedTarget = line.match(computedTargetPattern);
|
|
1698
|
+
/* istanbul ignore if */
|
|
1699
|
+
if (matchedComputedTarget) {
|
|
1700
|
+
matchedLineIndex = i;
|
|
1701
|
+
return true;
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
// If we've matched a clock method line, then there may still be
|
|
1705
|
+
// others further down the trace. Return false to keep iterating.
|
|
1706
|
+
const matchedClockMethod = line.match(clockMethodPattern);
|
|
1707
|
+
if (matchedClockMethod) {
|
|
1708
|
+
matchedLineIndex = i;
|
|
1709
|
+
return false;
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1712
|
+
// If we haven't matched anything on this line, but we matched
|
|
1713
|
+
// previously and set the matched line index, then we can stop.
|
|
1714
|
+
// If we haven't matched previously, then we should keep iterating.
|
|
1715
|
+
return matchedLineIndex >= 0;
|
|
1716
|
+
});
|
|
1717
|
+
|
|
1718
|
+
const stack = `${infiniteLoopError}\n${job.type || "Microtask"} - ${
|
|
1719
|
+
job.func.name || "anonymous"
|
|
1720
|
+
}\n${job.error.stack
|
|
1721
|
+
.split("\n")
|
|
1722
|
+
.slice(matchedLineIndex + 1)
|
|
1723
|
+
.join("\n")}`;
|
|
1724
|
+
|
|
1725
|
+
try {
|
|
1726
|
+
Object.defineProperty(infiniteLoopError, "stack", {
|
|
1727
|
+
value: stack,
|
|
1728
|
+
});
|
|
1729
|
+
} catch (e) {
|
|
1730
|
+
// noop
|
|
1731
|
+
}
|
|
1732
|
+
|
|
1733
|
+
return infiniteLoopError;
|
|
1734
|
+
}
|
|
1735
|
+
|
|
1736
|
+
//eslint-disable-next-line jsdoc/require-jsdoc
|
|
1737
|
+
function createDate() {
|
|
1738
|
+
class ClockDate extends NativeDate {
|
|
1739
|
+
/**
|
|
1740
|
+
* @param {number} year
|
|
1741
|
+
* @param {number} month
|
|
1742
|
+
* @param {number} date
|
|
1743
|
+
* @param {number} hour
|
|
1744
|
+
* @param {number} minute
|
|
1745
|
+
* @param {number} second
|
|
1746
|
+
* @param {number} ms
|
|
1747
|
+
* @returns void
|
|
1748
|
+
*/
|
|
1749
|
+
// eslint-disable-next-line no-unused-vars
|
|
1750
|
+
constructor(year, month, date, hour, minute, second, ms) {
|
|
1751
|
+
// Defensive and verbose to avoid potential harm in passing
|
|
1752
|
+
// explicit undefined when user does not pass argument
|
|
1753
|
+
if (arguments.length === 0) {
|
|
1754
|
+
super(ClockDate.clock.now);
|
|
1755
|
+
} else {
|
|
1756
|
+
super(...arguments);
|
|
1757
|
+
}
|
|
1758
|
+
|
|
1759
|
+
// ensures identity checks using the constructor prop still works
|
|
1760
|
+
// this should have no other functional effect
|
|
1761
|
+
Object.defineProperty(this, "constructor", {
|
|
1762
|
+
value: NativeDate,
|
|
1763
|
+
enumerable: false,
|
|
1764
|
+
});
|
|
1765
|
+
}
|
|
1766
|
+
|
|
1767
|
+
static [Symbol.hasInstance](instance) {
|
|
1768
|
+
return instance instanceof NativeDate;
|
|
1769
|
+
}
|
|
1770
|
+
}
|
|
1771
|
+
|
|
1772
|
+
ClockDate.isFake = true;
|
|
1773
|
+
|
|
1774
|
+
if (NativeDate.now) {
|
|
1775
|
+
ClockDate.now = function now() {
|
|
1776
|
+
return ClockDate.clock.now;
|
|
1777
|
+
};
|
|
1778
|
+
}
|
|
1779
|
+
|
|
1780
|
+
if (NativeDate.toSource) {
|
|
1781
|
+
ClockDate.toSource = function toSource() {
|
|
1782
|
+
return NativeDate.toSource();
|
|
1783
|
+
};
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
ClockDate.toString = function toString() {
|
|
1787
|
+
return NativeDate.toString();
|
|
1788
|
+
};
|
|
1789
|
+
|
|
1790
|
+
// noinspection UnnecessaryLocalVariableJS
|
|
1791
|
+
/**
|
|
1792
|
+
* A normal Class constructor cannot be called without `new`, but Date can, so we need
|
|
1793
|
+
* to wrap it in a Proxy in order to ensure this functionality of Date is kept intact
|
|
1794
|
+
* @type {ClockDate}
|
|
1795
|
+
*/
|
|
1796
|
+
const ClockDateProxy = new Proxy(ClockDate, {
|
|
1797
|
+
// handler for [[Call]] invocations (i.e. not using `new`)
|
|
1798
|
+
apply() {
|
|
1799
|
+
// the Date constructor called as a function, ref Ecma-262 Edition 5.1, section 15.9.2.
|
|
1800
|
+
// This remains so in the 10th edition of 2019 as well.
|
|
1801
|
+
if (this instanceof ClockDate) {
|
|
1802
|
+
throw new TypeError(
|
|
1803
|
+
"A Proxy should only capture `new` calls with the `construct` handler. This is not supposed to be possible, so check the logic.",
|
|
1804
|
+
);
|
|
1805
|
+
}
|
|
1806
|
+
|
|
1807
|
+
return new NativeDate(ClockDate.clock.now).toString();
|
|
1808
|
+
},
|
|
1809
|
+
});
|
|
1810
|
+
|
|
1811
|
+
return ClockDateProxy;
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1814
|
+
/**
|
|
1815
|
+
* Mirror Intl by default on our fake implementation
|
|
1816
|
+
*
|
|
1817
|
+
* Most of the properties are the original native ones,
|
|
1818
|
+
* but we need to take control of those that have a
|
|
1819
|
+
* dependency on the current clock.
|
|
1820
|
+
* @returns {object} the partly fake Intl implementation
|
|
1821
|
+
*/
|
|
1822
|
+
function createIntl() {
|
|
1823
|
+
const ClockIntl = {};
|
|
1824
|
+
/*
|
|
1825
|
+
* All properties of Intl are non-enumerable, so we need
|
|
1826
|
+
* to do a bit of work to get them out.
|
|
1827
|
+
*/
|
|
1828
|
+
Object.getOwnPropertyNames(NativeIntl).forEach(
|
|
1829
|
+
(property) => (ClockIntl[property] = NativeIntl[property]),
|
|
1830
|
+
);
|
|
1831
|
+
|
|
1832
|
+
ClockIntl.DateTimeFormat = function (...args) {
|
|
1833
|
+
const realFormatter = new NativeIntl.DateTimeFormat(...args);
|
|
1834
|
+
const formatter = {};
|
|
1835
|
+
|
|
1836
|
+
["formatRange", "formatRangeToParts", "resolvedOptions"].forEach(
|
|
1837
|
+
(method) => {
|
|
1838
|
+
formatter[method] =
|
|
1839
|
+
realFormatter[method].bind(realFormatter);
|
|
1840
|
+
},
|
|
1841
|
+
);
|
|
1842
|
+
|
|
1843
|
+
["format", "formatToParts"].forEach((method) => {
|
|
1844
|
+
formatter[method] = function (date) {
|
|
1845
|
+
return realFormatter[method](date || ClockIntl.clock.now);
|
|
1846
|
+
};
|
|
1847
|
+
});
|
|
1848
|
+
|
|
1849
|
+
return formatter;
|
|
1850
|
+
};
|
|
1851
|
+
|
|
1852
|
+
ClockIntl.DateTimeFormat.prototype = Object.create(
|
|
1853
|
+
NativeIntl.DateTimeFormat.prototype,
|
|
1854
|
+
);
|
|
1855
|
+
|
|
1856
|
+
ClockIntl.DateTimeFormat.supportedLocalesOf =
|
|
1857
|
+
NativeIntl.DateTimeFormat.supportedLocalesOf;
|
|
1858
|
+
|
|
1859
|
+
return ClockIntl;
|
|
1860
|
+
}
|
|
1861
|
+
|
|
1862
|
+
//eslint-disable-next-line jsdoc/require-jsdoc
|
|
1863
|
+
function enqueueJob(clock, job) {
|
|
1864
|
+
// enqueues a microtick-deferred task - ecma262/#sec-enqueuejob
|
|
1865
|
+
if (!clock.jobs) {
|
|
1866
|
+
clock.jobs = [];
|
|
1867
|
+
}
|
|
1868
|
+
clock.jobs.push(job);
|
|
1869
|
+
}
|
|
1870
|
+
|
|
1871
|
+
//eslint-disable-next-line jsdoc/require-jsdoc
|
|
1872
|
+
function runJobs(clock) {
|
|
1873
|
+
// runs all microtick-deferred tasks - ecma262/#sec-runjobs
|
|
1874
|
+
if (!clock.jobs) {
|
|
1875
|
+
return;
|
|
1876
|
+
}
|
|
1877
|
+
for (let i = 0; i < clock.jobs.length; i++) {
|
|
1878
|
+
const job = clock.jobs[i];
|
|
1879
|
+
job.func.apply(null, job.args);
|
|
1880
|
+
|
|
1881
|
+
checkIsNearInfiniteLimit(clock, i);
|
|
1882
|
+
if (clock.loopLimit && i > clock.loopLimit) {
|
|
1883
|
+
throw getInfiniteLoopError(clock, job);
|
|
1884
|
+
}
|
|
1885
|
+
}
|
|
1886
|
+
resetIsNearInfiniteLimit();
|
|
1887
|
+
clock.jobs = [];
|
|
1888
|
+
}
|
|
1889
|
+
|
|
1890
|
+
/**
|
|
1891
|
+
* @param {Clock} clock
|
|
1892
|
+
* @param {Timer} timer
|
|
1893
|
+
* @returns {number} id of the created timer
|
|
1894
|
+
*/
|
|
1895
|
+
function addTimer(clock, timer) {
|
|
1896
|
+
if (timer.func === undefined) {
|
|
1897
|
+
throw new Error("Callback must be provided to timer calls");
|
|
1898
|
+
}
|
|
1899
|
+
|
|
1900
|
+
if (addTimerReturnsObject) {
|
|
1901
|
+
// Node.js environment
|
|
1902
|
+
if (typeof timer.func !== "function") {
|
|
1903
|
+
throw new TypeError(
|
|
1904
|
+
`[ERR_INVALID_CALLBACK]: Callback must be a function. Received ${
|
|
1905
|
+
timer.func
|
|
1906
|
+
} of type ${typeof timer.func}`,
|
|
1907
|
+
);
|
|
1908
|
+
}
|
|
1909
|
+
}
|
|
1910
|
+
|
|
1911
|
+
if (isNearInfiniteLimit) {
|
|
1912
|
+
timer.error = new Error();
|
|
1913
|
+
}
|
|
1914
|
+
|
|
1915
|
+
timer.type = timer.immediate ? "Immediate" : "Timeout";
|
|
1916
|
+
|
|
1917
|
+
if (timer.hasOwnProperty("delay")) {
|
|
1918
|
+
if (typeof timer.delay !== "number") {
|
|
1919
|
+
timer.delay = parseInt(timer.delay, 10);
|
|
1920
|
+
}
|
|
1921
|
+
|
|
1922
|
+
if (!isNumberFinite(timer.delay)) {
|
|
1923
|
+
timer.delay = 0;
|
|
1924
|
+
}
|
|
1925
|
+
timer.delay = timer.delay > maxTimeout ? 1 : timer.delay;
|
|
1926
|
+
timer.delay = Math.max(0, timer.delay);
|
|
1927
|
+
}
|
|
1928
|
+
|
|
1929
|
+
if (timer.hasOwnProperty("interval")) {
|
|
1930
|
+
timer.type = "Interval";
|
|
1931
|
+
timer.interval = timer.interval > maxTimeout ? 1 : timer.interval;
|
|
1932
|
+
}
|
|
1933
|
+
|
|
1934
|
+
if (timer.hasOwnProperty("animation")) {
|
|
1935
|
+
timer.type = "AnimationFrame";
|
|
1936
|
+
timer.animation = true;
|
|
1937
|
+
}
|
|
1938
|
+
|
|
1939
|
+
if (timer.hasOwnProperty("idleCallback")) {
|
|
1940
|
+
timer.type = "IdleCallback";
|
|
1941
|
+
timer.idleCallback = true;
|
|
1942
|
+
}
|
|
1943
|
+
|
|
1944
|
+
if (!clock.timers) {
|
|
1945
|
+
clock.timers = {};
|
|
1946
|
+
}
|
|
1947
|
+
|
|
1948
|
+
timer.id = uniqueTimerId++;
|
|
1949
|
+
timer.createdAt = clock.now;
|
|
1950
|
+
timer.callAt =
|
|
1951
|
+
clock.now + (parseInt(timer.delay) || (clock.duringTick ? 1 : 0));
|
|
1952
|
+
|
|
1953
|
+
clock.timers[timer.id] = timer;
|
|
1954
|
+
|
|
1955
|
+
if (addTimerReturnsObject) {
|
|
1956
|
+
const res = {
|
|
1957
|
+
refed: true,
|
|
1958
|
+
ref: function () {
|
|
1959
|
+
this.refed = true;
|
|
1960
|
+
return res;
|
|
1961
|
+
},
|
|
1962
|
+
unref: function () {
|
|
1963
|
+
this.refed = false;
|
|
1964
|
+
return res;
|
|
1965
|
+
},
|
|
1966
|
+
hasRef: function () {
|
|
1967
|
+
return this.refed;
|
|
1968
|
+
},
|
|
1969
|
+
refresh: function () {
|
|
1970
|
+
timer.callAt =
|
|
1971
|
+
clock.now +
|
|
1972
|
+
(parseInt(timer.delay) || (clock.duringTick ? 1 : 0));
|
|
1973
|
+
|
|
1974
|
+
// it _might_ have been removed, but if not the assignment is perfectly fine
|
|
1975
|
+
clock.timers[timer.id] = timer;
|
|
1976
|
+
|
|
1977
|
+
return res;
|
|
1978
|
+
},
|
|
1979
|
+
[Symbol.toPrimitive]: function () {
|
|
1980
|
+
return timer.id;
|
|
1981
|
+
},
|
|
1982
|
+
};
|
|
1983
|
+
return res;
|
|
1984
|
+
}
|
|
1985
|
+
|
|
1986
|
+
return timer.id;
|
|
1987
|
+
}
|
|
1988
|
+
|
|
1989
|
+
/* eslint consistent-return: "off" */
|
|
1990
|
+
/**
|
|
1991
|
+
* Timer comparitor
|
|
1992
|
+
* @param {Timer} a
|
|
1993
|
+
* @param {Timer} b
|
|
1994
|
+
* @returns {number}
|
|
1995
|
+
*/
|
|
1996
|
+
function compareTimers(a, b) {
|
|
1997
|
+
// Sort first by absolute timing
|
|
1998
|
+
if (a.callAt < b.callAt) {
|
|
1999
|
+
return -1;
|
|
2000
|
+
}
|
|
2001
|
+
if (a.callAt > b.callAt) {
|
|
2002
|
+
return 1;
|
|
2003
|
+
}
|
|
2004
|
+
|
|
2005
|
+
// Sort next by immediate, immediate timers take precedence
|
|
2006
|
+
if (a.immediate && !b.immediate) {
|
|
2007
|
+
return -1;
|
|
2008
|
+
}
|
|
2009
|
+
if (!a.immediate && b.immediate) {
|
|
2010
|
+
return 1;
|
|
2011
|
+
}
|
|
2012
|
+
|
|
2013
|
+
// Sort next by creation time, earlier-created timers take precedence
|
|
2014
|
+
if (a.createdAt < b.createdAt) {
|
|
2015
|
+
return -1;
|
|
2016
|
+
}
|
|
2017
|
+
if (a.createdAt > b.createdAt) {
|
|
2018
|
+
return 1;
|
|
2019
|
+
}
|
|
2020
|
+
|
|
2021
|
+
// Sort next by id, lower-id timers take precedence
|
|
2022
|
+
if (a.id < b.id) {
|
|
2023
|
+
return -1;
|
|
2024
|
+
}
|
|
2025
|
+
if (a.id > b.id) {
|
|
2026
|
+
return 1;
|
|
2027
|
+
}
|
|
2028
|
+
|
|
2029
|
+
// As timer ids are unique, no fallback `0` is necessary
|
|
2030
|
+
}
|
|
2031
|
+
|
|
2032
|
+
/**
|
|
2033
|
+
* @param {Clock} clock
|
|
2034
|
+
* @param {number} from
|
|
2035
|
+
* @param {number} to
|
|
2036
|
+
* @returns {Timer}
|
|
2037
|
+
*/
|
|
2038
|
+
function firstTimerInRange(clock, from, to) {
|
|
2039
|
+
const timers = clock.timers;
|
|
2040
|
+
let timer = null;
|
|
2041
|
+
let id, isInRange;
|
|
2042
|
+
|
|
2043
|
+
for (id in timers) {
|
|
2044
|
+
if (timers.hasOwnProperty(id)) {
|
|
2045
|
+
isInRange = inRange(from, to, timers[id]);
|
|
2046
|
+
|
|
2047
|
+
if (
|
|
2048
|
+
isInRange &&
|
|
2049
|
+
(!timer || compareTimers(timer, timers[id]) === 1)
|
|
2050
|
+
) {
|
|
2051
|
+
timer = timers[id];
|
|
2052
|
+
}
|
|
2053
|
+
}
|
|
2054
|
+
}
|
|
2055
|
+
|
|
2056
|
+
return timer;
|
|
2057
|
+
}
|
|
2058
|
+
|
|
2059
|
+
/**
|
|
2060
|
+
* @param {Clock} clock
|
|
2061
|
+
* @returns {Timer}
|
|
2062
|
+
*/
|
|
2063
|
+
function firstTimer(clock) {
|
|
2064
|
+
const timers = clock.timers;
|
|
2065
|
+
let timer = null;
|
|
2066
|
+
let id;
|
|
2067
|
+
|
|
2068
|
+
for (id in timers) {
|
|
2069
|
+
if (timers.hasOwnProperty(id)) {
|
|
2070
|
+
if (!timer || compareTimers(timer, timers[id]) === 1) {
|
|
2071
|
+
timer = timers[id];
|
|
2072
|
+
}
|
|
2073
|
+
}
|
|
2074
|
+
}
|
|
2075
|
+
|
|
2076
|
+
return timer;
|
|
2077
|
+
}
|
|
2078
|
+
|
|
2079
|
+
/**
|
|
2080
|
+
* @param {Clock} clock
|
|
2081
|
+
* @returns {Timer}
|
|
2082
|
+
*/
|
|
2083
|
+
function lastTimer(clock) {
|
|
2084
|
+
const timers = clock.timers;
|
|
2085
|
+
let timer = null;
|
|
2086
|
+
let id;
|
|
2087
|
+
|
|
2088
|
+
for (id in timers) {
|
|
2089
|
+
if (timers.hasOwnProperty(id)) {
|
|
2090
|
+
if (!timer || compareTimers(timer, timers[id]) === -1) {
|
|
2091
|
+
timer = timers[id];
|
|
2092
|
+
}
|
|
2093
|
+
}
|
|
2094
|
+
}
|
|
2095
|
+
|
|
2096
|
+
return timer;
|
|
2097
|
+
}
|
|
2098
|
+
|
|
2099
|
+
/**
|
|
2100
|
+
* @param {Clock} clock
|
|
2101
|
+
* @param {Timer} timer
|
|
2102
|
+
*/
|
|
2103
|
+
function callTimer(clock, timer) {
|
|
2104
|
+
if (typeof timer.interval === "number") {
|
|
2105
|
+
clock.timers[timer.id].callAt += timer.interval;
|
|
2106
|
+
} else {
|
|
2107
|
+
delete clock.timers[timer.id];
|
|
2108
|
+
}
|
|
2109
|
+
|
|
2110
|
+
if (typeof timer.func === "function") {
|
|
2111
|
+
timer.func.apply(null, timer.args);
|
|
2112
|
+
} else {
|
|
2113
|
+
/* eslint no-eval: "off" */
|
|
2114
|
+
const eval2 = eval;
|
|
2115
|
+
(function () {
|
|
2116
|
+
eval2(timer.func);
|
|
2117
|
+
})();
|
|
2118
|
+
}
|
|
2119
|
+
}
|
|
2120
|
+
|
|
2121
|
+
/**
|
|
2122
|
+
* Gets clear handler name for a given timer type
|
|
2123
|
+
* @param {string} ttype
|
|
2124
|
+
*/
|
|
2125
|
+
function getClearHandler(ttype) {
|
|
2126
|
+
if (ttype === "IdleCallback" || ttype === "AnimationFrame") {
|
|
2127
|
+
return `cancel${ttype}`;
|
|
2128
|
+
}
|
|
2129
|
+
return `clear${ttype}`;
|
|
2130
|
+
}
|
|
2131
|
+
|
|
2132
|
+
/**
|
|
2133
|
+
* Gets schedule handler name for a given timer type
|
|
2134
|
+
* @param {string} ttype
|
|
2135
|
+
*/
|
|
2136
|
+
function getScheduleHandler(ttype) {
|
|
2137
|
+
if (ttype === "IdleCallback" || ttype === "AnimationFrame") {
|
|
2138
|
+
return `request${ttype}`;
|
|
2139
|
+
}
|
|
2140
|
+
return `set${ttype}`;
|
|
2141
|
+
}
|
|
2142
|
+
|
|
2143
|
+
/**
|
|
2144
|
+
* Creates an anonymous function to warn only once
|
|
2145
|
+
*/
|
|
2146
|
+
function createWarnOnce() {
|
|
2147
|
+
let calls = 0;
|
|
2148
|
+
return function (msg) {
|
|
2149
|
+
// eslint-disable-next-line
|
|
2150
|
+
!calls++ && console.warn(msg);
|
|
2151
|
+
};
|
|
2152
|
+
}
|
|
2153
|
+
const warnOnce = createWarnOnce();
|
|
2154
|
+
|
|
2155
|
+
/**
|
|
2156
|
+
* @param {Clock} clock
|
|
2157
|
+
* @param {number} timerId
|
|
2158
|
+
* @param {string} ttype
|
|
2159
|
+
*/
|
|
2160
|
+
function clearTimer(clock, timerId, ttype) {
|
|
2161
|
+
if (!timerId) {
|
|
2162
|
+
// null appears to be allowed in most browsers, and appears to be
|
|
2163
|
+
// relied upon by some libraries, like Bootstrap carousel
|
|
2164
|
+
return;
|
|
2165
|
+
}
|
|
2166
|
+
|
|
2167
|
+
if (!clock.timers) {
|
|
2168
|
+
clock.timers = {};
|
|
2169
|
+
}
|
|
2170
|
+
|
|
2171
|
+
// in Node, the ID is stored as the primitive value for `Timeout` objects
|
|
2172
|
+
// for `Immediate` objects, no ID exists, so it gets coerced to NaN
|
|
2173
|
+
const id = Number(timerId);
|
|
2174
|
+
|
|
2175
|
+
if (Number.isNaN(id) || id < idCounterStart) {
|
|
2176
|
+
const handlerName = getClearHandler(ttype);
|
|
2177
|
+
|
|
2178
|
+
if (clock.shouldClearNativeTimers === true) {
|
|
2179
|
+
const nativeHandler = clock[`_${handlerName}`];
|
|
2180
|
+
return typeof nativeHandler === "function"
|
|
2181
|
+
? nativeHandler(timerId)
|
|
2182
|
+
: undefined;
|
|
2183
|
+
}
|
|
2184
|
+
warnOnce(
|
|
2185
|
+
`FakeTimers: ${handlerName} was invoked to clear a native timer instead of one created by this library.` +
|
|
2186
|
+
"\nTo automatically clean-up native timers, use `shouldClearNativeTimers`.",
|
|
2187
|
+
);
|
|
2188
|
+
}
|
|
2189
|
+
|
|
2190
|
+
if (clock.timers.hasOwnProperty(id)) {
|
|
2191
|
+
// check that the ID matches a timer of the correct type
|
|
2192
|
+
const timer = clock.timers[id];
|
|
2193
|
+
if (
|
|
2194
|
+
timer.type === ttype ||
|
|
2195
|
+
(timer.type === "Timeout" && ttype === "Interval") ||
|
|
2196
|
+
(timer.type === "Interval" && ttype === "Timeout")
|
|
2197
|
+
) {
|
|
2198
|
+
delete clock.timers[id];
|
|
2199
|
+
} else {
|
|
2200
|
+
const clear = getClearHandler(ttype);
|
|
2201
|
+
const schedule = getScheduleHandler(timer.type);
|
|
2202
|
+
throw new Error(
|
|
2203
|
+
`Cannot clear timer: timer created with ${schedule}() but cleared with ${clear}()`,
|
|
2204
|
+
);
|
|
2205
|
+
}
|
|
2206
|
+
}
|
|
2207
|
+
}
|
|
2208
|
+
|
|
2209
|
+
/**
|
|
2210
|
+
* @param {Clock} clock
|
|
2211
|
+
* @param {Config} config
|
|
2212
|
+
* @returns {Timer[]}
|
|
2213
|
+
*/
|
|
2214
|
+
function uninstall(clock, config) {
|
|
2215
|
+
let method, i, l;
|
|
2216
|
+
const installedHrTime = "_hrtime";
|
|
2217
|
+
const installedNextTick = "_nextTick";
|
|
2218
|
+
|
|
2219
|
+
for (i = 0, l = clock.methods.length; i < l; i++) {
|
|
2220
|
+
method = clock.methods[i];
|
|
2221
|
+
if (method === "hrtime" && _global.process) {
|
|
2222
|
+
_global.process.hrtime = clock[installedHrTime];
|
|
2223
|
+
} else if (method === "nextTick" && _global.process) {
|
|
2224
|
+
_global.process.nextTick = clock[installedNextTick];
|
|
2225
|
+
} else if (method === "performance") {
|
|
2226
|
+
const originalPerfDescriptor = Object.getOwnPropertyDescriptor(
|
|
2227
|
+
clock,
|
|
2228
|
+
`_${method}`,
|
|
2229
|
+
);
|
|
2230
|
+
if (
|
|
2231
|
+
originalPerfDescriptor &&
|
|
2232
|
+
originalPerfDescriptor.get &&
|
|
2233
|
+
!originalPerfDescriptor.set
|
|
2234
|
+
) {
|
|
2235
|
+
Object.defineProperty(
|
|
2236
|
+
_global,
|
|
2237
|
+
method,
|
|
2238
|
+
originalPerfDescriptor,
|
|
2239
|
+
);
|
|
2240
|
+
} else if (originalPerfDescriptor.configurable) {
|
|
2241
|
+
_global[method] = clock[`_${method}`];
|
|
2242
|
+
}
|
|
2243
|
+
} else {
|
|
2244
|
+
if (_global[method] && _global[method].hadOwnProperty) {
|
|
2245
|
+
_global[method] = clock[`_${method}`];
|
|
2246
|
+
} else {
|
|
2247
|
+
try {
|
|
2248
|
+
delete _global[method];
|
|
2249
|
+
} catch (ignore) {
|
|
2250
|
+
/* eslint no-empty: "off" */
|
|
2251
|
+
}
|
|
2252
|
+
}
|
|
2253
|
+
}
|
|
2254
|
+
if (clock.timersModuleMethods !== undefined) {
|
|
2255
|
+
for (let j = 0; j < clock.timersModuleMethods.length; j++) {
|
|
2256
|
+
const entry = clock.timersModuleMethods[j];
|
|
2257
|
+
timersModule[entry.methodName] = entry.original;
|
|
2258
|
+
}
|
|
2259
|
+
}
|
|
2260
|
+
if (clock.timersPromisesModuleMethods !== undefined) {
|
|
2261
|
+
for (
|
|
2262
|
+
let j = 0;
|
|
2263
|
+
j < clock.timersPromisesModuleMethods.length;
|
|
2264
|
+
j++
|
|
2265
|
+
) {
|
|
2266
|
+
const entry = clock.timersPromisesModuleMethods[j];
|
|
2267
|
+
timersPromisesModule[entry.methodName] = entry.original;
|
|
2268
|
+
}
|
|
2269
|
+
}
|
|
2270
|
+
}
|
|
2271
|
+
|
|
2272
|
+
if (config.shouldAdvanceTime === true) {
|
|
2273
|
+
_global.clearInterval(clock.attachedInterval);
|
|
2274
|
+
}
|
|
2275
|
+
|
|
2276
|
+
// Prevent multiple executions which will completely remove these props
|
|
2277
|
+
clock.methods = [];
|
|
2278
|
+
|
|
2279
|
+
for (const [listener, signal] of clock.abortListenerMap.entries()) {
|
|
2280
|
+
signal.removeEventListener("abort", listener);
|
|
2281
|
+
clock.abortListenerMap.delete(listener);
|
|
2282
|
+
}
|
|
2283
|
+
|
|
2284
|
+
// return pending timers, to enable checking what timers remained on uninstall
|
|
2285
|
+
if (!clock.timers) {
|
|
2286
|
+
return [];
|
|
2287
|
+
}
|
|
2288
|
+
return Object.keys(clock.timers).map(function mapper(key) {
|
|
2289
|
+
return clock.timers[key];
|
|
2290
|
+
});
|
|
2291
|
+
}
|
|
2292
|
+
|
|
2293
|
+
/**
|
|
2294
|
+
* @param {object} target the target containing the method to replace
|
|
2295
|
+
* @param {string} method the keyname of the method on the target
|
|
2296
|
+
* @param {Clock} clock
|
|
2297
|
+
*/
|
|
2298
|
+
function hijackMethod(target, method, clock) {
|
|
2299
|
+
clock[method].hadOwnProperty = Object.prototype.hasOwnProperty.call(
|
|
2300
|
+
target,
|
|
2301
|
+
method,
|
|
2302
|
+
);
|
|
2303
|
+
clock[`_${method}`] = target[method];
|
|
2304
|
+
|
|
2305
|
+
if (method === "Date") {
|
|
2306
|
+
target[method] = clock[method];
|
|
2307
|
+
} else if (method === "Intl") {
|
|
2308
|
+
target[method] = clock[method];
|
|
2309
|
+
} else if (method === "performance") {
|
|
2310
|
+
const originalPerfDescriptor = Object.getOwnPropertyDescriptor(
|
|
2311
|
+
target,
|
|
2312
|
+
method,
|
|
2313
|
+
);
|
|
2314
|
+
// JSDOM has a read only performance field so we have to save/copy it differently
|
|
2315
|
+
if (
|
|
2316
|
+
originalPerfDescriptor &&
|
|
2317
|
+
originalPerfDescriptor.get &&
|
|
2318
|
+
!originalPerfDescriptor.set
|
|
2319
|
+
) {
|
|
2320
|
+
Object.defineProperty(
|
|
2321
|
+
clock,
|
|
2322
|
+
`_${method}`,
|
|
2323
|
+
originalPerfDescriptor,
|
|
2324
|
+
);
|
|
2325
|
+
|
|
2326
|
+
const perfDescriptor = Object.getOwnPropertyDescriptor(
|
|
2327
|
+
clock,
|
|
2328
|
+
method,
|
|
2329
|
+
);
|
|
2330
|
+
Object.defineProperty(target, method, perfDescriptor);
|
|
2331
|
+
} else {
|
|
2332
|
+
target[method] = clock[method];
|
|
2333
|
+
}
|
|
2334
|
+
} else {
|
|
2335
|
+
target[method] = function () {
|
|
2336
|
+
return clock[method].apply(clock, arguments);
|
|
2337
|
+
};
|
|
2338
|
+
|
|
2339
|
+
Object.defineProperties(
|
|
2340
|
+
target[method],
|
|
2341
|
+
Object.getOwnPropertyDescriptors(clock[method]),
|
|
2342
|
+
);
|
|
2343
|
+
}
|
|
2344
|
+
|
|
2345
|
+
target[method].clock = clock;
|
|
2346
|
+
}
|
|
2347
|
+
|
|
2348
|
+
/**
|
|
2349
|
+
* @param {Clock} clock
|
|
2350
|
+
* @param {number} advanceTimeDelta
|
|
2351
|
+
*/
|
|
2352
|
+
function doIntervalTick(clock, advanceTimeDelta) {
|
|
2353
|
+
clock.tick(advanceTimeDelta);
|
|
2354
|
+
}
|
|
2355
|
+
|
|
2356
|
+
/**
|
|
2357
|
+
* @typedef {object} Timers
|
|
2358
|
+
* @property {setTimeout} setTimeout
|
|
2359
|
+
* @property {clearTimeout} clearTimeout
|
|
2360
|
+
* @property {setInterval} setInterval
|
|
2361
|
+
* @property {clearInterval} clearInterval
|
|
2362
|
+
* @property {Date} Date
|
|
2363
|
+
* @property {Intl} Intl
|
|
2364
|
+
* @property {SetImmediate=} setImmediate
|
|
2365
|
+
* @property {function(NodeImmediate): void=} clearImmediate
|
|
2366
|
+
* @property {function(number[]):number[]=} hrtime
|
|
2367
|
+
* @property {NextTick=} nextTick
|
|
2368
|
+
* @property {Performance=} performance
|
|
2369
|
+
* @property {RequestAnimationFrame=} requestAnimationFrame
|
|
2370
|
+
* @property {boolean=} queueMicrotask
|
|
2371
|
+
* @property {function(number): void=} cancelAnimationFrame
|
|
2372
|
+
* @property {RequestIdleCallback=} requestIdleCallback
|
|
2373
|
+
* @property {function(number): void=} cancelIdleCallback
|
|
2374
|
+
*/
|
|
2375
|
+
|
|
2376
|
+
/** @type {Timers} */
|
|
2377
|
+
const timers = {
|
|
2378
|
+
setTimeout: _global.setTimeout,
|
|
2379
|
+
clearTimeout: _global.clearTimeout,
|
|
2380
|
+
setInterval: _global.setInterval,
|
|
2381
|
+
clearInterval: _global.clearInterval,
|
|
2382
|
+
Date: _global.Date,
|
|
2383
|
+
};
|
|
2384
|
+
|
|
2385
|
+
if (isPresent.setImmediate) {
|
|
2386
|
+
timers.setImmediate = _global.setImmediate;
|
|
2387
|
+
}
|
|
2388
|
+
|
|
2389
|
+
if (isPresent.clearImmediate) {
|
|
2390
|
+
timers.clearImmediate = _global.clearImmediate;
|
|
2391
|
+
}
|
|
2392
|
+
|
|
2393
|
+
if (isPresent.hrtime) {
|
|
2394
|
+
timers.hrtime = _global.process.hrtime;
|
|
2395
|
+
}
|
|
2396
|
+
|
|
2397
|
+
if (isPresent.nextTick) {
|
|
2398
|
+
timers.nextTick = _global.process.nextTick;
|
|
2399
|
+
}
|
|
2400
|
+
|
|
2401
|
+
if (isPresent.performance) {
|
|
2402
|
+
timers.performance = _global.performance;
|
|
2403
|
+
}
|
|
2404
|
+
|
|
2405
|
+
if (isPresent.requestAnimationFrame) {
|
|
2406
|
+
timers.requestAnimationFrame = _global.requestAnimationFrame;
|
|
2407
|
+
}
|
|
2408
|
+
|
|
2409
|
+
if (isPresent.queueMicrotask) {
|
|
2410
|
+
timers.queueMicrotask = _global.queueMicrotask;
|
|
2411
|
+
}
|
|
2412
|
+
|
|
2413
|
+
if (isPresent.cancelAnimationFrame) {
|
|
2414
|
+
timers.cancelAnimationFrame = _global.cancelAnimationFrame;
|
|
2415
|
+
}
|
|
2416
|
+
|
|
2417
|
+
if (isPresent.requestIdleCallback) {
|
|
2418
|
+
timers.requestIdleCallback = _global.requestIdleCallback;
|
|
2419
|
+
}
|
|
2420
|
+
|
|
2421
|
+
if (isPresent.cancelIdleCallback) {
|
|
2422
|
+
timers.cancelIdleCallback = _global.cancelIdleCallback;
|
|
2423
|
+
}
|
|
2424
|
+
|
|
2425
|
+
if (isPresent.Intl) {
|
|
2426
|
+
timers.Intl = NativeIntl;
|
|
2427
|
+
}
|
|
2428
|
+
|
|
2429
|
+
const originalSetTimeout = _global.setImmediate || _global.setTimeout;
|
|
2430
|
+
|
|
2431
|
+
/**
|
|
2432
|
+
* @param {Date|number} [start] the system time - non-integer values are floored
|
|
2433
|
+
* @param {number} [loopLimit] maximum number of timers that will be run when calling runAll()
|
|
2434
|
+
* @returns {Clock}
|
|
2435
|
+
*/
|
|
2436
|
+
function createClock(start, loopLimit) {
|
|
2437
|
+
// eslint-disable-next-line no-param-reassign
|
|
2438
|
+
start = Math.floor(getEpoch(start));
|
|
2439
|
+
// eslint-disable-next-line no-param-reassign
|
|
2440
|
+
loopLimit = loopLimit || 1000;
|
|
2441
|
+
let nanos = 0;
|
|
2442
|
+
const adjustedSystemTime = [0, 0]; // [millis, nanoremainder]
|
|
2443
|
+
|
|
2444
|
+
const clock = {
|
|
2445
|
+
now: start,
|
|
2446
|
+
Date: createDate(),
|
|
2447
|
+
loopLimit: loopLimit,
|
|
2448
|
+
};
|
|
2449
|
+
|
|
2450
|
+
clock.Date.clock = clock;
|
|
2451
|
+
|
|
2452
|
+
//eslint-disable-next-line jsdoc/require-jsdoc
|
|
2453
|
+
function getTimeToNextFrame() {
|
|
2454
|
+
return 16 - ((clock.now - start) % 16);
|
|
2455
|
+
}
|
|
2456
|
+
|
|
2457
|
+
//eslint-disable-next-line jsdoc/require-jsdoc
|
|
2458
|
+
function hrtime(prev) {
|
|
2459
|
+
const millisSinceStart = clock.now - adjustedSystemTime[0] - start;
|
|
2460
|
+
const secsSinceStart = Math.floor(millisSinceStart / 1000);
|
|
2461
|
+
const remainderInNanos =
|
|
2462
|
+
(millisSinceStart - secsSinceStart * 1e3) * 1e6 +
|
|
2463
|
+
nanos -
|
|
2464
|
+
adjustedSystemTime[1];
|
|
2465
|
+
|
|
2466
|
+
if (Array.isArray(prev)) {
|
|
2467
|
+
if (prev[1] > 1e9) {
|
|
2468
|
+
throw new TypeError(
|
|
2469
|
+
"Number of nanoseconds can't exceed a billion",
|
|
2470
|
+
);
|
|
2471
|
+
}
|
|
2472
|
+
|
|
2473
|
+
const oldSecs = prev[0];
|
|
2474
|
+
let nanoDiff = remainderInNanos - prev[1];
|
|
2475
|
+
let secDiff = secsSinceStart - oldSecs;
|
|
2476
|
+
|
|
2477
|
+
if (nanoDiff < 0) {
|
|
2478
|
+
nanoDiff += 1e9;
|
|
2479
|
+
secDiff -= 1;
|
|
2480
|
+
}
|
|
2481
|
+
|
|
2482
|
+
return [secDiff, nanoDiff];
|
|
2483
|
+
}
|
|
2484
|
+
return [secsSinceStart, remainderInNanos];
|
|
2485
|
+
}
|
|
2486
|
+
|
|
2487
|
+
/**
|
|
2488
|
+
* A high resolution timestamp in milliseconds.
|
|
2489
|
+
* @typedef {number} DOMHighResTimeStamp
|
|
2490
|
+
*/
|
|
2491
|
+
|
|
2492
|
+
/**
|
|
2493
|
+
* performance.now()
|
|
2494
|
+
* @returns {DOMHighResTimeStamp}
|
|
2495
|
+
*/
|
|
2496
|
+
function fakePerformanceNow() {
|
|
2497
|
+
const hrt = hrtime();
|
|
2498
|
+
const millis = hrt[0] * 1000 + hrt[1] / 1e6;
|
|
2499
|
+
return millis;
|
|
2500
|
+
}
|
|
2501
|
+
|
|
2502
|
+
if (isPresent.hrtimeBigint) {
|
|
2503
|
+
hrtime.bigint = function () {
|
|
2504
|
+
const parts = hrtime();
|
|
2505
|
+
return BigInt(parts[0]) * BigInt(1e9) + BigInt(parts[1]); // eslint-disable-line
|
|
2506
|
+
};
|
|
2507
|
+
}
|
|
2508
|
+
|
|
2509
|
+
if (isPresent.Intl) {
|
|
2510
|
+
clock.Intl = createIntl();
|
|
2511
|
+
clock.Intl.clock = clock;
|
|
2512
|
+
}
|
|
2513
|
+
|
|
2514
|
+
clock.requestIdleCallback = function requestIdleCallback(
|
|
2515
|
+
func,
|
|
2516
|
+
timeout,
|
|
2517
|
+
) {
|
|
2518
|
+
let timeToNextIdlePeriod = 0;
|
|
2519
|
+
|
|
2520
|
+
if (clock.countTimers() > 0) {
|
|
2521
|
+
timeToNextIdlePeriod = 50; // const for now
|
|
2522
|
+
}
|
|
2523
|
+
|
|
2524
|
+
const result = addTimer(clock, {
|
|
2525
|
+
func: func,
|
|
2526
|
+
args: Array.prototype.slice.call(arguments, 2),
|
|
2527
|
+
delay:
|
|
2528
|
+
typeof timeout === "undefined"
|
|
2529
|
+
? timeToNextIdlePeriod
|
|
2530
|
+
: Math.min(timeout, timeToNextIdlePeriod),
|
|
2531
|
+
idleCallback: true,
|
|
2532
|
+
});
|
|
2533
|
+
|
|
2534
|
+
return Number(result);
|
|
2535
|
+
};
|
|
2536
|
+
|
|
2537
|
+
clock.cancelIdleCallback = function cancelIdleCallback(timerId) {
|
|
2538
|
+
return clearTimer(clock, timerId, "IdleCallback");
|
|
2539
|
+
};
|
|
2540
|
+
|
|
2541
|
+
clock.setTimeout = function setTimeout(func, timeout) {
|
|
2542
|
+
return addTimer(clock, {
|
|
2543
|
+
func: func,
|
|
2544
|
+
args: Array.prototype.slice.call(arguments, 2),
|
|
2545
|
+
delay: timeout,
|
|
2546
|
+
});
|
|
2547
|
+
};
|
|
2548
|
+
if (typeof _global.Promise !== "undefined" && utilPromisify) {
|
|
2549
|
+
clock.setTimeout[utilPromisify.custom] =
|
|
2550
|
+
function promisifiedSetTimeout(timeout, arg) {
|
|
2551
|
+
return new _global.Promise(function setTimeoutExecutor(
|
|
2552
|
+
resolve,
|
|
2553
|
+
) {
|
|
2554
|
+
addTimer(clock, {
|
|
2555
|
+
func: resolve,
|
|
2556
|
+
args: [arg],
|
|
2557
|
+
delay: timeout,
|
|
2558
|
+
});
|
|
2559
|
+
});
|
|
2560
|
+
};
|
|
2561
|
+
}
|
|
2562
|
+
|
|
2563
|
+
clock.clearTimeout = function clearTimeout(timerId) {
|
|
2564
|
+
return clearTimer(clock, timerId, "Timeout");
|
|
2565
|
+
};
|
|
2566
|
+
|
|
2567
|
+
clock.nextTick = function nextTick(func) {
|
|
2568
|
+
return enqueueJob(clock, {
|
|
2569
|
+
func: func,
|
|
2570
|
+
args: Array.prototype.slice.call(arguments, 1),
|
|
2571
|
+
error: isNearInfiniteLimit ? new Error() : null,
|
|
2572
|
+
});
|
|
2573
|
+
};
|
|
2574
|
+
|
|
2575
|
+
clock.queueMicrotask = function queueMicrotask(func) {
|
|
2576
|
+
return clock.nextTick(func); // explicitly drop additional arguments
|
|
2577
|
+
};
|
|
2578
|
+
|
|
2579
|
+
clock.setInterval = function setInterval(func, timeout) {
|
|
2580
|
+
// eslint-disable-next-line no-param-reassign
|
|
2581
|
+
timeout = parseInt(timeout, 10);
|
|
2582
|
+
return addTimer(clock, {
|
|
2583
|
+
func: func,
|
|
2584
|
+
args: Array.prototype.slice.call(arguments, 2),
|
|
2585
|
+
delay: timeout,
|
|
2586
|
+
interval: timeout,
|
|
2587
|
+
});
|
|
2588
|
+
};
|
|
2589
|
+
|
|
2590
|
+
clock.clearInterval = function clearInterval(timerId) {
|
|
2591
|
+
return clearTimer(clock, timerId, "Interval");
|
|
2592
|
+
};
|
|
2593
|
+
|
|
2594
|
+
if (isPresent.setImmediate) {
|
|
2595
|
+
clock.setImmediate = function setImmediate(func) {
|
|
2596
|
+
return addTimer(clock, {
|
|
2597
|
+
func: func,
|
|
2598
|
+
args: Array.prototype.slice.call(arguments, 1),
|
|
2599
|
+
immediate: true,
|
|
2600
|
+
});
|
|
2601
|
+
};
|
|
2602
|
+
|
|
2603
|
+
if (typeof _global.Promise !== "undefined" && utilPromisify) {
|
|
2604
|
+
clock.setImmediate[utilPromisify.custom] =
|
|
2605
|
+
function promisifiedSetImmediate(arg) {
|
|
2606
|
+
return new _global.Promise(
|
|
2607
|
+
function setImmediateExecutor(resolve) {
|
|
2608
|
+
addTimer(clock, {
|
|
2609
|
+
func: resolve,
|
|
2610
|
+
args: [arg],
|
|
2611
|
+
immediate: true,
|
|
2612
|
+
});
|
|
2613
|
+
},
|
|
2614
|
+
);
|
|
2615
|
+
};
|
|
2616
|
+
}
|
|
2617
|
+
|
|
2618
|
+
clock.clearImmediate = function clearImmediate(timerId) {
|
|
2619
|
+
return clearTimer(clock, timerId, "Immediate");
|
|
2620
|
+
};
|
|
2621
|
+
}
|
|
2622
|
+
|
|
2623
|
+
clock.countTimers = function countTimers() {
|
|
2624
|
+
return (
|
|
2625
|
+
Object.keys(clock.timers || {}).length +
|
|
2626
|
+
(clock.jobs || []).length
|
|
2627
|
+
);
|
|
2628
|
+
};
|
|
2629
|
+
|
|
2630
|
+
clock.requestAnimationFrame = function requestAnimationFrame(func) {
|
|
2631
|
+
const result = addTimer(clock, {
|
|
2632
|
+
func: func,
|
|
2633
|
+
delay: getTimeToNextFrame(),
|
|
2634
|
+
get args() {
|
|
2635
|
+
return [fakePerformanceNow()];
|
|
2636
|
+
},
|
|
2637
|
+
animation: true,
|
|
2638
|
+
});
|
|
2639
|
+
|
|
2640
|
+
return Number(result);
|
|
2641
|
+
};
|
|
2642
|
+
|
|
2643
|
+
clock.cancelAnimationFrame = function cancelAnimationFrame(timerId) {
|
|
2644
|
+
return clearTimer(clock, timerId, "AnimationFrame");
|
|
2645
|
+
};
|
|
2646
|
+
|
|
2647
|
+
clock.runMicrotasks = function runMicrotasks() {
|
|
2648
|
+
runJobs(clock);
|
|
2649
|
+
};
|
|
2650
|
+
|
|
2651
|
+
/**
|
|
2652
|
+
* @param {number|string} tickValue milliseconds or a string parseable by parseTime
|
|
2653
|
+
* @param {boolean} isAsync
|
|
2654
|
+
* @param {Function} resolve
|
|
2655
|
+
* @param {Function} reject
|
|
2656
|
+
* @returns {number|undefined} will return the new `now` value or nothing for async
|
|
2657
|
+
*/
|
|
2658
|
+
function doTick(tickValue, isAsync, resolve, reject) {
|
|
2659
|
+
const msFloat =
|
|
2660
|
+
typeof tickValue === "number"
|
|
2661
|
+
? tickValue
|
|
2662
|
+
: parseTime(tickValue);
|
|
2663
|
+
const ms = Math.floor(msFloat);
|
|
2664
|
+
const remainder = nanoRemainder(msFloat);
|
|
2665
|
+
let nanosTotal = nanos + remainder;
|
|
2666
|
+
let tickTo = clock.now + ms;
|
|
2667
|
+
|
|
2668
|
+
if (msFloat < 0) {
|
|
2669
|
+
throw new TypeError("Negative ticks are not supported");
|
|
2670
|
+
}
|
|
2671
|
+
|
|
2672
|
+
// adjust for positive overflow
|
|
2673
|
+
if (nanosTotal >= 1e6) {
|
|
2674
|
+
tickTo += 1;
|
|
2675
|
+
nanosTotal -= 1e6;
|
|
2676
|
+
}
|
|
2677
|
+
|
|
2678
|
+
nanos = nanosTotal;
|
|
2679
|
+
let tickFrom = clock.now;
|
|
2680
|
+
let previous = clock.now;
|
|
2681
|
+
// ESLint fails to detect this correctly
|
|
2682
|
+
/* eslint-disable prefer-const */
|
|
2683
|
+
let timer,
|
|
2684
|
+
firstException,
|
|
2685
|
+
oldNow,
|
|
2686
|
+
nextPromiseTick,
|
|
2687
|
+
compensationCheck,
|
|
2688
|
+
postTimerCall;
|
|
2689
|
+
/* eslint-enable prefer-const */
|
|
2690
|
+
|
|
2691
|
+
clock.duringTick = true;
|
|
2692
|
+
|
|
2693
|
+
// perform microtasks
|
|
2694
|
+
oldNow = clock.now;
|
|
2695
|
+
runJobs(clock);
|
|
2696
|
+
if (oldNow !== clock.now) {
|
|
2697
|
+
// compensate for any setSystemTime() call during microtask callback
|
|
2698
|
+
tickFrom += clock.now - oldNow;
|
|
2699
|
+
tickTo += clock.now - oldNow;
|
|
2700
|
+
}
|
|
2701
|
+
|
|
2702
|
+
//eslint-disable-next-line jsdoc/require-jsdoc
|
|
2703
|
+
function doTickInner() {
|
|
2704
|
+
// perform each timer in the requested range
|
|
2705
|
+
timer = firstTimerInRange(clock, tickFrom, tickTo);
|
|
2706
|
+
// eslint-disable-next-line no-unmodified-loop-condition
|
|
2707
|
+
while (timer && tickFrom <= tickTo) {
|
|
2708
|
+
if (clock.timers[timer.id]) {
|
|
2709
|
+
tickFrom = timer.callAt;
|
|
2710
|
+
clock.now = timer.callAt;
|
|
2711
|
+
oldNow = clock.now;
|
|
2712
|
+
try {
|
|
2713
|
+
runJobs(clock);
|
|
2714
|
+
callTimer(clock, timer);
|
|
2715
|
+
} catch (e) {
|
|
2716
|
+
firstException = firstException || e;
|
|
2717
|
+
}
|
|
2718
|
+
|
|
2719
|
+
if (isAsync) {
|
|
2720
|
+
// finish up after native setImmediate callback to allow
|
|
2721
|
+
// all native es6 promises to process their callbacks after
|
|
2722
|
+
// each timer fires.
|
|
2723
|
+
originalSetTimeout(nextPromiseTick);
|
|
2724
|
+
return;
|
|
2725
|
+
}
|
|
2726
|
+
|
|
2727
|
+
compensationCheck();
|
|
2728
|
+
}
|
|
2729
|
+
|
|
2730
|
+
postTimerCall();
|
|
2731
|
+
}
|
|
2732
|
+
|
|
2733
|
+
// perform process.nextTick()s again
|
|
2734
|
+
oldNow = clock.now;
|
|
2735
|
+
runJobs(clock);
|
|
2736
|
+
if (oldNow !== clock.now) {
|
|
2737
|
+
// compensate for any setSystemTime() call during process.nextTick() callback
|
|
2738
|
+
tickFrom += clock.now - oldNow;
|
|
2739
|
+
tickTo += clock.now - oldNow;
|
|
2740
|
+
}
|
|
2741
|
+
clock.duringTick = false;
|
|
2742
|
+
|
|
2743
|
+
// corner case: during runJobs new timers were scheduled which could be in the range [clock.now, tickTo]
|
|
2744
|
+
timer = firstTimerInRange(clock, tickFrom, tickTo);
|
|
2745
|
+
if (timer) {
|
|
2746
|
+
try {
|
|
2747
|
+
clock.tick(tickTo - clock.now); // do it all again - for the remainder of the requested range
|
|
2748
|
+
} catch (e) {
|
|
2749
|
+
firstException = firstException || e;
|
|
2750
|
+
}
|
|
2751
|
+
} else {
|
|
2752
|
+
// no timers remaining in the requested range: move the clock all the way to the end
|
|
2753
|
+
clock.now = tickTo;
|
|
2754
|
+
|
|
2755
|
+
// update nanos
|
|
2756
|
+
nanos = nanosTotal;
|
|
2757
|
+
}
|
|
2758
|
+
if (firstException) {
|
|
2759
|
+
throw firstException;
|
|
2760
|
+
}
|
|
2761
|
+
|
|
2762
|
+
if (isAsync) {
|
|
2763
|
+
resolve(clock.now);
|
|
2764
|
+
} else {
|
|
2765
|
+
return clock.now;
|
|
2766
|
+
}
|
|
2767
|
+
}
|
|
2768
|
+
|
|
2769
|
+
nextPromiseTick =
|
|
2770
|
+
isAsync &&
|
|
2771
|
+
function () {
|
|
2772
|
+
try {
|
|
2773
|
+
compensationCheck();
|
|
2774
|
+
postTimerCall();
|
|
2775
|
+
doTickInner();
|
|
2776
|
+
} catch (e) {
|
|
2777
|
+
reject(e);
|
|
2778
|
+
}
|
|
2779
|
+
};
|
|
2780
|
+
|
|
2781
|
+
compensationCheck = function () {
|
|
2782
|
+
// compensate for any setSystemTime() call during timer callback
|
|
2783
|
+
if (oldNow !== clock.now) {
|
|
2784
|
+
tickFrom += clock.now - oldNow;
|
|
2785
|
+
tickTo += clock.now - oldNow;
|
|
2786
|
+
previous += clock.now - oldNow;
|
|
2787
|
+
}
|
|
2788
|
+
};
|
|
2789
|
+
|
|
2790
|
+
postTimerCall = function () {
|
|
2791
|
+
timer = firstTimerInRange(clock, previous, tickTo);
|
|
2792
|
+
previous = tickFrom;
|
|
2793
|
+
};
|
|
2794
|
+
|
|
2795
|
+
return doTickInner();
|
|
2796
|
+
}
|
|
2797
|
+
|
|
2798
|
+
/**
|
|
2799
|
+
* @param {string|number} tickValue number of milliseconds or a human-readable value like "01:11:15"
|
|
2800
|
+
* @returns {number} will return the new `now` value
|
|
2801
|
+
*/
|
|
2802
|
+
clock.tick = function tick(tickValue) {
|
|
2803
|
+
return doTick(tickValue, false);
|
|
2804
|
+
};
|
|
2805
|
+
|
|
2806
|
+
if (typeof _global.Promise !== "undefined") {
|
|
2807
|
+
/**
|
|
2808
|
+
* @param {string|number} tickValue number of milliseconds or a human-readable value like "01:11:15"
|
|
2809
|
+
* @returns {Promise}
|
|
2810
|
+
*/
|
|
2811
|
+
clock.tickAsync = function tickAsync(tickValue) {
|
|
2812
|
+
return new _global.Promise(function (resolve, reject) {
|
|
2813
|
+
originalSetTimeout(function () {
|
|
2814
|
+
try {
|
|
2815
|
+
doTick(tickValue, true, resolve, reject);
|
|
2816
|
+
} catch (e) {
|
|
2817
|
+
reject(e);
|
|
2818
|
+
}
|
|
2819
|
+
});
|
|
2820
|
+
});
|
|
2821
|
+
};
|
|
2822
|
+
}
|
|
2823
|
+
|
|
2824
|
+
clock.next = function next() {
|
|
2825
|
+
runJobs(clock);
|
|
2826
|
+
const timer = firstTimer(clock);
|
|
2827
|
+
if (!timer) {
|
|
2828
|
+
return clock.now;
|
|
2829
|
+
}
|
|
2830
|
+
|
|
2831
|
+
clock.duringTick = true;
|
|
2832
|
+
try {
|
|
2833
|
+
clock.now = timer.callAt;
|
|
2834
|
+
callTimer(clock, timer);
|
|
2835
|
+
runJobs(clock);
|
|
2836
|
+
return clock.now;
|
|
2837
|
+
} finally {
|
|
2838
|
+
clock.duringTick = false;
|
|
2839
|
+
}
|
|
2840
|
+
};
|
|
2841
|
+
|
|
2842
|
+
if (typeof _global.Promise !== "undefined") {
|
|
2843
|
+
clock.nextAsync = function nextAsync() {
|
|
2844
|
+
return new _global.Promise(function (resolve, reject) {
|
|
2845
|
+
originalSetTimeout(function () {
|
|
2846
|
+
try {
|
|
2847
|
+
const timer = firstTimer(clock);
|
|
2848
|
+
if (!timer) {
|
|
2849
|
+
resolve(clock.now);
|
|
2850
|
+
return;
|
|
2851
|
+
}
|
|
2852
|
+
|
|
2853
|
+
let err;
|
|
2854
|
+
clock.duringTick = true;
|
|
2855
|
+
clock.now = timer.callAt;
|
|
2856
|
+
try {
|
|
2857
|
+
callTimer(clock, timer);
|
|
2858
|
+
} catch (e) {
|
|
2859
|
+
err = e;
|
|
2860
|
+
}
|
|
2861
|
+
clock.duringTick = false;
|
|
2862
|
+
|
|
2863
|
+
originalSetTimeout(function () {
|
|
2864
|
+
if (err) {
|
|
2865
|
+
reject(err);
|
|
2866
|
+
} else {
|
|
2867
|
+
resolve(clock.now);
|
|
2868
|
+
}
|
|
2869
|
+
});
|
|
2870
|
+
} catch (e) {
|
|
2871
|
+
reject(e);
|
|
2872
|
+
}
|
|
2873
|
+
});
|
|
2874
|
+
});
|
|
2875
|
+
};
|
|
2876
|
+
}
|
|
2877
|
+
|
|
2878
|
+
clock.runAll = function runAll() {
|
|
2879
|
+
let numTimers, i;
|
|
2880
|
+
runJobs(clock);
|
|
2881
|
+
for (i = 0; i < clock.loopLimit; i++) {
|
|
2882
|
+
if (!clock.timers) {
|
|
2883
|
+
resetIsNearInfiniteLimit();
|
|
2884
|
+
return clock.now;
|
|
2885
|
+
}
|
|
2886
|
+
|
|
2887
|
+
numTimers = Object.keys(clock.timers).length;
|
|
2888
|
+
if (numTimers === 0) {
|
|
2889
|
+
resetIsNearInfiniteLimit();
|
|
2890
|
+
return clock.now;
|
|
2891
|
+
}
|
|
2892
|
+
|
|
2893
|
+
clock.next();
|
|
2894
|
+
checkIsNearInfiniteLimit(clock, i);
|
|
2895
|
+
}
|
|
2896
|
+
|
|
2897
|
+
const excessJob = firstTimer(clock);
|
|
2898
|
+
throw getInfiniteLoopError(clock, excessJob);
|
|
2899
|
+
};
|
|
2900
|
+
|
|
2901
|
+
clock.runToFrame = function runToFrame() {
|
|
2902
|
+
return clock.tick(getTimeToNextFrame());
|
|
2903
|
+
};
|
|
2904
|
+
|
|
2905
|
+
if (typeof _global.Promise !== "undefined") {
|
|
2906
|
+
clock.runAllAsync = function runAllAsync() {
|
|
2907
|
+
return new _global.Promise(function (resolve, reject) {
|
|
2908
|
+
let i = 0;
|
|
2909
|
+
/**
|
|
2910
|
+
*
|
|
2911
|
+
*/
|
|
2912
|
+
function doRun() {
|
|
2913
|
+
originalSetTimeout(function () {
|
|
2914
|
+
try {
|
|
2915
|
+
runJobs(clock);
|
|
2916
|
+
|
|
2917
|
+
let numTimers;
|
|
2918
|
+
if (i < clock.loopLimit) {
|
|
2919
|
+
if (!clock.timers) {
|
|
2920
|
+
resetIsNearInfiniteLimit();
|
|
2921
|
+
resolve(clock.now);
|
|
2922
|
+
return;
|
|
2923
|
+
}
|
|
2924
|
+
|
|
2925
|
+
numTimers = Object.keys(
|
|
2926
|
+
clock.timers,
|
|
2927
|
+
).length;
|
|
2928
|
+
if (numTimers === 0) {
|
|
2929
|
+
resetIsNearInfiniteLimit();
|
|
2930
|
+
resolve(clock.now);
|
|
2931
|
+
return;
|
|
2932
|
+
}
|
|
2933
|
+
|
|
2934
|
+
clock.next();
|
|
2935
|
+
|
|
2936
|
+
i++;
|
|
2937
|
+
|
|
2938
|
+
doRun();
|
|
2939
|
+
checkIsNearInfiniteLimit(clock, i);
|
|
2940
|
+
return;
|
|
2941
|
+
}
|
|
2942
|
+
|
|
2943
|
+
const excessJob = firstTimer(clock);
|
|
2944
|
+
reject(getInfiniteLoopError(clock, excessJob));
|
|
2945
|
+
} catch (e) {
|
|
2946
|
+
reject(e);
|
|
2947
|
+
}
|
|
2948
|
+
});
|
|
2949
|
+
}
|
|
2950
|
+
doRun();
|
|
2951
|
+
});
|
|
2952
|
+
};
|
|
2953
|
+
}
|
|
2954
|
+
|
|
2955
|
+
clock.runToLast = function runToLast() {
|
|
2956
|
+
const timer = lastTimer(clock);
|
|
2957
|
+
if (!timer) {
|
|
2958
|
+
runJobs(clock);
|
|
2959
|
+
return clock.now;
|
|
2960
|
+
}
|
|
2961
|
+
|
|
2962
|
+
return clock.tick(timer.callAt - clock.now);
|
|
2963
|
+
};
|
|
2964
|
+
|
|
2965
|
+
if (typeof _global.Promise !== "undefined") {
|
|
2966
|
+
clock.runToLastAsync = function runToLastAsync() {
|
|
2967
|
+
return new _global.Promise(function (resolve, reject) {
|
|
2968
|
+
originalSetTimeout(function () {
|
|
2969
|
+
try {
|
|
2970
|
+
const timer = lastTimer(clock);
|
|
2971
|
+
if (!timer) {
|
|
2972
|
+
runJobs(clock);
|
|
2973
|
+
resolve(clock.now);
|
|
2974
|
+
}
|
|
2975
|
+
|
|
2976
|
+
resolve(clock.tickAsync(timer.callAt - clock.now));
|
|
2977
|
+
} catch (e) {
|
|
2978
|
+
reject(e);
|
|
2979
|
+
}
|
|
2980
|
+
});
|
|
2981
|
+
});
|
|
2982
|
+
};
|
|
2983
|
+
}
|
|
2984
|
+
|
|
2985
|
+
clock.reset = function reset() {
|
|
2986
|
+
nanos = 0;
|
|
2987
|
+
clock.timers = {};
|
|
2988
|
+
clock.jobs = [];
|
|
2989
|
+
clock.now = start;
|
|
2990
|
+
};
|
|
2991
|
+
|
|
2992
|
+
clock.setSystemTime = function setSystemTime(systemTime) {
|
|
2993
|
+
// determine time difference
|
|
2994
|
+
const newNow = getEpoch(systemTime);
|
|
2995
|
+
const difference = newNow - clock.now;
|
|
2996
|
+
let id, timer;
|
|
2997
|
+
|
|
2998
|
+
adjustedSystemTime[0] = adjustedSystemTime[0] + difference;
|
|
2999
|
+
adjustedSystemTime[1] = adjustedSystemTime[1] + nanos;
|
|
3000
|
+
// update 'system clock'
|
|
3001
|
+
clock.now = newNow;
|
|
3002
|
+
nanos = 0;
|
|
3003
|
+
|
|
3004
|
+
// update timers and intervals to keep them stable
|
|
3005
|
+
for (id in clock.timers) {
|
|
3006
|
+
if (clock.timers.hasOwnProperty(id)) {
|
|
3007
|
+
timer = clock.timers[id];
|
|
3008
|
+
timer.createdAt += difference;
|
|
3009
|
+
timer.callAt += difference;
|
|
3010
|
+
}
|
|
3011
|
+
}
|
|
3012
|
+
};
|
|
3013
|
+
|
|
3014
|
+
/**
|
|
3015
|
+
* @param {string|number} tickValue number of milliseconds or a human-readable value like "01:11:15"
|
|
3016
|
+
* @returns {number} will return the new `now` value
|
|
3017
|
+
*/
|
|
3018
|
+
clock.jump = function jump(tickValue) {
|
|
3019
|
+
const msFloat =
|
|
3020
|
+
typeof tickValue === "number"
|
|
3021
|
+
? tickValue
|
|
3022
|
+
: parseTime(tickValue);
|
|
3023
|
+
const ms = Math.floor(msFloat);
|
|
3024
|
+
|
|
3025
|
+
for (const timer of Object.values(clock.timers)) {
|
|
3026
|
+
if (clock.now + ms > timer.callAt) {
|
|
3027
|
+
timer.callAt = clock.now + ms;
|
|
3028
|
+
}
|
|
3029
|
+
}
|
|
3030
|
+
clock.tick(ms);
|
|
3031
|
+
};
|
|
3032
|
+
|
|
3033
|
+
if (isPresent.performance) {
|
|
3034
|
+
clock.performance = Object.create(null);
|
|
3035
|
+
clock.performance.now = fakePerformanceNow;
|
|
3036
|
+
}
|
|
3037
|
+
|
|
3038
|
+
if (isPresent.hrtime) {
|
|
3039
|
+
clock.hrtime = hrtime;
|
|
3040
|
+
}
|
|
3041
|
+
|
|
3042
|
+
return clock;
|
|
3043
|
+
}
|
|
3044
|
+
|
|
3045
|
+
/* eslint-disable complexity */
|
|
3046
|
+
|
|
3047
|
+
/**
|
|
3048
|
+
* @param {Config=} [config] Optional config
|
|
3049
|
+
* @returns {Clock}
|
|
3050
|
+
*/
|
|
3051
|
+
function install(config) {
|
|
3052
|
+
if (
|
|
3053
|
+
arguments.length > 1 ||
|
|
3054
|
+
config instanceof Date ||
|
|
3055
|
+
Array.isArray(config) ||
|
|
3056
|
+
typeof config === "number"
|
|
3057
|
+
) {
|
|
3058
|
+
throw new TypeError(
|
|
3059
|
+
`FakeTimers.install called with ${String(
|
|
3060
|
+
config,
|
|
3061
|
+
)} install requires an object parameter`,
|
|
3062
|
+
);
|
|
3063
|
+
}
|
|
3064
|
+
|
|
3065
|
+
if (_global.Date.isFake === true) {
|
|
3066
|
+
// Timers are already faked; this is a problem.
|
|
3067
|
+
// Make the user reset timers before continuing.
|
|
3068
|
+
throw new TypeError(
|
|
3069
|
+
"Can't install fake timers twice on the same global object.",
|
|
3070
|
+
);
|
|
3071
|
+
}
|
|
3072
|
+
|
|
3073
|
+
// eslint-disable-next-line no-param-reassign
|
|
3074
|
+
config = typeof config !== "undefined" ? config : {};
|
|
3075
|
+
config.shouldAdvanceTime = config.shouldAdvanceTime || false;
|
|
3076
|
+
config.advanceTimeDelta = config.advanceTimeDelta || 20;
|
|
3077
|
+
config.shouldClearNativeTimers =
|
|
3078
|
+
config.shouldClearNativeTimers || false;
|
|
3079
|
+
|
|
3080
|
+
if (config.target) {
|
|
3081
|
+
throw new TypeError(
|
|
3082
|
+
"config.target is no longer supported. Use `withGlobal(target)` instead.",
|
|
3083
|
+
);
|
|
3084
|
+
}
|
|
3085
|
+
|
|
3086
|
+
/**
|
|
3087
|
+
* @param {string} timer/object the name of the thing that is not present
|
|
3088
|
+
* @param timer
|
|
3089
|
+
*/
|
|
3090
|
+
function handleMissingTimer(timer) {
|
|
3091
|
+
if (config.ignoreMissingTimers) {
|
|
3092
|
+
return;
|
|
3093
|
+
}
|
|
3094
|
+
|
|
3095
|
+
throw new ReferenceError(
|
|
3096
|
+
`non-existent timers and/or objects cannot be faked: '${timer}'`,
|
|
3097
|
+
);
|
|
3098
|
+
}
|
|
3099
|
+
|
|
3100
|
+
let i, l;
|
|
3101
|
+
const clock = createClock(config.now, config.loopLimit);
|
|
3102
|
+
clock.shouldClearNativeTimers = config.shouldClearNativeTimers;
|
|
3103
|
+
|
|
3104
|
+
clock.uninstall = function () {
|
|
3105
|
+
return uninstall(clock, config);
|
|
3106
|
+
};
|
|
3107
|
+
|
|
3108
|
+
clock.abortListenerMap = new Map();
|
|
3109
|
+
|
|
3110
|
+
clock.methods = config.toFake || [];
|
|
3111
|
+
|
|
3112
|
+
if (clock.methods.length === 0) {
|
|
3113
|
+
clock.methods = Object.keys(timers);
|
|
3114
|
+
}
|
|
3115
|
+
|
|
3116
|
+
if (config.shouldAdvanceTime === true) {
|
|
3117
|
+
const intervalTick = doIntervalTick.bind(
|
|
3118
|
+
null,
|
|
3119
|
+
clock,
|
|
3120
|
+
config.advanceTimeDelta,
|
|
3121
|
+
);
|
|
3122
|
+
const intervalId = _global.setInterval(
|
|
3123
|
+
intervalTick,
|
|
3124
|
+
config.advanceTimeDelta,
|
|
3125
|
+
);
|
|
3126
|
+
clock.attachedInterval = intervalId;
|
|
3127
|
+
}
|
|
3128
|
+
|
|
3129
|
+
if (clock.methods.includes("performance")) {
|
|
3130
|
+
const proto = (() => {
|
|
3131
|
+
if (hasPerformanceConstructorPrototype) {
|
|
3132
|
+
return _global.performance.constructor.prototype;
|
|
3133
|
+
}
|
|
3134
|
+
if (hasPerformancePrototype) {
|
|
3135
|
+
return _global.Performance.prototype;
|
|
3136
|
+
}
|
|
3137
|
+
})();
|
|
3138
|
+
if (proto) {
|
|
3139
|
+
Object.getOwnPropertyNames(proto).forEach(function (name) {
|
|
3140
|
+
if (name !== "now") {
|
|
3141
|
+
clock.performance[name] =
|
|
3142
|
+
name.indexOf("getEntries") === 0
|
|
3143
|
+
? NOOP_ARRAY
|
|
3144
|
+
: NOOP;
|
|
3145
|
+
}
|
|
3146
|
+
});
|
|
3147
|
+
// ensure `mark` returns a value that is valid
|
|
3148
|
+
clock.performance.mark = (name) =>
|
|
3149
|
+
new FakePerformanceEntry(name, "mark", 0, 0);
|
|
3150
|
+
clock.performance.measure = (name) =>
|
|
3151
|
+
new FakePerformanceEntry(name, "measure", 0, 100);
|
|
3152
|
+
// `timeOrigin` should return the time of when the Window session started
|
|
3153
|
+
// (or the Worker was installed)
|
|
3154
|
+
clock.performance.timeOrigin = getEpoch(config.now);
|
|
3155
|
+
} else if ((config.toFake || []).includes("performance")) {
|
|
3156
|
+
return handleMissingTimer("performance");
|
|
3157
|
+
}
|
|
3158
|
+
}
|
|
3159
|
+
if (_global === globalObject && timersModule) {
|
|
3160
|
+
clock.timersModuleMethods = [];
|
|
3161
|
+
}
|
|
3162
|
+
if (_global === globalObject && timersPromisesModule) {
|
|
3163
|
+
clock.timersPromisesModuleMethods = [];
|
|
3164
|
+
}
|
|
3165
|
+
for (i = 0, l = clock.methods.length; i < l; i++) {
|
|
3166
|
+
const nameOfMethodToReplace = clock.methods[i];
|
|
3167
|
+
|
|
3168
|
+
if (!isPresent[nameOfMethodToReplace]) {
|
|
3169
|
+
handleMissingTimer(nameOfMethodToReplace);
|
|
3170
|
+
// eslint-disable-next-line
|
|
3171
|
+
continue;
|
|
3172
|
+
}
|
|
3173
|
+
|
|
3174
|
+
if (nameOfMethodToReplace === "hrtime") {
|
|
3175
|
+
if (
|
|
3176
|
+
_global.process &&
|
|
3177
|
+
typeof _global.process.hrtime === "function"
|
|
3178
|
+
) {
|
|
3179
|
+
hijackMethod(_global.process, nameOfMethodToReplace, clock);
|
|
3180
|
+
}
|
|
3181
|
+
} else if (nameOfMethodToReplace === "nextTick") {
|
|
3182
|
+
if (
|
|
3183
|
+
_global.process &&
|
|
3184
|
+
typeof _global.process.nextTick === "function"
|
|
3185
|
+
) {
|
|
3186
|
+
hijackMethod(_global.process, nameOfMethodToReplace, clock);
|
|
3187
|
+
}
|
|
3188
|
+
} else {
|
|
3189
|
+
hijackMethod(_global, nameOfMethodToReplace, clock);
|
|
3190
|
+
}
|
|
3191
|
+
if (
|
|
3192
|
+
clock.timersModuleMethods !== undefined &&
|
|
3193
|
+
timersModule[nameOfMethodToReplace]
|
|
3194
|
+
) {
|
|
3195
|
+
const original = timersModule[nameOfMethodToReplace];
|
|
3196
|
+
clock.timersModuleMethods.push({
|
|
3197
|
+
methodName: nameOfMethodToReplace,
|
|
3198
|
+
original: original,
|
|
3199
|
+
});
|
|
3200
|
+
timersModule[nameOfMethodToReplace] =
|
|
3201
|
+
_global[nameOfMethodToReplace];
|
|
3202
|
+
}
|
|
3203
|
+
if (clock.timersPromisesModuleMethods !== undefined) {
|
|
3204
|
+
if (nameOfMethodToReplace === "setTimeout") {
|
|
3205
|
+
clock.timersPromisesModuleMethods.push({
|
|
3206
|
+
methodName: "setTimeout",
|
|
3207
|
+
original: timersPromisesModule.setTimeout,
|
|
3208
|
+
});
|
|
3209
|
+
|
|
3210
|
+
timersPromisesModule.setTimeout = (
|
|
3211
|
+
delay,
|
|
3212
|
+
value,
|
|
3213
|
+
options = {},
|
|
3214
|
+
) =>
|
|
3215
|
+
new Promise((resolve, reject) => {
|
|
3216
|
+
const abort = () => {
|
|
3217
|
+
options.signal.removeEventListener(
|
|
3218
|
+
"abort",
|
|
3219
|
+
abort,
|
|
3220
|
+
);
|
|
3221
|
+
clock.abortListenerMap.delete(abort);
|
|
3222
|
+
|
|
3223
|
+
// This is safe, there is no code path that leads to this function
|
|
3224
|
+
// being invoked before handle has been assigned.
|
|
3225
|
+
// eslint-disable-next-line no-use-before-define
|
|
3226
|
+
clock.clearTimeout(handle);
|
|
3227
|
+
reject(options.signal.reason);
|
|
3228
|
+
};
|
|
3229
|
+
|
|
3230
|
+
const handle = clock.setTimeout(() => {
|
|
3231
|
+
if (options.signal) {
|
|
3232
|
+
options.signal.removeEventListener(
|
|
3233
|
+
"abort",
|
|
3234
|
+
abort,
|
|
3235
|
+
);
|
|
3236
|
+
clock.abortListenerMap.delete(abort);
|
|
3237
|
+
}
|
|
3238
|
+
|
|
3239
|
+
resolve(value);
|
|
3240
|
+
}, delay);
|
|
3241
|
+
|
|
3242
|
+
if (options.signal) {
|
|
3243
|
+
if (options.signal.aborted) {
|
|
3244
|
+
abort();
|
|
3245
|
+
} else {
|
|
3246
|
+
options.signal.addEventListener(
|
|
3247
|
+
"abort",
|
|
3248
|
+
abort,
|
|
3249
|
+
);
|
|
3250
|
+
clock.abortListenerMap.set(
|
|
3251
|
+
abort,
|
|
3252
|
+
options.signal,
|
|
3253
|
+
);
|
|
3254
|
+
}
|
|
3255
|
+
}
|
|
3256
|
+
});
|
|
3257
|
+
} else if (nameOfMethodToReplace === "setImmediate") {
|
|
3258
|
+
clock.timersPromisesModuleMethods.push({
|
|
3259
|
+
methodName: "setImmediate",
|
|
3260
|
+
original: timersPromisesModule.setImmediate,
|
|
3261
|
+
});
|
|
3262
|
+
|
|
3263
|
+
timersPromisesModule.setImmediate = (value, options = {}) =>
|
|
3264
|
+
new Promise((resolve, reject) => {
|
|
3265
|
+
const abort = () => {
|
|
3266
|
+
options.signal.removeEventListener(
|
|
3267
|
+
"abort",
|
|
3268
|
+
abort,
|
|
3269
|
+
);
|
|
3270
|
+
clock.abortListenerMap.delete(abort);
|
|
3271
|
+
|
|
3272
|
+
// This is safe, there is no code path that leads to this function
|
|
3273
|
+
// being invoked before handle has been assigned.
|
|
3274
|
+
// eslint-disable-next-line no-use-before-define
|
|
3275
|
+
clock.clearImmediate(handle);
|
|
3276
|
+
reject(options.signal.reason);
|
|
3277
|
+
};
|
|
3278
|
+
|
|
3279
|
+
const handle = clock.setImmediate(() => {
|
|
3280
|
+
if (options.signal) {
|
|
3281
|
+
options.signal.removeEventListener(
|
|
3282
|
+
"abort",
|
|
3283
|
+
abort,
|
|
3284
|
+
);
|
|
3285
|
+
clock.abortListenerMap.delete(abort);
|
|
3286
|
+
}
|
|
3287
|
+
|
|
3288
|
+
resolve(value);
|
|
3289
|
+
});
|
|
3290
|
+
|
|
3291
|
+
if (options.signal) {
|
|
3292
|
+
if (options.signal.aborted) {
|
|
3293
|
+
abort();
|
|
3294
|
+
} else {
|
|
3295
|
+
options.signal.addEventListener(
|
|
3296
|
+
"abort",
|
|
3297
|
+
abort,
|
|
3298
|
+
);
|
|
3299
|
+
clock.abortListenerMap.set(
|
|
3300
|
+
abort,
|
|
3301
|
+
options.signal,
|
|
3302
|
+
);
|
|
3303
|
+
}
|
|
3304
|
+
}
|
|
3305
|
+
});
|
|
3306
|
+
} else if (nameOfMethodToReplace === "setInterval") {
|
|
3307
|
+
clock.timersPromisesModuleMethods.push({
|
|
3308
|
+
methodName: "setInterval",
|
|
3309
|
+
original: timersPromisesModule.setInterval,
|
|
3310
|
+
});
|
|
3311
|
+
|
|
3312
|
+
timersPromisesModule.setInterval = (
|
|
3313
|
+
delay,
|
|
3314
|
+
value,
|
|
3315
|
+
options = {},
|
|
3316
|
+
) => ({
|
|
3317
|
+
[Symbol.asyncIterator]: () => {
|
|
3318
|
+
const createResolvable = () => {
|
|
3319
|
+
let resolve, reject;
|
|
3320
|
+
const promise = new Promise((res, rej) => {
|
|
3321
|
+
resolve = res;
|
|
3322
|
+
reject = rej;
|
|
3323
|
+
});
|
|
3324
|
+
promise.resolve = resolve;
|
|
3325
|
+
promise.reject = reject;
|
|
3326
|
+
return promise;
|
|
3327
|
+
};
|
|
3328
|
+
|
|
3329
|
+
let done = false;
|
|
3330
|
+
let hasThrown = false;
|
|
3331
|
+
let returnCall;
|
|
3332
|
+
let nextAvailable = 0;
|
|
3333
|
+
const nextQueue = [];
|
|
3334
|
+
|
|
3335
|
+
const handle = clock.setInterval(() => {
|
|
3336
|
+
if (nextQueue.length > 0) {
|
|
3337
|
+
nextQueue.shift().resolve();
|
|
3338
|
+
} else {
|
|
3339
|
+
nextAvailable++;
|
|
3340
|
+
}
|
|
3341
|
+
}, delay);
|
|
3342
|
+
|
|
3343
|
+
const abort = () => {
|
|
3344
|
+
options.signal.removeEventListener(
|
|
3345
|
+
"abort",
|
|
3346
|
+
abort,
|
|
3347
|
+
);
|
|
3348
|
+
clock.abortListenerMap.delete(abort);
|
|
3349
|
+
|
|
3350
|
+
clock.clearInterval(handle);
|
|
3351
|
+
done = true;
|
|
3352
|
+
for (const resolvable of nextQueue) {
|
|
3353
|
+
resolvable.resolve();
|
|
3354
|
+
}
|
|
3355
|
+
};
|
|
3356
|
+
|
|
3357
|
+
if (options.signal) {
|
|
3358
|
+
if (options.signal.aborted) {
|
|
3359
|
+
done = true;
|
|
3360
|
+
} else {
|
|
3361
|
+
options.signal.addEventListener(
|
|
3362
|
+
"abort",
|
|
3363
|
+
abort,
|
|
3364
|
+
);
|
|
3365
|
+
clock.abortListenerMap.set(
|
|
3366
|
+
abort,
|
|
3367
|
+
options.signal,
|
|
3368
|
+
);
|
|
3369
|
+
}
|
|
3370
|
+
}
|
|
3371
|
+
|
|
3372
|
+
return {
|
|
3373
|
+
next: async () => {
|
|
3374
|
+
if (options.signal?.aborted && !hasThrown) {
|
|
3375
|
+
hasThrown = true;
|
|
3376
|
+
throw options.signal.reason;
|
|
3377
|
+
}
|
|
3378
|
+
|
|
3379
|
+
if (done) {
|
|
3380
|
+
return { done: true, value: undefined };
|
|
3381
|
+
}
|
|
3382
|
+
|
|
3383
|
+
if (nextAvailable > 0) {
|
|
3384
|
+
nextAvailable--;
|
|
3385
|
+
return { done: false, value: value };
|
|
3386
|
+
}
|
|
3387
|
+
|
|
3388
|
+
const resolvable = createResolvable();
|
|
3389
|
+
nextQueue.push(resolvable);
|
|
3390
|
+
|
|
3391
|
+
await resolvable;
|
|
3392
|
+
|
|
3393
|
+
if (returnCall && nextQueue.length === 0) {
|
|
3394
|
+
returnCall.resolve();
|
|
3395
|
+
}
|
|
3396
|
+
|
|
3397
|
+
if (options.signal?.aborted && !hasThrown) {
|
|
3398
|
+
hasThrown = true;
|
|
3399
|
+
throw options.signal.reason;
|
|
3400
|
+
}
|
|
3401
|
+
|
|
3402
|
+
if (done) {
|
|
3403
|
+
return { done: true, value: undefined };
|
|
3404
|
+
}
|
|
3405
|
+
|
|
3406
|
+
return { done: false, value: value };
|
|
3407
|
+
},
|
|
3408
|
+
return: async () => {
|
|
3409
|
+
if (done) {
|
|
3410
|
+
return { done: true, value: undefined };
|
|
3411
|
+
}
|
|
3412
|
+
|
|
3413
|
+
if (nextQueue.length > 0) {
|
|
3414
|
+
returnCall = createResolvable();
|
|
3415
|
+
await returnCall;
|
|
3416
|
+
}
|
|
3417
|
+
|
|
3418
|
+
clock.clearInterval(handle);
|
|
3419
|
+
done = true;
|
|
3420
|
+
|
|
3421
|
+
if (options.signal) {
|
|
3422
|
+
options.signal.removeEventListener(
|
|
3423
|
+
"abort",
|
|
3424
|
+
abort,
|
|
3425
|
+
);
|
|
3426
|
+
clock.abortListenerMap.delete(abort);
|
|
3427
|
+
}
|
|
3428
|
+
|
|
3429
|
+
return { done: true, value: undefined };
|
|
3430
|
+
},
|
|
3431
|
+
};
|
|
3432
|
+
},
|
|
3433
|
+
});
|
|
3434
|
+
}
|
|
3435
|
+
}
|
|
3436
|
+
}
|
|
3437
|
+
|
|
3438
|
+
return clock;
|
|
3439
|
+
}
|
|
3440
|
+
|
|
3441
|
+
/* eslint-enable complexity */
|
|
3442
|
+
|
|
3443
|
+
return {
|
|
3444
|
+
timers: timers,
|
|
3445
|
+
createClock: createClock,
|
|
3446
|
+
install: install,
|
|
3447
|
+
withGlobal: withGlobal,
|
|
3448
|
+
};
|
|
3449
|
+
}
|
|
3450
|
+
|
|
3451
|
+
/**
|
|
3452
|
+
* @typedef {object} FakeTimers
|
|
3453
|
+
* @property {Timers} timers
|
|
3454
|
+
* @property {createClock} createClock
|
|
3455
|
+
* @property {Function} install
|
|
3456
|
+
* @property {withGlobal} withGlobal
|
|
3457
|
+
*/
|
|
3458
|
+
|
|
3459
|
+
/* eslint-enable complexity */
|
|
3460
|
+
|
|
3461
|
+
/** @type {FakeTimers} */
|
|
3462
|
+
const defaultImplementation = withGlobal(globalObject);
|
|
3463
|
+
|
|
3464
|
+
fakeTimersSrc.timers = defaultImplementation.timers;
|
|
3465
|
+
fakeTimersSrc.createClock = defaultImplementation.createClock;
|
|
3466
|
+
fakeTimersSrc.install = defaultImplementation.install;
|
|
3467
|
+
fakeTimersSrc.withGlobal = withGlobal;
|
|
3468
|
+
return fakeTimersSrc;
|
|
3469
|
+
}
|
|
3470
|
+
|
|
3471
|
+
var fakeTimersSrcExports = requireFakeTimersSrc();
|
|
3472
|
+
|
|
3473
|
+
class FakeTimers {
|
|
3474
|
+
_global;
|
|
3475
|
+
_clock;
|
|
3476
|
+
// | _fakingTime | _fakingDate |
|
|
3477
|
+
// +-------------+-------------+
|
|
3478
|
+
// | false | falsy | initial
|
|
3479
|
+
// | false | truthy | vi.setSystemTime called first (for mocking only Date without fake timers)
|
|
3480
|
+
// | true | falsy | vi.useFakeTimers called first
|
|
3481
|
+
// | true | truthy | unreachable
|
|
3482
|
+
_fakingTime;
|
|
3483
|
+
_fakingDate;
|
|
3484
|
+
_fakeTimers;
|
|
3485
|
+
_userConfig;
|
|
3486
|
+
_now = RealDate.now;
|
|
3487
|
+
constructor({ global, config }) {
|
|
3488
|
+
this._userConfig = config;
|
|
3489
|
+
this._fakingDate = null;
|
|
3490
|
+
this._fakingTime = false;
|
|
3491
|
+
this._fakeTimers = fakeTimersSrcExports.withGlobal(global);
|
|
3492
|
+
this._global = global;
|
|
3493
|
+
}
|
|
3494
|
+
clearAllTimers() {
|
|
3495
|
+
if (this._fakingTime) this._clock.reset();
|
|
3496
|
+
}
|
|
3497
|
+
dispose() {
|
|
3498
|
+
this.useRealTimers();
|
|
3499
|
+
}
|
|
3500
|
+
runAllTimers() {
|
|
3501
|
+
if (this._checkFakeTimers()) this._clock.runAll();
|
|
3502
|
+
}
|
|
3503
|
+
async runAllTimersAsync() {
|
|
3504
|
+
if (this._checkFakeTimers()) await this._clock.runAllAsync();
|
|
3505
|
+
}
|
|
3506
|
+
runOnlyPendingTimers() {
|
|
3507
|
+
if (this._checkFakeTimers()) this._clock.runToLast();
|
|
3508
|
+
}
|
|
3509
|
+
async runOnlyPendingTimersAsync() {
|
|
3510
|
+
if (this._checkFakeTimers()) await this._clock.runToLastAsync();
|
|
3511
|
+
}
|
|
3512
|
+
advanceTimersToNextTimer(steps = 1) {
|
|
3513
|
+
if (this._checkFakeTimers()) for (let i = steps; i > 0; i--) {
|
|
3514
|
+
this._clock.next();
|
|
3515
|
+
// Fire all timers at this point: https://github.com/sinonjs/fake-timers/issues/250
|
|
3516
|
+
this._clock.tick(0);
|
|
3517
|
+
if (this._clock.countTimers() === 0) break;
|
|
3518
|
+
}
|
|
3519
|
+
}
|
|
3520
|
+
async advanceTimersToNextTimerAsync(steps = 1) {
|
|
3521
|
+
if (this._checkFakeTimers()) for (let i = steps; i > 0; i--) {
|
|
3522
|
+
await this._clock.nextAsync();
|
|
3523
|
+
// Fire all timers at this point: https://github.com/sinonjs/fake-timers/issues/250
|
|
3524
|
+
this._clock.tick(0);
|
|
3525
|
+
if (this._clock.countTimers() === 0) break;
|
|
3526
|
+
}
|
|
3527
|
+
}
|
|
3528
|
+
advanceTimersByTime(msToRun) {
|
|
3529
|
+
if (this._checkFakeTimers()) this._clock.tick(msToRun);
|
|
3530
|
+
}
|
|
3531
|
+
async advanceTimersByTimeAsync(msToRun) {
|
|
3532
|
+
if (this._checkFakeTimers()) await this._clock.tickAsync(msToRun);
|
|
3533
|
+
}
|
|
3534
|
+
advanceTimersToNextFrame() {
|
|
3535
|
+
if (this._checkFakeTimers()) this._clock.runToFrame();
|
|
3536
|
+
}
|
|
3537
|
+
runAllTicks() {
|
|
3538
|
+
if (this._checkFakeTimers())
|
|
3539
|
+
// @ts-expect-error method not exposed
|
|
3540
|
+
this._clock.runMicrotasks();
|
|
3541
|
+
}
|
|
3542
|
+
useRealTimers() {
|
|
3543
|
+
if (this._fakingDate) {
|
|
3544
|
+
resetDate();
|
|
3545
|
+
this._fakingDate = null;
|
|
3546
|
+
}
|
|
3547
|
+
if (this._fakingTime) {
|
|
3548
|
+
this._clock.uninstall();
|
|
3549
|
+
this._fakingTime = false;
|
|
3550
|
+
}
|
|
3551
|
+
}
|
|
3552
|
+
useFakeTimers() {
|
|
3553
|
+
const fakeDate = this._fakingDate || Date.now();
|
|
3554
|
+
if (this._fakingDate) {
|
|
3555
|
+
resetDate();
|
|
3556
|
+
this._fakingDate = null;
|
|
3557
|
+
}
|
|
3558
|
+
if (this._fakingTime) this._clock.uninstall();
|
|
3559
|
+
const toFake = Object.keys(this._fakeTimers.timers).filter((timer) => timer !== "nextTick" && timer !== "queueMicrotask");
|
|
3560
|
+
if (this._userConfig?.toFake?.includes("nextTick") && isChildProcess()) throw new Error("process.nextTick cannot be mocked inside child_process");
|
|
3561
|
+
this._clock = this._fakeTimers.install({
|
|
3562
|
+
now: fakeDate,
|
|
3563
|
+
...this._userConfig,
|
|
3564
|
+
toFake: this._userConfig?.toFake || toFake,
|
|
3565
|
+
ignoreMissingTimers: true
|
|
3566
|
+
});
|
|
3567
|
+
this._fakingTime = true;
|
|
3568
|
+
}
|
|
3569
|
+
reset() {
|
|
3570
|
+
if (this._checkFakeTimers()) {
|
|
3571
|
+
const { now } = this._clock;
|
|
3572
|
+
this._clock.reset();
|
|
3573
|
+
this._clock.setSystemTime(now);
|
|
3574
|
+
}
|
|
3575
|
+
}
|
|
3576
|
+
setSystemTime(now) {
|
|
3577
|
+
const date = typeof now === "undefined" || now instanceof Date ? now : new Date(now);
|
|
3578
|
+
if (this._fakingTime) this._clock.setSystemTime(date);
|
|
3579
|
+
else {
|
|
3580
|
+
this._fakingDate = date ?? new Date(this.getRealSystemTime());
|
|
3581
|
+
mockDate(this._fakingDate);
|
|
3582
|
+
}
|
|
3583
|
+
}
|
|
3584
|
+
getMockedSystemTime() {
|
|
3585
|
+
return this._fakingTime ? new Date(this._clock.now) : this._fakingDate;
|
|
3586
|
+
}
|
|
3587
|
+
getRealSystemTime() {
|
|
3588
|
+
return this._now();
|
|
3589
|
+
}
|
|
3590
|
+
getTimerCount() {
|
|
3591
|
+
if (this._checkFakeTimers()) return this._clock.countTimers();
|
|
3592
|
+
return 0;
|
|
3593
|
+
}
|
|
3594
|
+
configure(config) {
|
|
3595
|
+
this._userConfig = config;
|
|
3596
|
+
}
|
|
3597
|
+
isFakeTimers() {
|
|
3598
|
+
return this._fakingTime;
|
|
3599
|
+
}
|
|
3600
|
+
_checkFakeTimers() {
|
|
3601
|
+
if (!this._fakingTime) throw new Error("A function to advance timers was called but the timers APIs are not mocked. Call `vi.useFakeTimers()` in the test file first.");
|
|
3602
|
+
return this._fakingTime;
|
|
3603
|
+
}
|
|
3604
|
+
}
|
|
3605
|
+
|
|
3606
|
+
function copyStackTrace(target, source) {
|
|
3607
|
+
if (source.stack !== void 0) target.stack = source.stack.replace(source.message, target.message);
|
|
3608
|
+
return target;
|
|
3609
|
+
}
|
|
3610
|
+
function waitFor(callback, options = {}) {
|
|
3611
|
+
const { setTimeout, setInterval, clearTimeout, clearInterval } = getSafeTimers();
|
|
3612
|
+
const { interval = 50, timeout = 1e3 } = typeof options === "number" ? { timeout: options } : options;
|
|
3613
|
+
const STACK_TRACE_ERROR = /* @__PURE__ */ new Error("STACK_TRACE_ERROR");
|
|
3614
|
+
return new Promise((resolve, reject) => {
|
|
3615
|
+
let lastError;
|
|
3616
|
+
let promiseStatus = "idle";
|
|
3617
|
+
let timeoutId;
|
|
3618
|
+
let intervalId;
|
|
3619
|
+
const onResolve = (result) => {
|
|
3620
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
3621
|
+
if (intervalId) clearInterval(intervalId);
|
|
3622
|
+
resolve(result);
|
|
3623
|
+
};
|
|
3624
|
+
const handleTimeout = () => {
|
|
3625
|
+
if (intervalId) clearInterval(intervalId);
|
|
3626
|
+
let error = lastError;
|
|
3627
|
+
if (!error) error = copyStackTrace(/* @__PURE__ */ new Error("Timed out in waitFor!"), STACK_TRACE_ERROR);
|
|
3628
|
+
reject(error);
|
|
3629
|
+
};
|
|
3630
|
+
const checkCallback = () => {
|
|
3631
|
+
if (vi.isFakeTimers()) vi.advanceTimersByTime(interval);
|
|
3632
|
+
if (promiseStatus === "pending") return;
|
|
3633
|
+
try {
|
|
3634
|
+
const result = callback();
|
|
3635
|
+
if (result !== null && typeof result === "object" && typeof result.then === "function") {
|
|
3636
|
+
const thenable = result;
|
|
3637
|
+
promiseStatus = "pending";
|
|
3638
|
+
thenable.then((resolvedValue) => {
|
|
3639
|
+
promiseStatus = "resolved";
|
|
3640
|
+
onResolve(resolvedValue);
|
|
3641
|
+
}, (rejectedValue) => {
|
|
3642
|
+
promiseStatus = "rejected";
|
|
3643
|
+
lastError = rejectedValue;
|
|
3644
|
+
});
|
|
3645
|
+
} else {
|
|
3646
|
+
onResolve(result);
|
|
3647
|
+
return true;
|
|
3648
|
+
}
|
|
3649
|
+
} catch (error) {
|
|
3650
|
+
lastError = error;
|
|
3651
|
+
}
|
|
3652
|
+
};
|
|
3653
|
+
if (checkCallback() === true) return;
|
|
3654
|
+
timeoutId = setTimeout(handleTimeout, timeout);
|
|
3655
|
+
intervalId = setInterval(checkCallback, interval);
|
|
3656
|
+
});
|
|
3657
|
+
}
|
|
3658
|
+
function waitUntil(callback, options = {}) {
|
|
3659
|
+
const { setTimeout, setInterval, clearTimeout, clearInterval } = getSafeTimers();
|
|
3660
|
+
const { interval = 50, timeout = 1e3 } = typeof options === "number" ? { timeout: options } : options;
|
|
3661
|
+
const STACK_TRACE_ERROR = /* @__PURE__ */ new Error("STACK_TRACE_ERROR");
|
|
3662
|
+
return new Promise((resolve, reject) => {
|
|
3663
|
+
let promiseStatus = "idle";
|
|
3664
|
+
let timeoutId;
|
|
3665
|
+
let intervalId;
|
|
3666
|
+
const onReject = (error) => {
|
|
3667
|
+
if (intervalId) clearInterval(intervalId);
|
|
3668
|
+
if (!error) error = copyStackTrace(/* @__PURE__ */ new Error("Timed out in waitUntil!"), STACK_TRACE_ERROR);
|
|
3669
|
+
reject(error);
|
|
3670
|
+
};
|
|
3671
|
+
const onResolve = (result) => {
|
|
3672
|
+
if (!result) return;
|
|
3673
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
3674
|
+
if (intervalId) clearInterval(intervalId);
|
|
3675
|
+
resolve(result);
|
|
3676
|
+
return true;
|
|
3677
|
+
};
|
|
3678
|
+
const checkCallback = () => {
|
|
3679
|
+
if (vi.isFakeTimers()) vi.advanceTimersByTime(interval);
|
|
3680
|
+
if (promiseStatus === "pending") return;
|
|
3681
|
+
try {
|
|
3682
|
+
const result = callback();
|
|
3683
|
+
if (result !== null && typeof result === "object" && typeof result.then === "function") {
|
|
3684
|
+
const thenable = result;
|
|
3685
|
+
promiseStatus = "pending";
|
|
3686
|
+
thenable.then((resolvedValue) => {
|
|
3687
|
+
promiseStatus = "resolved";
|
|
3688
|
+
onResolve(resolvedValue);
|
|
3689
|
+
}, (rejectedValue) => {
|
|
3690
|
+
promiseStatus = "rejected";
|
|
3691
|
+
onReject(rejectedValue);
|
|
3692
|
+
});
|
|
3693
|
+
} else return onResolve(result);
|
|
3694
|
+
} catch (error) {
|
|
3695
|
+
onReject(error);
|
|
3696
|
+
}
|
|
3697
|
+
};
|
|
3698
|
+
if (checkCallback() === true) return;
|
|
3699
|
+
timeoutId = setTimeout(onReject, timeout);
|
|
3700
|
+
intervalId = setInterval(checkCallback, interval);
|
|
3701
|
+
});
|
|
3702
|
+
}
|
|
3703
|
+
|
|
3704
|
+
function createVitest() {
|
|
3705
|
+
let _config = null;
|
|
3706
|
+
const state = () => getWorkerState();
|
|
3707
|
+
let _timers;
|
|
3708
|
+
const timers = () => _timers ||= new FakeTimers({
|
|
3709
|
+
global: globalThis,
|
|
3710
|
+
config: state().config.fakeTimers
|
|
3711
|
+
});
|
|
3712
|
+
const _stubsGlobal = /* @__PURE__ */ new Map();
|
|
3713
|
+
const _stubsEnv = /* @__PURE__ */ new Map();
|
|
3714
|
+
const _envBooleans = [
|
|
3715
|
+
"PROD",
|
|
3716
|
+
"DEV",
|
|
3717
|
+
"SSR"
|
|
3718
|
+
];
|
|
3719
|
+
const utils = {
|
|
3720
|
+
useFakeTimers(config) {
|
|
3721
|
+
if (isChildProcess()) {
|
|
3722
|
+
if (config?.toFake?.includes("nextTick") || state().config?.fakeTimers?.toFake?.includes("nextTick")) throw new Error("vi.useFakeTimers({ toFake: [\"nextTick\"] }) is not supported in node:child_process. Use --pool=threads if mocking nextTick is required.");
|
|
3723
|
+
}
|
|
3724
|
+
if (config) timers().configure({
|
|
3725
|
+
...state().config.fakeTimers,
|
|
3726
|
+
...config
|
|
3727
|
+
});
|
|
3728
|
+
else timers().configure(state().config.fakeTimers);
|
|
3729
|
+
timers().useFakeTimers();
|
|
3730
|
+
return utils;
|
|
3731
|
+
},
|
|
3732
|
+
isFakeTimers() {
|
|
3733
|
+
return timers().isFakeTimers();
|
|
3734
|
+
},
|
|
3735
|
+
useRealTimers() {
|
|
3736
|
+
timers().useRealTimers();
|
|
3737
|
+
return utils;
|
|
3738
|
+
},
|
|
3739
|
+
runOnlyPendingTimers() {
|
|
3740
|
+
timers().runOnlyPendingTimers();
|
|
3741
|
+
return utils;
|
|
3742
|
+
},
|
|
3743
|
+
async runOnlyPendingTimersAsync() {
|
|
3744
|
+
await timers().runOnlyPendingTimersAsync();
|
|
3745
|
+
return utils;
|
|
3746
|
+
},
|
|
3747
|
+
runAllTimers() {
|
|
3748
|
+
timers().runAllTimers();
|
|
3749
|
+
return utils;
|
|
3750
|
+
},
|
|
3751
|
+
async runAllTimersAsync() {
|
|
3752
|
+
await timers().runAllTimersAsync();
|
|
3753
|
+
return utils;
|
|
3754
|
+
},
|
|
3755
|
+
runAllTicks() {
|
|
3756
|
+
timers().runAllTicks();
|
|
3757
|
+
return utils;
|
|
3758
|
+
},
|
|
3759
|
+
advanceTimersByTime(ms) {
|
|
3760
|
+
timers().advanceTimersByTime(ms);
|
|
3761
|
+
return utils;
|
|
3762
|
+
},
|
|
3763
|
+
async advanceTimersByTimeAsync(ms) {
|
|
3764
|
+
await timers().advanceTimersByTimeAsync(ms);
|
|
3765
|
+
return utils;
|
|
3766
|
+
},
|
|
3767
|
+
advanceTimersToNextTimer() {
|
|
3768
|
+
timers().advanceTimersToNextTimer();
|
|
3769
|
+
return utils;
|
|
3770
|
+
},
|
|
3771
|
+
async advanceTimersToNextTimerAsync() {
|
|
3772
|
+
await timers().advanceTimersToNextTimerAsync();
|
|
3773
|
+
return utils;
|
|
3774
|
+
},
|
|
3775
|
+
advanceTimersToNextFrame() {
|
|
3776
|
+
timers().advanceTimersToNextFrame();
|
|
3777
|
+
return utils;
|
|
3778
|
+
},
|
|
3779
|
+
getTimerCount() {
|
|
3780
|
+
return timers().getTimerCount();
|
|
3781
|
+
},
|
|
3782
|
+
setSystemTime(time) {
|
|
3783
|
+
timers().setSystemTime(time);
|
|
3784
|
+
return utils;
|
|
3785
|
+
},
|
|
3786
|
+
getMockedSystemTime() {
|
|
3787
|
+
return timers().getMockedSystemTime();
|
|
3788
|
+
},
|
|
3789
|
+
getRealSystemTime() {
|
|
3790
|
+
return timers().getRealSystemTime();
|
|
3791
|
+
},
|
|
3792
|
+
clearAllTimers() {
|
|
3793
|
+
timers().clearAllTimers();
|
|
3794
|
+
return utils;
|
|
3795
|
+
},
|
|
3796
|
+
spyOn,
|
|
3797
|
+
fn,
|
|
3798
|
+
waitFor,
|
|
3799
|
+
waitUntil,
|
|
3800
|
+
hoisted(factory) {
|
|
3801
|
+
assertTypes(factory, "\"vi.hoisted\" factory", ["function"]);
|
|
3802
|
+
return factory();
|
|
3803
|
+
},
|
|
3804
|
+
mock(path, factory) {
|
|
3805
|
+
if (typeof path !== "string") throw new TypeError(`vi.mock() expects a string path, but received a ${typeof path}`);
|
|
3806
|
+
const importer = getImporter("mock");
|
|
3807
|
+
_mocker().queueMock(path, importer, typeof factory === "function" ? () => factory(() => _mocker().importActual(path, importer, _mocker().getMockContext().callstack)) : factory);
|
|
3808
|
+
},
|
|
3809
|
+
unmock(path) {
|
|
3810
|
+
if (typeof path !== "string") throw new TypeError(`vi.unmock() expects a string path, but received a ${typeof path}`);
|
|
3811
|
+
_mocker().queueUnmock(path, getImporter("unmock"));
|
|
3812
|
+
},
|
|
3813
|
+
doMock(path, factory) {
|
|
3814
|
+
if (typeof path !== "string") throw new TypeError(`vi.doMock() expects a string path, but received a ${typeof path}`);
|
|
3815
|
+
const importer = getImporter("doMock");
|
|
3816
|
+
_mocker().queueMock(path, importer, typeof factory === "function" ? () => factory(() => _mocker().importActual(path, importer, _mocker().getMockContext().callstack)) : factory);
|
|
3817
|
+
},
|
|
3818
|
+
doUnmock(path) {
|
|
3819
|
+
if (typeof path !== "string") throw new TypeError(`vi.doUnmock() expects a string path, but received a ${typeof path}`);
|
|
3820
|
+
const importer = getImporter("doUnmock");
|
|
3821
|
+
_mocker().queueUnmock(path, importer);
|
|
3822
|
+
},
|
|
3823
|
+
async importActual(path) {
|
|
3824
|
+
const importer = getImporter("importActual");
|
|
3825
|
+
return _mocker().importActual(path, importer, _mocker().getMockContext().callstack);
|
|
3826
|
+
},
|
|
3827
|
+
async importMock(path) {
|
|
3828
|
+
const importer = getImporter("importMock");
|
|
3829
|
+
return _mocker().importMock(path, importer);
|
|
3830
|
+
},
|
|
3831
|
+
mockObject(value, options) {
|
|
3832
|
+
return _mocker().mockObject({ value }, void 0, options?.spy ? "autospy" : "automock").value;
|
|
3833
|
+
},
|
|
3834
|
+
mocked(item, _options = {}) {
|
|
3835
|
+
return item;
|
|
3836
|
+
},
|
|
3837
|
+
isMockFunction(fn) {
|
|
3838
|
+
return isMockFunction(fn);
|
|
3839
|
+
},
|
|
3840
|
+
clearAllMocks() {
|
|
3841
|
+
clearAllMocks();
|
|
3842
|
+
return utils;
|
|
3843
|
+
},
|
|
3844
|
+
resetAllMocks() {
|
|
3845
|
+
resetAllMocks();
|
|
3846
|
+
return utils;
|
|
3847
|
+
},
|
|
3848
|
+
restoreAllMocks() {
|
|
3849
|
+
restoreAllMocks();
|
|
3850
|
+
return utils;
|
|
3851
|
+
},
|
|
3852
|
+
stubGlobal(name, value) {
|
|
3853
|
+
if (!_stubsGlobal.has(name)) _stubsGlobal.set(name, Object.getOwnPropertyDescriptor(globalThis, name));
|
|
3854
|
+
Object.defineProperty(globalThis, name, {
|
|
3855
|
+
value,
|
|
3856
|
+
writable: true,
|
|
3857
|
+
configurable: true,
|
|
3858
|
+
enumerable: true
|
|
3859
|
+
});
|
|
3860
|
+
return utils;
|
|
3861
|
+
},
|
|
3862
|
+
stubEnv(name, value) {
|
|
3863
|
+
const env = state().metaEnv;
|
|
3864
|
+
if (!_stubsEnv.has(name)) _stubsEnv.set(name, env[name]);
|
|
3865
|
+
if (_envBooleans.includes(name)) env[name] = value ? "1" : "";
|
|
3866
|
+
else if (value === void 0) delete env[name];
|
|
3867
|
+
else env[name] = String(value);
|
|
3868
|
+
return utils;
|
|
3869
|
+
},
|
|
3870
|
+
unstubAllGlobals() {
|
|
3871
|
+
_stubsGlobal.forEach((original, name) => {
|
|
3872
|
+
if (!original) Reflect.deleteProperty(globalThis, name);
|
|
3873
|
+
else Object.defineProperty(globalThis, name, original);
|
|
3874
|
+
});
|
|
3875
|
+
_stubsGlobal.clear();
|
|
3876
|
+
return utils;
|
|
3877
|
+
},
|
|
3878
|
+
unstubAllEnvs() {
|
|
3879
|
+
const env = state().metaEnv;
|
|
3880
|
+
_stubsEnv.forEach((original, name) => {
|
|
3881
|
+
if (original === void 0) delete env[name];
|
|
3882
|
+
else env[name] = original;
|
|
3883
|
+
});
|
|
3884
|
+
_stubsEnv.clear();
|
|
3885
|
+
return utils;
|
|
3886
|
+
},
|
|
3887
|
+
resetModules() {
|
|
3888
|
+
resetModules(state().evaluatedModules);
|
|
3889
|
+
return utils;
|
|
3890
|
+
},
|
|
3891
|
+
async dynamicImportSettled() {
|
|
3892
|
+
return waitForImportsToResolve();
|
|
3893
|
+
},
|
|
3894
|
+
setConfig(config) {
|
|
3895
|
+
if (!_config) _config = { ...state().config };
|
|
3896
|
+
Object.assign(state().config, config);
|
|
3897
|
+
},
|
|
3898
|
+
resetConfig() {
|
|
3899
|
+
if (_config) Object.assign(state().config, _config);
|
|
3900
|
+
}
|
|
3901
|
+
};
|
|
3902
|
+
return utils;
|
|
3903
|
+
}
|
|
3904
|
+
const vitest = createVitest();
|
|
3905
|
+
const vi = vitest;
|
|
3906
|
+
function _mocker() {
|
|
3907
|
+
// @ts-expect-error injected by vite-nide
|
|
3908
|
+
return typeof __vitest_mocker__ !== "undefined" ? __vitest_mocker__ : new Proxy({}, { get(_, name) {
|
|
3909
|
+
throw new Error(`Vitest mocker was not initialized in this environment. vi.${String(name)}() is forbidden.`);
|
|
3910
|
+
} });
|
|
3911
|
+
}
|
|
3912
|
+
function getImporter(name) {
|
|
3913
|
+
const stackArray = createSimpleStackTrace({ stackTraceLimit: 5 }).split("\n");
|
|
3914
|
+
return parseSingleStack(stackArray[stackArray.findLastIndex((stack) => {
|
|
3915
|
+
return stack.includes(` at Object.${name}`) || stack.includes(`${name}@`) || stack.includes(` at ${name} (`);
|
|
3916
|
+
}) + 1])?.file || "";
|
|
3917
|
+
}
|
|
3918
|
+
|
|
3919
|
+
export { getSnapshotClient as a, assert as b, createExpect as c, vitest as d, globalExpect as g, inject as i, should as s, vi as v };
|