Skip to content

Commit ff6531d

Browse files
committed
test: add e2e case
1 parent 52527b8 commit ff6531d

File tree

3 files changed

+42
-44
lines changed

3 files changed

+42
-44
lines changed

test/basic/index.test.ts

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
1-
import { dirname } from 'node:path';
1+
import { readFileSync } from 'node:fs';
2+
import { dirname, join } from 'node:path';
23
import { fileURLToPath } from 'node:url';
34
import { expect, test } from '@playwright/test';
45
import { createRsbuild } from '@rsbuild/core';
56
import { pluginImageCompress } from '../../src';
6-
import { getRandomPort } from '../helper';
77

88
const __dirname = dirname(fileURLToPath(import.meta.url));
99

10-
test('should render page as expected', async ({ page }) => {
10+
test('should compress image with use plugin-image-compress', async () => {
1111
const rsbuild = await createRsbuild({
1212
cwd: __dirname,
1313
rsbuildConfig: {
1414
plugins: [pluginImageCompress()],
15-
server: {
16-
port: getRandomPort(),
17-
},
1815
},
1916
});
2017

21-
const { server, urls } = await rsbuild.startDevServer();
22-
23-
await page.goto(urls[0]);
24-
expect(await page.evaluate('window.test')).toBe(1);
25-
26-
await server.close();
27-
});
28-
29-
test('should build succeed', async ({ page }) => {
30-
const rsbuild = await createRsbuild({
31-
cwd: __dirname,
32-
rsbuildConfig: {
33-
plugins: [pluginImageCompress()],
34-
},
35-
});
36-
37-
await rsbuild.build();
38-
const { server, urls } = await rsbuild.preview();
39-
40-
await page.goto(urls[0]);
41-
expect(await page.evaluate('window.test')).toBe(1);
42-
43-
await server.close();
18+
await expect(rsbuild.build()).resolves.toBeDefined();
19+
20+
const jpeg = readFileSync(
21+
join(__dirname, 'dist/static/image/image.jpeg'),
22+
'utf-8',
23+
);
24+
const png = readFileSync(
25+
join(__dirname, 'dist/static/image/image.png'),
26+
'utf-8',
27+
);
28+
const svg = readFileSync(
29+
join(__dirname, 'dist/static/image/mobile.svg'),
30+
'utf-8',
31+
);
32+
// const ico = names.find((item) => item.endsWith('.ico'))!;
33+
34+
const assetsDir = join(__dirname, '../assets');
35+
const originJpeg = readFileSync(join(assetsDir, 'image.jpeg'), 'utf-8');
36+
const originPng = readFileSync(join(assetsDir, 'image.png'), 'utf-8');
37+
const originSvg = readFileSync(join(assetsDir, 'mobile.svg'), 'utf-8');
38+
// const originIco = readFileSync(join(assetsDir, 'image.ico'), 'utf-8');
39+
40+
expect(outputs[jpeg].length).toBeLessThan(originJpeg.length);
41+
expect(outputs[png].length).toBeLessThan(originPng.length);
42+
expect(outputs[svg].length).toBeLessThan(originSvg.length);
43+
// TODO ico file size is not less than origin
44+
// expect(outputs[ico].length).toBeLessThan(originIco.length);
4445
});

test/basic/src/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1-
window.test = 1;
1+
import imageIco from '../../assets/image.ico?url';
2+
import imageJpeg from '../../assets/image.jpeg?url';
3+
import imagePng from '../../assets/image.png?url';
4+
import imageSvg from '../../assets/mobile.svg?url';
5+
6+
const images = [imageIco, imagePng, imageJpeg, imageSvg];
7+
8+
for (const image of images) {
9+
const el = new Image();
10+
el.src = image;
11+
document.body.appendChild(el);
12+
}

test/helper.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)