Skip to content

Commit c53de69

Browse files
committed
test: setup e2e test cases
1 parent 1408057 commit c53de69

File tree

12 files changed

+297
-34
lines changed

12 files changed

+297
-34
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@
4646
"nano-staged": "^0.8.0",
4747
"playwright": "^1.45.3",
4848
"simple-git-hooks": "^2.11.1",
49+
"strip-ansi": "^6.0.1",
4950
"tsup": "^8.2.3",
50-
"typescript": "^5.5.4"
51+
"typescript": "^5.5.4",
52+
"upath": "^2.0.1"
5153
},
5254
"peerDependencies": {
5355
"@rsbuild/core": "0.x || 1.x"

pnpm-lock.yaml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/printErrors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function printErrors(
4444
4545
- If you want to downgrade the syntax, you can compile the specified module through the \`source.include\` config.
4646
- If you don't want to downgrade the syntax, you can adjust the project's browserslist to match the syntax.
47-
- If you don't want to check the syntax of specified files, you can use the \`exclude\` option to exclude the files to be checked.`,
47+
- If you don't want to check the syntax of specified files, you can use the \`exclude\` option to exclude the files to be checked.\n`,
4848
);
4949
}
5050

test/basic/index.test.ts

Lines changed: 99 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,125 @@
11
import { dirname } from 'node:path';
22
import { fileURLToPath } from 'node:url';
33
import { expect, test } from '@playwright/test';
4-
import { createRsbuild } from '@rsbuild/core';
5-
import { pluginCheckSyntax } from '../../src';
6-
import { getRandomPort } from '../helper';
4+
import { createRsbuild, loadConfig, mergeRsbuildConfig } from '@rsbuild/core';
5+
import stripAnsi from 'strip-ansi';
6+
import { pluginCheckSyntax } from '../../dist';
7+
import { normalizeToPosixPath, proxyConsole } from '../helper';
78

89
const __dirname = dirname(fileURLToPath(import.meta.url));
910

