Skip to content

Commit d0238e7

Browse files
chenjiahanCopilot
andauthored
test(e2e): replace manual file lookup with helpers (#6248)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 893a85a commit d0238e7

File tree

25 files changed

+116
-158
lines changed

25 files changed

+116
-158
lines changed

e2e/cases/assets/asset-prefix/index.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, test } from '@e2e/helper';
1+
import { expect, getFileContent, test } from '@e2e/helper';
22

33
test('should allow dev.assetPrefix to be `auto`', async ({ page, dev }) => {
44
await dev({
@@ -98,8 +98,7 @@ test('should use output.assetPrefix in none mode', async ({ build }) => {
9898
});
9999

100100
const files = result.getDistFiles();
101-
const indexHtml =
102-
files[Object.keys(files).find((file) => file.endsWith('index.html'))!];
101+
const indexHtml = getFileContent(files, 'index.html');
103102
expect(indexHtml).toContain('http://prod.com');
104103
expect(indexHtml).not.toContain('http://dev.com');
105104
});

e2e/cases/browserslist/extends-browserslist/index.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { expect, test } from '@e2e/helper';
1+
import { expect, getFileContent, test } from '@e2e/helper';
22

33
test('should extend browserslist and downgrade syntax', async ({ build }) => {
44
const originalCwd = process.cwd();
55
process.chdir(__dirname);
66

77
const rsbuild = await build();
8-
98
const files = rsbuild.getDistFiles();
10-
11-
const indexFile =
12-
files[Object.keys(files).find((file) => file.endsWith('.js'))!];
9+
const indexFile = getFileContent(files, 'index.js');
1310

1411
expect(indexFile.includes('async ')).toBeFalsy();
1512

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import { expect, test } from '@e2e/helper';
1+
import { expect, getFileContent, test } from '@e2e/helper';
22

33
test('should read browserslist array from package.json', async ({ build }) => {
44
const rsbuild = await build();
5-
65
const files = rsbuild.getDistFiles();
7-
8-
const indexFile =
9-
files[Object.keys(files).find((file) => file.endsWith('.js'))!];
10-
6+
const indexFile = getFileContent(files, 'index.js');
117
expect(indexFile.includes('async ')).toBeFalsy();
128
});
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import { expect, test } from '@e2e/helper';
1+
import { expect, getFileContent, test } from '@e2e/helper';
22

33
test('should read browserslist string from package.json', async ({ build }) => {
44
const rsbuild = await build();
5-
65
const files = rsbuild.getDistFiles();
7-
8-
const indexFile =
9-
files[Object.keys(files).find((file) => file.endsWith('.js'))!];
10-
6+
const indexFile = getFileContent(files, 'index.js');
117
expect(indexFile.includes('async ')).toBeFalsy();
128
});
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import { expect, test } from '@e2e/helper';
1+
import { expect, getFileContent, test } from '@e2e/helper';
22

33
test('should read browserslist string from package.json', async ({ build }) => {
44
const rsbuild = await build();
5-
65
const files = rsbuild.getDistFiles();
7-
8-
const indexFile =
9-
files[Object.keys(files).find((file) => file.endsWith('.js'))!];
10-
6+
const indexFile = getFileContent(files, 'index.js');
117
expect(indexFile.includes('async ')).toBeFalsy();
128
});
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { expect, test } from '@e2e/helper';
1+
import { expect, getFileContent, test } from '@e2e/helper';
22

33
test('should allow to configure options of CSSExtractPlugin', async ({
44
build,
55
}) => {
66
const rsbuild = await build();
77
const files = rsbuild.getDistFiles();
8-
const content =
9-
files[Object.keys(files).find((file) => file.endsWith('my-css.css'))!];
10-
8+
const content = getFileContent(files, 'my-css.css');
119
expect(content).toEqual('body{color:red}');
1210
});

e2e/cases/css/css-layers/index.test.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import { expect, rspackTest } from '@e2e/helper';
1+
import { expect, getFileContent, rspackTest } from '@e2e/helper';
22

33
rspackTest(
44
'should bundle CSS layers as expected in build',
55
async ({ build }) => {
66
const rsbuild = await build();
77
const files = rsbuild.getDistFiles();
8-
9-
const content =
10-
files[Object.keys(files).find((file) => file.endsWith('.css'))!];
11-
8+
const content = getFileContent(files, 'index.css');
129
expect(content).toEqual(
1310
'@layer a{.a{color:red}}@layer b{.b{color:green}}@layer c{@layer{.c-sub{color:#00f}.c-sub2{color:green}}.c{color:#00f}}',
1411
);
@@ -18,10 +15,7 @@ rspackTest(
1815
rspackTest('should bundle CSS layers as expected in dev', async ({ dev }) => {
1916
const rsbuild = await dev();
2017
const files = rsbuild.getDistFiles();
21-
22-
const content =
23-
files[Object.keys(files).find((file) => file.endsWith('.css'))!];
24-
18+
const content = getFileContent(files, 'index.css');
2519
expect(content.trim()).toEqual(`@layer a {
2620
.a {
2721
color: red;

e2e/cases/css/css-modules-composes/index.test.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import path from 'node:path';
2-
import { expect, rspackTest } from '@e2e/helper';
2+
import { expect, getFileContent, rspackTest } from '@e2e/helper';
33

44
rspackTest(
55
'should compile CSS Modules composes correctly',
66
async ({ build }) => {
77
const rsbuild = await build();
88
const files = rsbuild.getDistFiles();
9-
10-
const content =
11-
files[Object.keys(files).find((file) => file.endsWith('.css'))!];
12-
9+
const content = getFileContent(files, 'index.css');
1310
expect(content).toMatch(
1411
/.*\{color:#ff0;background:red\}.*\{background:#00f\}/,
1512
);
@@ -27,10 +24,7 @@ rspackTest(
2724
},
2825
});
2926
const files = rsbuild.getDistFiles();
30-
31-
const content =
32-
files[Object.keys(files).find((file) => file.endsWith('.css'))!];
33-
27+
const content = getFileContent(files, 'external.css');
3428
expect(content).toMatch(
3529
/.*\{color:#000;background:#0ff\}.*\{background:green\}/,
3630
);

e2e/cases/css/css-modules-export-locals/index.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { type BuildResult, expect, rspackTest } from '@e2e/helper';
1+
import {
2+
type BuildResult,
3+
expect,
4+
getFileContent,
5+
rspackTest,
6+
} from '@e2e/helper';
27

38
declare global {
49
interface Window {
@@ -8,8 +13,7 @@ declare global {
813

914
const expectCSSContext = async (rsbuild: BuildResult) => {
1015
const files = rsbuild.getDistFiles();
11-
const content =
12-
files[Object.keys(files).find((file) => file.endsWith('.css'))!];
16+
const content = getFileContent(files, 'index.css');
1317
expect(content).toMatch(
1418
/\.the-dash-class-\w{6}{color:#00f}\.theCamelClass-\w{6}{color:red}\.the_underscore_class-\w{6}{color:green}/,
1519
);

e2e/cases/css/css-modules-exports-global/index.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, rspackTest } from '@e2e/helper';
1+
import { expect, getFileContent, rspackTest } from '@e2e/helper';
22

33
rspackTest(
44
'should exports global in CSS Modules correctly in dev build',
@@ -25,8 +25,7 @@ rspackTest(
2525
await expect(test2Locator).toHaveCSS('color', 'rgb(0, 0, 255)');
2626

2727
const files = rsbuild.getDistFiles();
28-
const content =
29-
files[Object.keys(files).find((file) => file.endsWith('.css'))!];
28+
const content = getFileContent(files, 'index.css');
3029
expect(content).toMatch(/\.foo-\w{6}{color:red}\.bar{color:#00f}/);
3130
},
3231
);

0 commit comments

Comments
 (0)