@modern-js/plugin-testing 2.0.0-beta.3 → 2.0.0-beta.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. package/CHANGELOG.md +77 -0
  2. package/dist/js/modern/base/config/index.js +29 -9
  3. package/dist/js/modern/base/config/patches/assetsModule.js +7 -7
  4. package/dist/js/modern/base/config/patches/filemock.js +4 -1
  5. package/dist/js/modern/base/config/patches/index.js +30 -7
  6. package/dist/js/modern/base/config/patches/transformer.js +13 -18
  7. package/dist/js/modern/base/config/resolver.js +11 -6
  8. package/dist/js/modern/base/config/testConfigOperator.js +29 -23
  9. package/dist/js/modern/base/config/transformer/babelTransformer.js +16 -8
  10. package/dist/js/modern/base/hook.js +10 -5
  11. package/dist/js/modern/base/index.js +14 -6
  12. package/dist/js/modern/base/runJest.js +73 -62
  13. package/dist/js/modern/base/utils.js +40 -23
  14. package/dist/js/modern/cli/bff/app.js +37 -12
  15. package/dist/js/modern/cli/bff/constant.js +4 -1
  16. package/dist/js/modern/cli/bff/index.js +95 -52
  17. package/dist/js/modern/cli/bff/mockAPI.js +79 -64
  18. package/dist/js/modern/cli/bff/setup.js +71 -34
  19. package/dist/js/modern/cli/bff/utils/index.js +31 -15
  20. package/dist/js/modern/cli/index.js +64 -30
  21. package/dist/js/modern/cli/test.js +53 -18
  22. package/dist/js/modern/constant.js +4 -1
  23. package/dist/js/modern/index.js +3 -3
  24. package/dist/js/modern/modern-app.env.d.js +0 -0
  25. package/dist/js/modern/runtime-testing/app.js +25 -14
  26. package/dist/js/modern/runtime-testing/base.js +7 -3
  27. package/dist/js/modern/runtime-testing/customRender.js +24 -10
  28. package/dist/js/modern/runtime-testing/index.js +1 -1
  29. package/dist/js/modern/runtime-testing/reduck.js +41 -19
  30. package/dist/js/modern/runtime-testing/request.js +4 -4
  31. package/dist/js/modern/runtime-testing/resolvePlugins.js +24 -8
  32. package/dist/js/node/base/config/index.js +56 -18
  33. package/dist/js/node/base/config/patches/assetsModule.js +30 -12
  34. package/dist/js/node/base/config/patches/filemock.js +22 -7
  35. package/dist/js/node/base/config/patches/index.js +50 -15
  36. package/dist/js/node/base/config/patches/transformer.js +38 -25
  37. package/dist/js/node/base/config/resolver.js +37 -10
  38. package/dist/js/node/base/config/testConfigOperator.js +49 -30
  39. package/dist/js/node/base/config/transformer/babelTransformer.js +40 -15
  40. package/dist/js/node/base/hook.js +28 -13
  41. package/dist/js/node/base/index.js +31 -63
  42. package/dist/js/node/base/runJest.js +99 -71
  43. package/dist/js/node/base/utils.js +66 -34
  44. package/dist/js/node/cli/bff/app.js +57 -22
  45. package/dist/js/node/cli/bff/constant.js +22 -7
  46. package/dist/js/node/cli/bff/index.js +121 -65
  47. package/dist/js/node/cli/bff/mockAPI.js +103 -71
  48. package/dist/js/node/cli/bff/setup.js +100 -38
  49. package/dist/js/node/cli/bff/utils/index.js +55 -22
  50. package/dist/js/node/cli/index.js +87 -46
  51. package/dist/js/node/cli/test.js +79 -27
  52. package/dist/js/node/constant.js +22 -7
  53. package/dist/js/node/index.js +19 -18
  54. package/dist/js/node/modern-app.env.d.js +0 -0
  55. package/dist/js/node/runtime-testing/app.js +53 -25
  56. package/dist/js/node/runtime-testing/base.js +30 -33
  57. package/dist/js/node/runtime-testing/customRender.js +49 -18
  58. package/dist/js/node/runtime-testing/index.js +18 -27
  59. package/dist/js/node/runtime-testing/reduck.js +67 -31
  60. package/dist/js/node/runtime-testing/request.js +32 -13
  61. package/dist/js/node/runtime-testing/resolvePlugins.js +41 -12
  62. package/dist/js/treeshaking/base/config/index.js +150 -31
  63. package/dist/js/treeshaking/base/config/patches/assetsModule.js +9 -11
  64. package/dist/js/treeshaking/base/config/patches/filemock.js +2 -1
  65. package/dist/js/treeshaking/base/config/patches/index.js +221 -64
  66. package/dist/js/treeshaking/base/config/patches/transformer.js +34 -37
  67. package/dist/js/treeshaking/base/config/resolver.js +24 -11
  68. package/dist/js/treeshaking/base/config/testConfigOperator.js +120 -69
  69. package/dist/js/treeshaking/base/config/transformer/babelTransformer.js +16 -10
  70. package/dist/js/treeshaking/base/hook.js +8 -7
  71. package/dist/js/treeshaking/base/index.js +7 -6
  72. package/dist/js/treeshaking/base/runJest.js +281 -164
  73. package/dist/js/treeshaking/base/types/index.js +1 -0
  74. package/dist/js/treeshaking/base/utils.js +72 -48
  75. package/dist/js/treeshaking/cli/bff/app.js +192 -68
  76. package/dist/js/treeshaking/cli/bff/constant.js +2 -1
  77. package/dist/js/treeshaking/cli/bff/index.js +291 -130
  78. package/dist/js/treeshaking/cli/bff/mockAPI.js +181 -91
  79. package/dist/js/treeshaking/cli/bff/setup.js +185 -59
  80. package/dist/js/treeshaking/cli/bff/utils/index.js +47 -21
  81. package/dist/js/treeshaking/cli/index.js +248 -108
  82. package/dist/js/treeshaking/cli/test.js +177 -44
  83. package/dist/js/treeshaking/constant.js +2 -1
  84. package/dist/js/treeshaking/index.js +3 -3
  85. package/dist/js/treeshaking/modern-app.env.d.js +1 -0
  86. package/dist/js/treeshaking/runtime-testing/app.js +85 -39
  87. package/dist/js/treeshaking/runtime-testing/base.js +4 -3
  88. package/dist/js/treeshaking/runtime-testing/customRender.js +38 -10
  89. package/dist/js/treeshaking/runtime-testing/index.js +1 -1
  90. package/dist/js/treeshaking/runtime-testing/reduck.js +85 -33
  91. package/dist/js/treeshaking/runtime-testing/request.js +49 -19
  92. package/dist/js/treeshaking/runtime-testing/resolvePlugins.js +45 -14
  93. package/dist/types/base/config/index.d.ts +1 -0
  94. package/dist/types/base/config/patches/assetsModule.d.ts +1 -0
  95. package/dist/types/base/config/patches/filemock.d.ts +1 -0
  96. package/dist/types/base/config/patches/transformer.d.ts +1 -0
  97. package/dist/types/base/config/resolver.d.ts +2 -1
  98. package/dist/types/base/config/testConfigOperator.d.ts +2 -0
  99. package/dist/types/base/runJest.d.ts +2 -0
  100. package/dist/types/base/utils.d.ts +1 -0
  101. package/dist/types/cli/bff/index.d.ts +2 -0
  102. package/dist/types/cli/bff/mockAPI.d.ts +1 -0
  103. package/dist/types/cli/index.d.ts +2 -0
  104. package/dist/types/runtime-testing/app.d.ts +3 -0
  105. package/dist/types/runtime-testing/customRender.d.ts +2 -0
  106. package/package.json +13 -13