10-
test('should render page as expected', async ({ page }) => {
11+
test('should throw error when exist syntax errors', async () => {
12+
const { logs, restore } = proxyConsole();
13+
1114
const rsbuild = await createRsbuild({
1215
cwd: __dirname,
1316
rsbuildConfig: {
17+
...(await loadConfig({ cwd: __dirname })).content,
1418
plugins: [pluginCheckSyntax()],
15-
server: {
16-
port: getRandomPort(),
17-
},
1819
},
1920
});
2021

21-
const { server, urls } = await rsbuild.startDevServer();
22+
await expect(rsbuild.build()).rejects.toThrowError(
23+
'[@rsbuild/plugin-check-syntax]',
24+
);
25+
26+
restore();
27+
28+
expect(
29+
logs.find((log) =>
30+
stripAnsi(log).includes(
31+
'Find some syntax that does not match "ecmaVersion <= 5"',
32+
),
33+
),
34+
).toBeTruthy();
35+
36+
expect(logs.find((log) => log.includes('ERROR 1'))).toBeTruthy();
37+
expect(
38+
logs.find((log) => log.includes('source:') && log.includes('src/test.js')),
39+
).toBeTruthy();
40+
expect(
41+
logs.find(
42+
(log) =>
43+
log.includes('output:') &&
44+
normalizeToPosixPath(log).includes('/dist/static/js/index'),
45+
),
46+
).toBeTruthy();
47+
expect(logs.find((log) => log.includes('reason:'))).toBeTruthy();
48+
expect(
49+
logs.find((log) => log.includes('> 1 | export const printLog = () => {')),
50+
).toBeTruthy();
51+
});
52+
53+
test('should check assets with query correctly', async () => {
54+
const { logs, restore } = proxyConsole();
55+
56+
const rsbuild = await createRsbuild({
57+
cwd: __dirname,
58+
rsbuildConfig: mergeRsbuildConfig(
59+
(await loadConfig({ cwd: __dirname })).content,
60+
{
61+
output: {
62+
filename: {
63+
js: '[name].js?v=[contenthash:8]',
64+
css: '[name].css?v=[contenthash:8]',
65+
},
66+
},
67+
plugins: [pluginCheckSyntax()],
68+
},
69+
),
70+
});
71+
72+
await expect(rsbuild.build()).rejects.toThrowError(
73+
'[@rsbuild/plugin-check-syntax]',
74+
);
2275

23-
await page.goto(urls[0]);
24-
expect(await page.evaluate('window.test')).toBe(1);
76+
restore();
2577

26-
await server.close();
78+
expect(logs.find((log) => log.includes('ERROR 1'))).toBeTruthy();
79+
expect(
80+
logs.find((log) => log.includes('source:') && log.includes('src/test.js')),
81+
).toBeTruthy();
82+
expect(
83+
logs.find(
84+
(log) =>
85+
log.includes('output:') &&
86+
normalizeToPosixPath(log).includes('/dist/static/js/index'),
87+
),
88+
).toBeTruthy();
89+
expect(logs.find((log) => log.includes('reason:'))).toBeTruthy();
90+
expect(
91+
logs.find((log) => log.includes('> 1 | export const printLog = () => {')),
92+
).toBeTruthy();
2793
});
2894

29-
test('should build succeed', async ({ page }) => {
95+
test('should not throw error when the file is excluded', async () => {
3096
const rsbuild = await createRsbuild({
3197
cwd: __dirname,
3298
rsbuildConfig: {
33-
plugins: [pluginCheckSyntax()],
99+
...(await loadConfig({ cwd: __dirname })).content,
100+
plugins: [
101+
pluginCheckSyntax({
102+
exclude: /src\/test/,
103+
}),
104+
],
34105
},
35106
});
36107

37-
await rsbuild.build();
38-
const { server, urls } = await rsbuild.preview();
108+
await expect(rsbuild.build()).resolves.toBeUndefined();
109+
});
39110

40-
await page.goto(urls[0]);
41-
expect(await page.evaluate('window.test')).toBe(1);
111+
test('should not throw error when the targets are support es6', async () => {
112+
const rsbuild = await createRsbuild({
113+
cwd: __dirname,
114+
rsbuildConfig: {
115+
...(await loadConfig({ cwd: __dirname })).content,
116+
plugins: [
117+
pluginCheckSyntax({
118+
targets: ['chrome >= 60', 'edge >= 15'],
119+
}),
120+
],
121+
},
122+
});
42123

43-
await server.close();
124+
await expect(rsbuild.build()).resolves.toBeUndefined();
44125
});

test/basic/rsbuild.config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import path from 'node:path';
2+
3+
export default {
4+
source: {
5+
exclude: [path.resolve(__dirname, './src/test.js')],
6+
},
7+
output: {
8+
sourceMap: {
9+
js: 'source-map',
10+
},
11+
overrideBrowserslist: ['ie 11'],
12+
},
13+
};

test/basic/src/index.js

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

test/basic/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+
};

test/esnext/index.test.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { dirname } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
import { expect, test } from '@playwright/test';
4+
import { createRsbuild, loadConfig } from '@rsbuild/core';
5+
import { pluginCheckSyntax } from '../../dist';
6+
import { normalizeToPosixPath, proxyConsole } from '../helper';
7+
8+
const __dirname = dirname(fileURLToPath(import.meta.url));
9+
10+
test('should throw error when using optional chaining and target is es6 browsers', async () => {
11+
const { logs, restore } = proxyConsole();
12+
13+
const rsbuild = await createRsbuild({
14+
cwd: __dirname,
15+
rsbuildConfig: {
16+
...(await loadConfig({ cwd: __dirname })).content,
17+
plugins: [
18+
pluginCheckSyntax({
19+
targets: ['chrome >= 53'],
20+
}),
21+
],
22+
},
23+
});
24+
25+
await expect(rsbuild.build()).rejects.toThrowError(
26+
'[@rsbuild/plugin-check-syntax]',
27+
);
28+
29+
restore();
30+
31+
expect(logs.find((log) => log.includes('ERROR 1'))).toBeTruthy();
32+
expect(
33+
logs.find((log) => log.includes('source:') && log.includes('src/test.js')),
34+
).toBeTruthy();
35+
expect(
36+
logs.find(
37+
(log) =>
38+
log.includes('output:') &&
39+
normalizeToPosixPath(log).includes('/dist/static/js/index'),
40+
),
41+
).toBeTruthy();
42+
expect(
43+
logs.find(
44+
(log) => log.includes('reason:') && log.includes('Unexpected token'),
45+
),
46+
).toBeTruthy();
47+
expect(
48+
logs.find((log) => log.includes('> 3 | console.log(arr, arr?.flat());')),
49+
).toBeTruthy();
50+
});
51+
52+
test('should throw error when using optional chaining and target is fully supports es6-module', async () => {
53+
const { logs, restore } = proxyConsole();
54+
55+
const rsbuild = await createRsbuild({
56+
cwd: __dirname,
57+
rsbuildConfig: {
58+
...(await loadConfig({ cwd: __dirname })).content,
59+
plugins: [
60+
pluginCheckSyntax({
61+
targets: ['fully supports es6-module'],
62+
}),
63+
],
64+
},
65+
});
66+
67+
await expect(rsbuild.build()).rejects.toThrowError(
68+
'[@rsbuild/plugin-check-syntax]',
69+
);
70+
71+
restore();
72+
73+
expect(logs.find((log) => log.includes('ERROR 1'))).toBeTruthy();
74+
expect(
75+
logs.find((log) => log.includes('source:') && log.includes('src/test.js')),
76+
).toBeTruthy();
77+
expect(
78+
logs.find(
79+
(log) =>
80+
log.includes('output:') &&
81+
normalizeToPosixPath(log).includes('/dist/static/js/index'),
82+
),
83+
).toBeTruthy();
84+
expect(
85+
logs.find(
86+
(log) => log.includes('reason:') && log.includes('Unexpected token'),
87+
),
88+
).toBeTruthy();
89+
expect(
90+
logs.find((log) => log.includes('> 3 | console.log(arr, arr?.flat());')),
91+
).toBeTruthy();
92+
});
93+
94+
test('should not throw error when using optional chaining and ecmaVersion is 2020', async () => {
95+
const rsbuild = await createRsbuild({
96+
cwd: __dirname,
97+
rsbuildConfig: {
98+
...(await loadConfig({ cwd: __dirname })).content,
99+
plugins: [
100+
pluginCheckSyntax({
101+
ecmaVersion: 2020,
102+
}),
103+
],
104+
},
105+
});
106+
107+
await expect(rsbuild.build()).resolves.toBeUndefined();
108+
});

test/esnext/rsbuild.config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import path from 'node:path';
2+
3+
export default {
4+
source: {
5+
exclude: [path.resolve(__dirname, './src/test.js')],
6+
},
7+
output: {
8+
sourceMap: {
9+
js: 'source-map',
10+
},
11+
overrideBrowserslist: ['ie 11'],
12+
},
13+
};

test/esnext/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';
2+
3+
printLog();

0 commit comments

Comments
 (0)