2020-03-31 20:42:07 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var path = _interopRequireWildcard(require('path'));
|
|
|
|
|
2020-05-15 05:33:08 +08:00
|
|
|
var _jestUtil = require('jest-util');
|
|
|
|
|
2020-03-31 20:42:07 +08:00
|
|
|
function _getRequireWildcardCache() {
|
|
|
|
if (typeof WeakMap !== 'function') return null;
|
|
|
|
var cache = new WeakMap();
|
|
|
|
_getRequireWildcardCache = function () {
|
|
|
|
return cache;
|
|
|
|
};
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _interopRequireWildcard(obj) {
|
|
|
|
if (obj && obj.__esModule) {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
|
|
|
|
return {default: obj};
|
|
|
|
}
|
|
|
|
var cache = _getRequireWildcardCache();
|
|
|
|
if (cache && cache.has(obj)) {
|
|
|
|
return cache.get(obj);
|
|
|
|
}
|
|
|
|
var newObj = {};
|
|
|
|
var hasPropertyDescriptor =
|
|
|
|
Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
|
|
for (var key in obj) {
|
|
|
|
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
|
|
var desc = hasPropertyDescriptor
|
|
|
|
? Object.getOwnPropertyDescriptor(obj, key)
|
|
|
|
: null;
|
|
|
|
if (desc && (desc.get || desc.set)) {
|
|
|
|
Object.defineProperty(newObj, key, desc);
|
|
|
|
} else {
|
|
|
|
newObj[key] = obj[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newObj.default = obj;
|
|
|
|
if (cache) {
|
|
|
|
cache.set(obj, newObj);
|
|
|
|
}
|
|
|
|
return newObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-04-30 20:40:07 +08:00
|
|
|
const FRAMEWORK_INITIALIZER = path.resolve(__dirname, './jestAdapterInit.js');
|
|
|
|
const EXPECT_INITIALIZER = path.resolve(__dirname, './jestExpect.js');
|
2020-03-31 20:42:07 +08:00
|
|
|
|
|
|
|
const jestAdapter = async (
|
|
|
|
globalConfig,
|
|
|
|
config,
|
|
|
|
environment,
|
|
|
|
runtime,
|
|
|
|
testPath
|
|
|
|
) => {
|
2020-04-30 20:40:07 +08:00
|
|
|
var _runtime$unstable_sho2;
|
|
|
|
|
2020-03-31 20:42:07 +08:00
|
|
|
const {
|
|
|
|
initialize,
|
|
|
|
runAndTransformResultsToJestFormat
|
|
|
|
} = runtime.requireInternalModule(FRAMEWORK_INITIALIZER);
|
2020-04-30 20:40:07 +08:00
|
|
|
runtime.requireInternalModule(EXPECT_INITIALIZER).default({
|
|
|
|
expand: globalConfig.expand
|
|
|
|
});
|
2020-03-31 20:42:07 +08:00
|
|
|
|
|
|
|
const getPrettier = () =>
|
|
|
|
config.prettierPath ? require(config.prettierPath) : null;
|
|
|
|
|
|
|
|
const getBabelTraverse = () => require('@babel/traverse').default;
|
|
|
|
|
2020-04-30 20:40:07 +08:00
|
|
|
const {globals, snapshotState} = await initialize({
|
2020-03-31 20:42:07 +08:00
|
|
|
config,
|
|
|
|
environment,
|
|
|
|
getBabelTraverse,
|
|
|
|
getPrettier,
|
|
|
|
globalConfig,
|
|
|
|
localRequire: runtime.requireModule.bind(runtime),
|
|
|
|
parentProcess: process,
|
|
|
|
testPath
|
|
|
|
});
|
|
|
|
|
2020-05-15 05:33:08 +08:00
|
|
|
if (config.timers === 'fake' || config.timers === 'legacy') {
|
2020-03-31 20:42:07 +08:00
|
|
|
// during setup, this cannot be null (and it's fine to explode if it is)
|
|
|
|
environment.fakeTimers.useFakeTimers();
|
2020-05-15 05:33:08 +08:00
|
|
|
} else if (config.timers === 'modern') {
|
|
|
|
environment.fakeTimersModern.useFakeTimers();
|
2020-03-31 20:42:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
globals.beforeEach(() => {
|
|
|
|
if (config.resetModules) {
|
|
|
|
runtime.resetModules();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.clearMocks) {
|
|
|
|
runtime.clearAllMocks();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.resetMocks) {
|
|
|
|
runtime.resetAllMocks();
|
|
|
|
|
|
|
|
if (config.timers === 'fake') {
|
|
|
|
// during setup, this cannot be null (and it's fine to explode if it is)
|
|
|
|
environment.fakeTimers.useFakeTimers();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.restoreMocks) {
|
|
|
|
runtime.restoreAllMocks();
|
|
|
|
}
|
|
|
|
});
|
2020-04-30 20:40:07 +08:00
|
|
|
|
|
|
|
for (const path of config.setupFilesAfterEnv) {
|
|
|
|
var _runtime$unstable_sho;
|
|
|
|
|
|
|
|
// TODO: remove ? in Jest 26
|
|
|
|
const esm =
|
|
|
|
(_runtime$unstable_sho = runtime.unstable_shouldLoadAsEsm) === null ||
|
|
|
|
_runtime$unstable_sho === void 0
|
|
|
|
? void 0
|
|
|
|
: _runtime$unstable_sho.call(runtime, path);
|
|
|
|
|
|
|
|
if (esm) {
|
|
|
|
await runtime.unstable_importModule(path);
|
|
|
|
} else {
|
|
|
|
runtime.requireModule(path);
|
|
|
|
}
|
|
|
|
} // TODO: remove ? in Jest 26
|
|
|
|
|
|
|
|
const esm =
|
|
|
|
(_runtime$unstable_sho2 = runtime.unstable_shouldLoadAsEsm) === null ||
|
|
|
|
_runtime$unstable_sho2 === void 0
|
|
|
|
? void 0
|
|
|
|
: _runtime$unstable_sho2.call(runtime, testPath);
|
|
|
|
|
|
|
|
if (esm) {
|
|
|
|
await runtime.unstable_importModule(testPath);
|
|
|
|
} else {
|
|
|
|
runtime.requireModule(testPath);
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:42:07 +08:00
|
|
|
const results = await runAndTransformResultsToJestFormat({
|
|
|
|
config,
|
|
|
|
globalConfig,
|
|
|
|
testPath
|
|
|
|
});
|
2020-05-15 05:33:08 +08:00
|
|
|
|
|
|
|
_addSnapshotData(results, snapshotState); // We need to copy the results object to ensure we don't leaks the prototypes
|
|
|
|
// from the VM. Jasmine creates the result objects in the parent process, we
|
|
|
|
// should consider doing that for circus as well.
|
|
|
|
|
|
|
|
return (0, _jestUtil.deepCyclicCopy)(results, {
|
|
|
|
keepPrototype: false
|
|
|
|
});
|
2020-03-31 20:42:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const _addSnapshotData = (results, snapshotState) => {
|
|
|
|
results.testResults.forEach(({fullName, status}) => {
|
|
|
|
if (status === 'pending' || status === 'failed') {
|
|
|
|
// if test is skipped or failed, we don't want to mark
|
|
|
|
// its snapshots as obsolete.
|
|
|
|
snapshotState.markSnapshotsAsCheckedForTest(fullName);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const uncheckedCount = snapshotState.getUncheckedCount();
|
|
|
|
const uncheckedKeys = snapshotState.getUncheckedKeys();
|
|
|
|
|
|
|
|
if (uncheckedCount) {
|
|
|
|
snapshotState.removeUncheckedKeys();
|
|
|
|
}
|
|
|
|
|
|
|
|
const status = snapshotState.save();
|
|
|
|
results.snapshot.fileDeleted = status.deleted;
|
|
|
|
results.snapshot.added = snapshotState.added;
|
|
|
|
results.snapshot.matched = snapshotState.matched;
|
|
|
|
results.snapshot.unmatched = snapshotState.unmatched;
|
|
|
|
results.snapshot.updated = snapshotState.updated;
|
|
|
|
results.snapshot.unchecked = !status.deleted ? uncheckedCount : 0; // Copy the array to prevent memory leaks
|
|
|
|
|
|
|
|
results.snapshot.uncheckedKeys = Array.from(uncheckedKeys);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = jestAdapter;
|