|
1 | 1 | import { dirname } from 'node:path'; |
2 | 2 | import { fileURLToPath } from 'node:url'; |
3 | 3 | 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'; |
7 | 8 |
|
8 | 9 | const __dirname = dirname(fileURLToPath(import.meta.url)); |
9 | 10 |
|
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 | + |
11 | 14 | const rsbuild = await createRsbuild({ |
12 | 15 | cwd: __dirname, |
13 | 16 | rsbuildConfig: { |
| 17 | + ...(await loadConfig({ cwd: __dirname })).content, |
14 | 18 | plugins: [pluginCheckSyntax()], |
15 | | - server: { |
16 | | - port: getRandomPort(), |
17 | | - }, |
18 | 19 | }, |
19 | 20 | }); |
20 | 21 |
|
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 | + ); |
22 | 75 |
|
23 | | - await page.goto(urls[0]); |
24 | | - expect(await page.evaluate('window.test')).toBe(1); |
| 76 | + restore(); |
25 | 77 |
|
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(); |
27 | 93 | }); |
28 | 94 |
|
29 | | -test('should build succeed', async ({ page }) => { |
| 95 | +test('should not throw error when the file is excluded', async () => { |
30 | 96 | const rsbuild = await createRsbuild({ |
31 | 97 | cwd: __dirname, |
32 | 98 | rsbuildConfig: { |
33 | | - plugins: [pluginCheckSyntax()], |
| 99 | + ...(await loadConfig({ cwd: __dirname })).content, |
| 100 | + plugins: [ |
| 101 | + pluginCheckSyntax({ |
| 102 | + exclude: /src\/test/, |
| 103 | + }), |
| 104 | + ], |
34 | 105 | }, |
35 | 106 | }); |
36 | 107 |
|
37 | | - await rsbuild.build(); |
38 | | - const { server, urls } = await rsbuild.preview(); |
| 108 | + await expect(rsbuild.build()).resolves.toBeUndefined(); |
| 109 | +}); |
39 | 110 |
|
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 | + }); |
42 | 123 |
|
43 | | - await server.close(); |
| 124 | + await expect(rsbuild.build()).resolves.toBeUndefined(); |
44 | 125 | }); |
0 commit comments