|
| 1 | +import path, { dirname } from 'node:path'; |
| 2 | +import { fileURLToPath } from 'node:url'; |
| 3 | +import { expect, test } from '@playwright/test'; |
| 4 | +import { rspack } from '@rspack/core'; |
| 5 | +import stripAnsi from 'strip-ansi'; |
| 6 | +import { CheckSyntaxRspackPlugin } from '../../dist'; |
| 7 | +import { normalizeToPosixPath, proxyConsole } from '../helper'; |
| 8 | + |
| 9 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 10 | + |
| 11 | +test('should throw error when exist syntax errors', async () => { |
| 12 | + const { logs, restore } = proxyConsole(); |
| 13 | + |
| 14 | + const compiler = rspack({ |
| 15 | + context: __dirname, |
| 16 | + target: ['web', 'es5'], |
| 17 | + mode: process.env.NODE_ENV === 'production' ? 'production' : 'development', |
| 18 | + devtool: 'source-map', |
| 19 | + plugins: [ |
| 20 | + new CheckSyntaxRspackPlugin({ |
| 21 | + ecmaVersion: 5, |
| 22 | + }), |
| 23 | + ], |
| 24 | + }); |
| 25 | + |
| 26 | + await expect( |
| 27 | + new Promise((resolve, reject) => { |
| 28 | + compiler.run((err, stats) => { |
| 29 | + if (err) { |
| 30 | + reject(err); |
| 31 | + return; |
| 32 | + } |
| 33 | + compiler.close(() => { |
| 34 | + resolve(stats); |
| 35 | + }); |
| 36 | + }); |
| 37 | + }), |
| 38 | + ).rejects.toThrowError('[@rsbuild/plugin-check-syntax]'); |
| 39 | + |
| 40 | + restore(); |
| 41 | + |
| 42 | + expect( |
| 43 | + logs.find((log) => |
| 44 | + stripAnsi(log).includes( |
| 45 | + 'Find some syntax that does not match "ecmaVersion <= 5"', |
| 46 | + ), |
| 47 | + ), |
| 48 | + ).toBeTruthy(); |
| 49 | + |
| 50 | + expect(logs.find((log) => log.includes('ERROR 1'))).toBeTruthy(); |
| 51 | + expect( |
| 52 | + logs.find((log) => log.includes('source:') && log.includes('src/test.js')), |
| 53 | + ).toBeTruthy(); |
| 54 | + expect( |
| 55 | + logs.find( |
| 56 | + (log) => |
| 57 | + log.includes('output:') && |
| 58 | + normalizeToPosixPath(log).includes('/dist/main'), |
| 59 | + ), |
| 60 | + ).toBeTruthy(); |
| 61 | + expect(logs.find((log) => log.includes('reason:'))).toBeTruthy(); |
| 62 | + expect( |
| 63 | + logs.find((log) => log.includes('> 1 | export const printLog = () => {')), |
| 64 | + ).toBeTruthy(); |
| 65 | +}); |
0 commit comments