Skip to content

Commit f727b18

Browse files
authored
test: replace npx with a faster helper (#1154)
1 parent 0f1a606 commit f727b18

File tree

8 files changed

+57
-39
lines changed

8 files changed

+57
-39
lines changed

tests/integration/cli/build-watch/build.test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
import { exec, spawn } from 'node:child_process';
1+
import { spawn } from 'node:child_process';
22
import path from 'node:path';
33
import { after } from 'node:test';
44
import { describe, expect, test } from '@rstest/core';
55
import fse from 'fs-extra';
6-
import { awaitFileChanges, awaitFileExists } from 'test-helper';
6+
import {
7+
awaitFileChanges,
8+
awaitFileExists,
9+
rslibBinPath,
10+
runCli,
11+
} from 'test-helper';
712

813
describe('build --watch command', async () => {
914
test('basic', async () => {
@@ -26,7 +31,7 @@ export default defineConfig({
2631
`,
2732
);
2833

29-
const process = exec(`npx rslib build --watch -c ${tempConfigFile}`, {
34+
const process = runCli(`build --watch -c ${tempConfigFile}`, {
3035
cwd: __dirname,
3136
});
3237

@@ -98,8 +103,8 @@ export default defineConfig({
98103
const distFoo2File = path.join(__dirname, 'dist/esm/foo2.js');
99104

100105
const child = spawn(
101-
'npx',
102-
['rslib', 'build', '--watch', '-c', tempConfigFile],
106+
'node',
107+
[rslibBinPath, 'build', '--watch', '-c', tempConfigFile],
103108
{
104109
cwd: __dirname,
105110
stdio: 'inherit',

tests/integration/cli/build/build.test.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { execSync } from 'node:child_process';
21
import path from 'node:path';
32
import { describe, expect, test } from '@rstest/core';
43
import fse from 'fs-extra';
5-
import { buildAndGetResults, globContentJSON } from 'test-helper';
4+
import { buildAndGetResults, globContentJSON, runCliSync } from 'test-helper';
65

76
describe('build command', async () => {
87
test('basic', async () => {
98
await fse.remove(path.join(__dirname, 'dist'));
10-
execSync('npx rslib build', {
9+
runCliSync('build', {
1110
cwd: __dirname,
1211
});
1312

@@ -23,7 +22,7 @@ describe('build command', async () => {
2322

2423
test('--lib', async () => {
2524
await fse.remove(path.join(__dirname, 'dist'));
26-
execSync('npx rslib build --lib esm', {
25+
runCliSync('build --lib esm', {
2726
cwd: __dirname,
2827
});
2928

@@ -38,7 +37,7 @@ describe('build command', async () => {
3837

3938
test('--lib multiple', async () => {
4039
await fse.remove(path.join(__dirname, 'dist'));
41-
execSync('npx rslib build --lib esm --lib cjs', {
40+
runCliSync('build --lib esm --lib cjs', {
4241
cwd: __dirname,
4342
});
4443

@@ -69,12 +68,9 @@ describe('build command', async () => {
6968

7069
test('--config', async () => {
7170
await fse.remove(path.join(__dirname, 'dist'));
72-
execSync(
73-
'npx rslib build --config ./custom-config/rslib.config.custom.ts',
74-
{
75-
cwd: __dirname,
76-
},
77-
);
71+
runCliSync('build --config ./custom-config/rslib.config.custom.ts', {
72+
cwd: __dirname,
73+
});
7874

7975
const files = await globContentJSON(path.join(__dirname, 'dist'));
8076
const fileNames = Object.keys(files).sort();
@@ -88,7 +84,7 @@ describe('build command', async () => {
8884

8985
test('--root', async () => {
9086
await fse.remove(path.join(__dirname, 'dist'));
91-
execSync('npx rslib build --root custom-root', {
87+
runCliSync('build --root custom-root', {
9288
cwd: __dirname,
9389
});
9490

tests/integration/cli/env/env.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { execSync } from 'node:child_process';
21
import fs from 'node:fs';
32
import path from 'node:path';
43
import { beforeEach, describe, expect, test } from '@rstest/core';
4+
import { runCliSync } from 'test-helper';
55

66
const localFile = path.join(__dirname, '.env.local');
77
const prodLocalFile = path.join(__dirname, '.env.production.local');
@@ -13,7 +13,7 @@ describe('load env file', async () => {
1313
});
1414

1515
test('should load .env config and allow rslib.config.ts to read env vars', async () => {
16-
execSync('npx rslib build', {
16+
runCliSync('build', {
1717
cwd: __dirname,
1818
});
1919

@@ -23,32 +23,34 @@ describe('load env file', async () => {
2323
test('should load .env.local with higher priority', async () => {
2424
fs.writeFileSync(localFile, 'FOO=2');
2525

26-
execSync('npx rslib build', {
26+
runCliSync('build', {
2727
cwd: __dirname,
2828
});
29+
2930
expect(fs.existsSync(path.join(__dirname, 'dist/2'))).toBeTruthy();
3031
});
3132

3233
test('should load .env.production.local with higher priority', async () => {
3334
fs.writeFileSync(localFile, 'FOO=2');
3435
fs.writeFileSync(prodLocalFile, 'FOO=3');
3536

36-
execSync('npx rslib build', {
37+
runCliSync('build', {
3738
cwd: __dirname,
3839
});
40+
3941
expect(fs.existsSync(path.join(__dirname, 'dist/3'))).toBeTruthy();
4042
});
4143

4244
test('should allow to specify env mode via --env-mode', async () => {
43-
execSync('npx rslib build --env-mode test', {
45+
runCliSync('build --env-mode test', {
4446
cwd: __dirname,
4547
});
4648

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

5052
test('should allow to custom env directory via --env-dir', async () => {
51-
execSync('npx rslib build --env-dir env', {
53+
runCliSync('build --env-dir env', {
5254
cwd: __dirname,
5355
});
5456

tests/integration/cli/inspect/inspect.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import { execSync } from 'node:child_process';
21
import path from 'node:path';
32
import { describe } from 'node:test';
43
import { expect, test } from '@rstest/core';
54
import fse from 'fs-extra';
6-
import { globContentJSON } from 'test-helper';
5+
import { globContentJSON, runCliSync } from 'test-helper';
76

87
describe('inspect command', async () => {
98
test('basic', async () => {
109
await fse.remove(path.join(__dirname, 'dist'));
11-
execSync('npx rslib inspect', {
10+
runCliSync('inspect', {
1211
cwd: __dirname,
1312
});
1413

@@ -41,7 +40,7 @@ describe('inspect command', async () => {
4140

4241
test('--lib', async () => {
4342
await fse.remove(path.join(__dirname, 'dist'));
44-
execSync('npx rslib inspect --lib esm', {
43+
runCliSync('inspect --lib esm', {
4544
cwd: __dirname,
4645
});
4746

@@ -75,7 +74,7 @@ describe('inspect command', async () => {
7574

7675
test('--lib multiple', async () => {
7776
await fse.remove(path.join(__dirname, 'dist'));
78-
execSync('npx rslib inspect --lib esm --lib cjs', {
77+
runCliSync('inspect --lib esm --lib cjs', {
7978
cwd: __dirname,
8079
});
8180

tests/integration/cli/mf/mf.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { exec, execSync } from 'node:child_process';
21
import { join } from 'node:path';
32
import { describe } from 'node:test';
43
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
54
import { startMFDevServer } from '@rslib/core';
65
import { expect, test } from '@rstest/core';
76
import fse from 'fs-extra';
8-
import { awaitFileExists } from 'test-helper';
7+
import { awaitFileExists, runCli, runCliSync } from 'test-helper';
98

109
const { existsSync } = fse;
1110

@@ -19,7 +18,7 @@ describe('mf-dev', () => {
1918
fse.removeSync(distFolder);
2019
const distPath = join(distFolder, 'index.js');
2120

22-
const childProcess = exec('npx rslib mf-dev --lib mf0', {
21+
const childProcess = runCli('mf-dev --lib mf0', {
2322
cwd: fixturePath,
2423
env: {
2524
...process.env,
@@ -49,7 +48,7 @@ describe('mf-dev', () => {
4948
const distPath1 = join(distFolder1, 'index.js');
5049
const distPath2 = join(distFolder2, 'index.js');
5150

52-
const childProcess = exec('npx rslib mf-dev --lib mf1 --lib mf2', {
51+
const childProcess = runCli('mf-dev --lib mf1 --lib mf2', {
5352
cwd: fixturePath,
5453
});
5554

@@ -107,7 +106,7 @@ describe('mf build', () => {
107106
const distPath = join(fixturePath, 'dist/mf');
108107
const rspackConfigFile = join(distPath, '.rsbuild/rspack.config.mf.mjs');
109108

110-
execSync('npx rslib build', {
109+
runCliSync('build', {
111110
cwd: fixturePath,
112111
env: {
113112
...process.env,

tests/integration/plugins/index.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { exec } from 'node:child_process';
21
import fs from 'node:fs';
32
import { join } from 'node:path';
43
import { expect, test } from '@rstest/core';
5-
import { awaitFileExists, buildAndGetResults } from 'test-helper';
4+
import { awaitFileExists, buildAndGetResults, runCli } from 'test-helper';
65

76
import { distIndex } from './basic/rslib.config';
87
import { plugin1Path, plugin2Path } from './mf-dev/rslib.config';
@@ -17,7 +16,7 @@ test('should run shared plugins only once', async () => {
1716

1817
test('should merge plugins correctly', async () => {
1918
const fixturePath = join(__dirname, 'mf-dev');
20-
const childProcess = exec('npx rslib mf-dev', {
19+
const childProcess = runCli('mf-dev', {
2120
cwd: fixturePath,
2221
});
2322

tests/integration/server/index.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { exec } from 'node:child_process';
21
import { existsSync } from 'node:fs';
32
import { join } from 'node:path';
43
import { describe, expect, test } from '@rstest/core';
54
import fse from 'fs-extra';
6-
import { awaitFileExists, buildAndGetResults } from 'test-helper';
5+
import { awaitFileExists, buildAndGetResults, runCli } from 'test-helper';
76

87
describe('server config', async () => {
98
test('basic config', async () => {
@@ -24,7 +23,7 @@ describe('server config', async () => {
2423

2524
fse.removeSync(distPath);
2625

27-
const childProcess = exec('npx rslib mf-dev', {
26+
const childProcess = runCli('mf-dev', {
2827
cwd: fixturePath,
2928
env: {
3029
...process.env,

tests/scripts/shared.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import assert from 'node:assert';
2+
import {
3+
type ExecOptions,
4+
type ExecSyncOptions,
5+
exec,
6+
execSync,
7+
} from 'node:child_process';
28
import fs from 'node:fs';
39
import { basename, dirname, join, normalize } from 'node:path';
410
import { fileURLToPath } from 'node:url';
@@ -14,6 +20,19 @@ import { globContentJSON } from './helper';
1420
const __filename = fileURLToPath(import.meta.url);
1521
const __dirname = dirname(__filename);
1622

23+
export const rslibBinPath = join(
24+
__dirname,
25+
'../node_modules/@rslib/core/bin/rslib.js',
26+
);
27+
28+
export function runCliSync(command: string, options?: ExecSyncOptions) {
29+
return execSync(`node ${rslibBinPath} ${command}`, options);
30+
}
31+
32+
export function runCli(command: string, options?: ExecOptions) {
33+
return exec(`node ${rslibBinPath} ${command}`, options);
34+
}
35+
1736
export function getCwdByExample(exampleName: string) {
1837
return join(__dirname, '../../examples', exampleName);
1938
}

0 commit comments

Comments
 (0)