@modern-js/plugin-testing 1.5.7 → 1.6.0
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/CHANGELOG.md +16 -0
- package/dist/js/modern/base/config/index.js +18 -0
- package/dist/js/modern/base/config/patches/assetsModule.js +11 -0
- package/dist/js/modern/base/config/patches/filemock.js +1 -0
- package/dist/js/modern/base/config/patches/index.js +13 -0
- package/dist/js/modern/base/config/patches/transformer.js +49 -0
- package/dist/js/modern/base/config/resolver.js +15 -0
- package/dist/js/modern/base/config/testConfigOperator.js +86 -0
- package/dist/js/modern/base/config/transformer/babelTransformer.js +12 -0
- package/dist/js/modern/base/hook.js +7 -0
- package/dist/js/modern/base/index.js +6 -0
- package/dist/js/modern/base/runJest.js +94 -0
- package/dist/js/modern/base/types/index.js +0 -0
- package/dist/js/modern/base/utils.js +59 -0
- package/dist/js/modern/cli/bff/app.js +22 -0
- package/dist/js/modern/cli/bff/constant.js +1 -0
- package/dist/js/modern/cli/bff/env.js +28 -0
- package/dist/js/modern/cli/bff/index.js +110 -0
- package/dist/js/modern/cli/bff/mockAPI.js +109 -0
- package/dist/js/modern/cli/bff/setup.js +33 -0
- package/dist/js/modern/cli/bff/utils/index.js +25 -0
- package/dist/js/modern/cli/index.js +2 -2
- package/dist/js/modern/cli/test.js +1 -1
- package/dist/js/modern/runtime-testing/base.js +1 -1
- package/dist/js/modern/runtime-testing/request.js +17 -0
- package/dist/js/node/base/config/index.js +31 -0
- package/dist/js/node/base/config/patches/assetsModule.js +20 -0
- package/dist/js/node/base/config/patches/filemock.js +8 -0
- package/dist/js/node/base/config/patches/index.js +24 -0
- package/dist/js/node/base/config/patches/transformer.js +58 -0
- package/dist/js/node/base/config/resolver.js +20 -0
- package/dist/js/node/base/config/testConfigOperator.js +93 -0
- package/dist/js/node/base/config/transformer/babelTransformer.js +23 -0
- package/dist/js/node/base/hook.js +18 -0
- package/dist/js/node/base/index.js +73 -0
- package/dist/js/node/base/runJest.js +109 -0
- package/dist/js/node/base/types/index.js +0 -0
- package/dist/js/node/base/utils.js +79 -0
- package/dist/js/node/cli/bff/app.js +34 -0
- package/dist/js/node/cli/bff/constant.js +8 -0
- package/dist/js/node/cli/bff/env.js +42 -0
- package/dist/js/node/cli/bff/index.js +129 -0
- package/dist/js/node/cli/bff/mockAPI.js +124 -0
- package/dist/js/node/cli/bff/setup.js +41 -0
- package/dist/js/node/cli/bff/utils/index.js +38 -0
- package/dist/js/node/cli/index.js +7 -7
- package/dist/js/node/cli/test.js +2 -2
- package/dist/js/node/runtime-testing/base.js +2 -2
- package/dist/js/node/runtime-testing/request.js +24 -0
- package/dist/js/treeshaking/base/config/index.js +40 -0
- package/dist/js/treeshaking/base/config/patches/assetsModule.js +11 -0
- package/dist/js/treeshaking/base/config/patches/filemock.js +1 -0
- package/dist/js/treeshaking/base/config/patches/index.js +85 -0
- package/dist/js/treeshaking/base/config/patches/transformer.js +45 -0
- package/dist/js/treeshaking/base/config/resolver.js +17 -0
- package/dist/js/treeshaking/base/config/testConfigOperator.js +91 -0
- package/dist/js/treeshaking/base/config/transformer/babelTransformer.js +12 -0
- package/dist/js/treeshaking/base/hook.js +7 -0
- package/dist/js/treeshaking/base/index.js +6 -0
- package/dist/js/treeshaking/base/runJest.js +194 -0
- package/dist/js/treeshaking/base/types/index.js +0 -0
- package/dist/js/treeshaking/base/utils.js +60 -0
- package/dist/js/treeshaking/cli/bff/app.js +47 -0
- package/dist/js/treeshaking/cli/bff/constant.js +1 -0
- package/dist/js/treeshaking/cli/bff/env.js +98 -0
- package/dist/js/treeshaking/cli/bff/index.js +145 -0
- package/dist/js/treeshaking/cli/bff/mockAPI.js +113 -0
- package/dist/js/treeshaking/cli/bff/setup.js +33 -0
- package/dist/js/treeshaking/cli/bff/utils/index.js +22 -0
- package/dist/js/treeshaking/cli/index.js +2 -2
- package/dist/js/treeshaking/cli/test.js +1 -1
- package/dist/js/treeshaking/runtime-testing/base.js +1 -1
- package/dist/js/treeshaking/runtime-testing/request.js +24 -0
- package/dist/types/base/config/index.d.ts +124 -0
- package/dist/types/base/config/patches/assetsModule.d.ts +6 -0
- package/dist/types/base/config/patches/filemock.d.ts +3 -0
- package/dist/types/base/config/patches/index.d.ts +2 -0
- package/dist/types/base/config/patches/transformer.d.ts +6 -0
- package/dist/types/base/config/resolver.d.ts +1 -0
- package/dist/types/base/config/testConfigOperator.d.ts +248 -0
- package/dist/types/base/config/transformer/babelTransformer.d.ts +2 -0
- package/dist/types/base/hook.d.ts +13 -0
- package/dist/types/base/index.d.ts +6 -0
- package/dist/types/base/runJest.d.ts +22 -0
- package/dist/types/base/types/index.d.ts +1 -0
- package/dist/types/base/utils.d.ts +12 -0
- package/dist/types/cli/bff/app.d.ts +6 -0
- package/dist/types/cli/bff/constant.d.ts +1 -0
- package/dist/types/cli/bff/env.d.ts +6 -0
- package/dist/types/cli/bff/index.d.ts +19 -0
- package/dist/types/cli/bff/mockAPI.d.ts +3 -0
- package/dist/types/cli/bff/setup.d.ts +1 -0
- package/dist/types/cli/bff/utils/index.d.ts +1 -0
- package/dist/types/cli/index.d.ts +1 -1
- package/dist/types/runtime-testing/base.d.ts +1 -1
- package/dist/types/runtime-testing/request.d.ts +4 -0
- package/package.json +23 -10
@@ -0,0 +1,33 @@
|
|
1
|
+
import path from 'path';
|
2
|
+
import { ApiRouter } from '@modern-js/bff-core';
|
3
|
+
import { bff_info_key } from "./constant";
|
4
|
+
import mockAPI from "./mockAPI";
|
5
|
+
let uped = false;
|
6
|
+
|
7
|
+
const setup = () => {
|
8
|
+
var _bff_info$modernUserC, _bff_info$modernUserC2;
|
9
|
+
|
10
|
+
if (uped) {
|
11
|
+
return;
|
12
|
+
}
|
13
|
+
|
14
|
+
uped = true;
|
15
|
+
const bff_info = global[bff_info_key];
|
16
|
+
const prefix = bff_info === null || bff_info === void 0 ? void 0 : (_bff_info$modernUserC = bff_info.modernUserConfig) === null || _bff_info$modernUserC === void 0 ? void 0 : (_bff_info$modernUserC2 = _bff_info$modernUserC.bff) === null || _bff_info$modernUserC2 === void 0 ? void 0 : _bff_info$modernUserC2.prefix;
|
17
|
+
const apiRouter = new ApiRouter({
|
18
|
+
apiDir: path.join(bff_info.appDir, './api'),
|
19
|
+
prefix
|
20
|
+
});
|
21
|
+
const apiInfos = apiRouter.getApiHandlers();
|
22
|
+
const apiInfosByFile = apiInfos.reduce((res, apiInfo) => {
|
23
|
+
if (!res[apiInfo.filename]) {
|
24
|
+
res[apiInfo.filename] = [];
|
25
|
+
}
|
26
|
+
|
27
|
+
res[apiInfo.filename].push(apiInfo);
|
28
|
+
return res;
|
29
|
+
}, {});
|
30
|
+
mockAPI(apiInfosByFile, global.app);
|
31
|
+
};
|
32
|
+
|
33
|
+
setup();
|
@@ -0,0 +1,25 @@
|
|
1
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
2
|
+
|
3
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
4
|
+
|
5
|
+
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; }
|
6
|
+
|
7
|
+
import path from 'path';
|
8
|
+
import { chalk } from '@modern-js/utils';
|
9
|
+
export const isBFFProject = pwd => {
|
10
|
+
try {
|
11
|
+
const packageJson = require(path.join(pwd, './package.json'));
|
12
|
+
|
13
|
+
const {
|
14
|
+
dependencies,
|
15
|
+
devDependencies
|
16
|
+
} = packageJson;
|
17
|
+
const isBFF = Object.keys(_objectSpread(_objectSpread({}, dependencies), devDependencies)).some(dependency => dependency.includes('plugin-bff'));
|
18
|
+
const isMWA = Object.keys(devDependencies).some(devDependency => devDependency.includes('app-tools'));
|
19
|
+
return isMWA && isBFF;
|
20
|
+
} catch (error) {
|
21
|
+
// eslint-disable-next-line no-console
|
22
|
+
console.log(chalk.red(error));
|
23
|
+
return false;
|
24
|
+
}
|
25
|
+
};
|
@@ -1,9 +1,9 @@
|
|
1
1
|
import path from 'path';
|
2
2
|
import { PLUGIN_SCHEMAS, createRuntimeExportsUtils, isApiOnly } from '@modern-js/utils';
|
3
|
-
import { testingHooks, getModuleNameMapper, DEFAULT_RESOLVER_PATH } from '@modern-js/testing';
|
4
3
|
import { getWebpackConfig, WebpackConfigTarget } from '@modern-js/webpack';
|
5
|
-
import
|
4
|
+
import { testingHooks, getModuleNameMapper, DEFAULT_RESOLVER_PATH } from "../base";
|
6
5
|
import { MODERNJS_CONFIG_KEY } from "../constant";
|
6
|
+
import TestingBffPlugin from "./bff";
|
7
7
|
import test from "./test";
|
8
8
|
export const mergeUserJestConfig = testUtils => {
|
9
9
|
const resolveJestConfig = testUtils.testConfig.jest; // resolveJestConfig 如果是函数类型,在所有测试插件 jestConfig 都执行后,再执行生成最终配置
|
@@ -0,0 +1,17 @@
|
|
1
|
+
/* eslint-disable eslint-comments/disable-enable-pair */
|
2
|
+
import supertest from 'supertest';
|
3
|
+
|
4
|
+
function request(...args) {
|
5
|
+
const [fn, ...extraArgs] = args;
|
6
|
+
|
7
|
+
if (!fn) {
|
8
|
+
return supertest(global.app);
|
9
|
+
}
|
10
|
+
|
11
|
+
fn.returnHttp = true;
|
12
|
+
const res = fn(...extraArgs);
|
13
|
+
fn.returnHttp = false;
|
14
|
+
return res;
|
15
|
+
}
|
16
|
+
|
17
|
+
export { request };
|
@@ -0,0 +1,31 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.patchConfig = exports.getJestUtils = exports.DEFAULT_RESOLVER_PATH = void 0;
|
7
|
+
|
8
|
+
var _patches = require("./patches");
|
9
|
+
|
10
|
+
var _testConfigOperator = require("./testConfigOperator");
|
11
|
+
|
12
|
+
/**
|
13
|
+
* Parse jest config
|
14
|
+
*/
|
15
|
+
const getJestUtils = testConfig => {
|
16
|
+
const testOperator = new _testConfigOperator.TestConfigOperator(testConfig);
|
17
|
+
return testOperator;
|
18
|
+
};
|
19
|
+
|
20
|
+
exports.getJestUtils = getJestUtils;
|
21
|
+
|
22
|
+
const patchConfig = async testOperator => {
|
23
|
+
await (0, _patches.applyPatches)(testOperator);
|
24
|
+
return testOperator.jestConfig;
|
25
|
+
};
|
26
|
+
|
27
|
+
exports.patchConfig = patchConfig;
|
28
|
+
|
29
|
+
const DEFAULT_RESOLVER_PATH = require.resolve("./resolver");
|
30
|
+
|
31
|
+
exports.DEFAULT_RESOLVER_PATH = DEFAULT_RESOLVER_PATH;
|
@@ -0,0 +1,20 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.patchAssetsModule = void 0;
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Merge config from testConfig.jest
|
10
|
+
*/
|
11
|
+
const patchAssetsModule = testOperator => {
|
12
|
+
testOperator.mergeJestConfig({
|
13
|
+
moduleNameMapper: {
|
14
|
+
'\\.(css|less|scss|sass)$': require.resolve('identity-obj-proxy'),
|
15
|
+
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': require.resolve("./filemock.js")
|
16
|
+
}
|
17
|
+
});
|
18
|
+
};
|
19
|
+
|
20
|
+
exports.patchAssetsModule = patchAssetsModule;
|
@@ -0,0 +1,24 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.applyPatches = void 0;
|
7
|
+
|
8
|
+
var _transformer = require("./transformer");
|
9
|
+
|
10
|
+
var _assetsModule = require("./assetsModule");
|
11
|
+
|
12
|
+
const _applyPatches = async (patches, testOperator) => {
|
13
|
+
for (const patch of patches) {
|
14
|
+
await patch(testOperator);
|
15
|
+
}
|
16
|
+
};
|
17
|
+
|
18
|
+
const patches = [_transformer.patchTransformer, _assetsModule.patchAssetsModule];
|
19
|
+
|
20
|
+
const applyPatches = async testConfig => {
|
21
|
+
await _applyPatches(patches, testConfig);
|
22
|
+
};
|
23
|
+
|
24
|
+
exports.applyPatches = applyPatches;
|
@@ -0,0 +1,58 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.patchTransformer = void 0;
|
7
|
+
|
8
|
+
var _utils = require("../../utils");
|
9
|
+
|
10
|
+
const resolveTsCompilerOptions = () => {
|
11
|
+
const tsCompilerOptions = (0, _utils.readCompilerOptions)() || {};
|
12
|
+
const {
|
13
|
+
jsx
|
14
|
+
} = tsCompilerOptions;
|
15
|
+
|
16
|
+
if (!jsx) {
|
17
|
+
return null;
|
18
|
+
}
|
19
|
+
|
20
|
+
tsCompilerOptions.jsx = 'react-jsx';
|
21
|
+
return tsCompilerOptions;
|
22
|
+
};
|
23
|
+
/**
|
24
|
+
* Map `TestConfig.transformer` to jest config
|
25
|
+
*/
|
26
|
+
|
27
|
+
|
28
|
+
const patchTransformer = testOperator => {
|
29
|
+
const {
|
30
|
+
transformer
|
31
|
+
} = testOperator.testConfig;
|
32
|
+
|
33
|
+
if (transformer === 'babel-jest') {
|
34
|
+
testOperator.mergeJestConfig({
|
35
|
+
transform: {
|
36
|
+
'\\.[jt]sx?$': require.resolve("../transformer/babelTransformer")
|
37
|
+
}
|
38
|
+
});
|
39
|
+
}
|
40
|
+
|
41
|
+
if (transformer === 'ts-jest') {
|
42
|
+
testOperator.mergeJestConfig({
|
43
|
+
transform: {
|
44
|
+
'\\.[jt]sx?$': require.resolve('ts-jest')
|
45
|
+
}
|
46
|
+
});
|
47
|
+
const compilerOptions = resolveTsCompilerOptions();
|
48
|
+
compilerOptions && testOperator.mergeJestConfig({
|
49
|
+
globals: {
|
50
|
+
'ts-jest': {
|
51
|
+
tsconfig: compilerOptions
|
52
|
+
}
|
53
|
+
}
|
54
|
+
});
|
55
|
+
}
|
56
|
+
};
|
57
|
+
|
58
|
+
exports.patchTransformer = patchTransformer;
|
@@ -0,0 +1,20 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
var _enhancedResolve = _interopRequireDefault(require("enhanced-resolve"));
|
4
|
+
|
5
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
6
|
+
|
7
|
+
const resolver = _enhancedResolve.default.create.sync({
|
8
|
+
conditionNames: ['require', 'node', 'default'],
|
9
|
+
extensions: ['.js', '.json', '.node', '.ts', '.tsx']
|
10
|
+
});
|
11
|
+
|
12
|
+
const shouldResolveByEnhance = url => /^@[^/]+\/[^/]+\/.*/.test(url);
|
13
|
+
|
14
|
+
module.exports = function (request, options) {
|
15
|
+
if (shouldResolveByEnhance(request)) {
|
16
|
+
return resolver(options.basedir, request);
|
17
|
+
}
|
18
|
+
|
19
|
+
return options.defaultResolver(request, options);
|
20
|
+
};
|
@@ -0,0 +1,93 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.TestConfigOperator = void 0;
|
7
|
+
|
8
|
+
var _lodash = require("@modern-js/utils/lodash");
|
9
|
+
|
10
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
11
|
+
|
12
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
13
|
+
|
14
|
+
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; }
|
15
|
+
|
16
|
+
class TestConfigOperator {
|
17
|
+
constructor(testConfig) {
|
18
|
+
_defineProperty(this, "_testConfig", void 0);
|
19
|
+
|
20
|
+
_defineProperty(this, "_jestConfig", void 0);
|
21
|
+
|
22
|
+
_defineProperty(this, "userJestConfig", void 0);
|
23
|
+
|
24
|
+
_defineProperty(this, "defaultTestConfig", {
|
25
|
+
transformer: 'babel-jest'
|
26
|
+
});
|
27
|
+
|
28
|
+
this._testConfig = testConfig;
|
29
|
+
this._jestConfig = {};
|
30
|
+
this.userJestConfig = testConfig.jest;
|
31
|
+
this.initial();
|
32
|
+
}
|
33
|
+
|
34
|
+
initial() {
|
35
|
+
this._testConfig = (0, _lodash.merge)({}, this.defaultTestConfig, this.testConfig);
|
36
|
+
}
|
37
|
+
|
38
|
+
get jestConfig() {
|
39
|
+
return this._jestConfig;
|
40
|
+
}
|
41
|
+
|
42
|
+
get testConfig() {
|
43
|
+
return this._testConfig;
|
44
|
+
}
|
45
|
+
|
46
|
+
mergeJestConfig(sourceConfig) {
|
47
|
+
this._jestConfig = (0, _lodash.merge)({}, this._jestConfig, sourceConfig);
|
48
|
+
}
|
49
|
+
|
50
|
+
setJestUserConfig() {
|
51
|
+
const {
|
52
|
+
userJestConfig
|
53
|
+
} = this;
|
54
|
+
|
55
|
+
if (typeof userJestConfig === 'object') {
|
56
|
+
this.setJestConfig(userJestConfig);
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
60
|
+
setJestConfig(sourceConfig, options) {
|
61
|
+
if (options) {
|
62
|
+
const {
|
63
|
+
force
|
64
|
+
} = options;
|
65
|
+
|
66
|
+
if (force) {
|
67
|
+
this._jestConfig = sourceConfig;
|
68
|
+
return;
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
this._jestConfig = _objectSpread(_objectSpread({}, this._jestConfig), sourceConfig);
|
73
|
+
}
|
74
|
+
|
75
|
+
getFinalConfig() {
|
76
|
+
const {
|
77
|
+
userJestConfig
|
78
|
+
} = this;
|
79
|
+
|
80
|
+
if (!userJestConfig) {
|
81
|
+
return this._jestConfig;
|
82
|
+
}
|
83
|
+
|
84
|
+
if (typeof userJestConfig === 'function') {
|
85
|
+
return userJestConfig(this._jestConfig);
|
86
|
+
}
|
87
|
+
|
88
|
+
return this.jestConfig;
|
89
|
+
}
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
exports.TestConfigOperator = TestConfigOperator;
|
@@ -0,0 +1,23 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.default = void 0;
|
7
|
+
|
8
|
+
var _babelJest = _interopRequireDefault(require("babel-jest"));
|
9
|
+
|
10
|
+
var _babelJest$createTran;
|
11
|
+
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
13
|
+
|
14
|
+
const babelTransformer = (_babelJest$createTran = _babelJest.default.createTransformer) === null || _babelJest$createTran === void 0 ? void 0 : _babelJest$createTran.call(_babelJest.default, {
|
15
|
+
presets: [[require.resolve('@modern-js/babel-preset-app'), {
|
16
|
+
appDirectory: process.cwd(),
|
17
|
+
modules: 'cjs'
|
18
|
+
}]],
|
19
|
+
configFile: false,
|
20
|
+
babelrc: false
|
21
|
+
});
|
22
|
+
var _default = babelTransformer;
|
23
|
+
exports.default = _default;
|
@@ -0,0 +1,18 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.testingHooks = exports.jestConfigHook = exports.afterTestHook = void 0;
|
7
|
+
|
8
|
+
var _plugin = require("@modern-js/plugin");
|
9
|
+
|
10
|
+
const jestConfigHook = (0, _plugin.createAsyncPipeline)();
|
11
|
+
exports.jestConfigHook = jestConfigHook;
|
12
|
+
const afterTestHook = (0, _plugin.createParallelWorkflow)();
|
13
|
+
exports.afterTestHook = afterTestHook;
|
14
|
+
const testingHooks = {
|
15
|
+
jestConfig: (0, _plugin.createAsyncPipeline)(),
|
16
|
+
afterTest: (0, _plugin.createParallelWorkflow)()
|
17
|
+
};
|
18
|
+
exports.testingHooks = testingHooks;
|
@@ -0,0 +1,73 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
var _exportNames = {
|
7
|
+
runTest: true,
|
8
|
+
runJest: true,
|
9
|
+
getModuleNameMapper: true,
|
10
|
+
DEFAULT_RESOLVER_PATH: true,
|
11
|
+
TestConfigOperator: true,
|
12
|
+
testingHooks: true
|
13
|
+
};
|
14
|
+
Object.defineProperty(exports, "DEFAULT_RESOLVER_PATH", {
|
15
|
+
enumerable: true,
|
16
|
+
get: function () {
|
17
|
+
return _config.DEFAULT_RESOLVER_PATH;
|
18
|
+
}
|
19
|
+
});
|
20
|
+
Object.defineProperty(exports, "TestConfigOperator", {
|
21
|
+
enumerable: true,
|
22
|
+
get: function () {
|
23
|
+
return _testConfigOperator.TestConfigOperator;
|
24
|
+
}
|
25
|
+
});
|
26
|
+
Object.defineProperty(exports, "getModuleNameMapper", {
|
27
|
+
enumerable: true,
|
28
|
+
get: function () {
|
29
|
+
return _utils.getModuleNameMapper;
|
30
|
+
}
|
31
|
+
});
|
32
|
+
Object.defineProperty(exports, "runJest", {
|
33
|
+
enumerable: true,
|
34
|
+
get: function () {
|
35
|
+
return _runJest.runJest;
|
36
|
+
}
|
37
|
+
});
|
38
|
+
Object.defineProperty(exports, "runTest", {
|
39
|
+
enumerable: true,
|
40
|
+
get: function () {
|
41
|
+
return _runJest.runTest;
|
42
|
+
}
|
43
|
+
});
|
44
|
+
Object.defineProperty(exports, "testingHooks", {
|
45
|
+
enumerable: true,
|
46
|
+
get: function () {
|
47
|
+
return _hook.testingHooks;
|
48
|
+
}
|
49
|
+
});
|
50
|
+
|
51
|
+
var _runJest = require("./runJest");
|
52
|
+
|
53
|
+
var _utils = require("./utils");
|
54
|
+
|
55
|
+
var _config = require("./config");
|
56
|
+
|
57
|
+
var _testConfigOperator = require("./config/testConfigOperator");
|
58
|
+
|
59
|
+
var _hook = require("./hook");
|
60
|
+
|
61
|
+
var _types = require("./types");
|
62
|
+
|
63
|
+
Object.keys(_types).forEach(function (key) {
|
64
|
+
if (key === "default" || key === "__esModule") return;
|
65
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
66
|
+
if (key in exports && exports[key] === _types[key]) return;
|
67
|
+
Object.defineProperty(exports, key, {
|
68
|
+
enumerable: true,
|
69
|
+
get: function () {
|
70
|
+
return _types[key];
|
71
|
+
}
|
72
|
+
});
|
73
|
+
});
|
@@ -0,0 +1,109 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.runJest = runJest;
|
7
|
+
exports.runTest = runTest;
|
8
|
+
|
9
|
+
var _yargs = _interopRequireDefault(require("yargs/yargs"));
|
10
|
+
|
11
|
+
var _jest = require("jest");
|
12
|
+
|
13
|
+
var _utils = require("@modern-js/utils");
|
14
|
+
|
15
|
+
var _config = require("./config");
|
16
|
+
|
17
|
+
var _utils2 = require("./utils");
|
18
|
+
|
19
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
20
|
+
|
21
|
+
/**
|
22
|
+
* @file run jest by nodejs API
|
23
|
+
* @description
|
24
|
+
* Jest does not provide node API to run jest.
|
25
|
+
* The followed code is inspired by
|
26
|
+
* https://github.com/facebook/jest/blob/fdc74af37235354e077edeeee8aa2d1a4a863032/packages/jest-cli/src/cli/index.ts#L21
|
27
|
+
*/
|
28
|
+
const buildArgv = async (rawArgv, config) => {
|
29
|
+
const argv = await (0, _yargs.default)(rawArgv).argv;
|
30
|
+
const result = {
|
31
|
+
$0: argv.$0,
|
32
|
+
_: argv._.slice(1)
|
33
|
+
};
|
34
|
+
Object.keys(argv).forEach(key => {
|
35
|
+
if (key.includes('-') || key === '_') {
|
36
|
+
return;
|
37
|
+
}
|
38
|
+
|
39
|
+
result[key] = argv[key];
|
40
|
+
});
|
41
|
+
|
42
|
+
if (config) {
|
43
|
+
result.config = JSON.stringify(config);
|
44
|
+
}
|
45
|
+
|
46
|
+
return result;
|
47
|
+
};
|
48
|
+
|
49
|
+
const readResultsAndExit = (result, globalConfig) => {
|
50
|
+
const code = !result || result.success ? 0 : globalConfig.testFailureExitCode; // Only exit if needed
|
51
|
+
|
52
|
+
process.on('exit', () => {
|
53
|
+
if (typeof code === 'number' && code !== 0) {
|
54
|
+
process.exitCode = code;
|
55
|
+
}
|
56
|
+
});
|
57
|
+
|
58
|
+
if (globalConfig.forceExit) {
|
59
|
+
if (!globalConfig.detectOpenHandles) {
|
60
|
+
console.warn(`${_utils.chalk.bold('Force exiting Jest: ')}Have you considered using \`--detectOpenHandles\` to detect ` + `async operations that kept running after all tests finished?`);
|
61
|
+
} // eslint-disable-next-line no-process-exit
|
62
|
+
|
63
|
+
|
64
|
+
process.exit(code);
|
65
|
+
} else if (!globalConfig.detectOpenHandles) {
|
66
|
+
setTimeout(() => {
|
67
|
+
console.warn(_utils.chalk.yellow.bold('Jest did not exit one second after the test run has completed.\n\n') + _utils.chalk.yellow('This usually means that there are asynchronous operations that ' + "weren't stopped in your tests. Consider running Jest with " + '`--detectOpenHandles` to troubleshoot this issue.'));
|
68
|
+
}, 1000).unref();
|
69
|
+
}
|
70
|
+
};
|
71
|
+
/**
|
72
|
+
* Node API: execute jest
|
73
|
+
*/
|
74
|
+
|
75
|
+
|
76
|
+
async function runJest(config, pwd = process.cwd()) {
|
77
|
+
try {
|
78
|
+
const argvConfig = await buildArgv(process.argv.slice(2), config);
|
79
|
+
const {
|
80
|
+
results,
|
81
|
+
globalConfig
|
82
|
+
} = await (0, _jest.runCLI)(argvConfig, [pwd]);
|
83
|
+
readResultsAndExit(results, globalConfig);
|
84
|
+
} catch (e) {
|
85
|
+
console.error(_utils.chalk.red((e === null || e === void 0 ? void 0 : e.stack) || e)); // eslint-disable-next-line no-process-exit
|
86
|
+
|
87
|
+
process.exit(1);
|
88
|
+
}
|
89
|
+
}
|
90
|
+
/**
|
91
|
+
* Node API: run test
|
92
|
+
*/
|
93
|
+
|
94
|
+
|
95
|
+
async function runTest(api, config, pwd = process.cwd()) {
|
96
|
+
process.env.NODE_ENV = 'test';
|
97
|
+
const jestUtils = (0, _config.getJestUtils)(config);
|
98
|
+
await (0, _config.patchConfig)(jestUtils); // 确保用户设置的配置可以被插件处理,比如设置在 projects 中
|
99
|
+
|
100
|
+
jestUtils.setJestUserConfig();
|
101
|
+
const hookRunners = api.useHookRunners();
|
102
|
+
const testConfigOperator = await hookRunners.jestConfig(jestUtils, {
|
103
|
+
onLast: input => input
|
104
|
+
});
|
105
|
+
const finalConfig = testConfigOperator.getFinalConfig();
|
106
|
+
(0, _utils2.debug)('Jest config:', finalConfig);
|
107
|
+
await runJest(finalConfig, pwd);
|
108
|
+
await hookRunners.afterTest();
|
109
|
+
}
|
File without changes
|
@@ -0,0 +1,79 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.readCompilerOptions = exports.getModuleNameMapper = exports.debug = void 0;
|
7
|
+
|
8
|
+
var _fs = _interopRequireDefault(require("fs"));
|
9
|
+
|
10
|
+
var _path = _interopRequireDefault(require("path"));
|
11
|
+
|
12
|
+
var _utils = require("@modern-js/utils");
|
13
|
+
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
15
|
+
|
16
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
17
|
+
|
18
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
19
|
+
|
20
|
+
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; }
|
21
|
+
|
22
|
+
const debug = (0, _utils.createDebugger)('test');
|
23
|
+
exports.debug = debug;
|
24
|
+
|
25
|
+
/**
|
26
|
+
* Read `compilerOptions` in the current pwd's tsconfig.json file
|
27
|
+
*/
|
28
|
+
const readCompilerOptions = (pwd = process.cwd(), filename = 'tsconfig.json') => {
|
29
|
+
let tsConfig = {};
|
30
|
+
let extendedCompilerOptions = {};
|
31
|
+
let tsconfigFile = '';
|
32
|
+
|
33
|
+
try {
|
34
|
+
const maybeTsconfigFile = _path.default.join(pwd, filename);
|
35
|
+
|
36
|
+
if (_fs.default.existsSync(maybeTsconfigFile)) {
|
37
|
+
tsconfigFile = maybeTsconfigFile;
|
38
|
+
} else {
|
39
|
+
tsconfigFile = require.resolve(filename);
|
40
|
+
}
|
41
|
+
|
42
|
+
({
|
43
|
+
config: tsConfig
|
44
|
+
} = require('typescript').parseConfigFileTextToJson(tsconfigFile, _fs.default.readFileSync(tsconfigFile, 'utf8')));
|
45
|
+
} catch (e) {
|
46
|
+
return {};
|
47
|
+
}
|
48
|
+
|
49
|
+
if (tsConfig.extends) {
|
50
|
+
extendedCompilerOptions = readCompilerOptions(_path.default.dirname(tsconfigFile), tsConfig.extends);
|
51
|
+
}
|
52
|
+
|
53
|
+
return _objectSpread(_objectSpread({}, extendedCompilerOptions), tsConfig.compilerOptions);
|
54
|
+
};
|
55
|
+
|
56
|
+
exports.readCompilerOptions = readCompilerOptions;
|
57
|
+
|
58
|
+
const getModuleNameMapper = alias => Object.keys(alias).reduce((memo, cur) => {
|
59
|
+
const aliasValue = Array.isArray(alias[cur]) ? alias[cur] : [alias[cur]];
|
60
|
+
const isFile = aliasValue.some(s => s.endsWith('.js')); // It's special for if using @modern-js/runtime alias other module @modern-js/runtime/model would not work.
|
61
|
+
|
62
|
+
if (cur === '@modern-js/runtime$') {
|
63
|
+
memo[`.+${cur}`] = aliasValue[0];
|
64
|
+
return memo;
|
65
|
+
}
|
66
|
+
|
67
|
+
if (isFile) {
|
68
|
+
memo[cur] = aliasValue[0];
|
69
|
+
}
|
70
|
+
|
71
|
+
const key = `^${cur}/(.*)$`;
|
72
|
+
|
73
|
+
const value = _path.default.normalize(`${aliasValue}/$1`);
|
74
|
+
|
75
|
+
memo[key] = value;
|
76
|
+
return memo;
|
77
|
+
}, {});
|
78
|
+
|
79
|
+
exports.getModuleNameMapper = getModuleNameMapper;
|