Skip to content

Commit 574c504

Browse files
authored
Merge pull request #226 from HomyeeKing/fix/test
Fix/test
2 parents 203f214 + 2d0195a commit 574c504

File tree

3 files changed

+56
-72
lines changed

3 files changed

+56
-72
lines changed

tests/cli.test.ts

Lines changed: 44 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,21 @@
1-
import { spawn } from 'node:child_process';
1+
import { fork } from 'node:child_process';
22
import chalk from 'chalk';
33
import coffee from 'coffee';
44
import open from 'open';
55
import stripAnsi from 'strip-ansi';
6-
import {
7-
afterAll,
8-
afterEach,
9-
beforeAll,
10-
beforeEach,
11-
describe,
12-
expect,
13-
it,
14-
vi,
15-
} from 'vitest';
6+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
167

178
import { unlink } from 'node:fs/promises';
9+
import path from 'node:path';
1810
import { onHome, onTest } from '../src/actions';
1911
import { NPMRC, NRMRC, REGISTRIES } from '../src/constants';
2012
import { isUnicodeSupported, readFile, writeFile } from '../src/helpers';
2113

22-
const isWin = process.platform === 'win32';
23-
2414
const shouldUseMain = isUnicodeSupported();
25-
2615
const pointer = shouldUseMain ? '❯' : '>';
2716
const radioOff = shouldUseMain ? '◯' : '( )';
2817
const radioOn = shouldUseMain ? '◉' : '(*)';
18+
const LOCAL_NRM = path.resolve(__dirname, '../dist/index.js');
2919

