generated from rspack-contrib/rsbuild-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1cdfe4b
commit 0a7efce
Showing
4 changed files
with
66 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,42 @@ | ||
import { dirname } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import { expect, test } from '@playwright/test'; | ||
import { createRsbuild } from '@rsbuild/core'; | ||
import { pluginPublint } from '../../src'; | ||
import { getRandomPort } from '../helper'; | ||
import { proxyConsole } from '../helper'; | ||
import { stripVTControlCharacters as stripAnsi } from 'node:util'; | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
const getPublintLogs = (logs: string[]) => { | ||
const result: string[] = []; | ||
|
||
test('should render page as expected', async ({ page }) => { | ||
const rsbuild = await createRsbuild({ | ||
cwd: __dirname, | ||
rsbuildConfig: { | ||
plugins: [pluginPublint()], | ||
server: { | ||
port: getRandomPort(), | ||
}, | ||
}, | ||
}); | ||
|
||
const { server, urls } = await rsbuild.startDevServer(); | ||
for (const log of logs) { | ||
if (log && (log.includes('Publint') || result.length > 0)) { | ||
result.push(stripAnsi(log)); | ||
} | ||
} | ||
return result; | ||
}; | ||
|
||
await page.goto(urls[0]); | ||
expect(await page.evaluate('window.test')).toBe(1); | ||
|
||
await server.close(); | ||
}); | ||
test('should run publint as expected', async ({ page }) => { | ||
const { logs, restore } = proxyConsole(); | ||
|
||
test('should build succeed', async ({ page }) => { | ||
const rsbuild = await createRsbuild({ | ||
cwd: __dirname, | ||
rsbuildConfig: { | ||
plugins: [pluginPublint()], | ||
}, | ||
}); | ||
|
||
await rsbuild.build(); | ||
const { server, urls } = await rsbuild.preview(); | ||
await expect(rsbuild.build()).rejects.toThrowError('Publint failed!'); | ||
|
||
await page.goto(urls[0]); | ||
expect(await page.evaluate('window.test')).toBe(1); | ||
expect(getPublintLogs(logs)).toEqual([ | ||
'error Publint found 3 errors:', | ||
'error pkg.exports["."].types should be the first in the object as required by TypeScript.', | ||
'error pkg.exports["."].import is ./dist/index.js but the file does not exist.', | ||
'error pkg.exports["."].types is ./dist/index.d.ts but the file does not exist.', | ||
'warn Publint found 1 warnings:', | ||
`warn pkg.repository.url is [email protected]:test/test.git which isn't a valid git URL. A valid git URL is usually in the form of "git+https://example.com/user/repo.git".`, | ||
'info Publint found 1 suggestions:', | ||
'info The package does not specify the "type" field. NodeJS may attempt to detect the package type causing a small performance hit. Consider adding "type": "commonjs".', | ||
]); | ||
|
||
await server.close(); | ||
restore(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "invalid-package-a", | ||
"version": "0.0.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "[email protected]:test/test.git" | ||
}, | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.js", | ||
"types": "./dist/index.d.ts" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,27 @@ | ||
const portMap = new Map(); | ||
|
||
export function getRandomPort( | ||
defaultPort = Math.ceil(Math.random() * 30000) + 15000, | ||
) { | ||
let port = defaultPort; | ||
while (true) { | ||
if (!portMap.get(port)) { | ||
portMap.set(port, 1); | ||
return port; | ||
} | ||
port++; | ||
export const proxyConsole = ( | ||
types: string | string[] = ['log', 'warn', 'info', 'error'], | ||
) => { | ||
const logs: string[] = []; | ||
const restores: Array<() => void> = []; | ||
|
||
for (const type of Array.isArray(types) ? types : [types]) { | ||
const method = console[type]; | ||
|
||
restores.push(() => { | ||
console[type] = method; | ||
}); | ||
|
||
console[type] = (log) => { | ||
logs.push(log); | ||
}; | ||
} | ||
} | ||
|
||
return { | ||
logs, | ||
restore: () => { | ||
for (const restore of restores) { | ||
restore(); | ||
} | ||
}, | ||
}; | ||
}; |