@@ -1,136 +1,297 @@
1
- import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
- import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
3
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4
- import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
5
- import path from 'path';
6
- import { isApiOnly } from '@modern-js/utils';
1
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
2
+ try {
3
+ var info = gen[key](arg);
4
+ var value = info.value;
5
+ } catch (error) {
6
+ reject(error);
7
+ return;
8
+ }
9
+ if (info.done) {
10
+ resolve(value);
11
+ } else {
12
+ Promise.resolve(value).then(_next, _throw);
13
+ }
14
+ }
15
+ function _asyncToGenerator(fn) {
16
+ return function() {
17
+ var self = this, args = arguments;
18
+ return new Promise(function(resolve, reject) {
19
+ var gen = fn.apply(self, args);
20
+ function _next(value) {
21
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
22
+ }
23
+ function _throw(err) {
24
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
25
+ }
26
+ _next(undefined);
27
+ });
28
+ };
29
+ }
30
+ function _defineProperty(obj, key, value) {
31
+ if (key in obj) {
32
+ Object.defineProperty(obj, key, {
33
+ value: value,
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true
37
+ });
38
+ } else {
39
+ obj[key] = value;
40
+ }
41
+ return obj;
42
+ }
43
+ function _objectSpread(target) {
44
+ for(var i = 1; i < arguments.length; i++){
45
+ var source = arguments[i] != null ? arguments[i] : {};
46
+ var ownKeys = Object.keys(source);
47
+ if (typeof Object.getOwnPropertySymbols === "function") {
48
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
49
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
50
+ }));
51
+ }
52
+ ownKeys.forEach(function(key) {
53
+ _defineProperty(target, key, source[key]);
54
+ });
55
+ }
56
+ return target;
57
+ }
58
+ var __generator = this && this.__generator || function(thisArg, body) {
59
+ var f, y, t, g, _ = {
60
+ label: 0,
61
+ sent: function() {
62
+ if (t[0] & 1) throw t[1];
63
+ return t[1];
64
+ },
65
+ trys: [],
66
+ ops: []
67
+ };
68
+ return(g = {
69
+ next: verb(0),
70
+ "throw": verb(1),
71
+ "return": verb(2)
72
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
73
+ return this;
74
+ }), g);
75
+ function verb(n) {
76
+ return function(v) {
77
+ return step([
78
+ n,
79
+ v
80
+ ]);
81
+ };
82
+ }
83
+ function step(op) {
84
+ if (f) throw new TypeError("Generator is already executing.");
85
+ while(_)try {
86
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
87
+ if (y = 0, t) op = [
88
+ op[0] & 2,
89
+ t.value
90
+ ];
91
+ switch(op[0]){
92
+ case 0:
93
+ case 1:
94
+ t = op;
95
+ break;
96
+ case 4:
97
+ _.label++;
98
+ return {
99
+ value: op[1],
100
+ done: false
101
+ };
102
+ case 5:
103
+ _.label++;
104
+ y = op[1];
105
+ op = [
106
+ 0
107
+ ];
108
+ continue;
109
+ case 7:
110
+ op = _.ops.pop();
111
+ _.trys.pop();
112
+ continue;
113
+ default:
114
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
115
+ _ = 0;
116
+ continue;
117
+ }
118
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
119
+ _.label = op[1];
120
+ break;
121
+ }
122
+ if (op[0] === 6 && _.label < t[1]) {
123
+ _.label = t[1];
124
+ t = op;
125
+ break;
126
+ }
127
+ if (t && _.label < t[2]) {
128
+ _.label = t[2];
129
+ _.ops.push(op);
130
+ break;
131
+ }
132
+ if (t[2]) _.ops.pop();
133
+ _.trys.pop();
134
+ continue;
135
+ }
136
+ op = body.call(thisArg, _);
137
+ } catch (e) {
138
+ op = [
139
+ 6,
140
+ e
141
+ ];
142
+ y = 0;
143
+ } finally{
144
+ f = t = 0;
145
+ }
146
+ if (op[0] & 5) throw op[1];
147
+ return {
148
+ value: op[0] ? op[1] : void 0,
149
+ done: true
150
+ };
151
+ }
152
+ };
153
+ import path from "path";
154
+ import { isApiOnly } from "@modern-js/utils";
7
155
  import { getModuleNameMapper, DEFAULT_RESOLVER_PATH } from "../../base";
