diff --git a/tests/integration/cli/build-watch/build.test.ts b/tests/integration/cli/build-watch/build.test.ts index df6e8f30d..79b1b7981 100644 --- a/tests/integration/cli/build-watch/build.test.ts +++ b/tests/integration/cli/build-watch/build.test.ts @@ -1,9 +1,14 @@ -import { exec, spawn } from 'node:child_process'; +import { spawn } from 'node:child_process'; import path from 'node:path'; import { after } from 'node:test'; import { describe, expect, test } from '@rstest/core'; import fse from 'fs-extra'; -import { awaitFileChanges, awaitFileExists } from 'test-helper'; +import { + awaitFileChanges, + awaitFileExists, + rslibBinPath, + runCli, +} from 'test-helper'; describe('build --watch command', async () => { test('basic', async () => { @@ -26,7 +31,7 @@ export default defineConfig({ `, ); - const process = exec(`npx rslib build --watch -c ${tempConfigFile}`, { + const process = runCli(`build --watch -c ${tempConfigFile}`, { cwd: __dirname, }); @@ -98,8 +103,8 @@ export default defineConfig({ const distFoo2File = path.join(__dirname, 'dist/esm/foo2.js'); const child = spawn( - 'npx', - ['rslib', 'build', '--watch', '-c', tempConfigFile], + 'node', + [rslibBinPath, 'build', '--watch', '-c', tempConfigFile], { cwd: __dirname, stdio: 'inherit', diff --git a/tests/integration/cli/build/build.test.ts b/tests/integration/cli/build/build.test.ts index badd0104c..130b96f32 100644 --- a/tests/integration/cli/build/build.test.ts +++ b/tests/integration/cli/build/build.test.ts @@ -1,13 +1,12 @@ -import { execSync } from 'node:child_process'; import path from 'node:path'; import { describe, expect, test } from '@rstest/core'; import fse from 'fs-extra'; -import { buildAndGetResults, globContentJSON } from 'test-helper'; +import { buildAndGetResults, globContentJSON, runCliSync } from 'test-helper'; describe('build command', async () => { test('basic', async () => { await fse.remove(path.join(__dirname, 'dist')); - execSync('npx rslib build', { + runCliSync('build', { cwd: __dirname, }); @@ -23,7 +22,7 @@ describe('build command', async () => { test('--lib', async () => { await fse.remove(path.join(__dirname, 'dist')); - execSync('npx rslib build --lib esm', { + runCliSync('build --lib esm', { cwd: __dirname, }); @@ -38,7 +37,7 @@ describe('build command', async () => { test('--lib multiple', async () => { await fse.remove(path.join(__dirname, 'dist')); - execSync('npx rslib build --lib esm --lib cjs', { + runCliSync('build --lib esm --lib cjs', { cwd: __dirname, }); @@ -69,12 +68,9 @@ describe('build command', async () => { test('--config', async () => { await fse.remove(path.join(__dirname, 'dist')); - execSync( - 'npx rslib build --config ./custom-config/rslib.config.custom.ts', - { - cwd: __dirname, - }, - ); + runCliSync('build --config ./custom-config/rslib.config.custom.ts', { + cwd: __dirname, + }); const files = await globContentJSON(path.join(__dirname, 'dist')); const fileNames = Object.keys(files).sort(); @@ -88,7 +84,7 @@ describe('build command', async () => { test('--root', async () => { await fse.remove(path.join(__dirname, 'dist')); - execSync('npx rslib build --root custom-root', { + runCliSync('build --root custom-root', { cwd: __dirname, }); diff --git a/tests/integration/cli/env/env.test.ts b/tests/integration/cli/env/env.test.ts index cf2d5eb0f..0091757c4 100644 --- a/tests/integration/cli/env/env.test.ts +++ b/tests/integration/cli/env/env.test.ts @@ -1,7 +1,7 @@ -import { execSync } from 'node:child_process'; import fs from 'node:fs'; import path from 'node:path'; import { beforeEach, describe, expect, test } from '@rstest/core'; +import { runCliSync } from 'test-helper'; const localFile = path.join(__dirname, '.env.local'); const prodLocalFile = path.join(__dirname, '.env.production.local'); @@ -13,7 +13,7 @@ describe('load env file', async () => { }); test('should load .env config and allow rslib.config.ts to read env vars', async () => { - execSync('npx rslib build', { + runCliSync('build', { cwd: __dirname, }); @@ -23,9 +23,10 @@ describe('load env file', async () => { test('should load .env.local with higher priority', async () => { fs.writeFileSync(localFile, 'FOO=2'); - execSync('npx rslib build', { + runCliSync('build', { cwd: __dirname, }); + expect(fs.existsSync(path.join(__dirname, 'dist/2'))).toBeTruthy(); }); @@ -33,14 +34,15 @@ describe('load env file', async () => { fs.writeFileSync(localFile, 'FOO=2'); fs.writeFileSync(prodLocalFile, 'FOO=3'); - execSync('npx rslib build', { + runCliSync('build', { cwd: __dirname, }); + expect(fs.existsSync(path.join(__dirname, 'dist/3'))).toBeTruthy(); }); test('should allow to specify env mode via --env-mode', async () => { - execSync('npx rslib build --env-mode test', { + runCliSync('build --env-mode test', { cwd: __dirname, }); @@ -48,7 +50,7 @@ describe('load env file', async () => { }); test('should allow to custom env directory via --env-dir', async () => { - execSync('npx rslib build --env-dir env', { + runCliSync('build --env-dir env', { cwd: __dirname, }); diff --git a/tests/integration/cli/inspect/inspect.test.ts b/tests/integration/cli/inspect/inspect.test.ts index 17d761a12..31262ea2b 100644 --- a/tests/integration/cli/inspect/inspect.test.ts +++ b/tests/integration/cli/inspect/inspect.test.ts @@ -1,14 +1,13 @@ -import { execSync } from 'node:child_process'; import path from 'node:path'; import { describe } from 'node:test'; import { expect, test } from '@rstest/core'; import fse from 'fs-extra'; -import { globContentJSON } from 'test-helper'; +import { globContentJSON, runCliSync } from 'test-helper'; describe('inspect command', async () => { test('basic', async () => { await fse.remove(path.join(__dirname, 'dist')); - execSync('npx rslib inspect', { + runCliSync('inspect', { cwd: __dirname, }); @@ -41,7 +40,7 @@ describe('inspect command', async () => { test('--lib', async () => { await fse.remove(path.join(__dirname, 'dist')); - execSync('npx rslib inspect --lib esm', { + runCliSync('inspect --lib esm', { cwd: __dirname, }); @@ -75,7 +74,7 @@ describe('inspect command', async () => { test('--lib multiple', async () => { await fse.remove(path.join(__dirname, 'dist')); - execSync('npx rslib inspect --lib esm --lib cjs', { + runCliSync('inspect --lib esm --lib cjs', { cwd: __dirname, }); diff --git a/tests/integration/cli/mf/mf.test.ts b/tests/integration/cli/mf/mf.test.ts index 7160e9152..283c551bd 100644 --- a/tests/integration/cli/mf/mf.test.ts +++ b/tests/integration/cli/mf/mf.test.ts @@ -1,11 +1,10 @@ -import { exec, execSync } from 'node:child_process'; import { join } from 'node:path'; import { describe } from 'node:test'; import { pluginModuleFederation } from '@module-federation/rsbuild-plugin'; import { startMFDevServer } from '@rslib/core'; import { expect, test } from '@rstest/core'; import fse from 'fs-extra'; -import { awaitFileExists } from 'test-helper'; +import { awaitFileExists, runCli, runCliSync } from 'test-helper'; const { existsSync } = fse; @@ -19,7 +18,7 @@ describe('mf-dev', () => { fse.removeSync(distFolder); const distPath = join(distFolder, 'index.js'); - const childProcess = exec('npx rslib mf-dev --lib mf0', { + const childProcess = runCli('mf-dev --lib mf0', { cwd: fixturePath, env: { ...process.env, @@ -49,7 +48,7 @@ describe('mf-dev', () => { const distPath1 = join(distFolder1, 'index.js'); const distPath2 = join(distFolder2, 'index.js'); - const childProcess = exec('npx rslib mf-dev --lib mf1 --lib mf2', { + const childProcess = runCli('mf-dev --lib mf1 --lib mf2', { cwd: fixturePath, }); @@ -107,7 +106,7 @@ describe('mf build', () => { const distPath = join(fixturePath, 'dist/mf'); const rspackConfigFile = join(distPath, '.rsbuild/rspack.config.mf.mjs'); - execSync('npx rslib build', { + runCliSync('build', { cwd: fixturePath, env: { ...process.env, diff --git a/tests/integration/plugins/index.test.ts b/tests/integration/plugins/index.test.ts index f0cd24fce..831eb4e79 100644 --- a/tests/integration/plugins/index.test.ts +++ b/tests/integration/plugins/index.test.ts @@ -1,8 +1,7 @@ -import { exec } from 'node:child_process'; import fs from 'node:fs'; import { join } from 'node:path'; import { expect, test } from '@rstest/core'; -import { awaitFileExists, buildAndGetResults } from 'test-helper'; +import { awaitFileExists, buildAndGetResults, runCli } from 'test-helper'; import { distIndex } from './basic/rslib.config'; import { plugin1Path, plugin2Path } from './mf-dev/rslib.config'; @@ -17,7 +16,7 @@ test('should run shared plugins only once', async () => { test('should merge plugins correctly', async () => { const fixturePath = join(__dirname, 'mf-dev'); - const childProcess = exec('npx rslib mf-dev', { + const childProcess = runCli('mf-dev', { cwd: fixturePath, }); diff --git a/tests/integration/server/index.test.ts b/tests/integration/server/index.test.ts index b138cd6cc..d1498bc34 100644 --- a/tests/integration/server/index.test.ts +++ b/tests/integration/server/index.test.ts @@ -1,9 +1,8 @@ -import { exec } from 'node:child_process'; import { existsSync } from 'node:fs'; import { join } from 'node:path'; import { describe, expect, test } from '@rstest/core'; import fse from 'fs-extra'; -import { awaitFileExists, buildAndGetResults } from 'test-helper'; +import { awaitFileExists, buildAndGetResults, runCli } from 'test-helper'; describe('server config', async () => { test('basic config', async () => { @@ -24,7 +23,7 @@ describe('server config', async () => { fse.removeSync(distPath); - const childProcess = exec('npx rslib mf-dev', { + const childProcess = runCli('mf-dev', { cwd: fixturePath, env: { ...process.env, diff --git a/tests/scripts/shared.ts b/tests/scripts/shared.ts index 4dc5cfc96..1d949c926 100644 --- a/tests/scripts/shared.ts +++ b/tests/scripts/shared.ts @@ -1,4 +1,10 @@ import assert from 'node:assert'; +import { + type ExecOptions, + type ExecSyncOptions, + exec, + execSync, +} from 'node:child_process'; import fs from 'node:fs'; import { basename, dirname, join, normalize } from 'node:path'; import { fileURLToPath } from 'node:url'; @@ -14,6 +20,19 @@ import { globContentJSON } from './helper'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); +export const rslibBinPath = join( + __dirname, + '../node_modules/@rslib/core/bin/rslib.js', +); + +export function runCliSync(command: string, options?: ExecSyncOptions) { + return execSync(`node ${rslibBinPath} ${command}`, options); +} + +export function runCli(command: string, options?: ExecOptions) { + return exec(`node ${rslibBinPath} ${command}`, options); +} + export function getCwdByExample(exampleName: string) { return join(__dirname, '../../examples', exampleName); }