@modern-js/plugin-testing 1.7.0 → 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.
- package/dist/js/modern/cli/bff/app.js +34 -13
- package/dist/js/modern/cli/bff/index.js +6 -3
- package/dist/js/modern/cli/bff/mockAPI.js +8 -6
- package/dist/js/modern/cli/bff/setup.js +12 -1
- package/dist/js/modern/runtime-testing/app.js +1 -1
- package/dist/js/node/cli/bff/app.js +39 -14
- package/dist/js/node/cli/bff/index.js +6 -3
- package/dist/js/node/cli/bff/mockAPI.js +8 -6
- package/dist/js/node/cli/bff/setup.js +13 -1
- package/dist/js/node/runtime-testing/app.js +2 -2
- package/dist/js/treeshaking/cli/bff/app.js +51 -6
- package/dist/js/treeshaking/cli/bff/index.js +10 -6
- package/dist/js/treeshaking/cli/bff/mockAPI.js +8 -6
- package/dist/js/treeshaking/cli/bff/setup.js +44 -1
- package/dist/js/treeshaking/runtime-testing/app.js +1 -1
- package/dist/types/cli/bff/app.d.ts +3 -1
- package/dist/types/cli/bff/mockAPI.d.ts +1 -1
- package/package.json +16 -7
@@ -2,21 +2,42 @@ import { AsyncLocalStorage } from 'async_hooks';
|
|
2
2
|
import { Server } from '@modern-js/server';
|
3
3
|
const store = new AsyncLocalStorage();
|
4
4
|
export const isInHandler = () => Boolean(store.getStore());
|
5
|
+
let server = null;
|
5
6
|
|
6
7
|
const createApp = async (pwd, config, plugins, routes) => {
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
8
|
+
if (!server) {
|
9
|
+
config.output.path = './';
|
10
|
+
server = new Server({
|
11
|
+
apiOnly: true,
|
12
|
+
dev: {
|
13
|
+
watch: false
|
14
|
+
},
|
15
|
+
pwd,
|
16
|
+
config,
|
17
|
+
plugins,
|
18
|
+
routes
|
19
|
+
});
|
20
|
+
await server.init();
|
21
|
+
}
|
22
|
+
|
23
|
+
const app = server.getRequestHandler();
|
24
|
+
return app;
|
25
|
+
};
|
26
|
+
|
27
|
+
const getApp = () => {
|
28
|
+
if (!server) {
|
29
|
+
throw new Error('please createApp first');
|
30
|
+
}
|
31
|
+
|
19
32
|
return server.getRequestHandler();
|
20
33
|
};
|
21
34
|
|
22
|
-
|
35
|
+
const closeServer = async () => {
|
36
|
+
if (!server) {
|
37
|
+
throw new Error('please createApp first');
|
38
|
+
}
|
39
|
+
|
40
|
+
await server.close();
|
41
|
+
};
|
42
|
+
|
43
|
+
export { createApp, getApp, closeServer };
|
@@ -21,7 +21,7 @@ export const setJestConfigForBFF = async ({
|
|
21
21
|
const bffConfig = {
|
22
22
|
rootDir: path.join(pwd, './api'),
|
23
23
|
setupFilesAfterEnv: [require.resolve("./setup")],
|
24
|
-
testEnvironment:
|
24
|
+
testEnvironment: 'node',
|
25
25
|
testMatch: [`**/api/**/*.test.[jt]s`],
|
26
26
|
globals: {
|
27
27
|
[bff_info_key]: {
|
@@ -38,9 +38,12 @@ export const setJestConfigForBFF = async ({
|
|
38
38
|
const alias = (userConfig === null || userConfig === void 0 ? void 0 : (_userConfig$source = userConfig.source) === null || _userConfig$source === void 0 ? void 0 : _userConfig$source.alias) || {};
|
39
39
|
const aliasMapper = getModuleNameMapper(alias);
|
40
40
|
const {
|
41
|
-
transform,
|
42
41
|
moduleNameMapper
|
43
|
-
} = jestConfig;
|
42
|
+
} = jestConfig; // 服务端统一使用 ts-jest
|
43
|
+
|
44
|
+
const transform = {
|
45
|
+
'\\.[jt]sx?$': require.resolve('ts-jest')
|
46
|
+
};
|
44
47
|
const apiOnly = await isApiOnly(pwd);
|
45
48
|
|
46
49
|
const mergedModuleNameMapper = _objectSpread(_objectSpread({}, moduleNameMapper), aliasMapper);
|
@@ -5,7 +5,7 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
5
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
6
|
|
7
7
|
import * as ptr from 'path-to-regexp';
|
8
|
-
import * as
|
8
|
+
import * as mockAppModule from "./app"; // eslint-disable-next-line @typescript-eslint/naming-convention
|
9
9
|
|
10
10
|
const mock_replaceUrlWithParams = (url, paramValues, payload) => {
|
11
11
|
const keys = [];
|
@@ -44,16 +44,17 @@ const mock_getParamsAndPayload = args => {
|
|
44
44
|
}
|
45
45
|
};
|
46
46
|
|
47
|
-
export default (
|
48
|
-
const files = Object.keys(
|
47
|
+
export default (mockApiInfosByFile => {
|
48
|
+
const files = Object.keys(mockApiInfosByFile);
|
49
49
|
files.forEach(mockedFile => {
|
50
50
|
jest.mock(mockedFile, () => {
|
51
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
51
52
|
const supertest = require('supertest');
|
52
53
|
|
53
|
-
return
|
54
|
+
return mockApiInfosByFile[mockedFile].reduce((res, info) => {
|
54
55
|
const module = {
|
55
56
|
[info.name]: (...args) => {
|
56
|
-
if (
|
57
|
+
if (mockAppModule.isInHandler()) {
|
57
58
|
return info.handler(...args);
|
58
59
|
}
|
59
60
|
|
@@ -62,7 +63,8 @@ export default ((mock_apiInfosByFile, mock_app) => {
|
|
62
63
|
returnHttp
|
63
64
|
} = module[info.name];
|
64
65
|
const url = mock_replaceUrlWithParams(info.routePath, params, payload.params);
|
65
|
-
|
66
|
+
const app = mockAppModule.getApp();
|
67
|
+
let test = supertest(app)[info.httpMethod.toLowerCase()](url);
|
66
68
|
|
67
69
|
if (payload.query) {
|
68
70
|
test = test.query(payload.query);
|
@@ -2,7 +2,9 @@ import path from 'path';
|
|
2
2
|
import { ApiRouter } from '@modern-js/bff-core';
|
3
3
|
import { bff_info_key } from "./constant";
|
4
4
|
import mockAPI from "./mockAPI";
|
5
|
+
import { createApp, closeServer } from "./app";
|
5
6
|
let uped = false;
|
7
|
+
const BEFORE_ALL_TIMEOUT = 30000;
|
6
8
|
|
7
9
|
const setup = () => {
|
8
10
|
var _bff_info$modernUserC, _bff_info$modernUserC2;
|
@@ -27,7 +29,16 @@ const setup = () => {
|
|
27
29
|
res[apiInfo.filename].push(apiInfo);
|
28
30
|
return res;
|
29
31
|
}, {});
|
30
|
-
|
32
|
+
let app = null;
|
33
|
+
beforeAll(async () => {
|
34
|
+
if (!app) {
|
35
|
+
app = await createApp(bff_info.appDir, bff_info.modernUserConfig, bff_info.plugins, bff_info.routes);
|
36
|
+
}
|
37
|
+
}, BEFORE_ALL_TIMEOUT);
|
38
|
+
afterAll(async () => {
|
39
|
+
await closeServer();
|
40
|
+
});
|
41
|
+
mockAPI(apiInfosByFile);
|
31
42
|
};
|
32
43
|
|
33
44
|
setup();
|
@@ -4,7 +4,7 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
4
4
|
|
5
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
6
|
|
7
|
-
import { createApp } from '@modern-js/runtime
|
7
|
+
import { createApp } from '@modern-js/runtime';
|
8
8
|
import { MODERNJS_CONFIG_KEY } from "../constant";
|
9
9
|
import resolvePlugins from "./resolvePlugins";
|
10
10
|
|
@@ -3,7 +3,7 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
exports.isInHandler = exports.createApp = void 0;
|
6
|
+
exports.isInHandler = exports.getApp = exports.createApp = exports.closeServer = void 0;
|
7
7
|
|
8
8
|
var _async_hooks = require("async_hooks");
|
9
9
|
|
@@ -14,21 +14,46 @@ const store = new _async_hooks.AsyncLocalStorage();
|
|
14
14
|
const isInHandler = () => Boolean(store.getStore());
|
15
15
|
|
16
16
|
exports.isInHandler = isInHandler;
|
17
|
+
let server = null;
|
17
18
|
|
18
19
|
const createApp = async (pwd, config, plugins, routes) => {
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
20
|
+
if (!server) {
|
21
|
+
config.output.path = './';
|
22
|
+
server = new _server.Server({
|
23
|
+
apiOnly: true,
|
24
|
+
dev: {
|
25
|
+
watch: false
|
26
|
+
},
|
27
|
+
pwd,
|
28
|
+
config,
|
29
|
+
plugins,
|
30
|
+
routes
|
31
|
+
});
|
32
|
+
await server.init();
|
33
|
+
}
|
34
|
+
|
35
|
+
const app = server.getRequestHandler();
|
36
|
+
return app;
|
37
|
+
};
|
38
|
+
|
39
|
+
exports.createApp = createApp;
|
40
|
+
|
41
|
+
const getApp = () => {
|
42
|
+
if (!server) {
|
43
|
+
throw new Error('please createApp first');
|
44
|
+
}
|
45
|
+
|
31
46
|
return server.getRequestHandler();
|
32
47
|
};
|
33
48
|
|
34
|
-
exports.
|
49
|
+
exports.getApp = getApp;
|
50
|
+
|
51
|
+
const closeServer = async () => {
|
52
|
+
if (!server) {
|
53
|
+
throw new Error('please createApp first');
|
54
|
+
}
|
55
|
+
|
56
|
+
await server.close();
|
57
|
+
};
|
58
|
+
|
59
|
+
exports.closeServer = closeServer;
|
@@ -35,7 +35,7 @@ const setJestConfigForBFF = async ({
|
|
35
35
|
const bffConfig = {
|
36
36
|
rootDir: _path.default.join(pwd, './api'),
|
37
37
|
setupFilesAfterEnv: [require.resolve("./setup")],
|
38
|
-
testEnvironment:
|
38
|
+
testEnvironment: 'node',
|
39
39
|
testMatch: [`**/api/**/*.test.[jt]s`],
|
40
40
|
globals: {
|
41
41
|
[_constant.bff_info_key]: {
|
@@ -52,9 +52,12 @@ const setJestConfigForBFF = async ({
|
|
52
52
|
const alias = (userConfig === null || userConfig === void 0 ? void 0 : (_userConfig$source = userConfig.source) === null || _userConfig$source === void 0 ? void 0 : _userConfig$source.alias) || {};
|
53
53
|
const aliasMapper = (0, _base.getModuleNameMapper)(alias);
|
54
54
|
const {
|
55
|
-
transform,
|
56
55
|
moduleNameMapper
|
57
|
-
} = jestConfig;
|
56
|
+
} = jestConfig; // 服务端统一使用 ts-jest
|
57
|
+
|
58
|
+
const transform = {
|
59
|
+
'\\.[jt]sx?$': require.resolve('ts-jest')
|
60
|
+
};
|
58
61
|
const apiOnly = await (0, _utils.isApiOnly)(pwd);
|
59
62
|
|
60
63
|
const mergedModuleNameMapper = _objectSpread(_objectSpread({}, moduleNameMapper), aliasMapper);
|
@@ -7,7 +7,7 @@ exports.default = void 0;
|
|
7
7
|
|
8
8
|
var ptr = _interopRequireWildcard(require("path-to-regexp"));
|
9
9
|
|
10
|
-
var
|
10
|
+
var mockAppModule = _interopRequireWildcard(require("./app"));
|
11
11
|
|
12
12
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
13
13
|
|
@@ -57,16 +57,17 @@ const mock_getParamsAndPayload = args => {
|
|
57
57
|
}
|
58
58
|
};
|
59
59
|
|
60
|
-
var _default =
|
61
|
-
const files = Object.keys(
|
60
|
+
var _default = mockApiInfosByFile => {
|
61
|
+
const files = Object.keys(mockApiInfosByFile);
|
62
62
|
files.forEach(mockedFile => {
|
63
63
|
jest.mock(mockedFile, () => {
|
64
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
64
65
|
const supertest = require('supertest');
|
65
66
|
|
66
|
-
return
|
67
|
+
return mockApiInfosByFile[mockedFile].reduce((res, info) => {
|
67
68
|
const module = {
|
68
69
|
[info.name]: (...args) => {
|
69
|
-
if (
|
70
|
+
if (mockAppModule.isInHandler()) {
|
70
71
|
return info.handler(...args);
|
71
72
|
}
|
72
73
|
|
@@ -75,7 +76,8 @@ var _default = (mock_apiInfosByFile, mock_app) => {
|
|
75
76
|
returnHttp
|
76
77
|
} = module[info.name];
|
77
78
|
const url = mock_replaceUrlWithParams(info.routePath, params, payload.params);
|
78
|
-
|
79
|
+
const app = mockAppModule.getApp();
|
80
|
+
let test = supertest(app)[info.httpMethod.toLowerCase()](url);
|
79
81
|
|
80
82
|
if (payload.query) {
|
81
83
|
test = test.query(payload.query);
|
@@ -8,9 +8,12 @@ var _constant = require("./constant");
|
|
8
8
|
|
9
9
|
var _mockAPI = _interopRequireDefault(require("./mockAPI"));
|
10
10
|
|
11
|
+
var _app = require("./app");
|
12
|
+
|
11
13
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
12
14
|
|
13
15
|
let uped = false;
|
16
|
+
const BEFORE_ALL_TIMEOUT = 30000;
|
14
17
|
|
15
18
|
const setup = () => {
|
16
19
|
var _bff_info$modernUserC, _bff_info$modernUserC2;
|
@@ -35,7 +38,16 @@ const setup = () => {
|
|
35
38
|
res[apiInfo.filename].push(apiInfo);
|
36
39
|
return res;
|
37
40
|
}, {});
|
38
|
-
|
41
|
+
let app = null;
|
42
|
+
beforeAll(async () => {
|
43
|
+
if (!app) {
|
44
|
+
app = await (0, _app.createApp)(bff_info.appDir, bff_info.modernUserConfig, bff_info.plugins, bff_info.routes);
|
45
|
+
}
|
46
|
+
}, BEFORE_ALL_TIMEOUT);
|
47
|
+
afterAll(async () => {
|
48
|
+
await (0, _app.closeServer)();
|
49
|
+
});
|
50
|
+
(0, _mockAPI.default)(apiInfosByFile);
|
39
51
|
};
|
40
52
|
|
41
53
|
setup();
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
});
|
6
6
|
exports.default = void 0;
|
7
7
|
|
8
|
-
var
|
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,
|
56
|
+
return (0, _runtime.createApp)({
|
57
57
|
plugins: (0, _resolvePlugins.default)(runtimeFeatures || {})
|
58
58
|
})(Component);
|
59
59
|
}
|
@@ -6,14 +6,20 @@ var store = new AsyncLocalStorage();
|
|
6
6
|
export var isInHandler = function isInHandler() {
|
7
7
|
return Boolean(store.getStore());
|
8
8
|
};
|
9
|
+
var server = null;
|
9
10
|
|
10
11
|
var createApp = /*#__PURE__*/function () {
|
11
12
|
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(pwd, config, plugins, routes) {
|
12
|
-
var
|
13
|
+
var app;
|
13
14
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
14
15
|
while (1) {
|
15
16
|
switch (_context.prev = _context.next) {
|
16
17
|
case 0:
|
18
|
+
if (server) {
|
19
|
+
_context.next = 5;
|
20
|
+
break;
|
21
|
+
}
|
22
|
+
|
17
23
|
config.output.path = './';
|
18
24
|
server = new Server({
|
19
25
|
apiOnly: true,
|
@@ -25,13 +31,14 @@ var createApp = /*#__PURE__*/function () {
|
|
25
31
|
plugins: plugins,
|
26
32
|
routes: routes
|
27
33
|
});
|
28
|
-
_context.next =
|
34
|
+
_context.next = 5;
|
29
35
|
return server.init();
|
30
36
|
|
31
|
-
case 4:
|
32
|
-
return _context.abrupt("return", server.getRequestHandler());
|
33
|
-
|
34
37
|
case 5:
|
38
|
+
app = server.getRequestHandler();
|
39
|
+
return _context.abrupt("return", app);
|
40
|
+
|
41
|
+
case 7:
|
35
42
|
case "end":
|
36
43
|
return _context.stop();
|
37
44
|
}
|
@@ -44,4 +51,42 @@ var createApp = /*#__PURE__*/function () {
|
|
44
51
|
};
|
45
52
|
}();
|
46
53
|
|
47
|
-
|
54
|
+
var getApp = function getApp() {
|
55
|
+
if (!server) {
|
56
|
+
throw new Error('please createApp first');
|
57
|
+
}
|
58
|
+
|
59
|
+
return server.getRequestHandler();
|
60
|
+
};
|
61
|
+
|
62
|
+
var closeServer = /*#__PURE__*/function () {
|
63
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
64
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
65
|
+
while (1) {
|
66
|
+
switch (_context2.prev = _context2.next) {
|
67
|
+
case 0:
|
68
|
+
if (server) {
|
69
|
+
_context2.next = 2;
|
70
|
+
break;
|
71
|
+
}
|
72
|
+
|
73
|
+
throw new Error('please createApp first');
|
74
|
+
|
75
|
+
case 2:
|
76
|
+
_context2.next = 4;
|
77
|
+
return server.close();
|
78
|
+
|
79
|
+
case 4:
|
80
|
+
case "end":
|
81
|
+
return _context2.stop();
|
82
|
+
}
|
83
|
+
}
|
84
|
+
}, _callee2);
|
85
|
+
}));
|
86
|
+
|
87
|
+
return function closeServer() {
|
88
|
+
return _ref2.apply(this, arguments);
|
89
|
+
};
|
90
|
+
}();
|
91
|
+
|
92
|
+
export { createApp, getApp, closeServer };
|
@@ -11,7 +11,7 @@ export var setJestConfigForBFF = /*#__PURE__*/function () {
|
|
11
11
|
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
|
12
12
|
var _userConfig$source;
|
13
13
|
|
14
|
-
var pwd, userConfig, plugins, routes, utils, bffConfig, jestConfig, alias, aliasMapper,
|
14
|
+
var pwd, userConfig, plugins, routes, utils, bffConfig, jestConfig, alias, aliasMapper, moduleNameMapper, transform, apiOnly, mergedModuleNameMapper, resolver, configFields, commonConfig;
|
15
15
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
16
16
|
while (1) {
|
17
17
|
switch (_context.prev = _context.next) {
|
@@ -20,7 +20,7 @@ export var setJestConfigForBFF = /*#__PURE__*/function () {
|
|
20
20
|
bffConfig = {
|
21
21
|
rootDir: path.join(pwd, './api'),
|
22
22
|
setupFilesAfterEnv: [require.resolve("./setup")],
|
23
|
-
testEnvironment:
|
23
|
+
testEnvironment: 'node',
|
24
24
|
testMatch: ["**/api/**/*.test.[jt]s"],
|
25
25
|
globals: _defineProperty({}, bff_info_key, {
|
26
26
|
appDir: pwd,
|
@@ -32,11 +32,15 @@ export var setJestConfigForBFF = /*#__PURE__*/function () {
|
|
32
32
|
jestConfig = utils.jestConfig;
|
33
33
|
alias = (userConfig === null || userConfig === void 0 ? void 0 : (_userConfig$source = userConfig.source) === null || _userConfig$source === void 0 ? void 0 : _userConfig$source.alias) || {};
|
34
34
|
aliasMapper = getModuleNameMapper(alias);
|
35
|
-
|
36
|
-
|
35
|
+
moduleNameMapper = jestConfig.moduleNameMapper; // 服务端统一使用 ts-jest
|
36
|
+
|
37
|
+
transform = {
|
38
|
+
'\\.[jt]sx?$': require.resolve('ts-jest')
|
39
|
+
};
|
40
|
+
_context.next = 9;
|
37
41
|
return isApiOnly(pwd);
|
38
42
|
|
39
|
-
case
|
43
|
+
case 9:
|
40
44
|
apiOnly = _context.sent;
|
41
45
|
mergedModuleNameMapper = _objectSpread(_objectSpread({}, moduleNameMapper), aliasMapper);
|
42
46
|
resolver = jestConfig.resolver || DEFAULT_RESOLVER_PATH; // 这三个配置不能设置在 projects 中,需要设置在外层(https://github.com/facebook/jest/issues/9696)
|
@@ -74,7 +78,7 @@ export var setJestConfigForBFF = /*#__PURE__*/function () {
|
|
74
78
|
|
75
79
|
utils.setJestConfig(commonConfig);
|
76
80
|
|
77
|
-
case
|
81
|
+
case 16:
|
78
82
|
case "end":
|
79
83
|
return _context.stop();
|
80
84
|
}
|
@@ -3,7 +3,7 @@ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
3
3
|
import _typeof from "@babel/runtime/helpers/esm/typeof";
|
4
4
|
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
5
5
|
import * as ptr from 'path-to-regexp';
|
6
|
-
import * as
|
6
|
+
import * as mockAppModule from "./app"; // eslint-disable-next-line @typescript-eslint/naming-convention
|
7
7
|
|
8
8
|
var mock_replaceUrlWithParams = function mock_replaceUrlWithParams(url, paramValues, payload) {
|
9
9
|
var keys = [];
|
@@ -42,19 +42,20 @@ var mock_getParamsAndPayload = function mock_getParamsAndPayload(args) {
|
|
42
42
|
}
|
43
43
|
};
|
44
44
|
|
45
|
-
export default (function (
|
46
|
-
var files = Object.keys(
|
45
|
+
export default (function (mockApiInfosByFile) {
|
46
|
+
var files = Object.keys(mockApiInfosByFile);
|
47
47
|
files.forEach(function (mockedFile) {
|
48
48
|
jest.mock(mockedFile, function () {
|
49
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
49
50
|
var supertest = require('supertest');
|
50
51
|
|
51
|
-
return
|
52
|
+
return mockApiInfosByFile[mockedFile].reduce(function (res, info) {
|
52
53
|
var module = _defineProperty({}, info.name, function () {
|
53
54
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
54
55
|
args[_key] = arguments[_key];
|
55
56
|
}
|
56
57
|
|
57
|
-
if (
|
58
|
+
if (mockAppModule.isInHandler()) {
|
58
59
|
return info.handler.apply(info, args);
|
59
60
|
}
|
60
61
|
|
@@ -66,7 +67,8 @@ export default (function (mock_apiInfosByFile, mock_app) {
|
|
66
67
|
var _ref = module[info.name],
|
67
68
|
returnHttp = _ref.returnHttp;
|
68
69
|
var url = mock_replaceUrlWithParams(info.routePath, params, payload.params);
|
69
|
-
var
|
70
|
+
var app = mockAppModule.getApp();
|
71
|
+
var test = supertest(app)[info.httpMethod.toLowerCase()](url);
|
70
72
|
|
71
73
|
if (payload.query) {
|
72
74
|
test = test.query(payload.query);
|
@@ -1,8 +1,12 @@
|
|
1
|
+
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
|
2
|
+
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
1
3
|
import path from 'path';
|
2
4
|
import { ApiRouter } from '@modern-js/bff-core';
|
3
5
|
import { bff_info_key } from "./constant";
|
4
6
|
import mockAPI from "./mockAPI";
|
7
|
+
import { createApp, closeServer } from "./app";
|
5
8
|
var uped = false;
|
9
|
+
var BEFORE_ALL_TIMEOUT = 30000;
|
6
10
|
|
7
11
|
var setup = function setup() {
|
8
12
|
var _bff_info$modernUserC, _bff_info$modernUserC2;
|
@@ -27,7 +31,46 @@ var setup = function setup() {
|
|
27
31
|
res[apiInfo.filename].push(apiInfo);
|
28
32
|
return res;
|
29
33
|
}, {});
|
30
|
-
|
34
|
+
var app = null;
|
35
|
+
beforeAll( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
36
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
37
|
+
while (1) {
|
38
|
+
switch (_context.prev = _context.next) {
|
39
|
+
case 0:
|
40
|
+
if (app) {
|
41
|
+
_context.next = 4;
|
42
|
+
break;
|
43
|
+
}
|
44
|
+
|
45
|
+
_context.next = 3;
|
46
|
+
return createApp(bff_info.appDir, bff_info.modernUserConfig, bff_info.plugins, bff_info.routes);
|
47
|
+
|
48
|
+
case 3:
|
49
|
+
app = _context.sent;
|
50
|
+
|
51
|
+
case 4:
|
52
|
+
case "end":
|
53
|
+
return _context.stop();
|
54
|
+
}
|
55
|
+
}
|
56
|
+
}, _callee);
|
57
|
+
})), BEFORE_ALL_TIMEOUT);
|
58
|
+
afterAll( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
59
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
60
|
+
while (1) {
|
61
|
+
switch (_context2.prev = _context2.next) {
|
62
|
+
case 0:
|
63
|
+
_context2.next = 2;
|
64
|
+
return closeServer();
|
65
|
+
|
66
|
+
case 2:
|
67
|
+
case "end":
|
68
|
+
return _context2.stop();
|
69
|
+
}
|
70
|
+
}
|
71
|
+
}, _callee2);
|
72
|
+
})));
|
73
|
+
mockAPI(apiInfosByFile);
|
31
74
|
};
|
32
75
|
|
33
76
|
setup();
|
@@ -2,7 +2,7 @@ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
2
|
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
3
3
|
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
4
4
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
5
|
-
import { createApp as _createApp } from '@modern-js/runtime
|
5
|
+
import { createApp as _createApp } from '@modern-js/runtime';
|
6
6
|
import { MODERNJS_CONFIG_KEY } from "../constant";
|
7
7
|
import resolvePlugins from "./resolvePlugins";
|
8
8
|
|
@@ -3,4 +3,6 @@
|
|
3
3
|
/// <reference types="node/http" />
|
4
4
|
export declare const isInHandler: () => boolean;
|
5
5
|
declare const createApp: (pwd: string, config: any, plugins: any[], routes: any[]) => Promise<(req: import("http").IncomingMessage, res: import("http").ServerResponse, next?: (() => void) | undefined) => void>;
|
6
|
-
|
6
|
+
declare const getApp: () => (req: import("http").IncomingMessage, res: import("http").ServerResponse, next?: (() => void) | undefined) => void;
|
7
|
+
declare const closeServer: () => Promise<void>;
|
8
|
+
export { createApp, getApp, closeServer };
|
package/package.json
CHANGED
@@ -11,7 +11,7 @@
|
|
11
11
|
"modern",
|
12
12
|
"modern.js"
|
13
13
|
],
|
14
|
-
"version": "1.7.0",
|
14
|
+
"version": "1.7.1-beta.0",
|
15
15
|
"jsnext:source": "./src/index.ts",
|
16
16
|
"types": "./dist/types/index.d.ts",
|
17
17
|
"main": "./dist/js/node/index.js",
|
@@ -100,9 +100,8 @@
|
|
100
100
|
"@modern-js-reduck/plugin-immutable": "^1.0.1",
|
101
101
|
"@modern-js-reduck/store": "^1.0.3",
|
102
102
|
"@modern-js/babel-compiler": "^1.2.6",
|
103
|
-
"@modern-js/
|
104
|
-
"@modern-js/
|
105
|
-
"@modern-js/webpack": "^1.12.2",
|
103
|
+
"@modern-js/utils": "^1.8.1",
|
104
|
+
"@modern-js/webpack": "^1.12.3",
|
106
105
|
"@modern-js/babel-preset-app": "^1.5.0",
|
107
106
|
"@modern-js/plugin": "^1.4.1",
|
108
107
|
"@modern-js/server": "^1.6.0",
|
@@ -119,9 +118,18 @@
|
|
119
118
|
"ts-jest": "^27.0.4",
|
120
119
|
"yargs": "^17.0.1"
|
121
120
|
},
|
121
|
+
"peerDependencies": {
|
122
|
+
"@modern-js/runtime": "^1.4.1"
|
123
|
+
},
|
124
|
+
"peerDependenciesMeta": {
|
125
|
+
"@modern-js/runtime": {
|
126
|
+
"optional": true
|
127
|
+
}
|
128
|
+
},
|
122
129
|
"devDependencies": {
|
123
|
-
"@modern-js/core": "1.13.
|
124
|
-
"@modern-js/
|
130
|
+
"@modern-js/core": "1.13.3",
|
131
|
+
"@modern-js/runtime": "1.4.4",
|
132
|
+
"@modern-js/bff-core": "1.2.1",
|
125
133
|
"@scripts/build": "0.0.0",
|
126
134
|
"@scripts/jest-config": "0.0.0",
|
127
135
|
"@types/jest": "^27",
|
@@ -140,7 +148,8 @@
|
|
140
148
|
"modernConfig": {},
|
141
149
|
"publishConfig": {
|
142
150
|
"registry": "https://registry.npmjs.org/",
|
143
|
-
"access": "public"
|
151
|
+
"access": "public",
|
152
|
+
"types": "./dist/types/index.d.ts"
|
144
153
|
},
|
145
154
|
"wireit": {
|
146
155
|
"build": {
|