Skip to content

Commit acd695a

Browse files
committed
Use assert.rejects
1 parent 270683f commit acd695a

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

test/socket-cdxgen.test.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11
import assert from 'node:assert/strict'
2-
import { spawnSync } from 'node:child_process'
32
import { describe, it } from 'node:test'
43

4+
import spawn from '@npmcli/promise-spawn'
5+
56
import { distPath } from './dist/constants'
67

7-
import type { SpawnSyncOptionsWithStringEncoding } from 'node:child_process'
8+
type PromiseSpawnOptions = Exclude<Parameters<typeof spawn>[2], undefined> & {
9+
encoding?: BufferEncoding | undefined
10+
}
811

9-
const spawnOpts: SpawnSyncOptionsWithStringEncoding = {
12+
const spawnOpts: PromiseSpawnOptions = {
1013
cwd: distPath,
1114
encoding: 'utf8'
1215
}
1316

1417
describe('Socket cdxgen command', async () => {
1518
it('should forwards known commands to cdxgen', async () => {
1619
for (const command of ['-h', '--help']) {
17-
const ret = spawnSync('./cli.js', ['cdxgen', command], spawnOpts)
18-
assert(ret.stdout.startsWith('cdxgen'), 'forwards commands to cdxgen')
20+
// eslint-disable-next-line no-await-in-loop
21+
const ret = await spawn('./cli.js', ['cdxgen', command], spawnOpts)
22+
assert.ok(ret.stdout.startsWith('cdxgen'), 'forwards commands to cdxgen')
1923
}
2024
})
2125
it('should not forward unknown commands to cdxgen', async () => {
2226
for (const command of ['-u', '--unknown']) {
23-
const ret = spawnSync('./cli.js', ['cdxgen', command], spawnOpts)
24-
assert(ret.stderr.startsWith(`Unknown argument: ${command}`), 'singular')
27+
// eslint-disable-next-line no-await-in-loop
28+
await assert.rejects(
29+
() => spawn('./cli.js', ['cdxgen', command], spawnOpts),
30+
e => e?.['stderr']?.startsWith(`Unknown argument: ${command}`),
31+
'singular'
32+
)
2533
}
26-
const ret = spawnSync(
27-
'./cli.js',
28-
['cdxgen', '-u', '-h', '--unknown'],
29-
spawnOpts
34+
await assert.rejects(
35+
() => spawn('./cli.js', ['cdxgen', '-u', '-h', '--unknown'], spawnOpts),
36+
e => e?.['stderr']?.startsWith('Unknown arguments: -u, --unknown'),
37+
'plural'
3038
)
31-
assert(ret.stderr.startsWith('Unknown arguments: -u, --unknown'), 'plural')
3239
})
3340
})

test/socket-npm.test.cjs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,19 @@ for (const npm of ['npm8', 'npm10']) {
3030

3131
describe(`Socket npm wrapper for ${npm}`, () => {
3232
it('should bail on new typosquat', async () => {
33-
try {
34-
await spawn(
35-
process.execPath,
36-
[entryPath, 'npm', 'install', 'bowserify'],
37-
{
33+
await assert.rejects(
34+
() =>
35+
spawn(process.execPath, [entryPath, 'npm', 'install', 'bowserify'], {
3836
cwd: path.join(npmFixturesPath, 'lacking-typosquat'),
3937
encoding: 'utf8',
4038
env: {
4139
// Make sure we don't borrow TTY from parent.
4240
SOCKET_SECURITY_TTY_IPC: undefined,
4341
PATH: `${npmBinPath}:${process.env.PATH}`
4442
}
45-
}
46-
)
47-
assert.ok(false, 'typosquat not error')
48-
} catch (e) {
49-
assert.ok(e?.stderr.includes('Unable to prompt'), e?.stderr)
50-
}
43+
}),
44+
e => e?.stderr.includes('Unable to prompt')
45+
)
5146
})
5247
})
5348
}

0 commit comments

Comments
 (0)