@modern-js/plugin-testing 1.5.7 → 1.7.1-beta.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.
Files changed (100) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/dist/js/modern/base/config/index.js +18 -0
  3. package/dist/js/modern/base/config/patches/assetsModule.js +11 -0
  4. package/dist/js/modern/base/config/patches/filemock.js +1 -0
  5. package/dist/js/modern/base/config/patches/index.js +13 -0
  6. package/dist/js/modern/base/config/patches/transformer.js +49 -0
  7. package/dist/js/modern/base/config/resolver.js +15 -0
  8. package/dist/js/modern/base/config/testConfigOperator.js +86 -0
  9. package/dist/js/modern/base/config/transformer/babelTransformer.js +12 -0
  10. package/dist/js/modern/base/hook.js +7 -0
  11. package/dist/js/modern/base/index.js +6 -0
  12. package/dist/js/modern/base/runJest.js +94 -0
  13. package/dist/js/modern/base/types/index.js +0 -0
  14. package/dist/js/modern/base/utils.js +59 -0
  15. package/dist/js/modern/cli/bff/app.js +43 -0
  16. package/dist/js/modern/cli/bff/constant.js +1 -0
  17. package/dist/js/modern/cli/bff/env.js +28 -0
  18. package/dist/js/modern/cli/bff/index.js +113 -0
  19. package/dist/js/modern/cli/bff/mockAPI.js +111 -0
  20. package/dist/js/modern/cli/bff/setup.js +44 -0
  21. package/dist/js/modern/cli/bff/utils/index.js +25 -0
  22. package/dist/js/modern/cli/index.js +2 -2
  23. package/dist/js/modern/cli/test.js +1 -1
  24. package/dist/js/modern/runtime-testing/app.js +1 -1
  25. package/dist/js/modern/runtime-testing/base.js +1 -1
  26. package/dist/js/modern/runtime-testing/request.js +17 -0
  27. package/dist/js/node/base/config/index.js +31 -0
  28. package/dist/js/node/base/config/patches/assetsModule.js +20 -0
  29. package/dist/js/node/base/config/patches/filemock.js +8 -0
  30. package/dist/js/node/base/config/patches/index.js +24 -0
  31. package/dist/js/node/base/config/patches/transformer.js +58 -0
  32. package/dist/js/node/base/config/resolver.js +20 -0
  33. package/dist/js/node/base/config/testConfigOperator.js +93 -0
  34. package/dist/js/node/base/config/transformer/babelTransformer.js +23 -0
  35. package/dist/js/node/base/hook.js +18 -0
  36. package/dist/js/node/base/index.js +73 -0
  37. package/dist/js/node/base/runJest.js +109 -0
  38. package/dist/js/node/base/types/index.js +0 -0
  39. package/dist/js/node/base/utils.js +79 -0
  40. package/dist/js/node/cli/bff/app.js +59 -0
  41. package/dist/js/node/cli/bff/constant.js +8 -0
  42. package/dist/js/node/cli/bff/env.js +42 -0
  43. package/dist/js/node/cli/bff/index.js +132 -0
  44. package/dist/js/node/cli/bff/mockAPI.js +126 -0
  45. package/dist/js/node/cli/bff/setup.js +53 -0
  46. package/dist/js/node/cli/bff/utils/index.js +38 -0
  47. package/dist/js/node/cli/index.js +7 -7
  48. package/dist/js/node/cli/test.js +2 -2
  49. package/dist/js/node/runtime-testing/app.js +2 -2
  50. package/dist/js/node/runtime-testing/base.js +2 -2
  51. package/dist/js/node/runtime-testing/request.js +24 -0
  52. package/dist/js/treeshaking/base/config/index.js +40 -0
  53. package/dist/js/treeshaking/base/config/patches/assetsModule.js +11 -0
  54. package/dist/js/treeshaking/base/config/patches/filemock.js +1 -0
  55. package/dist/js/treeshaking/base/config/patches/index.js +85 -0
  56. package/dist/js/treeshaking/base/config/patches/transformer.js +45 -0
  57. package/dist/js/treeshaking/base/config/resolver.js +17 -0
  58. package/dist/js/treeshaking/base/config/testConfigOperator.js +91 -0
  59. package/dist/js/treeshaking/base/config/transformer/babelTransformer.js +12 -0
  60. package/dist/js/treeshaking/base/hook.js +7 -0
  61. package/dist/js/treeshaking/base/index.js +6 -0
  62. package/dist/js/treeshaking/base/runJest.js +194 -0
  63. package/dist/js/treeshaking/base/types/index.js +0 -0
  64. package/dist/js/treeshaking/base/utils.js +60 -0
  65. package/dist/js/treeshaking/cli/bff/app.js +92 -0
  66. package/dist/js/treeshaking/cli/bff/constant.js +1 -0
  67. package/dist/js/treeshaking/cli/bff/env.js +98 -0
  68. package/dist/js/treeshaking/cli/bff/index.js +149 -0
  69. package/dist/js/treeshaking/cli/bff/mockAPI.js +115 -0
  70. package/dist/js/treeshaking/cli/bff/setup.js +76 -0
  71. package/dist/js/treeshaking/cli/bff/utils/index.js +22 -0
  72. package/dist/js/treeshaking/cli/index.js +2 -2
  73. package/dist/js/treeshaking/cli/test.js +1 -1
  74. package/dist/js/treeshaking/runtime-testing/app.js +1 -1
  75. package/dist/js/treeshaking/runtime-testing/base.js +1 -1
  76. package/dist/js/treeshaking/runtime-testing/request.js +24 -0
  77. package/dist/types/base/config/index.d.ts +124 -0
  78. package/dist/types/base/config/patches/assetsModule.d.ts +6 -0
  79. package/dist/types/base/config/patches/filemock.d.ts +3 -0
  80. package/dist/types/base/config/patches/index.d.ts +2 -0
  81. package/dist/types/base/config/patches/transformer.d.ts +6 -0
  82. package/dist/types/base/config/resolver.d.ts +1 -0
  83. package/dist/types/base/config/testConfigOperator.d.ts +248 -0
  84. package/dist/types/base/config/transformer/babelTransformer.d.ts +2 -0
  85. package/dist/types/base/hook.d.ts +13 -0
  86. package/dist/types/base/index.d.ts +6 -0
  87. package/dist/types/base/runJest.d.ts +22 -0
  88. package/dist/types/base/types/index.d.ts +1 -0
  89. package/dist/types/base/utils.d.ts +12 -0
  90. package/dist/types/cli/bff/app.d.ts +8 -0
  91. package/dist/types/cli/bff/constant.d.ts +1 -0
  92. package/dist/types/cli/bff/env.d.ts +6 -0
  93. package/dist/types/cli/bff/index.d.ts +19 -0
  94. package/dist/types/cli/bff/mockAPI.d.ts +3 -0
  95. package/dist/types/cli/bff/setup.d.ts +1 -0
  96. package/dist/types/cli/bff/utils/index.d.ts +1 -0
  97. package/dist/types/cli/index.d.ts +1 -1
  98. package/dist/types/runtime-testing/base.d.ts +1 -1
  99. package/dist/types/runtime-testing/request.d.ts +4 -0
  100. package/package.json +47 -11
