@modern-js/plugin-testing 1.0.0-rc.2

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 (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +32 -0
  3. package/dist/js/modern/cli/index.js +43 -0
  4. package/dist/js/modern/cli/plugins/modern.js +44 -0
  5. package/dist/js/modern/cli/resolver.js +15 -0
  6. package/dist/js/modern/cli/test.js +43 -0
  7. package/dist/js/modern/constant.js +1 -0
  8. package/dist/js/modern/index.js +2 -0
  9. package/dist/js/modern/runtime-testing/app.js +51 -0
  10. package/dist/js/modern/runtime-testing/customRender.js +17 -0
  11. package/dist/js/modern/runtime-testing/index.js +2 -0
  12. package/dist/js/modern/runtime-testing/resolvePlugins.js +23 -0
  13. package/dist/js/node/cli/index.js +59 -0
  14. package/dist/js/node/cli/plugins/modern.js +59 -0
  15. package/dist/js/node/cli/resolver.js +20 -0
  16. package/dist/js/node/cli/test.js +60 -0
  17. package/dist/js/node/constant.js +8 -0
  18. package/dist/js/node/index.js +20 -0
  19. package/dist/js/node/runtime-testing/app.js +64 -0
  20. package/dist/js/node/runtime-testing/customRender.js +29 -0
  21. package/dist/js/node/runtime-testing/index.js +32 -0
  22. package/dist/js/node/runtime-testing/resolvePlugins.js +31 -0
  23. package/dist/js/treeshaking/cli/index.js +54 -0
  24. package/dist/js/treeshaking/cli/plugins/modern.js +47 -0
  25. package/dist/js/treeshaking/cli/resolver.js +17 -0
  26. package/dist/js/treeshaking/cli/test.js +78 -0
  27. package/dist/js/treeshaking/constant.js +1 -0
  28. package/dist/js/treeshaking/index.js +2 -0
  29. package/dist/js/treeshaking/runtime-testing/app.js +64 -0
  30. package/dist/js/treeshaking/runtime-testing/customRender.js +21 -0
  31. package/dist/js/treeshaking/runtime-testing/index.js +2 -0
  32. package/dist/js/treeshaking/runtime-testing/resolvePlugins.js +23 -0
  33. package/dist/types/cli/index.d.ts +3 -0
  34. package/dist/types/cli/plugins/modern.d.ts +3 -0
  35. package/dist/types/cli/resolver.d.ts +1 -0
  36. package/dist/types/cli/test.d.ts +8 -0
  37. package/dist/types/constant.d.ts +1 -0
  38. package/dist/types/index.d.ts +2 -0
  39. package/dist/types/runtime-testing/app.d.ts +17 -0
  40. package/dist/types/runtime-testing/customRender.d.ts +5 -0
  41. package/dist/types/runtime-testing/index.d.ts +2 -0
  42. package/dist/types/runtime-testing/resolvePlugins.d.ts +2 -0
  43. package/modern.config.js +2 -0
  44. package/package.json +44 -0
  45. package/src/.eslintrc.json +3 -0
  46. package/src/cli/index.ts +48 -0
  47. package/src/cli/plugins/modern.ts +50 -0
  48. package/src/cli/resolver.ts +16 -0
  49. package/src/cli/test.ts +66 -0
  50. package/src/constant.ts +1 -0
  51. package/src/index.ts +3 -0
  52. package/src/runtime-testing/app.ts +48 -0
  53. package/src/runtime-testing/customRender.ts +11 -0
  54. package/src/runtime-testing/index.ts +3 -0
  55. package/src/runtime-testing/resolvePlugins.ts +23 -0
  56. package/tsconfig.json +12 -0
  57. package/type.d.ts +4 -0
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = resolvePlugins;
7
+
8
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
9
+
10
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
11
+
12
+ 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; }
13
+
14
+ const allowedFeatures = ['router', 'state'];
15
+
16
+ function resolvePlugins(features) {
17
+ const plugins = [];
18
+
19
+ if (!features) {
20
+ return plugins;
21
+ }
22
+
23
+ Object.keys(features).forEach(feature => {
24
+ if (allowedFeatures.includes(feature)) {
25
+ const curPluginRes = require(`@modern-js/runtime/plugins`)[feature](_objectSpread({}, features[feature]));
26
+
27
+ plugins.push(curPluginRes);
28
+ }
29
+ });
30
+ return plugins;
31
+ }
@@ -0,0 +1,54 @@
1
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
2
+
3
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
4
+
5
+ import path from 'path';
6
+ import { createPlugin, useAppContext } from '@modern-js/core';
7
+ import { createRuntimeExportsUtils, PLUGIN_SCHEMAS } from '@modern-js/utils';
8
+ import test from "./test";
9
+ export default createPlugin(function () {
10
+ var testingExportsUtils;
11
+ return {
12
+ commands: function commands(_ref) {
13
+ var program = _ref.program;
14
+ program.command('test').allowUnknownOption().usage('[options]').action( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
15
+ return regeneratorRuntime.wrap(function _callee$(_context) {
16
+ while (1) {
17
+ switch (_context.prev = _context.next) {
18
+ case 0:
19
+ _context.next = 2;
20
+ return test();
21
+
22
+ case 2:
23
+ case "end":
24
+ return _context.stop();
25
+ }
26
+ }
27
+ }, _callee);
28
+ })));
29
+ },
30
+ validateSchema: function validateSchema() {
31
+ return PLUGIN_SCHEMAS['@modern-js/plugin-testing'];
32
+ },
33
+ config: function config() {
34
+ // eslint-disable-next-line react-hooks/rules-of-hooks
35
+ var _useAppContext = useAppContext(),
36
+ appContext = _useAppContext.value;
37
+
38
+ testingExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, 'testing');
39
+ return {
40
+ source: {
41
+ alias: {
42
+ '@modern-js/runtime/testing': testingExportsUtils.getPath()
43
+ }
44
+ }
45
+ };
46
+ },
47
+ addRuntimeExports: function addRuntimeExports() {
48
+ var testingPath = path.resolve(__dirname, '../');
49
+ testingExportsUtils.addExport("export * from '".concat(testingPath, "'"));
50
+ }
51
+ };
52
+ }, {
53
+ name: '@modern-js/plugin-testing'
54
+ });
@@ -0,0 +1,47 @@
1
+ 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; }
2
+
3
+ import path from 'path';
4
+ import { createPlugin } from '@modern-js/testing';
5
+ import { modernjs_config_key } from "../../constant";
6
+
7
+ var getModuleNameMapper = function getModuleNameMapper(config) {
8
+ var _config$resolve$alias = config.resolve.alias,
9
+ alias = _config$resolve$alias === void 0 ? {} : _config$resolve$alias;
10
+ return Object.keys(alias).reduce(function (memo, cur) {
11
+ var aliasValue = Array.isArray(alias[cur]) ? alias[cur] : [alias[cur]];
12
+ var isFile = aliasValue.some(function (s) {
13
+ return s.endsWith('.js');
14
+ });
15
+
16
+ if (isFile) {
17
+ memo[cur] = aliasValue[0];
18
+ }
19
+
20
+ var key = "^".concat(cur, "/(.*)$");
21
+ var value = path.normalize("".concat(aliasValue, "/$1"));
22
+ memo[key] = value;
23
+ return memo;
24
+ }, {});
25
+ };
26
+
27
+ export default (function (webpackConfig, userConfig, pwd) {
28
+ return createPlugin(function () {
29
+ return {
30
+ jestConfig: function jestConfig(utils, next) {
31
+ utils.mergeJestConfig({
32
+ globals: _defineProperty({}, modernjs_config_key, userConfig),
33
+ moduleNameMapper: getModuleNameMapper(webpackConfig),
34
+ testEnvironment: 'jsdom',
35
+ resolver: require.resolve("../resolver")
36
+ });
37
+ utils.setJestConfig({
38
+ // todo: diffrent test root for diffrent solutions
39
+ testMatch: ["".concat(pwd || process.cwd(), "/(src|tests|api)/**/*.test.[jt]s?(x)")]
40
+ });
41
+ return next(utils);
42
+ }
43
+ };
44
+ }, {
45
+ name: '@modern-js/testing-plugin-modern'
46
+ });
47
+ });
@@ -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,78 @@
1
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
2
+
3
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
4
+
5
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
6
+
7
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
8
+
9
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
10
+
11
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
12
+
13
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
14
+
15
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
16
+
17
+ import path from 'path';
18
+ import { compiler } from '@modern-js/babel-compiler';
19
+ import { useAppContext, useResolvedConfigContext } from '@modern-js/core';
20
+ import { runTest } from '@modern-js/testing';
21
+ import { getWebpackConfig, WebpackConfigTarget } from '@modern-js/webpack'; // import testingBffPlugin from '@modern-js/testing-plugin-bff';
22
+
23
+ import modernTestPlugin from "./plugins/modern";
24
+
25
+ var test = /*#__PURE__*/function () {
26
+ var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
27
+ var _useResolvedConfigCon, userConfig, _useAppContext, config, webpackConfigs, runtimeExportsPath;
28
+
29
+ return regeneratorRuntime.wrap(function _callee$(_context) {
30
+ while (1) {
31
+ switch (_context.prev = _context.next) {
32
+ case 0:
33
+ // eslint-disable-next-line react-hooks/rules-of-hooks
34
+ _useResolvedConfigCon = useResolvedConfigContext(), userConfig = _useResolvedConfigCon.value; // eslint-disable-next-line react-hooks/rules-of-hooks
35
+
36
+ _useAppContext = useAppContext(), config = _useAppContext.value; // todo: consider lib-tools ...
37
+
38
+ webpackConfigs = getWebpackConfig(WebpackConfigTarget.CLIENT);
39
+ userConfig.testing = userConfig.testing || {};
40
+ userConfig.testing.jest = userConfig.testing.jest || userConfig.tools.jest;
41
+ userConfig.testing.plugins = [].concat(_toConsumableArray(userConfig.testing.plugins || []), [modernTestPlugin(webpackConfigs, userConfig, config.appDirectory) // testingBffPlugin({
42
+ // pwd: config.appDirectory,
43
+ // userConfig: userConfig,
44
+ // plugins: config.plugins.map(p => p.server).filter(Boolean),
45
+ // routes: (config as any).serverRoutes
46
+ // }),
47
+ ]);
48
+ runtimeExportsPath = path.join(config.internalDirectory, '.runtime-exports');
49
+ _context.next = 9;
50
+ return compiler({
51
+ sourceDir: runtimeExportsPath,
52
+ rootDir: runtimeExportsPath,
53
+ distDir: runtimeExportsPath,
54
+ quiet: true
55
+ }, {
56
+ presets: [[require.resolve('@babel/preset-env'), {
57
+ modules: 'cjs'
58
+ }]]
59
+ });
60
+
61
+ case 9:
62
+ _context.next = 11;
63
+ return runTest(userConfig.testing);
64
+
65
+ case 11:
66
+ case "end":
67
+ return _context.stop();
68
+ }
69
+ }
70
+ }, _callee);
71
+ }));
72
+
73
+ return function test() {
74
+ return _ref.apply(this, arguments);
75
+ };
76
+ }();
77
+
78
+ export default test;
@@ -0,0 +1 @@
1
+ export var modernjs_config_key = '__modernjs_config__';
@@ -0,0 +1,2 @@
1
+ import '@testing-library/jest-dom/extend-expect';
2
+ export * from "./runtime-testing";
@@ -0,0 +1,64 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
+
9
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
10
+
11
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
12
+
13
+ import { createApp as _createApp } from '@modern-js/runtime-core';
14
+ import resolvePlugins from "./resolvePlugins";
15
+ import { modernjs_config_key } from "../constant";
16
+
17
+ var ModernRuntime = /*#__PURE__*/function () {
18
+ function ModernRuntime(options) {
19
+ _classCallCheck(this, ModernRuntime);
20
+
21
+ this.options = void 0;
22
+ this.options = options;
23
+ }
24
+
25
+ _createClass(ModernRuntime, [{
26
+ key: "init",
27
+ value: function init(options) {
28
+ this.options = options;
29
+ }
30
+ }, {
31
+ key: "createApp",
32
+ value: function createApp(props) {
33
+ var _this$options;
34
+
35
+ var _ref = props || {},
36
+ entry = _ref.entry,
37
+ children = _ref.children;
38
+
39
+ var runtimeFeatures = (_this$options = this.options) === null || _this$options === void 0 ? void 0 : _this$options.runtime;
40
+
41
+ if (entry) {
42
+ var _this$options$runtime;
43
+
44
+ runtimeFeatures = _objectSpread(_objectSpread({}, runtimeFeatures || {}), (_this$options$runtime = this.options.runtimeByEntries) === null || _this$options$runtime === void 0 ? void 0 : _this$options$runtime[entry]);
45
+ }
46
+
47
+ var Component = function Component() {
48
+ if (!children) {
49
+ return null;
50
+ }
51
+
52
+ return children;
53
+ };
54
+
55
+ return _createApp({
56
+ plugins: resolvePlugins(runtimeFeatures || {})
57
+ })(Component);
58
+ }
59
+ }]);
60
+
61
+ return ModernRuntime;
62
+ }();
63
+
64
+ export default new ModernRuntime(global[modernjs_config_key] || {});
@@ -0,0 +1,21 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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 React from 'react';
8
+ import { render } from '@testing-library/react';
9
+ import app from "./app";
10
+
11
+ var WrapModernProviders = function WrapModernProviders(props) {
12
+ return /*#__PURE__*/React.createElement(app.createApp(props));
13
+ };
14
+
15
+ var customRender = function customRender(ui, options) {
16
+ return render(ui, _objectSpread({
17
+ wrapper: WrapModernProviders
18
+ }, options));
19
+ };
20
+
21
+ export default customRender;
@@ -0,0 +1,2 @@
1
+ export { default as renderApp } from "./customRender";
2
+ export * from '@testing-library/react'; // export { testBff } from '@modern-js/testing-plugin-bff';
@@ -0,0 +1,23 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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
+ var allowedFeatures = ['router', 'state'];
8
+ export default function resolvePlugins(features) {
9
+ var plugins = [];
10
+
11
+ if (!features) {
12
+ return plugins;
13
+ }
14
+
15
+ Object.keys(features).forEach(function (feature) {
16
+ if (allowedFeatures.includes(feature)) {
17
+ var curPluginRes = require("@modern-js/runtime/plugins")[feature](_objectSpread({}, features[feature]));
18
+
19
+ plugins.push(curPluginRes);
20
+ }
21
+ });
22
+ return plugins;
23
+ }
@@ -0,0 +1,3 @@
1
+ declare const _default: any;
2
+
3
+ export default _default;
@@ -0,0 +1,3 @@
1
+ declare const _default: (webpackConfig: any, userConfig: any, pwd: string) => any;
2
+
3
+ export default _default;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import { TestConfig } from '@modern-js/testing';
2
+ declare module '@modern-js/core' {
3
+ interface UserConfig {
4
+ testing: TestConfig;
5
+ }
6
+ }
7
+ declare const test: () => Promise<void>;
8
+ export default test;
@@ -0,0 +1 @@
1
+ export declare const modernjs_config_key = "__modernjs_config__";
@@ -0,0 +1,2 @@
1
+ import '@testing-library/jest-dom/extend-expect';
2
+ export * from './runtime-testing';
@@ -0,0 +1,17 @@
1
+ import React from 'react';
2
+ import { UserConfig } from '@modern-js/core';
3
+ interface CreateAppProps {
4
+ entry?: string;
5
+ children?: React.ReactElement;
6
+ }
7
+
8
+ declare class ModernRuntime {
9
+ private options;
10
+ constructor(options: UserConfig);
11
+ init(options: UserConfig): void;
12
+ createApp(props?: CreateAppProps): React.ComponentType<any>;
13
+ }
14
+
15
+ declare const _default: ModernRuntime;
16
+
17
+ export default _default;
@@ -0,0 +1,5 @@
1
+ import { render } from '@testing-library/react';
2
+
3
+ declare const _default: typeof render;
4
+
5
+ export default _default;
@@ -0,0 +1,2 @@
1
+ export { default as renderApp } from './customRender';
2
+ export * from '@testing-library/react';
@@ -0,0 +1,2 @@
1
+ import { NormalizedConfig } from '@modern-js/core';
2
+ export default function resolvePlugins(features: NormalizedConfig['runtime']): any[];
@@ -0,0 +1,2 @@
1
+ /** @type {import('@modern-js/module-tools').UserConfig} */
2
+ module.exports = {};
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@modern-js/plugin-testing",
3
+ "version": "1.0.0-rc.2",
4
+ "jsnext:source": "./src/index.ts",
5
+ "types": "./dist/types/index.d.ts",
6
+ "main": "./dist/js/node/index.js",
7
+ "module": "./dist/js/treeshaking/index.js",
8
+ "jsnext:modern": "./dist/js/modern/index.js",
9
+ "exports": {
10
+ "./cli": "./dist/js/node/cli/index.js"
11
+ },
12
+ "dependencies": {
13
+ "@babel/preset-env": "^7.15.6",
14
+ "@babel/runtime": "^7",
15
+ "@modern-js/babel-compiler": "^1.0.0-rc.2",
16
+ "@modern-js/core": "^1.0.0-rc.2",
17
+ "@modern-js/testing": "^1.0.0-rc.2",
18
+ "@modern-js/utils": "^1.0.0-rc.2",
19
+ "@modern-js/webpack": "^1.0.0-rc.2",
20
+ "@testing-library/jest-dom": "^5.14.1",
21
+ "@testing-library/react": "^12.0.0",
22
+ "enhanced-resolve": "^5.8.2"
23
+ },
24
+ "peerDependencies": {
25
+ "@modern-js/runtime-core": "^1.0.0-rc.2"
26
+ },
27
+ "devDependencies": {
28
+ "@modern-js/runtime-core": "^1.0.0-rc.2",
29
+ "@types/jest": "^26",
30
+ "@types/node": "^14",
31
+ "@types/react": "^17",
32
+ "@types/react-dom": "^17",
33
+ "typescript": "^4",
34
+ "@modern-js/plugin-testing": "^1.0.0-rc.2",
35
+ "@modern-js/module-tools": "^1.0.0-rc.2"
36
+ },
37
+ "sideEffects": false,
38
+ "modernConfig": {},
39
+ "scripts": {
40
+ "new": "modern new",
41
+ "build": "modern build",
42
+ "test": "modern test --passWithNoTests"
43
+ }
44
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": ["@modern-js"]
3
+ }
@@ -0,0 +1,48 @@
1
+ import path from 'path';
2
+ import { createPlugin, useAppContext } from '@modern-js/core';
3
+ import { createRuntimeExportsUtils, PLUGIN_SCHEMAS } from '@modern-js/utils';
4
+ import test from './test';
5
+
6
+ export default createPlugin(
7
+ () => {
8
+ let testingExportsUtils: ReturnType<typeof createRuntimeExportsUtils>;
9
+
10
+ return {
11
+ commands: ({ program }: any) => {
12
+ program
13
+ .command('test')
14
+ .allowUnknownOption()
15
+ .usage('[options]')
16
+ .action(async () => {
17
+ await test();
18
+ });
19
+ },
20
+ validateSchema() {
21
+ return PLUGIN_SCHEMAS['@modern-js/plugin-testing'];
22
+ },
23
+ config() {
24
+ // eslint-disable-next-line react-hooks/rules-of-hooks
25
+ const { value: appContext } = useAppContext();
26
+
27
+ testingExportsUtils = createRuntimeExportsUtils(
28
+ appContext.internalDirectory,
29
+ 'testing',
30
+ );
31
+
32
+ return {
33
+ source: {
34
+ alias: {
35
+ '@modern-js/runtime/testing': testingExportsUtils.getPath(),
36
+ },
37
+ },
38
+ };
39
+ },
40
+ addRuntimeExports() {
41
+ const testingPath = path.resolve(__dirname, '../');
42
+
43
+ testingExportsUtils.addExport(`export * from '${testingPath}'`);
44
+ },
45
+ };
46
+ },
47
+ { name: '@modern-js/plugin-testing' },
48
+ ) as any;
@@ -0,0 +1,50 @@
1
+ import path from 'path';
2
+ import { createPlugin } from '@modern-js/testing';
3
+ import { modernjs_config_key } from '@/constant';
4
+
5
+ const getModuleNameMapper = (config: any) => {
6
+ const {
7
+ resolve: { alias = {} },
8
+ } = config;
9
+
10
+ return Object.keys(alias).reduce((memo, cur) => {
11
+ const aliasValue = Array.isArray(alias[cur]) ? alias[cur] : [alias[cur]];
12
+
13
+ const isFile = aliasValue.some((s: string) => s.endsWith('.js'));
14
+
15
+ if (isFile) {
16
+ memo[cur] = aliasValue[0];
17
+ }
18
+
19
+ const key = `^${cur}/(.*)$`;
20
+ const value = path.normalize(`${aliasValue}/$1`);
21
+ memo[key] = value;
22
+ return memo;
23
+ }, {} as any);
24
+ };
25
+
26
+ export default (webpackConfig: any, userConfig: any, pwd: string) =>
27
+ createPlugin(
28
+ () => ({
29
+ jestConfig: (utils: any, next: any) => {
30
+ utils.mergeJestConfig({
31
+ globals: {
32
+ [modernjs_config_key]: userConfig,
33
+ },
34
+ moduleNameMapper: getModuleNameMapper(webpackConfig),
35
+ testEnvironment: 'jsdom',
36
+ resolver: require.resolve('../resolver'),
37
+ });
38
+
39
+ utils.setJestConfig({
40
+ // todo: diffrent test root for diffrent solutions
41
+ testMatch: [`${pwd || process.cwd()}/(src|tests|api)/**/*.test.[jt]s?(x)`],
42
+ });
43
+
44
+ return next(utils);
45
+ },
46
+ }),
47
+ {
48
+ name: '@modern-js/testing-plugin-modern',
49
+ },
50
+ ) as any;
@@ -0,0 +1,16 @@
1
+ import enhanceResolve from 'enhanced-resolve';
2
+
3
+ const resolver = enhanceResolve.create.sync({
4
+ conditionNames: ['require', 'node', 'default'],
5
+ extensions: ['.js', '.json', '.node', '.ts', '.tsx'],
6
+ });
7
+
8
+ const shouldResolveByEnhance = (url: string) => /^@[^/]+\/[^/]+\/.*/.test(url);
9
+
10
+ module.exports = function (request: string, options: any) {
11
+ if (shouldResolveByEnhance(request)) {
12
+ return resolver(options.basedir, request);
13
+ }
14
+
15
+ return options.defaultResolver(request, options);
16
+ };
@@ -0,0 +1,66 @@
1
+ import path from 'path';
2
+ import { compiler } from '@modern-js/babel-compiler';
3
+ import { useAppContext, useResolvedConfigContext } from '@modern-js/core';
4
+ import { TestConfig, runTest } from '@modern-js/testing';
5
+ import { getWebpackConfig, WebpackConfigTarget } from '@modern-js/webpack';
6
+ // import testingBffPlugin from '@modern-js/testing-plugin-bff';
7
+ import modernTestPlugin from './plugins/modern';
8
+
9
+ declare module '@modern-js/core' {
10
+ interface UserConfig {
11
+ testing: TestConfig;
12
+ }
13
+ }
14
+
15
+ const test = async () => {
16
+ // eslint-disable-next-line react-hooks/rules-of-hooks
17
+ const { value: userConfig } = useResolvedConfigContext();
18
+ // eslint-disable-next-line react-hooks/rules-of-hooks
19
+ const { value: config } = useAppContext();
20
+
21
+ // todo: consider lib-tools ...
22
+ const webpackConfigs = getWebpackConfig(WebpackConfigTarget.CLIENT);
23
+
24
+ userConfig.testing = userConfig.testing || {};
25
+ userConfig.testing.jest =
26
+ userConfig.testing.jest || (userConfig.tools as any).jest;
27
+
28
+ userConfig.testing.plugins = [
29
+ ...(userConfig.testing.plugins || []),
30
+ modernTestPlugin(webpackConfigs, userConfig, config.appDirectory),
31
+ // testingBffPlugin({
32
+ // pwd: config.appDirectory,
33
+ // userConfig: userConfig,
34
+ // plugins: config.plugins.map(p => p.server).filter(Boolean),
35
+ // routes: (config as any).serverRoutes
36
+ // }),
37
+ ];
38
+
39
+ const runtimeExportsPath = path.join(
40
+ config.internalDirectory,
41
+ '.runtime-exports',
42
+ );
43
+
44
+ await compiler(
45
+ {
46
+ sourceDir: runtimeExportsPath,
47
+ rootDir: runtimeExportsPath,
48
+ distDir: runtimeExportsPath,
49
+ quiet: true,
50
+ },
51
+ {
52
+ presets: [
53
+ [
54
+ require.resolve('@babel/preset-env'),
55
+ {
56
+ modules: 'cjs',
57
+ },
58
+ ],
59
+ ],
60
+ },
61
+ );
62
+
63
+ await runTest(userConfig.testing);
64
+ };
65
+
66
+ export default test;
@@ -0,0 +1 @@
1
+ export const modernjs_config_key = '__modernjs_config__';