Skip to content

Commit

Permalink
fix: correct split-chunks tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed Nov 6, 2024
1 parent 666d40a commit 61ab4dd
Show file tree
Hide file tree
Showing 43 changed files with 203 additions and 245 deletions.
1 change: 1 addition & 0 deletions packages/rspack-test-tools/etc/test-tools.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1462,6 +1462,7 @@ export type TTestConfig<T extends ECompilerType> = {
beforeExecute?: () => void;
afterExecute?: () => void;
moduleScope?: (ms: IBasicModuleScope, stats?: TCompilerStatsCompilation<T>) => IBasicModuleScope;
checkStats?: (stepName: string, stats: TCompilerStatsCompilation<T>) => boolean;
findBundle?: (index: number, options: TCompilerOptions<T>) => string | string[];
bundlePath?: string[];
nonEsmThis?: (p: string | string[]) => Object;
Expand Down
6 changes: 4 additions & 2 deletions packages/rspack-test-tools/src/helper/util/checkStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ exports.checkChunkModules = function checkChunkModules(

const expectedModules = chunkModulesMap[chunkId];
const chunkModules = chunk.modules.map(m => m.identifier);
if (strict) {
expectedModules.length === chunkModules.length;
if (strict && expectedModules.length !== chunkModules.length) {
throw new Error(
`expect chunk ${chunkId} has ${chunkModules.length} modules: ${chunkModules}\nbut received ${chunkModules.length} modules`
);
}

for (const module of expectedModules) {
Expand Down
6 changes: 6 additions & 0 deletions packages/rspack-test-tools/src/processor/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ export class WatchProcessor<
const warnings: Array<{ message: string; stack?: string }> = [];
const compiler = this.getCompiler(context);
const stats = compiler.getStats();
const checkStats = testConfig.checkStats || (() => true);

if (stats) {
fs.writeFileSync(
path.join(context.getDist(), "stats.txt"),
Expand All @@ -107,6 +109,10 @@ export class WatchProcessor<
const jsonStats = stats.toJson({
errorDetails: true
});

if (!checkStats(this._watchOptions.stepName, jsonStats)) {
throw new Error("stats check failed");
}
fs.writeFileSync(
path.join(context.getDist(), "stats.json"),
JSON.stringify(jsonStats, null, 2),
Expand Down
4 changes: 4 additions & 0 deletions packages/rspack-test-tools/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ export type TTestConfig<T extends ECompilerType> = {
ms: IBasicModuleScope,
stats?: TCompilerStatsCompilation<T>
) => IBasicModuleScope;
checkStats?: (
stepName: string,
stats: TCompilerStatsCompilation<T>
) => boolean;
findBundle?: (
index: number,
options: TCompilerOptions<T>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
const { checkChunkModules } = __non_webpack_require__("@rspack/test-tools");

it('should compile', async () => {
const [v1, v2] = await Promise.all([
import('./dyn-1').then(m => m.default),
import('./dyn-2').then(m => m.default)
])
expect(v1.default).toBe('shared')
expect(v2.default).toBe('shared')

checkChunkModules(STATS_JSON, {
'dyn-1': [
'dyn-1.js',
'm.js'
],
'dyn-2': [
'dyn-2.js',
],
shared: [
'shared.js',
'm.js',
]
})
})

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { checkChunkModules } = require("@rspack/test-tools");

module.exports = {
checkStats(stepName, stats) {
switch (stepName) {
case '0':
checkChunkModules(stats, {
'dyn-1': [
'dyn-1.js',
'm.js'
],
'dyn-2': [
'dyn-2.js',
],
shared: [
'shared.js',
'm.js',
]
})
break
case '1':
checkChunkModules(stats, {
'dyn-1': [
'dyn-1.js',
'm.js'
],
'dyn-2': [
'dyn-2.js',
'm.js'
],
shared: [
'shared.js',
]
})
break;
default:
throw "no have more step"
}

return true
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default 'v';
export default 1;
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,4 @@ const { checkChunkModules } = __non_webpack_require__("@rspack/test-tools");
it('should compile', async () => {
const v1 = await import('./dyn-1').then(m => m.default)
expect(v1.default).toBe('shared')

checkChunkModules(STATS_JSON, {
'dyn-1': [
'dyn-1.js',
'm.js',
],
shared: [
'shared.js',
]
})
})

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,5 @@ it('should compile', async () => {
expect(v1.default).toBe('shared')
expect(v2.default).toBe('shared')

checkChunkModules(STATS_JSON, {
'dyn-1': [
'dyn-1.js',
'm.js'
],
'dyn-2': [
'dyn-2.js',
],
shared: [
'shared.js',
'm.js'
]
})

})

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const { checkChunkModules } = require("@rspack/test-tools");

module.exports = {
checkStats(stepName, stats) {
switch (stepName) {
case '0':
checkChunkModules(stats, {
'dyn-1': [
'dyn-1.js',
'm.js',
],
shared: [
'shared.js',
]
})
break
case '1':
checkChunkModules(stats, {
'dyn-1': [
'dyn-1.js',
'm.js'
],
'dyn-2': [
'dyn-2.js',
],
shared: [
'shared.js',
'm.js'
]
})
break;
case '2':
checkChunkModules(stats, {
'dyn-1': [
'dyn-1.js',
'm.js'
],
'dyn-2': [
'dyn-2.js',
'm.js'
],
shared: [
'shared.js',
]
})
break;
default:
throw "no have more step"
}

return true
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
const { checkChunkModules } = __non_webpack_require__("@rspack/test-tools");

it('should compile', async () => {
const [v1, v2] = await Promise.all([
import('./dyn-1').then(m => m.default),
import('./dyn-2').then(m => m.default)
])
expect(v1.default).toBe('shared')
expect(v2.default).toBe('shared')

checkChunkModules(STATS_JSON, {
'dyn-1': [
'dyn-1.js',
'm.js'
],
'dyn-2': [
'dyn-2.js',
],
shared: [
'shared.js',
'm.js',
]
})
})

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
const { checkChunkModules } = __non_webpack_require__("@rspack/test-tools");

it('should compile', async () => {
const v1 = await import('./dyn-1').then(m => m.default)
expect(v1.default).toBe('shared')

checkChunkModules(STATS_JSON, {
'dyn-1': [
'dyn-1.js',
'm.js'
],
shared: [
'shared.js',
]
})
})

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
const { checkChunkModules } = __non_webpack_require__("@rspack/test-tools");

it('should compile', async () => {
const [v1, v2] = await Promise.all([
import('./dyn-1').then(m => m.default),
import('./dyn-2').then(m => m.default)
])
expect(v1.default).toBe('shared')
expect(v2.default).toBe('shared')

checkChunkModules(STATS_JSON, {
'dyn-1': [
'dyn-1.js',
'm.js'
],
'dyn-2': [
'dyn-2.js',
'm.js'
],
shared: [
'shared.js',
]
})
})

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 61ab4dd

Please sign in to comment.