Skip to content

Commit 913a5db

Browse files
authored
test: add test case for the Rspack plugin (#30)
1 parent 2e428b6 commit 913a5db

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

playground/rspack.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CheckSyntaxRspackPlugin } from '../dist/index.js';
33

44
export default defineConfig({
55
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
6+
target: ['web', 'es5'],
67
devtool: 'source-map',
78
plugins: [
89
new CheckSyntaxRspackPlugin({

src/generateError.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ async function tryGenerateErrorFromSourceMap({
8383
rootPath: string;
8484
}): Promise<ECMASyntaxError | null> {
8585
const sourceMapPath = `${filepath}.map`;
86+
8687
if (!fs.existsSync(sourceMapPath)) {
8788
return null;
8889
}

test/rspack/index.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import path, { dirname } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
import { expect, test } from '@playwright/test';
4+
import { rspack } from '@rspack/core';
5+
import stripAnsi from 'strip-ansi';
6+
import { CheckSyntaxRspackPlugin } from '../../dist';
7+
import { normalizeToPosixPath, proxyConsole } from '../helper';
8+
9+
const __dirname = dirname(fileURLToPath(import.meta.url));
10+
11+
test('should throw error when exist syntax errors', async () => {
12+
const { logs, restore } = proxyConsole();
13+
14+
const compiler = rspack({
15+
context: __dirname,
16+
target: ['web', 'es5'],
17+
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
18+
devtool: 'source-map',
19+
plugins: [
20+
new CheckSyntaxRspackPlugin({
21+
ecmaVersion: 5,
22+
}),
23+
],
24+
});
25+
26+
await expect(
27+
new Promise((resolve, reject) => {
28+
compiler.run((err, stats) => {
29+
if (err) {
30+
reject(err);
31+
return;
32+
}
33+
compiler.close(() => {
34+
resolve(stats);
35+
});
36+
});
37+
}),
38+
).rejects.toThrowError('[@rsbuild/plugin-check-syntax]');
39+
40+
restore();
41+
42+
expect(
43+
logs.find((log) =>
44+
stripAnsi(log).includes(
45+
'Find some syntax that does not match "ecmaVersion <= 5"',
46+
),
47+
),
48+
).toBeTruthy();
49+
50+
expect(logs.find((log) => log.includes('ERROR 1'))).toBeTruthy();
51+
expect(
52+
logs.find((log) => log.includes('source:') && log.includes('src/test.js')),
53+
).toBeTruthy();
54+
expect(
55+
logs.find(
56+
(log) =>
57+
log.includes('output:') &&
58+
normalizeToPosixPath(log).includes('/dist/main'),
59+
),
60+
).toBeTruthy();
61+
expect(logs.find((log) => log.includes('reason:'))).toBeTruthy();
62+
expect(
63+
logs.find((log) => log.includes('> 1 | export const printLog = () => {')),
64+
).toBeTruthy();
65+
});

test/rspack/src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { printLog } from './test.js';
2+
3+
printLog();

test/rspack/src/test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const printLog = () => {
2+
const arr = [1, 2, 3, 4, [5, 6, [7, 8]]];
3+
console.log(arr, arr.flat());
4+
};

0 commit comments

Comments
 (0)