@@ -9,14 +9,14 @@ var _path = _interopRequireDefault(require("path"));
9
9
 
10
10
  var _utils = require("@modern-js/utils");
11
11
 
12
- var _testing = require("@modern-js/testing");
13
-
14
12
  var _webpack = require("@modern-js/webpack");
15
13
 
16
- var _testingPluginBff = _interopRequireDefault(require("@modern-js/testing-plugin-bff"));
14
+ var _base = require("../base");
17
15
 
18
16
  var _constant = require("../constant");
19
17
 
18
+ var _bff = _interopRequireDefault(require("./bff"));
19
+
20
20
  var _test = _interopRequireDefault(require("./test"));
21
21
 
22
22
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -32,12 +32,12 @@ const mergeUserJestConfig = testUtils => {
32
32
  exports.mergeUserJestConfig = mergeUserJestConfig;
33
33
 
34
34
  var _default = () => {
35
- const BffPlugin = (0, _testingPluginBff.default)();
35
+ const BffPlugin = (0, _bff.default)();
36
36
  return {
37
37
  name: '@modern-js/plugin-testing',
38
38
  usePlugins: [BffPlugin],
39
39
  post: [BffPlugin.name],
40
- registerHook: _testing.testingHooks,
40
+ registerHook: _base.testingHooks,
41
41
  setup: api => {
42
42
  let testingExportsUtils;
43
43
  return {
@@ -90,9 +90,9 @@ var _default = () => {
90
90
  globals: {
91
91
  [_constant.MODERNJS_CONFIG_KEY]: userConfig
92
92
  },
93
- moduleNameMapper: (0, _testing.getModuleNameMapper)(alias),
93
+ moduleNameMapper: (0, _base.getModuleNameMapper)(alias),
94
94
  testEnvironment: 'jsdom',
95
- resolver: _testing.DEFAULT_RESOLVER_PATH,
95
+ resolver: _base.DEFAULT_RESOLVER_PATH,
96
96
  rootDir: appContext.appDirectory || process.cwd(),
97
97
  // todo: diffrernt test root for diffrent solutions
98
98
  // testMatch: [`<rootDir>/(src|tests|electron)/**/*.test.[jt]s?(x)`],
@@ -9,7 +9,7 @@ var _path = _interopRequireDefault(require("path"));
9
9
 
10
10
  var _babelCompiler = require("@modern-js/babel-compiler");
11
11
 
12
- var _testing = require("@modern-js/testing");
12
+ var _base = require("../base");
13
13
 
14
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
15
 
@@ -37,7 +37,7 @@ const test = async api => {
37
37
  modules: 'cjs'
38
38
  }]]
39
39
  });
40
- await (0, _testing.runTest)(api, userConfig.testing);
40
+ await (0, _base.runTest)(api, userConfig.testing);
41
41
  };
42
42
 
43
43
  var _default = test;
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _runtimeCore = require("@modern-js/runtime-core");
8
+ var _runtime = require("@modern-js/runtime");
9
9
 
10
10
  var _constant = require("../constant");
11
11
 
@@ -53,7 +53,7 @@ class ModernRuntime {
53
53
  return children;
54
54
  };
55
55
 
56
- return (0, _runtimeCore.createApp)({
56
+ return (0, _runtime.createApp)({
57
57
  plugins: (0, _resolvePlugins.default)(runtimeFeatures || {})
58
58
  })(Component);
59
59
  }
@@ -16,7 +16,7 @@ Object.defineProperty(exports, "renderApp", {
16
16
  Object.defineProperty(exports, "testBff", {
17
17
  enumerable: true,
18
18
  get: function () {
19
- return _testingPluginBff.testBff;
19
+ return _request.request;
20
20
  }
21
21
  });
22
22
 
@@ -36,6 +36,6 @@ Object.keys(_react).forEach(function (key) {
36
36
  });
37
37
  });
38
38
 
39
- var _testingPluginBff = require("@modern-js/testing-plugin-bff");
39
+ var _request = require("./request");
40
40
 
41
41
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.request = request;
7
+
8
+ var _supertest = _interopRequireDefault(require("supertest"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+
12
+ /* eslint-disable eslint-comments/disable-enable-pair */
13
+ function request(...args) {
14
+ const [fn, ...extraArgs] = args;
15
+
16
+ if (!fn) {
17
+ return (0, _supertest.default)(global.app);
18
+ }
19
+
20
+ fn.returnHttp = true;
21
+ const res = fn(...extraArgs);
22
+ fn.returnHttp = false;
23
+ return res;
24
+ }
@@ -0,0 +1,40 @@
1
+ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
+ import { applyPatches } from "./patches";
4
+ import { TestConfigOperator } from "./testConfigOperator";
5
+ /**
6
+ * Parse jest config
7
+ */
8
+
9
+ var getJestUtils = function getJestUtils(testConfig) {
10
+ var testOperator = new TestConfigOperator(testConfig);
11
+ return testOperator;
12
+ };
13
+
14
+ var patchConfig = /*#__PURE__*/function () {
15
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(testOperator) {
16
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
17
+ while (1) {
18
+ switch (_context.prev = _context.next) {
19
+ case 0:
20
+ _context.next = 2;
21
+ return applyPatches(testOperator);
22
+
23
+ case 2:
24
+ return _context.abrupt("return", testOperator.jestConfig);
25
+
26
+ case 3:
27
+ case "end":
28
+ return _context.stop();
29
+ }
30
+ }
31
+ }, _callee);
32
+ }));
33
+
34
+ return function patchConfig(_x) {
35
+ return _ref.apply(this, arguments);
36
+ };
37
+ }();
38
+
39
+ export var DEFAULT_RESOLVER_PATH = require.resolve("./resolver");
40
+ export { getJestUtils, patchConfig };
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Merge config from testConfig.jest
3
+ */
4
+ export var patchAssetsModule = function patchAssetsModule(testOperator) {
5
+ testOperator.mergeJestConfig({
6
+ moduleNameMapper: {
7
+ '\\.(css|less|scss|sass)$': require.resolve('identity-obj-proxy'),
8
+ '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': require.resolve("./filemock.js")
9
+ }
10
+ });
11
+ };
@@ -0,0 +1 @@
1
+ export default 'test-file-stub';
@@ -0,0 +1,85 @@
1
+ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
+ import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
3
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
4
+ import { patchTransformer } from "./transformer";
5
+ import { patchAssetsModule } from "./assetsModule";
6
+
7
+ var _applyPatches = /*#__PURE__*/function () {
8
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(patches, testOperator) {
9
+ var _iterator, _step, patch;
10
+
11
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
12
+ while (1) {
13
+ switch (_context.prev = _context.next) {
14
+ case 0:
15
+ _iterator = _createForOfIteratorHelper(patches);
16
+ _context.prev = 1;
17
+
18
+ _iterator.s();
19
+
20
+ case 3:
21
+ if ((_step = _iterator.n()).done) {
22
+ _context.next = 9;
23
+ break;
24
+ }
25
+
26
+ patch = _step.value;
27
+ _context.next = 7;
28
+ return patch(testOperator);
29
+
30
+ case 7:
31
+ _context.next = 3;
32
+ break;
33
+
34
+ case 9:
35
+ _context.next = 14;
36
+ break;
37
+
38
+ case 11:
39
+ _context.prev = 11;
40
+ _context.t0 = _context["catch"](1);
41
+
42
+ _iterator.e(_context.t0);
43
+
44
+ case 14:
45
+ _context.prev = 14;
46
+
47
+ _iterator.f();
48
+
49
+ return _context.finish(14);
50
+
51
+ case 17:
52
+ case "end":
53
+ return _context.stop();
54
+ }
55
+ }
56
+ }, _callee, null, [[1, 11, 14, 17]]);
57
+ }));
58
+
59
+ return function _applyPatches(_x, _x2) {
60
+ return _ref.apply(this, arguments);
61
+ };
62
+ }();
63
+
64
+ var patches = [patchTransformer, patchAssetsModule];
65
+ export var applyPatches = /*#__PURE__*/function () {
66
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(testConfig) {
67
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
68
+ while (1) {
69
+ switch (_context2.prev = _context2.next) {
70
+ case 0:
71
+ _context2.next = 2;
72
+ return _applyPatches(patches, testConfig);
73
+
74
+ case 2:
75
+ case "end":
76
+ return _context2.stop();
77
+ }
78
+ }
79
+ }, _callee2);
80
+ }));
81
+
82
+ return function applyPatches(_x3) {
83
+ return _ref2.apply(this, arguments);
84
+ };
85
+ }();
@@ -0,0 +1,45 @@
1
+ import { readCompilerOptions } from "../../utils";
2
+
3
+ var resolveTsCompilerOptions = function resolveTsCompilerOptions() {
4
+ var tsCompilerOptions = readCompilerOptions() || {};
5
+ var jsx = tsCompilerOptions.jsx;
6
+
7
+ if (!jsx) {
8
+ return null;
9
+ }
10
+
11
+ tsCompilerOptions.jsx = 'react-jsx';
12
+ return tsCompilerOptions;
13
+ };
14
+ /**
15
+ * Map `TestConfig.transformer` to jest config
16
+ */
17
+
18
+
19
+ export var patchTransformer = function patchTransformer(testOperator) {
20
+ var transformer = testOperator.testConfig.transformer;
21
+
22
+ if (transformer === 'babel-jest') {
23
+ testOperator.mergeJestConfig({
24
+ transform: {
25
+ '\\.[jt]sx?$': require.resolve("../transformer/babelTransformer")
26
+ }
27
+ });
28
+ }
29
+
30
+ if (transformer === 'ts-jest') {
31
+ testOperator.mergeJestConfig({
32
+ transform: {
33
+ '\\.[jt]sx?$': require.resolve('ts-jest')
34
+ }
35
+ });
36
+ var compilerOptions = resolveTsCompilerOptions();
37
+ compilerOptions && testOperator.mergeJestConfig({
38
+ globals: {
39
+ 'ts-jest': {
40
+ tsconfig: compilerOptions
41
+ }
42
+ }
43
+ });
44
+ }
45
+ };
@@ -0,0 +1,17 @@
1
+ import enhanceResolve from 'enhanced-resolve';
2
+ var resolver = enhanceResolve.create.sync({
3
+ conditionNames: ['require', 'node', 'default'],
4
+ extensions: ['.js', '.json', '.node', '.ts', '.tsx']
5
+ });
6
+
7
+ var shouldResolveByEnhance = function shouldResolveByEnhance(url) {
8
+ return /^@[^/]+\/[^/]+\/.*/.test(url);
9
+ };
10
+
11
+ module.exports = function (request, options) {
12
+ if (shouldResolveByEnhance(request)) {
13
+ return resolver(options.basedir, request);
14
+ }
15
+
16
+ return options.defaultResolver(request, options);
17
+ };
@@ -0,0 +1,91 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import _typeof from "@babel/runtime/helpers/esm/typeof";
3
+ import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
4
+ import _createClass from "@babel/runtime/helpers/esm/createClass";
5
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
6
+ import { merge } from '@modern-js/utils/lodash';
7
+
8
+ var TestConfigOperator = /*#__PURE__*/function () {
9
+ function TestConfigOperator(testConfig) {
10
+ _classCallCheck(this, TestConfigOperator);
11
+
12
+ _defineProperty(this, "_testConfig", void 0);
13
+
14
+ _defineProperty(this, "_jestConfig", void 0);
15
+
16
+ _defineProperty(this, "userJestConfig", void 0);
17
+
18
+ _defineProperty(this, "defaultTestConfig", {
19
+ transformer: 'babel-jest'
20
+ });
21
+
22
+ this._testConfig = testConfig;
23
+ this._jestConfig = {};
24
+ this.userJestConfig = testConfig.jest;
25
+ this.initial();
26
+ }
27
+
28
+ _createClass(TestConfigOperator, [{
29
+ key: "initial",
30
+ value: function initial() {
31
+ this._testConfig = merge({}, this.defaultTestConfig, this.testConfig);
32
+ }
33
+ }, {
34
+ key: "jestConfig",
35
+ get: function get() {
36
+ return this._jestConfig;
37
+ }
38
+ }, {
39
+ key: "testConfig",
40
+ get: function get() {
41
+ return this._testConfig;
42
+ }
43
+ }, {
44
+ key: "mergeJestConfig",
45
+ value: function mergeJestConfig(sourceConfig) {
46
+ this._jestConfig = merge({}, this._jestConfig, sourceConfig);
47
+ }
48
+ }, {
49
+ key: "setJestUserConfig",
50
+ value: function setJestUserConfig() {
51
+ var userJestConfig = this.userJestConfig;
52
+
53
+ if (_typeof(userJestConfig) === 'object') {
54
+ this.setJestConfig(userJestConfig);
55
+ }
56
+ }
57
+ }, {
58
+ key: "setJestConfig",
59
+ value: function setJestConfig(sourceConfig, options) {
60
+ if (options) {
61
+ var force = options.force;
62
+
63
+ if (force) {
64
+ this._jestConfig = sourceConfig;
65
+ return;
66
+ }
67
+ }
68
+
69
+ this._jestConfig = _objectSpread(_objectSpread({}, this._jestConfig), sourceConfig);
70
+ }
71
+ }, {
72
+ key: "getFinalConfig",
73
+ value: function getFinalConfig() {
74
+ var userJestConfig = this.userJestConfig;
75
+
76
+ if (!userJestConfig) {
77
+ return this._jestConfig;
78
+ }
79
+
80
+ if (typeof userJestConfig === 'function') {
81
+ return userJestConfig(this._jestConfig);
82
+ }
83
+
84
+ return this.jestConfig;
85
+ }
86
+ }]);
87
+
88
+ return TestConfigOperator;
89
+ }();
90
+
91
+ export { TestConfigOperator };
@@ -0,0 +1,12 @@
1
+ var _babelJest$createTran;
2
+
3
+ import babelJest from 'babel-jest';
4
+ var babelTransformer = (_babelJest$createTran = babelJest.createTransformer) === null || _babelJest$createTran === void 0 ? void 0 : _babelJest$createTran.call(babelJest, {
5
+ presets: [[require.resolve('@modern-js/babel-preset-app'), {
6
+ appDirectory: process.cwd(),
7
+ modules: 'cjs'
8
+ }]],
9
+ configFile: false,
10
+ babelrc: false
11
+ });
12
+ export default babelTransformer;
@@ -0,0 +1,7 @@
1
+ import { createAsyncPipeline, createParallelWorkflow } from '@modern-js/plugin';
2
+ export var jestConfigHook = createAsyncPipeline();
3
+ export var afterTestHook = createParallelWorkflow();
4
+ export var testingHooks = {
5
+ jestConfig: createAsyncPipeline(),
6
+ afterTest: createParallelWorkflow()
7
+ };
@@ -0,0 +1,6 @@
1
+ export { runTest, runJest } from "./runJest";
2
+ export { getModuleNameMapper } from "./utils";
3
+ export { DEFAULT_RESOLVER_PATH } from "./config";
4
+ export { TestConfigOperator } from "./config/testConfigOperator";
5
+ export { testingHooks } from "./hook";
6
+ export * from "./types";
@@ -0,0 +1,194 @@
1
+ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
+
4
+ /**
5
+ * @file run jest by nodejs API
6
+ * @description
7
+ * Jest does not provide node API to run jest.
8
+ * The followed code is inspired by
9
+ * https://github.com/facebook/jest/blob/fdc74af37235354e077edeeee8aa2d1a4a863032/packages/jest-cli/src/cli/index.ts#L21
10
+ */
11
+ import yargs from 'yargs/yargs';
12
+ import { runCLI } from 'jest';
13
+ import { chalk } from '@modern-js/utils';
14
+ import { getJestUtils, patchConfig } from "./config";
15
+ import { debug } from "./utils";
16
+
17
+ var buildArgv = /*#__PURE__*/function () {
18
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(rawArgv, config) {
19
+ var argv, result;
20
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
21
+ while (1) {
22
+ switch (_context.prev = _context.next) {
23
+ case 0:
24
+ _context.next = 2;
25
+ return yargs(rawArgv).argv;
26
+
27
+ case 2:
28
+ argv = _context.sent;
29
+ result = {
30
+ $0: argv.$0,
31
+ _: argv._.slice(1)
32
+ };
33
+ Object.keys(argv).forEach(function (key) {
34
+ if (key.includes('-') || key === '_') {
35
+ return;
36
+ }
37
+
38
+ result[key] = argv[key];
39
+ });
40
+
41
+ if (config) {
42
+ result.config = JSON.stringify(config);
43
+ }
44
+
45
+ return _context.abrupt("return", result);
46
+
47
+ case 7:
48
+ case "end":
49
+ return _context.stop();
50
+ }
51
+ }
52
+ }, _callee);
53
+ }));
54
+
55
+ return function buildArgv(_x, _x2) {
56
+ return _ref.apply(this, arguments);
57
+ };
58
+ }();
59
+
60
+ var readResultsAndExit = function readResultsAndExit(result, globalConfig) {
61
+ var code = !result || result.success ? 0 : globalConfig.testFailureExitCode; // Only exit if needed
62
+
63
+ process.on('exit', function () {
64
+ if (typeof code === 'number' && code !== 0) {
65
+ process.exitCode = code;
66
+ }
67
+ });
68
+
69
+ if (globalConfig.forceExit) {
70
+ if (!globalConfig.detectOpenHandles) {
71
+ console.warn("".concat(chalk.bold('Force exiting Jest: '), "Have you considered using `--detectOpenHandles` to detect ") + "async operations that kept running after all tests finished?");
72
+ } // eslint-disable-next-line no-process-exit
73
+
74
+
75
+ process.exit(code);
76
+ } else if (!globalConfig.detectOpenHandles) {
77
+ setTimeout(function () {
78
+ console.warn(chalk.yellow.bold('Jest did not exit one second after the test run has completed.\n\n') + 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.'));
79
+ }, 1000).unref();
80
+ }
81
+ };
82
+ /**
83
+ * Node API: execute jest
84
+ */
85
+
86
+
87
+ export function runJest(_x3) {
88
+ return _runJest.apply(this, arguments);
89
+ }
90
+ /**
91
+ * Node API: run test
92
+ */
93
+
94
+ function _runJest() {
95
+ _runJest = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(config) {
96
+ var pwd,
97
+ argvConfig,
98
+ _yield$runCLI,
99
+ results,
100
+ globalConfig,
101
+ _args2 = arguments;
102
+
103
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
104
+ while (1) {
105
+ switch (_context2.prev = _context2.next) {
106
+ case 0:
107
+ pwd = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : process.cwd();
108
+ _context2.prev = 1;
109
+ _context2.next = 4;
110
+ return buildArgv(process.argv.slice(2), config);
111
+
112
+ case 4:
113
+ argvConfig = _context2.sent;
114
+ _context2.next = 7;
115
+ return runCLI(argvConfig, [pwd]);
116
+
117
+ case 7:
118
+ _yield$runCLI = _context2.sent;
119
+ results = _yield$runCLI.results;
120
+ globalConfig = _yield$runCLI.globalConfig;
121
+ readResultsAndExit(results, globalConfig);
122
+ _context2.next = 17;
123
+ break;
124
+
125
+ case 13:
126
+ _context2.prev = 13;
127
+ _context2.t0 = _context2["catch"](1);
128
+ console.error(chalk.red((_context2.t0 === null || _context2.t0 === void 0 ? void 0 : _context2.t0.stack) || _context2.t0)); // eslint-disable-next-line no-process-exit
129
+
130
+ process.exit(1);
131
+
132
+ case 17:
133
+ case "end":
134
+ return _context2.stop();
135
+ }
136
+ }
137
+ }, _callee2, null, [[1, 13]]);
138
+ }));
139
+ return _runJest.apply(this, arguments);
140
+ }
141
+
142
+ export function runTest(_x4, _x5) {
143
+ return _runTest.apply(this, arguments);
144
+ }
145
+
146
+ function _runTest() {
147
+ _runTest = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(api, config) {
148
+ var pwd,
149
+ jestUtils,
150
+ hookRunners,
151
+ testConfigOperator,
152
+ finalConfig,
153
+ _args3 = arguments;
154
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
155
+ while (1) {
156
+ switch (_context3.prev = _context3.next) {
157
+ case 0:
158
+ pwd = _args3.length > 2 && _args3[2] !== undefined ? _args3[2] : process.cwd();
159
+ process.env.NODE_ENV = 'test';
160
+ jestUtils = getJestUtils(config);
161
+ _context3.next = 5;
162
+ return patchConfig(jestUtils);
163
+
164
+ case 5:
165
+ // 确保用户设置的配置可以被插件处理,比如设置在 projects 中
166
+ jestUtils.setJestUserConfig();
167
+ hookRunners = api.useHookRunners();
168
+ _context3.next = 9;
169
+ return hookRunners.jestConfig(jestUtils, {
170
+ onLast: function onLast(input) {
171
+ return input;
172
+ }
173
+ });
174
+
175
+ case 9:
176
+ testConfigOperator = _context3.sent;
177
+ finalConfig = testConfigOperator.getFinalConfig();
178
+ debug('Jest config:', finalConfig);
179
+ _context3.next = 14;
180
+ return runJest(finalConfig, pwd);
181
+
182
+ case 14:
183
+ _context3.next = 16;
184
+ return hookRunners.afterTest();
185
+
186
+ case 16:
187
+ case "end":
188
+ return _context3.stop();
189
+ }
190
+ }
191
+ }, _callee3);
192
+ }));
193
+ return _runTest.apply(this, arguments);
194
+ }
File without changes