Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions tests/integration/cli/build-watch/build.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand All @@ -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,
});

Expand Down Expand Up @@ -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',
Expand Down
20 changes: 8 additions & 12 deletions tests/integration/cli/build/build.test.ts
Original file line number Diff line number Diff line change
@@ -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,
});

Expand All @@ -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,
});

Expand All @@ -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,
});

Expand Down Expand Up @@ -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();
Expand All @@ -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,
});

Expand Down
14 changes: 8 additions & 6 deletions tests/integration/cli/env/env.test.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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,
});

Expand All @@ -23,32 +23,34 @@ 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();
});

test('should load .env.production.local with higher priority', 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,
});

expect(fs.existsSync(path.join(__dirname, 'dist/5'))).toBeTruthy();
});

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,
});

Expand Down
9 changes: 4 additions & 5 deletions tests/integration/cli/inspect/inspect.test.ts
Original file line number Diff line number Diff line change
@@ -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,
});

Expand Down Expand Up @@ -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,
});

Expand Down Expand Up @@ -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,
});

Expand Down
9 changes: 4 additions & 5 deletions tests/integration/cli/mf/mf.test.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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,
Expand Down Expand Up @@ -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,
});

Expand Down Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions tests/integration/plugins/index.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
});

Expand Down
5 changes: 2 additions & 3 deletions tests/integration/server/index.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand All @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions tests/scripts/shared.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
}
Expand Down
Loading