初始化

This commit is contained in:
yziiy
2025-08-11 11:06:07 +08:00
parent 083bc37c00
commit 5607d11395
19772 changed files with 3108723 additions and 18 deletions

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) Facebook, Inc. and its affiliates.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,29 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/// <reference types="node" />
import { Context } from 'vm';
import type { JestEnvironment } from '@jest/environment';
import { LegacyFakeTimers, ModernFakeTimers } from '@jest/fake-timers';
import type { Config, Global } from '@jest/types';
import { ModuleMocker } from 'jest-mock';
declare type Timer = {
id: number;
ref: () => Timer;
unref: () => Timer;
};
declare class NodeEnvironment implements JestEnvironment<Timer> {
context: Context | null;
fakeTimers: LegacyFakeTimers<Timer> | null;
fakeTimersModern: ModernFakeTimers | null;
global: Global.Global;
moduleMocker: ModuleMocker | null;
constructor(config: Config.ProjectConfig);
setup(): Promise<void>;
teardown(): Promise<void>;
getVmContext(): Context | null;
}
export = NodeEnvironment;

View File

@@ -0,0 +1,184 @@
'use strict';
function _vm() {
const data = require('vm');
_vm = function () {
return data;
};
return data;
}
function _fakeTimers() {
const data = require('@jest/fake-timers');
_fakeTimers = function () {
return data;
};
return data;
}
function _jestMock() {
const data = require('jest-mock');
_jestMock = function () {
return data;
};
return data;
}
function _jestUtil() {
const data = require('jest-util');
_jestUtil = function () {
return data;
};
return data;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
class NodeEnvironment {
constructor(config) {
_defineProperty(this, 'context', void 0);
_defineProperty(this, 'fakeTimers', void 0);
_defineProperty(this, 'fakeTimersModern', void 0);
_defineProperty(this, 'global', void 0);
_defineProperty(this, 'moduleMocker', void 0);
this.context = (0, _vm().createContext)();
const global = (this.global = (0, _vm().runInContext)(
'this',
Object.assign(this.context, config.testEnvironmentOptions)
));
global.global = global;
global.clearInterval = clearInterval;
global.clearTimeout = clearTimeout;
global.setInterval = setInterval;
global.setTimeout = setTimeout;
global.Buffer = Buffer;
global.setImmediate = setImmediate;
global.clearImmediate = clearImmediate;
global.ArrayBuffer = ArrayBuffer; // TextEncoder (global or via 'util') references a Uint8Array constructor
// different than the global one used by users in tests. This makes sure the
// same constructor is referenced by both.
global.Uint8Array = Uint8Array; // URL and URLSearchParams are global in Node >= 10
if (typeof URL !== 'undefined' && typeof URLSearchParams !== 'undefined') {
global.URL = URL;
global.URLSearchParams = URLSearchParams;
} // TextDecoder and TextDecoder are global in Node >= 11
if (
typeof TextEncoder !== 'undefined' &&
typeof TextDecoder !== 'undefined'
) {
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
} // queueMicrotask is global in Node >= 11
if (typeof queueMicrotask !== 'undefined') {
global.queueMicrotask = queueMicrotask;
} // AbortController is global in Node >= 15
if (typeof AbortController !== 'undefined') {
global.AbortController = AbortController;
} // AbortSignal is global in Node >= 15
if (typeof AbortSignal !== 'undefined') {
global.AbortSignal = AbortSignal;
} // Event is global in Node >= 15.4
if (typeof Event !== 'undefined') {
global.Event = Event;
} // EventTarget is global in Node >= 15.4
if (typeof EventTarget !== 'undefined') {
global.EventTarget = EventTarget;
} // performance is global in Node >= 16
if (typeof performance !== 'undefined') {
global.performance = performance;
} // atob and btoa are global in Node >= 16
if (typeof atob !== 'undefined' && typeof btoa !== 'undefined') {
global.atob = atob;
global.btoa = btoa;
}
(0, _jestUtil().installCommonGlobals)(global, config.globals);
this.moduleMocker = new (_jestMock().ModuleMocker)(global);
const timerIdToRef = id => ({
id,
ref() {
return this;
},
unref() {
return this;
}
});
const timerRefToId = timer => (timer && timer.id) || undefined;
const timerConfig = {
idToRef: timerIdToRef,
refToId: timerRefToId
};
this.fakeTimers = new (_fakeTimers().LegacyFakeTimers)({
config,
global,
moduleMocker: this.moduleMocker,
timerConfig
});
this.fakeTimersModern = new (_fakeTimers().ModernFakeTimers)({
config,
global
});
}
async setup() {}
async teardown() {
if (this.fakeTimers) {
this.fakeTimers.dispose();
}
if (this.fakeTimersModern) {
this.fakeTimersModern.dispose();
}
this.context = null;
this.fakeTimers = null;
this.fakeTimersModern = null;
}
getVmContext() {
return this.context;
}
}
module.exports = NodeEnvironment;

View File

@@ -0,0 +1,37 @@
{
"name": "jest-environment-node",
"version": "27.5.1",
"repository": {
"type": "git",
"url": "https://github.com/facebook/jest.git",
"directory": "packages/jest-environment-node"
},
"license": "MIT",
"main": "./build/index.js",
"types": "./build/index.d.ts",
"exports": {
".": {
"types": "./build/index.d.ts",
"default": "./build/index.js"
},
"./package.json": "./package.json"
},
"dependencies": {
"@jest/environment": "^27.5.1",
"@jest/fake-timers": "^27.5.1",
"@jest/types": "^27.5.1",
"@types/node": "*",
"jest-mock": "^27.5.1",
"jest-util": "^27.5.1"
},
"devDependencies": {
"@jest/test-utils": "^27.5.1"
},
"engines": {
"node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0"
},
"publishConfig": {
"access": "public"
},
"gitHead": "67c1aa20c5fec31366d733e901fee2b981cb1850"
}