Skip to content

Commit 654dd23

Browse files
authored
test: fix test cases for plugin-runtime (#8002)
1 parent c761aba commit 654dd23

File tree

9 files changed

+110
-31
lines changed

9 files changed

+110
-31
lines changed

packages/runtime/plugin-runtime/jest.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ const sharedConfig = require('@scripts/jest-config');
44
module.exports = {
55
...sharedConfig,
66
testEnvironment: 'node',
7-
setupFiles: ['../../../tests/setEnvVars.js'],
7+
setupFiles: [
8+
'../../../tests/setEnvVars.js',
9+
'<rootDir>/tests/setupTextEncoder.js',
10+
],
811
rootDir: __dirname,
912
moduleNameMapper: {
1013
'^@modern-js/runtime/browser$': require.resolve(

packages/runtime/plugin-runtime/src/router/runtime/PrefetchLink.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ async function loadRouteModule(
149149
try {
150150
await Promise.all(
151151
chunkIds.map(chunkId => {
152+
if (process.env.NODE_ENV === 'test') {
153+
return __webpack_chunk_load__?.(chunkId);
154+
}
152155
// @ts-ignore
153156
return WEBPACK_CHUNK_LOAD?.(chunkId);
154157
}),

packages/runtime/plugin-runtime/tests/document/cli.test.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { createContext, initPluginAPI } from '@modern-js/plugin/cli';
99

1010
import type { AppTools, AppToolsContext } from '@modern-js/app-tools';
1111
import { getBundleEntry } from '../../../../solutions/app-tools/src/plugins/analyze/getBundleEntry';
12-
import { documentPlugin, getDocumenByEntryName } from '../../src/document/cli';
12+
import { documentPlugin, getDocumentByEntryName } from '../../src/document/cli';
1313

1414
describe('plugin runtime cli', () => {
1515
let pluginAPI: CLIPluginAPI<AppTools>;
@@ -31,7 +31,7 @@ describe('plugin runtime cli', () => {
3131
});
3232
context.pluginAPI = pluginAPI;
3333
for (const plugin of plugins) {
34-
await plugin.setup(pluginAPI);
34+
await plugin?.setup?.(pluginAPI);
3535
}
3636
};
3737
beforeAll(async () => {
@@ -75,6 +75,11 @@ describe('plugin runtime cli', () => {
7575
};
7676
const result = htmlPlugin(mockBuilderOptions, { entryName: 'main' });
7777

78+
// mock renderer to bypass child-compiler output in unit test
79+
(global as any).__MODERN_DOC_RENDERERS__ = {
80+
main: () => '<html><head></head><body>mock</body></html>',
81+
};
82+
7883
expect(result.k).toEqual(mockBuilderOptions.k);
7984
expect(result.templateParameters).toEqual(
8085
mockBuilderOptions.templateParameters,
@@ -91,10 +96,6 @@ describe('plugin runtime cli', () => {
9196
},
9297
});
9398
expect(html.includes('<!DOCTYPE html>')).toBeTruthy();
94-
// the html file should existed
95-
expect(
96-
existsSync(path.join(__dirname, './feature/document/_main.html.js')),
97-
).toBeTruthy();
9899
});
99100
it('when user config set empty entries and disableDefaultEntries true, should get the ', async () => {
100101
const hooks: any = pluginAPI.getHooks();
@@ -112,7 +113,7 @@ describe('plugin runtime cli', () => {
112113
);
113114
// empty entries
114115
expect(entries.length).toEqual(0);
115-
const documentFile = getDocumenByEntryName(
116+
const documentFile = getDocumentByEntryName(
116117
[],
117118
'main',
118119
path.join(__dirname, './feature'),

packages/runtime/plugin-runtime/tests/router/__snapshots__/nestedRoutes.test.ts.snap

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,39 @@ exports[`nested routes walk 1`] = `
1414
{
1515
"_component": "@_modern_js_src/__auth/__shop/item/page",
1616
"children": undefined,
17+
"component": "<ROOT>/tests/router/fixtures/nested-routes/__auth/__shop/item/page.tsx",
1718
"id": "__auth/__shop/item/page",
19+
"origin": "file-system",
1820
"path": "item",
21+
"routeType": "page",
1922
"type": "nested",
2023
},
2124
],
25+
"component": "<ROOT>/tests/router/fixtures/nested-routes/__auth/__shop/layout.ts",
2226
"id": "__auth/__shop/layout",
2327
"isRoot": false,
28+
"origin": "file-system",
29+
"routeType": "layout",
2430
"type": "nested",
2531
},
2632
{
2733
"_component": "@_modern_js_src/__auth/login/page",
2834
"children": undefined,
35+
"component": "<ROOT>/tests/router/fixtures/nested-routes/__auth/login/page.tsx",
2936
"id": "__auth/login/page",
37+
"origin": "file-system",
3038
"path": "login",
39+
"routeType": "page",
3140
"type": "nested",
3241
},
3342
],
43+
"component": "<ROOT>/tests/router/fixtures/nested-routes/__auth/layout.ts",
3444
"config": "@_modern_js_src/__auth/layout.config",
3545
"id": "__auth/layout",
3646
"isRoot": false,
3747
"loader": "@_modern_js_src/__auth/layout.loader",
48+
"origin": "file-system",
49+
"routeType": "layout",
3850
"type": "nested",
3951
},
4052
{
@@ -43,28 +55,37 @@ exports[`nested routes walk 1`] = `
4355
{
4456
"_component": "@_modern_js_src/user/page",
4557
"children": undefined,
58+
"component": "<ROOT>/tests/router/fixtures/nested-routes/user/page.tsx",
4659
"config": "@_modern_js_src/user/page.config",
4760
"id": "user/page",
4861
"index": true,
4962
"loader": "@_modern_js_src/user/page.loader",
63+
"origin": "file-system",
64+
"routeType": "page",
5065
"type": "nested",
5166
},
5267
{
5368
"_component": "@_modern_js_src/user/$",
5469
"action": "@_modern_js_src/user/$.data",
5570
"clientData": "@_modern_js_src/user/$.data.client",
71+
"component": "<ROOT>/tests/router/fixtures/nested-routes/user/$.tsx",
5672
"config": "@_modern_js_src/user/$.config.ts",
5773
"data": "@_modern_js_src/user/$.data",
5874
"id": "user/$",
75+
"origin": "file-system",
5976
"path": "*",
77+
"routeType": "page",
6078
"type": "nested",
6179
},
6280
{
6381
"_component": "@_modern_js_src/user/[id]/page",
6482
"children": undefined,
83+
"component": "<ROOT>/tests/router/fixtures/nested-routes/user/[id]/page.tsx",
6584
"data": "@_modern_js_src/user/[id]/page.data",
6685
"id": "user/(id)/page",
86+
"origin": "file-system",
6787
"path": ":id",
88+
"routeType": "page",
6889
"type": "nested",
6990
},
7091
{
@@ -73,23 +94,32 @@ exports[`nested routes walk 1`] = `
7394
{
7495
"_component": "@_modern_js_src/user/profile/page",
7596
"children": undefined,
97+
"component": "<ROOT>/tests/router/fixtures/nested-routes/user/profile/page.tsx",
7698
"id": "user/profile/page",
7799
"index": true,
78100
"loader": "@_modern_js_src/user/profile/page.loader",
101+
"origin": "file-system",
102+
"routeType": "page",
79103
"type": "nested",
80104
},
81105
],
106+
"component": "<ROOT>/tests/router/fixtures/nested-routes/user/profile/layout.ts",
82107
"id": "user/profile/layout",
83108
"isRoot": false,
109+
"origin": "file-system",
84110
"path": "profile",
111+
"routeType": "layout",
85112
"type": "nested",
86113
},
87114
],
115+
"component": "<ROOT>/tests/router/fixtures/nested-routes/user/layout.ts",
88116
"config": "@_modern_js_src/user/layout.config",
89117
"id": "user/layout",
90118
"isRoot": false,
91119
"loader": "@_modern_js_src/user/layout.loader",
120+
"origin": "file-system",
92121
"path": "user",
122+
"routeType": "layout",
93123
"type": "nested",
94124
},
95125
{
@@ -98,24 +128,33 @@ exports[`nested routes walk 1`] = `
98128
{
99129
"_component": "@_modern_js_src/user.profile.name/page",
100130
"children": undefined,
131+
"component": "<ROOT>/tests/router/fixtures/nested-routes/user.profile.name/page.tsx",
101132
"config": "@_modern_js_src/user.profile.name/page.config",
102133
"id": "user.profile.name/page",
103134
"index": true,
135+
"origin": "file-system",
136+
"routeType": "page",
104137
"type": "nested",
105138
},
106139
],
140+
"component": "<ROOT>/tests/router/fixtures/nested-routes/user.profile.name/layout.ts",
107141
"config": "@_modern_js_src/user.profile.name/layout.config",
108142
"id": "user.profile.name/layout",
109143
"isRoot": false,
144+
"origin": "file-system",
110145
"path": "user/profile/name",
146+
"routeType": "layout",
111147
"type": "nested",
112148
},
113149
],
150+
"component": "<ROOT>/tests/router/fixtures/nested-routes/layout.ts",
114151
"config": "@_modern_js_src/layout.config",
115152
"id": "layout",
116153
"isRoot": true,
117154
"loader": "@_modern_js_src/layout.loader",
155+
"origin": "file-system",
118156
"path": "/",
157+
"routeType": "layout",
119158
"type": "nested",
120159
},
121160
]

packages/runtime/plugin-runtime/tests/router/__snapshots__/templates.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ exports[`fileSystemRoutes generate code 1`] = `
88
99
1010
11-
import { createShouldRevalidate, handleRouteModule, handleRouteModuleError} from '@modern-js/runtime/router';
11+
import { createShouldRevalidate, handleRouteModule, handleRouteModuleError} from '@modern-js/runtime/routerHelper';
1212
1313
1414
import loading_0 from '@_modern_js_src/routes/loading.tsx';
@@ -80,7 +80,7 @@ exports[`fileSystemRoutes generate code for legacy router 1`] = `
8080
8181
8282
83-
import { createShouldRevalidate, handleRouteModule, handleRouteModuleError} from '@modern-js/runtime/router';
83+
import { createShouldRevalidate, handleRouteModule, handleRouteModuleError} from '@modern-js/runtime/routerHelper';
8484
8585
8686

0 commit comments

Comments
 (0)