Skip to content

Commit

Permalink
test: add basic test case
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Dec 11, 2024
1 parent 1cdfe4b commit 0a7efce
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const pluginPublint = (
}

if (formatted.errors.length > 0) {
process.exit(1);
throw new Error('Publint failed!');
}
});
},
Expand Down
52 changes: 25 additions & 27 deletions test/basic/index.test.ts
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();
});
14 changes: 14 additions & 0 deletions test/basic/package.json
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"
}
}
}
39 changes: 26 additions & 13 deletions test/helper.ts
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();
}
},
};
};

0 comments on commit 0a7efce

Please sign in to comment.