Skip to content

Commit 82fbb32

Browse files
authored
test(e2e): add cases for create-rsbuild (#2912)
1 parent ca70480 commit 82fbb32

File tree

5 files changed

+220
-9
lines changed

5 files changed

+220
-9
lines changed

Diff for: e2e/cases/create-rsbuild/helper.ts

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { execSync } from 'node:child_process';
2+
import { existsSync } from 'node:fs';
3+
import path from 'node:path';
4+
import { expect } from '@playwright/test';
5+
import fse from 'fs-extra';
6+
7+
export const expectPackageJson = (
8+
pkgJson: Record<string, any>,
9+
name: string,
10+
) => {
11+
expect(pkgJson.name).toBe(name);
12+
expect(pkgJson.scripts.dev).toBe('rsbuild dev --open');
13+
expect(pkgJson.scripts.build).toBe('rsbuild build');
14+
expect(pkgJson.devDependencies['@rsbuild/core']).toBeTruthy();
15+
};
16+
17+
export const createAndValidate = (
18+
cwd: string,
19+
template: string,
20+
{
21+
name = `test-temp-${template}`,
22+
tools = [],
23+
clean = true,
24+
}: {
25+
name?: string;
26+
tools?: string[];
27+
clean?: boolean;
28+
} = {},
29+
) => {
30+
const dir = path.join(cwd, name);
31+
fse.removeSync(dir);
32+
33+
let command = `npx create-rsbuild -d ${name} -t ${template}`;
34+
if (tools.length) {
35+
const toolsCmd = tools.map((tool) => `--tools ${tool}`).join(' ');
36+
command += ` ${toolsCmd}`;
37+
}
38+
39+
execSync(command, { cwd });
40+
41+
const pkgJson = fse.readJSONSync(path.join(dir, 'package.json'));
42+
expectPackageJson(pkgJson, path.basename(name));
43+
44+
if (template.endsWith('-ts')) {
45+
expect(pkgJson.devDependencies.typescript).toBeTruthy();
46+
expect(existsSync(path.join(dir, 'tsconfig.json'))).toBeTruthy();
47+
}
48+
49+
const cleanFn = () => fse.removeSync(dir);
50+
if (clean) {
51+
cleanFn();
52+
}
53+
54+
return { dir, pkgJson, clean: cleanFn };
55+
};

Diff for: e2e/cases/create-rsbuild/index.test.ts

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import { existsSync } from 'node:fs';
2+
import { join } from 'node:path';
3+
import { rspackOnlyTest } from '@e2e/helper';
4+
import { expect } from '@playwright/test';
5+
import { createAndValidate } from './helper';
6+
7+
rspackOnlyTest('should create vanilla project as expected', async () => {
8+
createAndValidate(__dirname, 'vanilla');
9+
});
10+
11+
rspackOnlyTest('should create vanilla-ts project as expected', async () => {
12+
createAndValidate(__dirname, 'vanilla-ts');
13+
});
14+
15+
rspackOnlyTest('should create react project as expected', async () => {
16+
const { pkgJson } = createAndValidate(__dirname, 'react');
17+
expect(pkgJson.dependencies.react).toBeTruthy();
18+
expect(pkgJson.dependencies['react-dom']).toBeTruthy();
19+
expect(pkgJson.devDependencies['@rsbuild/plugin-react']).toBeTruthy();
20+
});
21+
22+
rspackOnlyTest('should create react-ts project as expected', async () => {
23+
const { pkgJson } = createAndValidate(__dirname, 'react-ts');
24+
expect(pkgJson.dependencies.react).toBeTruthy();
25+
expect(pkgJson.dependencies['react-dom']).toBeTruthy();
26+
expect(pkgJson.devDependencies['@rsbuild/plugin-react']).toBeTruthy();
27+
});
28+
29+
rspackOnlyTest('should create preact project as expected', async () => {
30+
const { pkgJson } = createAndValidate(__dirname, 'preact');
31+
expect(pkgJson.dependencies.preact).toBeTruthy();
32+
expect(pkgJson.devDependencies['@rsbuild/plugin-preact']).toBeTruthy();
33+
});
34+
35+
rspackOnlyTest('should create preact-ts project as expected', async () => {
36+
const { pkgJson } = createAndValidate(__dirname, 'preact-ts');
37+
expect(pkgJson.dependencies.preact).toBeTruthy();
38+
expect(pkgJson.devDependencies['@rsbuild/plugin-preact']).toBeTruthy();
39+
});
40+
41+
rspackOnlyTest('should create vue3 project as expected', async () => {
42+
const { pkgJson } = createAndValidate(__dirname, 'vue3');
43+
expect(pkgJson.dependencies.vue).toBeTruthy();
44+
expect(pkgJson.devDependencies['@rsbuild/plugin-vue']).toBeTruthy();
45+
});
46+
47+
rspackOnlyTest('should create vue3-ts project as expected', async () => {
48+
const { pkgJson } = createAndValidate(__dirname, 'vue3-ts');
49+
expect(pkgJson.dependencies.vue).toBeTruthy();
50+
expect(pkgJson.devDependencies['@rsbuild/plugin-vue']).toBeTruthy();
51+
});
52+
53+
rspackOnlyTest('should create vue2 project as expected', async () => {
54+
const { pkgJson } = createAndValidate(__dirname, 'vue2');
55+
expect(pkgJson.dependencies.vue).toBeTruthy();
56+
expect(pkgJson.devDependencies['@rsbuild/plugin-vue2']).toBeTruthy();
57+
});
58+
59+
rspackOnlyTest('should create vue2-ts project as expected', async () => {
60+
const { pkgJson } = createAndValidate(__dirname, 'vue2-ts');
61+
expect(pkgJson.dependencies.vue).toBeTruthy();
62+
expect(pkgJson.devDependencies['@rsbuild/plugin-vue2']).toBeTruthy();
63+
});
64+
65+
rspackOnlyTest('should create lit project as expected', async () => {
66+
const { pkgJson } = createAndValidate(__dirname, 'lit');
67+
expect(pkgJson.dependencies.lit).toBeTruthy();
68+
});
69+
70+
rspackOnlyTest('should create lit-ts project as expected', async () => {
71+
const { pkgJson } = createAndValidate(__dirname, 'lit-ts');
72+
expect(pkgJson.dependencies.lit).toBeTruthy();
73+
});
74+
75+
rspackOnlyTest('should create solid project as expected', async () => {
76+
const { pkgJson } = createAndValidate(__dirname, 'solid');
77+
expect(pkgJson.dependencies['solid-js']).toBeTruthy();
78+
expect(pkgJson.devDependencies['@rsbuild/plugin-solid']).toBeTruthy();
79+
});
80+
81+
rspackOnlyTest('should create solid-ts project as expected', async () => {
82+
const { pkgJson } = createAndValidate(__dirname, 'solid-ts');
83+
expect(pkgJson.dependencies['solid-js']).toBeTruthy();
84+
expect(pkgJson.devDependencies['@rsbuild/plugin-solid']).toBeTruthy();
85+
});
86+
87+
rspackOnlyTest('should create svelte project as expected', async () => {
88+
const { pkgJson } = createAndValidate(__dirname, 'svelte');
89+
expect(pkgJson.dependencies.svelte).toBeTruthy();
90+
expect(pkgJson.devDependencies['@rsbuild/plugin-svelte']).toBeTruthy();
91+
});
92+
93+
rspackOnlyTest('should create svelte-ts project as expected', async () => {
94+
const { pkgJson } = createAndValidate(__dirname, 'svelte-ts');
95+
expect(pkgJson.dependencies.svelte).toBeTruthy();
96+
expect(pkgJson.devDependencies['@rsbuild/plugin-svelte']).toBeTruthy();
97+
});
98+
99+
rspackOnlyTest('should allow to create project in sub dir', async () => {
100+
createAndValidate(__dirname, 'vanilla', {
101+
name: 'test-temp-dir/rsbuild-project',
102+
});
103+
});
104+
105+
rspackOnlyTest('should allow to create project in relative dir', async () => {
106+
createAndValidate(__dirname, 'vanilla', {
107+
name: './test-temp-relative-dir',
108+
});
109+
});
110+
111+
rspackOnlyTest('should create project with eslint as expected', async () => {
112+
const { dir, pkgJson, clean } = createAndValidate(__dirname, 'vanilla', {
113+
name: 'test-temp-eslint',
114+
tools: ['eslint'],
115+
clean: false,
116+
});
117+
expect(pkgJson.devDependencies.eslint).toBeTruthy();
118+
expect(existsSync(join(dir, 'eslint.config.mjs'))).toBeTruthy();
119+
clean();
120+
});
121+
122+
rspackOnlyTest('should create project with prettier as expected', async () => {
123+
const { dir, pkgJson, clean } = createAndValidate(__dirname, 'vanilla', {
124+
name: 'test-temp-prettier',
125+
tools: ['prettier'],
126+
clean: false,
127+
});
128+
expect(pkgJson.devDependencies.prettier).toBeTruthy();
129+
expect(existsSync(join(dir, '.prettierrc'))).toBeTruthy();
130+
clean();
131+
});
132+
133+
rspackOnlyTest(
134+
'should create project with eslint and prettier as expected',
135+
async () => {
136+
const { dir, pkgJson, clean } = createAndValidate(__dirname, 'vanilla', {
137+
name: 'test-temp-eslint-prettier',
138+
tools: ['eslint', 'prettier'],
139+
clean: false,
140+
});
141+
expect(pkgJson.devDependencies.eslint).toBeTruthy();
142+
expect(pkgJson.devDependencies.prettier).toBeTruthy();
143+
expect(existsSync(join(dir, '.prettierrc'))).toBeTruthy();
144+
expect(existsSync(join(dir, 'eslint.config.mjs'))).toBeTruthy();
145+
clean();
146+
},
147+
);
148+
149+
rspackOnlyTest('should create project with biome as expected', async () => {
150+
const { dir, pkgJson, clean } = createAndValidate(__dirname, 'vanilla', {
151+
name: 'test-temp-eslint',
152+
tools: ['biome'],
153+
clean: false,
154+
});
155+
expect(pkgJson.devDependencies['@biomejs/biome']).toBeTruthy();
156+
expect(existsSync(join(dir, 'biome.json'))).toBeTruthy();
157+
clean();
158+
});

Diff for: e2e/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"@types/node": "18.x",
5353
"@types/react": "^18.3.3",
5454
"@types/react-dom": "^18.3.0",
55+
"create-rsbuild": "workspace:*",
5556
"fast-glob": "^3.3.2",
5657
"fs-extra": "^11.2.0",
5758
"playwright": "1.44.1",

Diff for: pnpm-lock.yaml

+5-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: pnpm-workspace.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ packages:
77
- '!**/compiled/**'
88
- '!**/dist-types/**'
99
- '!**/create-rsbuild/template-*/**'
10+
- '!**/test-temp-*/**'

0 commit comments

Comments
 (0)