@modern-js/plugin-testing 1.8.0 → 1.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/dist/js/modern/cli/bff/app.js +34 -13
- package/dist/js/modern/cli/bff/index.js +11 -3
- package/dist/js/modern/cli/bff/mockAPI.js +7 -6
- package/dist/js/modern/cli/bff/setup.js +11 -1
- package/dist/js/modern/runtime-testing/request.js +3 -1
- package/dist/js/node/cli/bff/app.js +39 -14
- package/dist/js/node/cli/bff/index.js +11 -3
- package/dist/js/node/cli/bff/mockAPI.js +7 -6
- package/dist/js/node/cli/bff/setup.js +12 -1
- package/dist/js/node/runtime-testing/request.js +4 -1
- package/dist/js/treeshaking/cli/bff/app.js +51 -6
- package/dist/js/treeshaking/cli/bff/index.js +17 -7
- package/dist/js/treeshaking/cli/bff/mockAPI.js +7 -6
- package/dist/js/treeshaking/cli/bff/setup.js +43 -1
- package/dist/js/treeshaking/runtime-testing/request.js +3 -1
- package/dist/types/cli/bff/app.d.ts +3 -1
- package/dist/types/cli/bff/mockAPI.d.ts +1 -1
- package/package.json +13 -13
- package/dist/js/modern/cli/bff/env.js +0 -28
- package/dist/js/node/cli/bff/env.js +0 -42
- package/dist/js/treeshaking/cli/bff/env.js +0 -98
- package/dist/types/cli/bff/env.d.ts +0 -6
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,29 @@
|
|
1
1
|
# @modern-js/plugin-testing
|
2
2
|
|
3
|
+
## 1.15.0
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- b1f7000: fix: Adjust createServer logic in bff test plugin
|
8
|
+
fix: 调整 BFF 测试中 create server 的逻辑
|
9
|
+
- Updated dependencies [8658a78]
|
10
|
+
- Updated dependencies [0df4970]
|
11
|
+
- Updated dependencies [335c97c]
|
12
|
+
- Updated dependencies [05d4a4f]
|
13
|
+
- Updated dependencies [b1f7000]
|
14
|
+
- Updated dependencies [ad05af9]
|
15
|
+
- Updated dependencies [e8a1e8e]
|
16
|
+
- Updated dependencies [5d53d1c]
|
17
|
+
- Updated dependencies [37cd159]
|
18
|
+
- Updated dependencies [a04a11b]
|
19
|
+
- @modern-js/utils@1.15.0
|
20
|
+
- @modern-js/webpack@1.15.0
|
21
|
+
- @modern-js/runtime@1.15.0
|
22
|
+
- @modern-js/server@1.15.0
|
23
|
+
- @modern-js/babel-preset-app@1.15.0
|
24
|
+
- @modern-js/babel-compiler@1.15.0
|
25
|
+
- @modern-js/plugin@1.15.0
|
26
|
+
|
3
27
|
## 1.8.0
|
4
28
|
|
5
29
|
### Minor Changes
|
@@ -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,9 +21,14 @@ 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
|
+
'ts-jest': {
|
28
|
+
diagnostics: {
|
29
|
+
warnOnly: true
|
30
|
+
}
|
31
|
+
},
|
27
32
|
[bff_info_key]: {
|
28
33
|
appDir: pwd,
|
29
34
|
modernUserConfig: userConfig,
|
@@ -38,9 +43,12 @@ export const setJestConfigForBFF = async ({
|
|
38
43
|
const alias = (userConfig === null || userConfig === void 0 ? void 0 : (_userConfig$source = userConfig.source) === null || _userConfig$source === void 0 ? void 0 : _userConfig$source.alias) || {};
|
39
44
|
const aliasMapper = getModuleNameMapper(alias);
|
40
45
|
const {
|
41
|
-
transform,
|
42
46
|
moduleNameMapper
|
43
|
-
} = jestConfig;
|
47
|
+
} = jestConfig; // 服务端统一使用 ts-jest
|
48
|
+
|
49
|
+
const transform = {
|
50
|
+
'\\.[jt]sx?$': require.resolve('ts-jest')
|
51
|
+
};
|
44
52
|
const apiOnly = await isApiOnly(pwd);
|
45
53
|
|
46
54
|
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,16 @@ 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
51
|
const supertest = require('supertest');
|
52
52
|
|
53
|
-
return
|
53
|
+
return mockApiInfosByFile[mockedFile].reduce((res, info) => {
|
54
54
|
const module = {
|
55
55
|
[info.name]: (...args) => {
|
56
|
-
if (
|
56
|
+
if (mockAppModule.isInHandler()) {
|
57
57
|
return info.handler(...args);
|
58
58
|
}
|
59
59
|
|
@@ -62,7 +62,8 @@ export default ((mock_apiInfosByFile, mock_app) => {
|
|
62
62
|
returnHttp
|
63
63
|
} = module[info.name];
|
64
64
|
const url = mock_replaceUrlWithParams(info.routePath, params, payload.params);
|
65
|
-
|
65
|
+
const app = mockAppModule.getApp();
|
66
|
+
let test = supertest(app)[info.httpMethod.toLowerCase()](url);
|
66
67
|
|
67
68
|
if (payload.query) {
|
68
69
|
test = test.query(payload.query);
|
@@ -2,6 +2,7 @@ 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;
|
6
7
|
|
7
8
|
const setup = () => {
|
@@ -27,7 +28,16 @@ const setup = () => {
|
|
27
28
|
res[apiInfo.filename].push(apiInfo);
|
28
29
|
return res;
|
29
30
|
}, {});
|
30
|
-
|
31
|
+
let app = null;
|
32
|
+
beforeAll(async () => {
|
33
|
+
if (!app) {
|
34
|
+
app = await createApp(bff_info.appDir, bff_info.modernUserConfig, bff_info.plugins, bff_info.routes);
|
35
|
+
}
|
36
|
+
});
|
37
|
+
afterAll(async () => {
|
38
|
+
await closeServer();
|
39
|
+
});
|
40
|
+
mockAPI(apiInfosByFile);
|
31
41
|
};
|
32
42
|
|
33
43
|
setup();
|
@@ -1,11 +1,13 @@
|
|
1
1
|
/* eslint-disable eslint-comments/disable-enable-pair */
|
2
2
|
import supertest from 'supertest';
|
3
|
+
import { getApp } from "../cli/bff/app";
|
3
4
|
|
4
5
|
function request(...args) {
|
5
6
|
const [fn, ...extraArgs] = args;
|
7
|
+
const app = getApp();
|
6
8
|
|
7
9
|
if (!fn) {
|
8
|
-
return supertest(
|
10
|
+
return supertest(app);
|
9
11
|
}
|
10
12
|
|
11
13
|
fn.returnHttp = true;
|
@@ -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,9 +35,14 @@ 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
|
+
'ts-jest': {
|
42
|
+
diagnostics: {
|
43
|
+
warnOnly: true
|
44
|
+
}
|
45
|
+
},
|
41
46
|
[_constant.bff_info_key]: {
|
42
47
|
appDir: pwd,
|
43
48
|
modernUserConfig: userConfig,
|
@@ -52,9 +57,12 @@ const setJestConfigForBFF = async ({
|
|
52
57
|
const alias = (userConfig === null || userConfig === void 0 ? void 0 : (_userConfig$source = userConfig.source) === null || _userConfig$source === void 0 ? void 0 : _userConfig$source.alias) || {};
|
53
58
|
const aliasMapper = (0, _base.getModuleNameMapper)(alias);
|
54
59
|
const {
|
55
|
-
transform,
|
56
60
|
moduleNameMapper
|
57
|
-
} = jestConfig;
|
61
|
+
} = jestConfig; // 服务端统一使用 ts-jest
|
62
|
+
|
63
|
+
const transform = {
|
64
|
+
'\\.[jt]sx?$': require.resolve('ts-jest')
|
65
|
+
};
|
58
66
|
const apiOnly = await (0, _utils.isApiOnly)(pwd);
|
59
67
|
|
60
68
|
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,16 @@ 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
64
|
const supertest = require('supertest');
|
65
65
|
|
66
|
-
return
|
66
|
+
return mockApiInfosByFile[mockedFile].reduce((res, info) => {
|
67
67
|
const module = {
|
68
68
|
[info.name]: (...args) => {
|
69
|
-
if (
|
69
|
+
if (mockAppModule.isInHandler()) {
|
70
70
|
return info.handler(...args);
|
71
71
|
}
|
72
72
|
|
@@ -75,7 +75,8 @@ var _default = (mock_apiInfosByFile, mock_app) => {
|
|
75
75
|
returnHttp
|
76
76
|
} = module[info.name];
|
77
77
|
const url = mock_replaceUrlWithParams(info.routePath, params, payload.params);
|
78
|
-
|
78
|
+
const app = mockAppModule.getApp();
|
79
|
+
let test = supertest(app)[info.httpMethod.toLowerCase()](url);
|
79
80
|
|
80
81
|
if (payload.query) {
|
81
82
|
test = test.query(payload.query);
|
@@ -8,6 +8,8 @@ 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;
|
@@ -35,7 +37,16 @@ const setup = () => {
|
|
35
37
|
res[apiInfo.filename].push(apiInfo);
|
36
38
|
return res;
|
37
39
|
}, {});
|
38
|
-
|
40
|
+
let app = null;
|
41
|
+
beforeAll(async () => {
|
42
|
+
if (!app) {
|
43
|
+
app = await (0, _app.createApp)(bff_info.appDir, bff_info.modernUserConfig, bff_info.plugins, bff_info.routes);
|
44
|
+
}
|
45
|
+
});
|
46
|
+
afterAll(async () => {
|
47
|
+
await (0, _app.closeServer)();
|
48
|
+
});
|
49
|
+
(0, _mockAPI.default)(apiInfosByFile);
|
39
50
|
};
|
40
51
|
|
41
52
|
setup();
|
@@ -7,14 +7,17 @@ exports.request = request;
|
|
7
7
|
|
8
8
|
var _supertest = _interopRequireDefault(require("supertest"));
|
9
9
|
|
10
|
+
var _app = require("../cli/bff/app");
|
11
|
+
|
10
12
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
11
13
|
|
12
14
|
/* eslint-disable eslint-comments/disable-enable-pair */
|
13
15
|
function request(...args) {
|
14
16
|
const [fn, ...extraArgs] = args;
|
17
|
+
const app = (0, _app.getApp)();
|
15
18
|
|
16
19
|
if (!fn) {
|
17
|
-
return (0, _supertest.default)(
|
20
|
+
return (0, _supertest.default)(app);
|
18
21
|
}
|
19
22
|
|
20
23
|
fn.returnHttp = true;
|
@@ -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,9 +20,15 @@ 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
|
-
globals: _defineProperty({
|
25
|
+
globals: _defineProperty({
|
26
|
+
'ts-jest': {
|
27
|
+
diagnostics: {
|
28
|
+
warnOnly: true
|
29
|
+
}
|
30
|
+
}
|
31
|
+
}, bff_info_key, {
|
26
32
|
appDir: pwd,
|
27
33
|
modernUserConfig: userConfig,
|
28
34
|
plugins: plugins,
|
@@ -32,11 +38,15 @@ export var setJestConfigForBFF = /*#__PURE__*/function () {
|
|
32
38
|
jestConfig = utils.jestConfig;
|
33
39
|
alias = (userConfig === null || userConfig === void 0 ? void 0 : (_userConfig$source = userConfig.source) === null || _userConfig$source === void 0 ? void 0 : _userConfig$source.alias) || {};
|
34
40
|
aliasMapper = getModuleNameMapper(alias);
|
35
|
-
|
36
|
-
|
41
|
+
moduleNameMapper = jestConfig.moduleNameMapper; // 服务端统一使用 ts-jest
|
42
|
+
|
43
|
+
transform = {
|
44
|
+
'\\.[jt]sx?$': require.resolve('ts-jest')
|
45
|
+
};
|
46
|
+
_context.next = 9;
|
37
47
|
return isApiOnly(pwd);
|
38
48
|
|
39
|
-
case
|
49
|
+
case 9:
|
40
50
|
apiOnly = _context.sent;
|
41
51
|
mergedModuleNameMapper = _objectSpread(_objectSpread({}, moduleNameMapper), aliasMapper);
|
42
52
|
resolver = jestConfig.resolver || DEFAULT_RESOLVER_PATH; // 这三个配置不能设置在 projects 中,需要设置在外层(https://github.com/facebook/jest/issues/9696)
|
@@ -74,7 +84,7 @@ export var setJestConfigForBFF = /*#__PURE__*/function () {
|
|
74
84
|
|
75
85
|
utils.setJestConfig(commonConfig);
|
76
86
|
|
77
|
-
case
|
87
|
+
case 16:
|
78
88
|
case "end":
|
79
89
|
return _context.stop();
|
80
90
|
}
|
@@ -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,19 @@ 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
49
|
var supertest = require('supertest');
|
50
50
|
|
51
|
-
return
|
51
|
+
return mockApiInfosByFile[mockedFile].reduce(function (res, info) {
|
52
52
|
var module = _defineProperty({}, info.name, function () {
|
53
53
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
54
54
|
args[_key] = arguments[_key];
|
55
55
|
}
|
56
56
|
|
57
|
-
if (
|
57
|
+
if (mockAppModule.isInHandler()) {
|
58
58
|
return info.handler.apply(info, args);
|
59
59
|
}
|
60
60
|
|
@@ -66,7 +66,8 @@ export default (function (mock_apiInfosByFile, mock_app) {
|
|
66
66
|
var _ref = module[info.name],
|
67
67
|
returnHttp = _ref.returnHttp;
|
68
68
|
var url = mock_replaceUrlWithParams(info.routePath, params, payload.params);
|
69
|
-
var
|
69
|
+
var app = mockAppModule.getApp();
|
70
|
+
var test = supertest(app)[info.httpMethod.toLowerCase()](url);
|
70
71
|
|
71
72
|
if (payload.query) {
|
72
73
|
test = test.query(payload.query);
|
@@ -1,7 +1,10 @@
|
|
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;
|
6
9
|
|
7
10
|
var setup = function setup() {
|
@@ -27,7 +30,46 @@ var setup = function setup() {
|
|
27
30
|
res[apiInfo.filename].push(apiInfo);
|
28
31
|
return res;
|
29
32
|
}, {});
|
30
|
-
|
33
|
+
var app = null;
|
34
|
+
beforeAll( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
35
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
36
|
+
while (1) {
|
37
|
+
switch (_context.prev = _context.next) {
|
38
|
+
case 0:
|
39
|
+
if (app) {
|
40
|
+
_context.next = 4;
|
41
|
+
break;
|
42
|
+
}
|
43
|
+
|
44
|
+
_context.next = 3;
|
45
|
+
return createApp(bff_info.appDir, bff_info.modernUserConfig, bff_info.plugins, bff_info.routes);
|
46
|
+
|
47
|
+
case 3:
|
48
|
+
app = _context.sent;
|
49
|
+
|
50
|
+
case 4:
|
51
|
+
case "end":
|
52
|
+
return _context.stop();
|
53
|
+
}
|
54
|
+
}
|
55
|
+
}, _callee);
|
56
|
+
})));
|
57
|
+
afterAll( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
58
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
59
|
+
while (1) {
|
60
|
+
switch (_context2.prev = _context2.next) {
|
61
|
+
case 0:
|
62
|
+
_context2.next = 2;
|
63
|
+
return closeServer();
|
64
|
+
|
65
|
+
case 2:
|
66
|
+
case "end":
|
67
|
+
return _context2.stop();
|
68
|
+
}
|
69
|
+
}
|
70
|
+
}, _callee2);
|
71
|
+
})));
|
72
|
+
mockAPI(apiInfosByFile);
|
31
73
|
};
|
32
74
|
|
33
75
|
setup();
|
@@ -2,6 +2,7 @@ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
2
2
|
|
3
3
|
/* eslint-disable eslint-comments/disable-enable-pair */
|
4
4
|
import supertest from 'supertest';
|
5
|
+
import { getApp } from "../cli/bff/app";
|
5
6
|
|
6
7
|
function request() {
|
7
8
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
@@ -10,9 +11,10 @@ function request() {
|
|
10
11
|
|
11
12
|
var fn = args[0],
|
12
13
|
extraArgs = args.slice(1);
|
14
|
+
var app = getApp();
|
13
15
|
|
14
16
|
if (!fn) {
|
15
|
-
return supertest(
|
17
|
+
return supertest(app);
|
16
18
|
}
|
17
19
|
|
18
20
|
fn.returnHttp = true;
|
@@ -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.
|
14
|
+
"version": "1.15.0",
|
15
15
|
"jsnext:source": "./src/index.ts",
|
16
16
|
"types": "./dist/types/index.d.ts",
|
17
17
|
"main": "./dist/js/node/index.js",
|
@@ -99,12 +99,12 @@
|
|
99
99
|
"@modern-js-reduck/plugin-effects": "^1.0.2",
|
100
100
|
"@modern-js-reduck/plugin-immutable": "^1.0.1",
|
101
101
|
"@modern-js-reduck/store": "^1.0.3",
|
102
|
-
"@modern-js/babel-compiler": "
|
103
|
-
"@modern-js/utils": "
|
104
|
-
"@modern-js/webpack": "
|
105
|
-
"@modern-js/babel-preset-app": "
|
106
|
-
"@modern-js/plugin": "
|
107
|
-
"@modern-js/server": "
|
102
|
+
"@modern-js/babel-compiler": "1.15.0",
|
103
|
+
"@modern-js/utils": "1.15.0",
|
104
|
+
"@modern-js/webpack": "1.15.0",
|
105
|
+
"@modern-js/babel-preset-app": "1.15.0",
|
106
|
+
"@modern-js/plugin": "1.15.0",
|
107
|
+
"@modern-js/server": "1.15.0",
|
108
108
|
"@testing-library/jest-dom": "^5.14.1",
|
109
109
|
"@testing-library/react": "^12.0.0",
|
110
110
|
"@types/testing-library__jest-dom": "^5.14.3",
|
@@ -119,7 +119,7 @@
|
|
119
119
|
"yargs": "^17.0.1"
|
120
120
|
},
|
121
121
|
"peerDependencies": {
|
122
|
-
"@modern-js/runtime": "^1.
|
122
|
+
"@modern-js/runtime": "^1.15.0"
|
123
123
|
},
|
124
124
|
"peerDependenciesMeta": {
|
125
125
|
"@modern-js/runtime": {
|
@@ -127,11 +127,11 @@
|
|
127
127
|
}
|
128
128
|
},
|
129
129
|
"devDependencies": {
|
130
|
-
"@modern-js/core": "1.
|
131
|
-
"@modern-js/runtime": "1.
|
132
|
-
"@modern-js/bff-core": "1.
|
133
|
-
"@scripts/build": "
|
134
|
-
"@scripts/jest-config": "
|
130
|
+
"@modern-js/core": "1.15.0",
|
131
|
+
"@modern-js/runtime": "1.15.0",
|
132
|
+
"@modern-js/bff-core": "1.15.0",
|
133
|
+
"@scripts/build": "1.15.0",
|
134
|
+
"@scripts/jest-config": "1.15.0",
|
135
135
|
"@types/jest": "^27",
|
136
136
|
"@types/supertest": "^2.0.11",
|
137
137
|
"@types/node": "^14",
|
@@ -1,28 +0,0 @@
|
|
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 NodeEnvironment from 'jest-environment-node';
|
4
|
-
import { createApp } from "./app";
|
5
|
-
import { bff_info_key } from "./constant";
|
6
|
-
export default class extends NodeEnvironment {
|
7
|
-
constructor(...args) {
|
8
|
-
super(...args);
|
9
|
-
|
10
|
-
_defineProperty(this, "app", void 0);
|
11
|
-
}
|
12
|
-
|
13
|
-
async setup() {
|
14
|
-
const bff_info = this.global[bff_info_key];
|
15
|
-
const {
|
16
|
-
plugins
|
17
|
-
} = bff_info; // eslint-disable-next-line no-multi-assign
|
18
|
-
|
19
|
-
this.global.app = this.app = await createApp(bff_info.appDir, bff_info.modernUserConfig, plugins, bff_info.routes);
|
20
|
-
}
|
21
|
-
|
22
|
-
async teardown() {
|
23
|
-
var _this$app, _this$app$server;
|
24
|
-
|
25
|
-
await ((_this$app = this.app) === null || _this$app === void 0 ? void 0 : (_this$app$server = _this$app.server) === null || _this$app$server === void 0 ? void 0 : _this$app$server.close());
|
26
|
-
}
|
27
|
-
|
28
|
-
}
|
@@ -1,42 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.default = void 0;
|
7
|
-
|
8
|
-
var _jestEnvironmentNode = _interopRequireDefault(require("jest-environment-node"));
|
9
|
-
|
10
|
-
var _app = require("./app");
|
11
|
-
|
12
|
-
var _constant = require("./constant");
|
13
|
-
|
14
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
15
|
-
|
16
|
-
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; }
|
17
|
-
|
18
|
-
class _default extends _jestEnvironmentNode.default {
|
19
|
-
constructor(...args) {
|
20
|
-
super(...args);
|
21
|
-
|
22
|
-
_defineProperty(this, "app", void 0);
|
23
|
-
}
|
24
|
-
|
25
|
-
async setup() {
|
26
|
-
const bff_info = this.global[_constant.bff_info_key];
|
27
|
-
const {
|
28
|
-
plugins
|
29
|
-
} = bff_info; // eslint-disable-next-line no-multi-assign
|
30
|
-
|
31
|
-
this.global.app = this.app = await (0, _app.createApp)(bff_info.appDir, bff_info.modernUserConfig, plugins, bff_info.routes);
|
32
|
-
}
|
33
|
-
|
34
|
-
async teardown() {
|
35
|
-
var _this$app, _this$app$server;
|
36
|
-
|
37
|
-
await ((_this$app = this.app) === null || _this$app === void 0 ? void 0 : (_this$app$server = _this$app.server) === null || _this$app$server === void 0 ? void 0 : _this$app$server.close());
|
38
|
-
}
|
39
|
-
|
40
|
-
}
|
41
|
-
|
42
|
-
exports.default = _default;
|
@@ -1,98 +0,0 @@
|
|
1
|
-
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
|
2
|
-
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
3
|
-
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
4
|
-
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
5
|
-
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
|
6
|
-
import _inherits from "@babel/runtime/helpers/esm/inherits";
|
7
|
-
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
|
8
|
-
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
9
|
-
import NodeEnvironment from 'jest-environment-node';
|
10
|
-
import { createApp } from "./app";
|
11
|
-
import { bff_info_key } from "./constant";
|
12
|
-
|
13
|
-
var _default = /*#__PURE__*/function (_NodeEnvironment) {
|
14
|
-
_inherits(_default, _NodeEnvironment);
|
15
|
-
|
16
|
-
var _super = _createSuper(_default);
|
17
|
-
|
18
|
-
function _default() {
|
19
|
-
var _this;
|
20
|
-
|
21
|
-
_classCallCheck(this, _default);
|
22
|
-
|
23
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
24
|
-
args[_key] = arguments[_key];
|
25
|
-
}
|
26
|
-
|
27
|
-
_this = _super.call.apply(_super, [this].concat(args));
|
28
|
-
|
29
|
-
_defineProperty(_assertThisInitialized(_this), "app", void 0);
|
30
|
-
|
31
|
-
return _this;
|
32
|
-
}
|
33
|
-
|
34
|
-
_createClass(_default, [{
|
35
|
-
key: "setup",
|
36
|
-
value: function () {
|
37
|
-
var _setup = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
38
|
-
var bff_info, plugins;
|
39
|
-
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
40
|
-
while (1) {
|
41
|
-
switch (_context.prev = _context.next) {
|
42
|
-
case 0:
|
43
|
-
bff_info = this.global[bff_info_key];
|
44
|
-
plugins = bff_info.plugins; // eslint-disable-next-line no-multi-assign
|
45
|
-
|
46
|
-
_context.next = 4;
|
47
|
-
return createApp(bff_info.appDir, bff_info.modernUserConfig, plugins, bff_info.routes);
|
48
|
-
|
49
|
-
case 4:
|
50
|
-
this.global.app = this.app = _context.sent;
|
51
|
-
|
52
|
-
case 5:
|
53
|
-
case "end":
|
54
|
-
return _context.stop();
|
55
|
-
}
|
56
|
-
}
|
57
|
-
}, _callee, this);
|
58
|
-
}));
|
59
|
-
|
60
|
-
function setup() {
|
61
|
-
return _setup.apply(this, arguments);
|
62
|
-
}
|
63
|
-
|
64
|
-
return setup;
|
65
|
-
}()
|
66
|
-
}, {
|
67
|
-
key: "teardown",
|
68
|
-
value: function () {
|
69
|
-
var _teardown = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
|
70
|
-
var _this$app, _this$app$server;
|
71
|
-
|
72
|
-
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
73
|
-
while (1) {
|
74
|
-
switch (_context2.prev = _context2.next) {
|
75
|
-
case 0:
|
76
|
-
_context2.next = 2;
|
77
|
-
return (_this$app = this.app) === null || _this$app === void 0 ? void 0 : (_this$app$server = _this$app.server) === null || _this$app$server === void 0 ? void 0 : _this$app$server.close();
|
78
|
-
|
79
|
-
case 2:
|
80
|
-
case "end":
|
81
|
-
return _context2.stop();
|
82
|
-
}
|
83
|
-
}
|
84
|
-
}, _callee2, this);
|
85
|
-
}));
|
86
|
-
|
87
|
-
function teardown() {
|
88
|
-
return _teardown.apply(this, arguments);
|
89
|
-
}
|
90
|
-
|
91
|
-
return teardown;
|
92
|
-
}()
|
93
|
-
}]);
|
94
|
-
|
95
|
-
return _default;
|
96
|
-
}(NodeEnvironment);
|
97
|
-
|
98
|
-
export { _default as default };
|