8
156
  import { bff_info_key } from "./constant";
9
157
  import { isBFFProject } from "./utils";
10
- export var setJestConfigForBFF = /*#__PURE__*/function () {
11
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
12
- var _userConfig$source;
13
- var pwd, userConfig, plugins, routes, utils, bffConfig, jestConfig, alias, aliasMapper, moduleNameMapper, transform, apiOnly, mergedModuleNameMapper, resolver, configFields, commonConfig;
14
- return _regeneratorRuntime().wrap(function _callee$(_context) {
15
- while (1) {
16
- switch (_context.prev = _context.next) {
17
- case 0:
18
- pwd = _ref.pwd, userConfig = _ref.userConfig, plugins = _ref.plugins, routes = _ref.routes, utils = _ref.utils;
19
- bffConfig = {
20
- rootDir: path.join(pwd, './api'),
21
- setupFilesAfterEnv: [require.resolve("./setup")],
22
- testEnvironment: 'node',
23
- testMatch: ["**/api/**/*.test.[jt]s"],
24
- globals: _defineProperty({
25
- 'ts-jest': {
26
- diagnostics: {
27
- warnOnly: true
28
- }
29
- }
30
- }, bff_info_key, {
31
- appDir: pwd,
32
- modernUserConfig: userConfig,
33
- plugins: plugins,
34
- routes: routes
35
- })
36
- };
37
- jestConfig = utils.jestConfig;
38
- alias = (userConfig === null || userConfig === void 0 ? void 0 : (_userConfig$source = userConfig.source) === null || _userConfig$source === void 0 ? void 0 : _userConfig$source.alias) || {};
39
- aliasMapper = getModuleNameMapper(alias);
40
- moduleNameMapper = jestConfig.moduleNameMapper; // 服务端统一使用 ts-jest
41
- transform = {
42
- '\\.[jt]sx?$': require.resolve('ts-jest')
43
- };
44
- _context.next = 9;
45
- return isApiOnly(pwd);
46
- case 9:
47
- apiOnly = _context.sent;
48
- mergedModuleNameMapper = _objectSpread(_objectSpread({}, moduleNameMapper), aliasMapper);
49
- resolver = jestConfig.resolver || DEFAULT_RESOLVER_PATH; // 这三个配置不能设置在 projects 中,需要设置在外层(https://github.com/facebook/jest/issues/9696)
50
- configFields = ['coverage', 'collectCoverage', 'testTimeout'];
51
- commonConfig = configFields.reduce(function (obj, field) {
52
- if (jestConfig.hasOwnProperty(field)) {
53
- obj[field] = jestConfig[field];
54
- }
55
- return obj;
56
- }, {});
57
- if (!apiOnly) {
58
- utils.setJestConfig({
59
- projects: [_objectSpread({}, jestConfig), _objectSpread({
60
- transform: transform,
61
- moduleNameMapper: mergedModuleNameMapper,
62
- resolver: resolver
63
- }, bffConfig)]
64
- }, {
65
- force: true
66
- });
67
- } else {
68
- utils.setJestConfig({
69
- projects: [_objectSpread({
70
- transform: transform,
71
- moduleNameMapper: mergedModuleNameMapper,
72
- resolver: resolver
73
- }, bffConfig)]
74
- }, {
75
- force: true
76
- });
158
+ var setJestConfigForBFF = function() {
159
+ var _ref = _asyncToGenerator(function(param) {
160
+ var pwd, userConfig, plugins, routes, utils, ref, bffConfig, jestConfig, alias, aliasMapper, moduleNameMapper, transform, apiOnly, mergedModuleNameMapper, resolver, configFields, commonConfig;
161
+ return __generator(this, function(_state) {
162
+ switch(_state.label){
163
+ case 0:
164
+ pwd = param.pwd, userConfig = param.userConfig, plugins = param.plugins, routes = param.routes, utils = param.utils;
165
+ bffConfig = {
166
+ rootDir: path.join(pwd, "./api"),
167
+ setupFilesAfterEnv: [
168
+ require.resolve("./setup")
169
+ ],
170
+ testEnvironment: "node",
171
+ testMatch: [
172
+ "**/api/**/*.test.[jt]s"
173
+ ],
174
+ globals: _defineProperty({
175
+ "ts-jest": {
176
+ diagnostics: {
177
+ warnOnly: true
178
+ }
179
+ }
180
+ }, bff_info_key, {
181
+ appDir: pwd,
182
+ modernUserConfig: userConfig,
183
+ plugins: plugins,
184
+ routes: routes
185
+ })
186
+ };
187
+ jestConfig = utils.jestConfig;
188
+ alias = (userConfig === null || userConfig === void 0 ? void 0 : (ref = userConfig.source) === null || ref === void 0 ? void 0 : ref.alias) || {};
189
+ aliasMapper = getModuleNameMapper(alias);
190
+ moduleNameMapper = jestConfig.moduleNameMapper;
191
+ transform = {
192
+ "\\.[jt]sx?$": require.resolve("ts-jest")
193
+ };
194
+ return [
195
+ 4,
196
+ isApiOnly(pwd)
197
+ ];
198
+ case 1:
199
+ apiOnly = _state.sent();
200
+ mergedModuleNameMapper = _objectSpread({}, moduleNameMapper, aliasMapper);
201
+ resolver = jestConfig.resolver || DEFAULT_RESOLVER_PATH;
202
+ configFields = [
203
+ "coverage",
204
+ "collectCoverage",
205
+ "testTimeout"
206
+ ];
207
+ commonConfig = configFields.reduce(function(obj, field) {
208
+ if (jestConfig.hasOwnProperty(field)) {
209
+ obj[field] = jestConfig[field];
210
+ }
211
+ return obj;
212
+ }, {});
213
+ if (!apiOnly) {
214
+ utils.setJestConfig({
215
+ projects: [
216
+ _objectSpread({}, jestConfig),
217
+ _objectSpread({
218
+ transform: transform,
219
+ moduleNameMapper: mergedModuleNameMapper,
220
+ resolver: resolver
221
+ }, bffConfig)
222
+ ]
223
+ }, {
224
+ force: true
225
+ });
226
+ } else {
227
+ utils.setJestConfig({
228
+ projects: [
229
+ _objectSpread({
230
+ transform: transform,
231
+ moduleNameMapper: mergedModuleNameMapper,
232
+ resolver: resolver
233
+ }, bffConfig)
234
+ ]
235
+ }, {
236
+ force: true
237
+ });
238
+ }
239
+ utils.setJestConfig(commonConfig);
240
+ return [
241
+ 2
242
+ ];
77
243
  }
78
- utils.setJestConfig(commonConfig);
79
- case 16:
80
- case "end":
81
- return _context.stop();
82
- }
83
- }
84
- }, _callee);
85
- }));
86
- return function setJestConfigForBFF(_x) {
87
- return _ref2.apply(this, arguments);
88
- };
244
+ });
245
+ });
246
+ return function setJestConfigForBFF(_) {
247
+ return _ref.apply(this, arguments);
248
+ };
89
249
  }();
