本文整理匯總了TypeScript中@jest/transform.ScriptTransformer類的典型用法代碼示例。如果您正苦於以下問題:TypeScript ScriptTransformer類的具體用法?TypeScript ScriptTransformer怎麽用?TypeScript ScriptTransformer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了ScriptTransformer類的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: pEachSeries
await pEachSeries(Array.from(globalModulePaths), async modulePath => {
if (!modulePath) {
return;
}
const correctConfig = allTests.find(
t => t.context.config[moduleName] === modulePath,
);
const projectConfig = correctConfig
? correctConfig.context.config
: // Fallback to first config
allTests[0].context.config;
const transformer = new ScriptTransformer(projectConfig);
// Load the transformer to avoid a cycle where we need to load a
// transformer in order to transform it in the require hooks
transformer.preloadTransformer(modulePath);
let transforming = false;
const revertHook = addHook(
(code, filename) => {
try {
transforming = true;
return (
transformer.transformSource(filename, code, false).code || code
);
} finally {
transforming = false;
}
},
{
exts: [extname(modulePath)],
ignoreNodeModules: false,
matcher: (...args) => {
if (transforming) {
// Don't transform any dependency required by the transformer itself
return false;
}
return transformer.shouldTransform(...args);
},
},
);
const globalModule = interopRequireDefault(require(modulePath)).default;
if (typeof globalModule !== 'function') {
throw new TypeError(
`${moduleName} file must export a function at ${modulePath}`,
);
}
await globalModule(globalConfig);
revertHook();
});
示例2: pEachSeries
await pEachSeries(Array.from(globalModulePaths), async modulePath => {
if (!modulePath) {
return;
}
const correctConfig = allTests.find(
t => t.context.config[moduleName] === modulePath,
);
const projectConfig = correctConfig
? correctConfig.context.config
: // Fallback to first config
allTests[0].context.config;
const transformer = new ScriptTransformer(projectConfig);
// Load the transformer to avoid a cycle where we need to load a
// transformer in order to transform it in the require hooks
transformer.preloadTransformer(modulePath);
const revertHook = addHook(
(code, filename) =>
transformer.transformSource(filename, code, false).code || code,
{
exts: [extname(modulePath)],
matcher: transformer.shouldTransform.bind(transformer),
},
);
const globalModule = _interopRequireDefault(require(modulePath)).default;
if (typeof globalModule !== 'function') {
throw new TypeError(
`${moduleName} file must export a function at ${modulePath}`,
);
}
await globalModule(globalConfig);
revertHook();
});
示例3: _execModule
private _execModule(
localModule: InitialModule,
options: InternalModuleOptions | undefined,
moduleRegistry: ModuleRegistry,
from: Config.Path | null,
) {
// If the environment was disposed, prevent this module from being executed.
if (!this._environment.global) {
return;
}
const isInternalModule = !!(options && options.isInternalModule);
const filename = localModule.filename;
const lastExecutingModulePath = this._currentlyExecutingModulePath;
this._currentlyExecutingModulePath = filename;
const origCurrExecutingManualMock = this._isCurrentlyExecutingManualMock;
this._isCurrentlyExecutingManualMock = filename;
const dirname = path.dirname(filename);
localModule.children = [];
Object.defineProperty(localModule, 'parent', {
enumerable: true,
get() {
const key = from || '';
return moduleRegistry[key] || null;
},
});
localModule.paths = this._resolver.getModulePaths(dirname);
Object.defineProperty(localModule, 'require', {
value: this._createRequireImplementation(localModule, options),
});
const extraGlobals = this._config.extraGlobals || [];
const transformedFile = this._scriptTransformer.transform(
filename,
{
changedFiles: this._coverageOptions.changedFiles,
collectCoverage: this._coverageOptions.collectCoverage,
collectCoverageFrom: this._coverageOptions.collectCoverageFrom,
collectCoverageOnlyFrom: this._coverageOptions.collectCoverageOnlyFrom,
extraGlobals,
isInternalModule,
},
this._cacheFS[filename],
);
if (transformedFile.sourceMapPath) {
this._sourceMapRegistry[filename] = transformedFile.sourceMapPath;
if (transformedFile.mapCoverage) {
this._needsCoverageMapped.add(filename);
}
}
const runScript = this._environment.runScript(transformedFile.script);
if (runScript === null) {
this._logFormattedReferenceError(
'You are trying to `import` a file after the Jest environment has been torn down.',
);
process.exitCode = 1;
return;
}
const wrapper = runScript[ScriptTransformer.EVAL_RESULT_VARIABLE];
const moduleArguments = new Set([
localModule, // module object
localModule.exports, // module exports
localModule.require, // require implementation
dirname, // __dirname
filename, // __filename
this._environment.global, // global object
this._createJestObjectFor(
filename,
localModule.require as LocalModuleRequire,
), // jest object
...extraGlobals.map(globalVariable => {
if (this._environment.global[globalVariable]) {
return this._environment.global[globalVariable];
}
throw new Error(
`You have requested '${globalVariable}' as a global variable, but it was not present. Please check your config or your global environment.`,
);
}),
]);
wrapper.call(localModule.exports, ...Array.from(moduleArguments));
this._isCurrentlyExecutingManualMock = origCurrExecutingManualMock;
this._currentlyExecutingModulePath = lastExecutingModulePath;
}
示例4: _loadModule
private _loadModule(
localModule: InitialModule,
from: Config.Path,
moduleName: string | undefined,
modulePath: Config.Path,
options: InternalModuleOptions | undefined,
moduleRegistry: ModuleRegistry,
) {
if (path.extname(modulePath) === '.json') {
const text = stripBOM(fs.readFileSync(modulePath, 'utf8'));
const transformedFile = this._scriptTransformer.transformJson(
modulePath,
this._getFullTransformationOptions(options),
text,
);
localModule.exports = this._environment.global.JSON.parse(
transformedFile,
);
} else if (path.extname(modulePath) === '.node') {
localModule.exports = require(modulePath);
} else {
// Only include the fromPath if a moduleName is given. Else treat as root.
const fromPath = moduleName ? from : null;
this._execModule(localModule, options, moduleRegistry, fromPath);
}
localModule.loaded = true;
}
示例5:
matcher: (...args) => {
if (transforming) {
// Don't transform any dependency required by the transformer itself
return false;
}
return transformer.shouldTransform(...args);
},
示例6: return
(code, filename) => {
try {
transforming = true;
return (
transformer.transformSource(filename, code, false).code || code
);
} finally {
transforming = false;
}
},
示例7:
(code, filename) =>
transformer.transformSource(filename, code, false).code || code,