From 0f0a4e41dbe94bc8e746c07823053f21bffd0bb7 Mon Sep 17 00:00:00 2001 From: Timeless0911 Date: Mon, 30 Nov 2026 17:13:23 +0800 Subject: [PATCH] test: reduce noise in unit and integration logs --- packages/core/tests/exe.test.ts | 10 ++- packages/create-rslib/test/index.test.ts | 5 +- pnpm-lock.yaml | 2 + .../auto-external/false/rslib.config.ts | 5 +- .../dts-api-new/bundle-false/index.test.ts | 33 +++++----- .../dts-api-new/bundle/index.test.ts | 21 ++++--- .../exports/build-error/package.json | 3 +- .../exports/bundleless/package.json | 3 +- tests/integration/exports/css/package.json | 3 +- .../exports/dts/matched/package.json | 3 +- .../exports/dual-format/package.json | 3 +- .../integration/exports/esm-only/package.json | 3 +- .../integration/exports/include/package.json | 3 +- tests/integration/exports/index.test.ts | 62 +++++++++++-------- .../exports/multiple-entries/package.json | 3 +- tests/integration/exports/source/package.json | 1 + tests/integration/exports/umd/package.json | 3 +- .../external-helpers/true/rslib.config.ts | 2 +- tests/integration/runtime-mode/package.json | 5 ++ tests/integration/shims/cjs/src/index.ts | 1 - tests/scripts/shared.ts | 23 +++---- 21 files changed, 115 insertions(+), 82 deletions(-) create mode 100644 tests/integration/runtime-mode/package.json diff --git a/packages/core/tests/exe.test.ts b/packages/core/tests/exe.test.ts index 51e4f280c1..2050cce58a 100644 --- a/packages/core/tests/exe.test.ts +++ b/packages/core/tests/exe.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, test } from '@rstest/core'; +import { describe, expect, onTestFinished, test } from '@rstest/core'; import path from 'node:path'; import { composeCreateRsbuildConfig } from '../src/config'; import { resolveExeTargets } from '../src/exe'; @@ -13,6 +13,7 @@ import { assertSupportedExeRuntime, isExeSupportedNodeVersion, } from '../src/exe/version'; +import { logger } from '../src/utils/logger'; const packageRoot = path.resolve(import.meta.dirname, '..'); @@ -192,6 +193,13 @@ describe('experiments.exe', () => { }); test('should force disable deprecated lib.autoExternal for executables', async () => { + // Suppress the expected warning from exercising the deprecated option. + const originalLogLevel = logger.level; + onTestFinished(() => { + logger.level = originalLogLevel; + }); + logger.level = 'error'; + const [config] = await withSupportedNodeRuntime(() => composeTestRslibConfig({ format: 'esm', diff --git a/packages/create-rslib/test/index.test.ts b/packages/create-rslib/test/index.test.ts index d80715a33b..5e1ac0e4b8 100644 --- a/packages/create-rslib/test/index.test.ts +++ b/packages/create-rslib/test/index.test.ts @@ -122,7 +122,10 @@ describe('node', () => { const { content, filePath } = await loadConfig({ cwd: dir }); expect(filePath).toBe(join(dir, `rslib.config.${c.lang}`)); - const rslib = await createRslib({ cwd: dir, config: content }); + const rslib = await createRslib({ + cwd: dir, + config: { ...content, logLevel: 'warn' }, + }); await rslib.build(); const distPath = join(dir, 'dist'); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1b17192bd5..f01f7b4198 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1511,6 +1511,8 @@ snapshots: "tests/integration/resolve/with-main-fields": {}, + "tests/integration/runtime-mode": {}, + "tests/integration/server/basic": {}, "tests/integration/server/mf-dev": {}, diff --git a/tests/integration/auto-external/false/rslib.config.ts b/tests/integration/auto-external/false/rslib.config.ts index 4360d713a8..3347af75b8 100644 --- a/tests/integration/auto-external/false/rslib.config.ts +++ b/tests/integration/auto-external/false/rslib.config.ts @@ -4,18 +4,19 @@ import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper'; export default defineConfig({ lib: [ generateBundleEsmConfig({ - autoExternal: false, dts: { bundle: true, }, }), generateBundleCjsConfig({ - autoExternal: false, dts: { bundle: true, }, }), ], + output: { + autoExternal: false, + }, source: { entry: { index: './src/index.ts', diff --git a/tests/integration/dts-api-new/bundle-false/index.test.ts b/tests/integration/dts-api-new/bundle-false/index.test.ts index 4b59eabc5f..5ff7987bd0 100644 --- a/tests/integration/dts-api-new/bundle-false/index.test.ts +++ b/tests/integration/dts-api-new/bundle-false/index.test.ts @@ -1,16 +1,14 @@ import { createRslib, loadConfig } from '@rslib/core'; -import { describe, expect, onTestFinished, test } from '@rstest/core'; +import { describe, expect, test } from '@rstest/core'; import { existsSync, writeFileSync } from 'node:fs'; import { basename, join } from 'node:path'; import { readFile, rm, writeFile } from 'node:fs/promises'; import { setImmediate } from 'node:timers/promises'; -import { stripVTControlCharacters } from 'node:util'; import { buildAndGetResults, createTempFiles, getFileBySuffix, globContentJSON, - proxyConsole, queryContent, rslibBuild, } from 'test-helper'; @@ -36,24 +34,21 @@ describe('dts with JavaScript API when bundle: false', () => { test('rebuilds declarations in watch mode', async () => { const loadedConfig = await loadConfig({ cwd: basicPath }); - const { content } = loadedConfig; - content.logLevel = 'info'; + loadedConfig.content.logLevel = 'warn'; const rslib = await createRslib({ cwd: basicPath, config: loadedConfig }); + let buildFinished = false; + rslib.onAfterCreateRsbuild(({ rsbuild }) => { + rsbuild.onAfterBuild(() => { + buildFinished = true; + }); + }); const sourceFile = join(basicPath, 'src/utils.ts'); const original = await readFile(sourceFile, 'utf8'); const configFile = join(basicPath, 'tsconfig.json'); const originalConfig = await readFile(configFile, 'utf8'); const declarationFile = join(basicPath, 'dist/esm/utils.d.ts'); - const { logs, restore } = proxyConsole('log'); - onTestFinished(restore); const waitForBuildEnd = async () => { - // The new API backend has no independent watcher. Rspack's afterCompile - // awaits its DTS task before the done hook prints this log. - await expect - .poll(() => stripVTControlCharacters(logs.join('\n')), { - timeout: 10_000, - }) - .toContain('built in'); + await expect.poll(() => buildFinished, { timeout: 10_000 }).toBe(true); // Rspack installs its watcher on the next tick after the done hooks. await setImmediate(); }; @@ -71,16 +66,16 @@ describe('dts with JavaScript API when bundle: false', () => { try { await waitForBuildEnd(); await waitForDeclaration('=> number'); - logs.length = 0; + buildFinished = false; writeFileSync( sourceFile, 'export const add = (a: number, b: number): string => String(a + b);', ); await waitForBuildEnd(); await waitForDeclaration('=> string'); - logs.length = 0; const config = JSON.parse(originalConfig); config.compilerOptions.declarationMap = true; + buildFinished = false; writeFileSync(configFile, JSON.stringify(config)); await waitForBuildEnd(); await waitForDeclaration('sourceMappingURL=utils.d.ts.map'); @@ -125,8 +120,9 @@ describe('dts with JavaScript API when bundle: false', () => { config.compilerOptions.target = 'invalid'; try { await writeFile(configFile, JSON.stringify(config)); + // The DTS subprocess inherits stdio and bypasses Rstest's console capture. await expect( - rslibBuild({ cwd: basicPath, lib: ['esm'] }), + rslibBuild({ cwd: basicPath, lib: ['esm'], logLevel: 'silent' }), ).rejects.toThrow('declaration files generation'); } finally { await writeFile(configFile, original); @@ -152,6 +148,7 @@ describe('dts with JavaScript API when bundle: false', () => { await expect( rslibBuild({ cwd: fixturePath, + logLevel: 'silent', modifyConfig(config) { for (const lib of config.lib!) { lib.dts = { ...(lib.dts as object), abortOnError: undefined }; @@ -163,6 +160,7 @@ describe('dts with JavaScript API when bundle: false', () => { const { js, dts } = await buildAndGetResults({ fixturePath, type: 'all', + logLevel: 'silent', }); expect(js.isSuccess).toBe(true); expect(getFileBySuffix(js.contents.esm, 'index.js')).toContain('getA'); @@ -350,6 +348,7 @@ describe('dts with JavaScript API when bundle: false', () => { const fixturePath = join(__dirname, 'no-emit-on-error'); const { buildResult } = await rslibBuild({ cwd: fixturePath, + logLevel: 'silent', modifyConfig(config) { config.source = { ...config.source, diff --git a/tests/integration/dts-api-new/bundle/index.test.ts b/tests/integration/dts-api-new/bundle/index.test.ts index feea141c62..485ea4eaff 100644 --- a/tests/integration/dts-api-new/bundle/index.test.ts +++ b/tests/integration/dts-api-new/bundle/index.test.ts @@ -1,7 +1,13 @@ import { describe, expect, test } from '@rstest/core'; import { existsSync } from 'node:fs'; +import { readdir } from 'node:fs/promises'; import { join } from 'node:path'; -import { buildAndGetResults, createTempFiles, queryContent } from 'test-helper'; +import { + buildAndGetResults, + createTempFiles, + queryContent, + runCliSync, +} from 'test-helper'; describe('dts with JavaScript API when bundle: true', () => { test('basic', async () => { @@ -47,15 +53,12 @@ describe('dts with JavaScript API when bundle: true', () => { }); test('abortOnError: false preserves JavaScript output but skips declaration bundling', async () => { - const { js, dts } = await buildAndGetResults({ - fixturePath: join(__dirname, 'abort-on-error'), - type: 'all', - }); + const fixturePath = join(__dirname, 'abort-on-error'); + const { status, stderr } = runCliSync('build', { cwd: fixturePath }); - expect(js.isSuccess).toBe(true); - expect(js.files.esm).toHaveLength(1); - expect(js.entries.esm).toContain('getA'); - expect(dts.files).toEqual({}); + expect(status).toBe(0); + expect(stderr).toContain('TS2322'); + expect(await readdir(join(fixturePath, 'dist/esm'))).toEqual(['index.js']); }); test('autoExtension: true', async () => { diff --git a/tests/integration/exports/build-error/package.json b/tests/integration/exports/build-error/package.json index 76e2e4f398..747e523733 100644 --- a/tests/integration/exports/build-error/package.json +++ b/tests/integration/exports/build-error/package.json @@ -1,5 +1,6 @@ { "name": "exports-build-error-test", "version": "1.0.0", - "private": true + "private": true, + "type": "module" } diff --git a/tests/integration/exports/bundleless/package.json b/tests/integration/exports/bundleless/package.json index d83e38342f..11724883a6 100644 --- a/tests/integration/exports/bundleless/package.json +++ b/tests/integration/exports/bundleless/package.json @@ -1,5 +1,6 @@ { "name": "exports-bundleless-test", "version": "1.0.0", - "private": true + "private": true, + "type": "module" } diff --git a/tests/integration/exports/css/package.json b/tests/integration/exports/css/package.json index 3e050a35ff..0add2154d7 100644 --- a/tests/integration/exports/css/package.json +++ b/tests/integration/exports/css/package.json @@ -1,5 +1,6 @@ { "name": "exports-css-test", "version": "1.0.0", - "private": true + "private": true, + "type": "module" } diff --git a/tests/integration/exports/dts/matched/package.json b/tests/integration/exports/dts/matched/package.json index 1cac2c101f..8f46a9ce9e 100644 --- a/tests/integration/exports/dts/matched/package.json +++ b/tests/integration/exports/dts/matched/package.json @@ -1,5 +1,6 @@ { "name": "exports-dts-matched-test", "version": "1.0.0", - "private": true + "private": true, + "type": "module" } diff --git a/tests/integration/exports/dual-format/package.json b/tests/integration/exports/dual-format/package.json index 0a19681c5d..37248e6b09 100644 --- a/tests/integration/exports/dual-format/package.json +++ b/tests/integration/exports/dual-format/package.json @@ -1,5 +1,6 @@ { "name": "exports-dual-format-test", "version": "1.0.0", - "private": true + "private": true, + "type": "module" } diff --git a/tests/integration/exports/esm-only/package.json b/tests/integration/exports/esm-only/package.json index fcf15437d0..7614002c90 100644 --- a/tests/integration/exports/esm-only/package.json +++ b/tests/integration/exports/esm-only/package.json @@ -1,5 +1,6 @@ { "name": "exports-esm-only-test", "version": "1.0.0", - "private": true + "private": true, + "type": "module" } diff --git a/tests/integration/exports/include/package.json b/tests/integration/exports/include/package.json index cf065b4141..16a9e4e904 100644 --- a/tests/integration/exports/include/package.json +++ b/tests/integration/exports/include/package.json @@ -1,5 +1,6 @@ { "name": "exports-include-test", "version": "1.0.0", - "private": true + "private": true, + "type": "module" } diff --git a/tests/integration/exports/index.test.ts b/tests/integration/exports/index.test.ts index 78085bc0ec..f42621e66e 100644 --- a/tests/integration/exports/index.test.ts +++ b/tests/integration/exports/index.test.ts @@ -48,8 +48,8 @@ describe('exports', () => { expect(readFixtureExports('dual-format')).toEqual({ '.': { - import: './dist/esm/index.mjs', - require: './dist/cjs/index.js', + import: './dist/esm/index.js', + require: './dist/cjs/index.cjs', }, }); }); @@ -59,7 +59,7 @@ describe('exports', () => { await buildAndGetResults({ fixturePath }); expect(readFixtureExports('esm-only')).toEqual({ - '.': './dist/esm/index.mjs', + '.': './dist/esm/index.js', }); }); @@ -154,6 +154,7 @@ describe('exports', () => { [ '{', '\t"name": "exports-esm-only-test",', + '\t"type": "module",', '\t"exports": {', '\t\t"./package.json": "./package.json",', '\t\t".": {', @@ -172,8 +173,9 @@ describe('exports', () => { [ '{', '\t"name": "exports-esm-only-test",', + '\t"type": "module",', '\t"exports": {', - '\t\t".": "./dist/esm/index.mjs"', + '\t\t".": "./dist/esm/index.js"', '\t}', '}', '', @@ -186,13 +188,17 @@ describe('exports', () => { const fixturePath = join(__dirname, fixture); writeFileSync( join(fixturePath, 'package.json'), - JSON.stringify({ name: 'exports-esm-only-test', exports: './index.js' }), + JSON.stringify({ + name: 'exports-esm-only-test', + type: 'module', + exports: './index.js', + }), ); await buildAndGetResults({ fixturePath }); expect(readFixtureExports(fixture)).toEqual({ - '.': './dist/esm/index.mjs', + '.': './dist/esm/index.js', }); }); @@ -248,7 +254,7 @@ describe('exports', () => { }); expect(readFixtureExports(fixture)).toEqual({ - '.': './dist/esm/index.mjs', + '.': './dist/esm/index.js', './package.json': './package.json', './pkg': './package.json', }); @@ -282,7 +288,7 @@ describe('exports', () => { expect(readFixtureExports(fixture)).toEqual({ '.': { browser: './dist/browser.js', - default: './dist/esm/index.mjs', + default: './dist/esm/index.js', }, './worker': './dist/esm/worker.mjs', }); @@ -299,8 +305,8 @@ describe('exports', () => { await buildAndGetResults({ fixturePath }); expect(readFixtureExports('multiple-entries')).toEqual({ - '.': './dist/esm/index.mjs', - './utils/numbers': './dist/esm/utils/numbers.mjs', + '.': './dist/esm/index.js', + './utils/numbers': './dist/esm/utils/numbers.js', }); }); @@ -309,7 +315,7 @@ describe('exports', () => { await buildAndGetResults({ fixturePath }); expect(readFixtureExports('bundleless')).toEqual({ - '.': './dist/esm/index.mjs', + '.': './dist/esm/index.js', }); }); @@ -326,7 +332,9 @@ describe('exports', () => { test('should not generate exports when the build fails', async () => { const fixturePath = join(__dirname, 'build-error'); - await expectBuildToFail(buildAndGetResults({ fixturePath })); + await expectBuildToFail( + buildAndGetResults({ fixturePath, logLevel: 'silent' }), + ); expect(readFixtureExports('build-error')).toBeUndefined(); }); @@ -337,8 +345,8 @@ describe('exports', () => { expect(readFixtureExports('dts/matched')).toEqual({ '.': { - import: './dist/esm/index.mjs', - require: './dist/cjs/index.js', + import: './dist/esm/index.js', + require: './dist/cjs/index.cjs', }, }); }); @@ -407,8 +415,8 @@ describe('exports', () => { await buildAndGetResults({ fixturePath }); expect(readFixtureExports('include')).toEqual({ - '.': './dist/esm/index.mjs', - './utils/numbers': './dist/esm/utils/numbers.mjs', + '.': './dist/esm/index.js', + './utils/numbers': './dist/esm/utils/numbers.js', }); }); @@ -418,8 +426,8 @@ describe('exports', () => { expect(readFixtureExports('css')).toEqual({ '.': { - import: './dist/esm/index.mjs', - require: './dist/cjs/index.js', + import: './dist/esm/index.js', + require: './dist/cjs/index.cjs', }, './index.css': './dist/esm/index.css', './theme.css': './dist/esm/theme.css', @@ -437,14 +445,14 @@ describe('exports', () => { await rslibBuild({ cwd: fixturePath, modifyConfig: (config) => { - config.exports = { include: ['index.{js,mjs}', 'theme'] }; + config.exports = { include: ['index.{js,cjs}', 'theme'] }; }, }); expect(readFixtureExports(fixture)).toEqual({ '.': { - import: './dist/esm/index.mjs', - require: './dist/cjs/index.js', + import: './dist/esm/index.js', + require: './dist/cjs/index.cjs', }, './theme.css': './dist/esm/theme.css', }); @@ -480,8 +488,8 @@ describe('exports', () => { access: 'public', exports: { '.': { - import: './dist/esm/index.mjs', - require: './dist/cjs/index.js', + import: './dist/esm/index.js', + require: './dist/cjs/index.cjs', }, }, }); @@ -509,8 +517,8 @@ describe('exports', () => { const pkg = JSON.parse(readFileSync(packageJsonPath, 'utf8')); expect(pkg.exports).toEqual({ '.': { - import: './dist/esm/index.mjs', - require: './dist/cjs/index.js', + import: './dist/esm/index.js', + require: './dist/cjs/index.cjs', }, }); expect(pkg.publishConfig).toEqual({ access: 'public' }); @@ -559,8 +567,8 @@ describe('exports', () => { expect(exports).toEqual({ '.': { '@acme/source': './src/index.ts', - import: './dist/esm/index.mjs', - require: './dist/cjs/index.js', + import: './dist/esm/index.js', + require: './dist/cjs/index.cjs', }, }); }); diff --git a/tests/integration/exports/multiple-entries/package.json b/tests/integration/exports/multiple-entries/package.json index a0d184502e..fd1207689b 100644 --- a/tests/integration/exports/multiple-entries/package.json +++ b/tests/integration/exports/multiple-entries/package.json @@ -1,5 +1,6 @@ { "name": "exports-multiple-entries-test", "version": "1.0.0", - "private": true + "private": true, + "type": "module" } diff --git a/tests/integration/exports/source/package.json b/tests/integration/exports/source/package.json index 25725d73be..13781d5050 100644 --- a/tests/integration/exports/source/package.json +++ b/tests/integration/exports/source/package.json @@ -2,6 +2,7 @@ "name": "exports-source-test", "version": "1.0.0", "private": true, + "type": "module", "publishConfig": { "access": "public" } diff --git a/tests/integration/exports/umd/package.json b/tests/integration/exports/umd/package.json index 166706732c..d24e6b53b6 100644 --- a/tests/integration/exports/umd/package.json +++ b/tests/integration/exports/umd/package.json @@ -1,5 +1,6 @@ { "name": "exports-umd-test", "version": "1.0.0", - "private": true + "private": true, + "type": "module" } diff --git a/tests/integration/external-helpers/true/rslib.config.ts b/tests/integration/external-helpers/true/rslib.config.ts index 1c2b91759e..7a568d2004 100644 --- a/tests/integration/external-helpers/true/rslib.config.ts +++ b/tests/integration/external-helpers/true/rslib.config.ts @@ -13,8 +13,8 @@ export default defineConfig({ generateBundleEsmConfig({ syntax: 'es5', externalHelpers: true, - autoExternal: false, output: { + autoExternal: false, distPath: './dist/2', }, }), diff --git a/tests/integration/runtime-mode/package.json b/tests/integration/runtime-mode/package.json new file mode 100644 index 0000000000..11d8da9994 --- /dev/null +++ b/tests/integration/runtime-mode/package.json @@ -0,0 +1,5 @@ +{ + "name": "runtime-mode-test", + "private": true, + "type": "module" +} diff --git a/tests/integration/shims/cjs/src/index.ts b/tests/integration/shims/cjs/src/index.ts index 040dd66347..7517879bee 100644 --- a/tests/integration/shims/cjs/src/index.ts +++ b/tests/integration/shims/cjs/src/index.ts @@ -16,7 +16,6 @@ const dynamicImportMetaUrl = async () => { import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); -console.log(__filename); const importMetaDirname = import.meta.dirname; const importMetaFilename = import.meta.filename; diff --git a/tests/scripts/shared.ts b/tests/scripts/shared.ts index 97ec4464f4..32aad80992 100644 --- a/tests/scripts/shared.ts +++ b/tests/scripts/shared.ts @@ -251,34 +251,29 @@ export async function getResults( }; } -const updateConfigForTest = - (logLevel?: LogLevel) => (rslibConfig: RslibConfig) => { - Object.assign(rslibConfig, { - performance: { - ...rslibConfig.performance, - // Do not print file size in tests - printFileSize: false, - }, - logLevel, - }); - }; - export async function rslibBuild({ cwd, path, modifyConfig, lib, + logLevel = 'warn', }: { cwd: string; path?: string; modifyConfig?: (config: RslibConfig) => void; lib?: string[]; + logLevel?: LogLevel; }) { const result = await loadConfig({ cwd, path, }); const rslibConfig = result.content; + rslibConfig.logLevel = logLevel; + rslibConfig.performance = { + ...rslibConfig.performance, + printFileSize: false, + }; modifyConfig?.(rslibConfig); process.chdir(cwd); const rslib = await createRslib({ @@ -314,7 +309,7 @@ export async function buildAndGetResults({ configPath, type = 'js', lib, - logLevel = 'warn', + logLevel, }: { fixturePath: string; configPath?: string; @@ -325,8 +320,8 @@ export async function buildAndGetResults({ const { rslib, buildResult, rslibConfig } = await rslibBuild({ cwd: fixturePath, path: configPath, - modifyConfig: updateConfigForTest(logLevel), lib, + logLevel, }); const { origin: { bundlerConfigs, rsbuildConfig },