3020
vi.setConfig({
3121
testTimeout: 20000,
@@ -51,34 +41,15 @@ vi.mock('undici', () => {
5141

5242
vi.stubGlobal('__NRM_VERSION__', null);
5343
vi.stubGlobal('__REGISTRY__', null);
54-
beforeAll(async () => {
55-
const { stdout } = await coffee.spawn('nrm', ['-V'], { shell: isWin }).end();
56-
global.__NRM_VERSION__ = stdout ? stdout : null;
57-
await coffee.spawn('npm', ['link'], { shell: isWin }).end();
58-
});
59-
60-
afterAll(async () => {
61-
await coffee.spawn('npm', ['unlink', 'nrm', '-g'], { shell: isWin }).end();
62-
63-
if (global.__NRM_VERSION__ !== null) {
64-
await coffee
65-
.spawn('npm', [`install -g nrm@${global.__NRM_VERSION__}`], {
66-
shell: isWin,
67-
})
68-
.end();
69-
}
70-
});
7144

7245
it('nrm ls', async () => {
7346
await coffee
74-
.spawn('nrm', ['use', 'cnpm'], { shell: isWin })
47+
.fork(LOCAL_NRM, ['use', 'cnpm'])
7548
.expect('stdout', /The registry has been changed to 'cnpm'/g)
7649
.expect('code', 0)
7750
.end();
7851

79-
const { stdout, code } = await coffee
80-
.spawn('nrm', ['ls'], { shell: isWin })
81-
.end();
52+
const { stdout, code } = await coffee.fork(LOCAL_NRM, ['ls']).end();
8253

8354
const match = `${chalk.green.bold('* ')}cnpm`;
8455

@@ -88,15 +59,15 @@ it('nrm ls', async () => {
8859

8960
it('nrm use <registry>', async () => {
9061
await coffee
91-
.spawn('nrm', ['use', 'cnpm'], { shell: isWin })
62+
.fork(LOCAL_NRM, ['use', 'cnpm'])
9263
.expect('stdout', /The registry has been changed to 'cnpm'/g)
9364
.expect('code', 0)
9465
.end();
9566
});
9667

9768
it('nrm use <registry> local', async () => {
9869
await coffee
99-
.spawn('nrm', ['use', 'cnpm', 'local'], { shell: isWin })
70+
.fork(LOCAL_NRM, ['use', 'cnpm', 'local'])
10071
.expect('stdout', /The registry has been changed to 'cnpm'/g)
10172
.expect('code', 0)
10273
.end();
@@ -106,7 +77,7 @@ it('nrm use <registry> local', async () => {
10677
expect(npmrc.registry).toBe(REGISTRIES.cnpm.registry);
10778

10879
await coffee
109-
.spawn('nrm', ['current'], { shell: isWin })
80+
.fork(LOCAL_NRM, ['current'])
11081
.expect('stdout', /cnpm/g)
11182
.expect('code', 0)
11283
.end();
@@ -116,7 +87,7 @@ it('nrm use <registry> local with user config', async () => {
11687
await writeFile(NPMRC, { abc: '123' });
11788

11889
await coffee
119-
.spawn('nrm', ['use', 'cnpm', 'local'], { shell: isWin })
90+
.fork(LOCAL_NRM, ['use', 'cnpm', 'local'])
12091
.expect('stdout', /The registry has been changed to 'cnpm'/g)
12192
.expect('code', 0)
12293
.end();
@@ -127,21 +98,20 @@ it('nrm use <registry> local with user config', async () => {
12798
expect(npmrc.abc).toBe('123');
12899

129100
await coffee
130-
.spawn('nrm', ['current'], { shell: isWin })
101+
.fork(LOCAL_NRM, ['current'])
131102
.expect('stdout', /cnpm/g)
132103
.expect('code', 0)
133104
.end();
134105
});
135106

136107
it('nrm use without argument', async () => {
137-
const { stdout } = spawn('nrm', ['use'], { shell: isWin });
108+
const { stdout } = fork(LOCAL_NRM, ['use'], { stdio: 'pipe' });
138109

139110
const message = await new Promise((resolve) => {
140-
stdout.on('data', (data) => {
111+
stdout?.on('data', (data) => {
141112
resolve(stripAnsi(data.toString()).trim());
142113
});
143114
});
144-
145115
expect(
146116
message,
147117
).toBe(`? Please select the registry you want to use (Use arrow keys)
@@ -156,13 +126,13 @@ ${pointer} npm
156126

157127
it('nrm current', async () => {
158128
await coffee
159-
.spawn('nrm', ['use', 'cnpm'], { shell: isWin })
129+
.fork(LOCAL_NRM, ['use', 'cnpm'])
160130
.expect('stdout', /The registry has been changed to 'cnpm'/g)
161131
.expect('code', 0)
162132
.end();
163133

164134
await coffee
165-
.spawn('nrm', ['current'], { shell: isWin })
135+
.fork(LOCAL_NRM, ['current'])
166136
.expect('stdout', /cnpm/g)
167137
.expect('code', 0)
168138
.end();
@@ -176,15 +146,15 @@ describe('nrm command which needs to add a custom registry', () => {
176146
__REGISTRY__ = customName;
177147

178148
await coffee
179-
.spawn('nrm', ['add', `${__REGISTRY__}`, `${url}`], { shell: isWin })
149+
.fork(LOCAL_NRM, ['add', `${__REGISTRY__}`, `${url}`])
180150
.expect('stdout', /success/g)
181151
.expect('code', 0)
182152
.end();
183153
});
184154

185155
afterEach(async () => {
186156
await coffee
187-
.spawn('nrm', ['del', `${__REGISTRY__}`], { shell: isWin })
157+
.fork(LOCAL_NRM, ['del', `${__REGISTRY__}`])
188158
.expect('stdout', /has been deleted successfully/g)
189159
.expect('code', 0)
190160
.end();
@@ -199,7 +169,7 @@ describe('nrm command which needs to add a custom registry', () => {
199169
);
200170

201171
await coffee
202-
.spawn('nrm', ['rename', `${customName}`, `${newName}`], { shell: isWin })
172+
.fork(LOCAL_NRM, ['rename', `${customName}`, `${newName}`])
203173
.expect('stdout', match)
204174
.expect('code', 0)
205175
.end();
@@ -210,11 +180,14 @@ describe('nrm command which needs to add a custom registry', () => {
210180
const value = 'value';
211181

212182
await coffee
213-
.spawn(
214-
'nrm',
215-
['set', `${__REGISTRY__}`, '-a', `${attr}`, '-v', `${value}`],
216-
{ shell: isWin },
217-
)
183+
.fork(LOCAL_NRM, [
184+
'set',
185+
`${__REGISTRY__}`,
186+
'-a',
187+
`${attr}`,
188+
'-v',
189+
`${value}`,
190+
])
218191
.expect('stdout', /successfully/g)
219192
.expect('code', 0)
220193
.end();
@@ -232,13 +205,13 @@ describe('nrm command which needs to add a custom registry', () => {
232205
const url = 'https://scope.example.org';
233206

234207
await coffee
235-
.spawn('nrm', ['set-scope', `${scopeName}`, `${url}`], { shell: isWin })
208+
.fork(LOCAL_NRM, ['set-scope', `${scopeName}`, `${url}`])
236209
.expect('stdout', /success/g)
237210
.expect('code', 0)
238211
.end();
239212

240213
await coffee
241-
.spawn('nrm', ['del-scope', `${scopeName}`], { shell: isWin })
214+
.fork(LOCAL_NRM, ['del-scope', `${scopeName}`])
242215
.expect('stdout', /success/g)
243216
.expect('code', 0)
244217
.end();
@@ -252,9 +225,7 @@ describe('nrm command which needs to add a custom registry', () => {
252225
);
253226

254227
await coffee
255-
.spawn('nrm', ['set-hosted-repo', `${__REGISTRY__}`, `${repo}`], {
256-
shell: isWin,
257-
})
228+
.fork(LOCAL_NRM, ['set-hosted-repo', `${__REGISTRY__}`, `${repo}`])
258229
.expect('stdout', match)
259230
.expect('code', 0)
260231
.end();
@@ -265,17 +236,20 @@ describe('nrm command which needs to add a custom registry', () => {
265236
const password = 'password';
266237

267238
await coffee
268-
.spawn(
269-
'nrm',
270-
['login', `${__REGISTRY__}`, '-u', `${username}`, '-p', `${password}`],
271-
{ shell: isWin },
272-
)
239+
.fork(LOCAL_NRM, [
240+
'login',
241+
`${__REGISTRY__}`,
242+
'-u',
243+
`${username}`,
244+
'-p',
245+
`${password}`,
246+
])
273247
.expect('stdout', /success/g)
274248
.expect('code', 0)
275249
.end();
276250

277251
await coffee
278-
.spawn('nrm', ['login', `${__REGISTRY__}`], { shell: isWin })
252+
.fork(LOCAL_NRM, ['login', `${__REGISTRY__}`])
279253
.expect(
280254
'stderr',
281255
/Authorization information in base64 format or username & password is required/g,
@@ -298,9 +272,7 @@ describe('nrm delete without argument (use keyword to select delete)', () => {
298272
beforeEach(async () => {
299273
for (const registry of registries) {
300274
await coffee
301-
.spawn('nrm', ['add', `${registry.name}`, `${registry.url}`], {
302-
shell: isWin,
303-
})
275+
.fork(LOCAL_NRM, ['add', `${registry.name}`, `${registry.url}`])
304276
.expect('stdout', /success/g)
305277
.expect('code', 0)
306278
.end();
@@ -312,10 +284,10 @@ describe('nrm delete without argument (use keyword to select delete)', () => {
312284
});
313285

314286
it('nrm delete', async () => {
315-
const { stdout } = spawn('nrm', ['del'], { shell: isWin });
287+
const { stdout } = fork(LOCAL_NRM, ['del'], { stdio: 'pipe' });
316288

317289
const message = await new Promise((resolve) => {
318-
stdout.on('data', (data) => {
290+
stdout?.on('data', (data) => {
319291
resolve(stripAnsi(data.toString()).trim());
320292
});
321293
});
@@ -330,12 +302,12 @@ describe('nrm delete without argument (use keyword to select delete)', () => {
330302
});
331303

332304
it('nrm delete (with keyword input)', async () => {
333-
const { stdout, stdin } = spawn('nrm', ['del'], { shell: isWin });
334-
stdin.write('\u001b[B');
305+
const { stdout, stdin } = fork(LOCAL_NRM, ['del'], { stdio: 'pipe' });
306+
stdin?.write('\u001b[B');
335307

336308
const message = await new Promise((resolve) => {
337309
const m: string[] = [];
338-
stdout.on('data', (data) => {
310+
stdout?.on('data', (data) => {
339311
m.push(stripAnsi(data.toString()).trim());
340312
// get the last output
341313
if (m.length === 2) {

tests/setup.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NPMRC } from '../src/constants';
2+
import { readFile, writeFile } from '../src/helpers';
3+
4+
export async function setup() {
5+
const npmrc = await readFile(NPMRC);
6+
return async function teardown() {
7+
if (npmrc) {
8+
await writeFile(NPMRC, npmrc);
9+
}
10+
};
11+
}

vitest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import { defineConfig } from 'vitest/config';
33
export default defineConfig({
44
test: {
55
environment: 'node',
6+
globalSetup: './tests/setup.ts',
67
},
78
});

0 commit comments

Comments
 (0)