90
- export default (function () {
91
- return {
92
- name: '@modern-js/testing-plugin-bff',
93
- setup: function setup(api) {
94
- return {
95
- jestConfig: function () {
96
- var _jestConfig = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(utils, next) {
97
- var appContext, pwd, userConfig;
98
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
99
- while (1) {
100
- switch (_context2.prev = _context2.next) {
101
- case 0:
102
- appContext = api.useAppContext();
103
- pwd = appContext.appDirectory;
104
- if (isBFFProject(pwd)) {
105
- _context2.next = 4;
106
- break;
107
- }
108
- return _context2.abrupt("return", next(utils));
109
- case 4:
110
- userConfig = api.useResolvedConfigContext();
111
- _context2.next = 7;
112
- return setJestConfigForBFF({
113
- pwd: pwd,
114
- userConfig: userConfig,
115
- routes: appContext.serverRoutes,
116
- plugins: appContext.serverInternalPlugins,
117
- utils: utils
250
+ var bff_default = function() {
251
+ return {
252
+ name: "@modern-js/testing-plugin-bff",
253
+ setup: function setup(api) {
254
+ return {
255
+ jestConfig: function() {
256
+ var _ref = _asyncToGenerator(function(utils, next) {
257
+ var appContext, pwd, userConfig;
258
+ return __generator(this, function(_state) {
259
+ switch(_state.label){
260
+ case 0:
261
+ appContext = api.useAppContext();
262
+ pwd = appContext.appDirectory;
263
+ if (!isBFFProject(pwd)) {
264
+ return [
265
+ 2,
266
+ next(utils)
267
+ ];
268
+ }
269
+ userConfig = api.useResolvedConfigContext();
270
+ return [
271
+ 4,
272
+ setJestConfigForBFF({
273
+ pwd: pwd,
274
+ userConfig: userConfig,
275
+ routes: appContext.serverRoutes,
276
+ plugins: appContext.serverInternalPlugins,
277
+ utils: utils
278
+ })
279
+ ];
280
+ case 1:
281
+ _state.sent();
282
+ return [
283
+ 2,
284
+ next(utils)
285
+ ];
286
+ }
287
+ });
118
288
  });
119
- case 7:
120
- return _context2.abrupt("return", next(utils));
121
- case 8:
122
- case "end":
123
- return _context2.stop();
124
- }
125
- }
126
- }, _callee2);
127
- }));
128
- function jestConfig(_x2, _x3) {
129
- return _jestConfig.apply(this, arguments);
130
- }
131
- return jestConfig;
132
- }()
133
- };
134
- }
135
- };
136
- });
289
+ return function(utils, next) {
290
+ return _ref.apply(this, arguments);
291
+ };
292
+ }()
293
+ };
294
+ }
295
+ };
296
+ };
297
+ export { bff_default as default, setJestConfigForBFF };