Skip to content

Commit

Permalink
Fix spawn in doctor tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Mar 13, 2024
1 parent 2da324b commit 1bcd1ef
Showing 1 changed file with 86 additions and 61 deletions.
147 changes: 86 additions & 61 deletions test/doctor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ const mockNpmVersions = {
}

/** Run the ncu CLI. */
const ncu = async (args: string[], options?: Record<string, unknown>) => {
const { stdout } = await spawn('node', [bin, ...args], options)
const ncu = async (
args: string[],
spawnPleaseOptions?: Parameters<typeof spawn>[2],
spawnOptions?: Parameters<typeof spawn>[3],
) => {
const { stdout } = await spawn('node', [bin, ...args], spawnPleaseOptions, spawnOptions)
return stdout
}

Expand Down Expand Up @@ -56,15 +60,18 @@ const testPass = ({ packageManager }: { packageManager: PackageManagerName }) =>

try {
// explicitly set packageManager to avoid auto yarn detection
await ncu(['--doctor', '-u', '-p', packageManager], {
cwd,
stdout: function (data: string) {
stdout += data
},
stderr: function (data: string) {
stderr += data
await ncu(
['--doctor', '-u', '-p', packageManager],
{
stdout: function (data: string) {
stdout += data
},
stderr: function (data: string) {
stderr += data
},
},
})
{ cwd },
)
} catch (e) {}

const pkgUpgraded = await fs.readFile(pkgPath, 'utf-8')
Expand Down Expand Up @@ -124,15 +131,18 @@ const testFail = ({ packageManager }: { packageManager: PackageManagerName }) =>

try {
// explicitly set packageManager to avoid auto yarn detection
await ncu(['--doctor', '-u', '-p', packageManager], {
cwd,
stdout: function (data: string) {
stdout += data
},
stderr: function (data: string) {
stderr += data
await ncu(
['--doctor', '-u', '-p', packageManager],
{
stdout: function (data: string) {
stdout += data
},
stderr: function (data: string) {
stderr += data
},
},
})
{ cwd },
)
} finally {
pkgUpgraded = await fs.readFile(pkgPath, 'utf-8')
await fs.writeFile(pkgPath, pkgOriginal)
Expand Down Expand Up @@ -177,7 +187,7 @@ describe('doctor', function () {
await chalkInit()
const { default: stripAnsi } = await import('strip-ansi')
const cwd = path.join(doctorTests, 'nopackagefile')
const output = await ncu(['--doctor'], { cwd })
const output = await ncu(['--doctor'], {}, { cwd })
return stripAnsi(output).should.equal(
`Usage: ncu --doctor\n\n${stripAnsi(
(cliOptionsMap.doctor.help as (options: { markdown: boolean }) => string)({ markdown: false }),
Expand All @@ -187,12 +197,12 @@ describe('doctor', function () {

it('throw an error if there is no package file', async () => {
const cwd = path.join(doctorTests, 'nopackagefile')
return ncu(['--doctor', '-u'], { cwd }).should.eventually.be.rejectedWith('Missing or invalid package.json')
return ncu(['--doctor', '-u'], {}, { cwd }).should.eventually.be.rejectedWith('Missing or invalid package.json')
})

it('throw an error if there is no test script', async () => {
const cwd = path.join(doctorTests, 'notestscript')
return ncu(['--doctor', '-u'], { cwd }).should.eventually.be.rejectedWith('No npm "test" script')
return ncu(['--doctor', '-u'], {}, { cwd }).should.eventually.be.rejectedWith('No npm "test" script')
})

it('throw an error if --packageData or --packageFile are supplied', async () => {
Expand Down Expand Up @@ -222,15 +232,18 @@ describe('doctor', function () {

try {
// check only ncu-test-v2 (excluding ncu-return-version)
await ncu(['--doctor', '-u', '--filter', 'ncu-test-v2'], {
cwd,
stdout: function (data: string) {
stdout += data
await ncu(
['--doctor', '-u', '--filter', 'ncu-test-v2'],
{
stdout: function (data: string) {
stdout += data
},
stderr: function (data: string) {
stderr += data
},
},
stderr: function (data: string) {
stderr += data
},
})
{ cwd },
)
} catch (e) {}

const pkgUpgraded = await fs.readFile(pkgPath, 'utf-8')
Expand Down Expand Up @@ -264,15 +277,18 @@ describe('doctor', function () {
let stderr = ''

try {
await ncu(['--doctor', '-u', '--doctorInstall', npmCmd + ' run myinstall'], {
cwd,
stdout: function (data: string) {
stdout += data
},
stderr: function (data: string) {
stderr += data
await ncu(
['--doctor', '-u', '--doctorInstall', npmCmd + ' run myinstall'],
{
stdout: function (data: string) {
stdout += data
},
stderr: function (data: string) {
stderr += data
},
},
})
{ cwd },
)
} catch (e) {}

const pkgUpgraded = await fs.readFile(pkgPath, 'utf-8')
Expand Down Expand Up @@ -305,15 +321,18 @@ describe('doctor', function () {
let stderr = ''

try {
await ncu(['--doctor', '-u', '--doctorTest', npmCmd + ' run mytest'], {
cwd,
stdout: function (data: string) {
stdout += data
},
stderr: function (data: string) {
stderr += data
await ncu(
['--doctor', '-u', '--doctorTest', npmCmd + ' run mytest'],
{
stdout: function (data: string) {
stdout += data
},
stderr: function (data: string) {
stderr += data
},
},
})
{ cwd },
)
} catch (e) {}

const pkgUpgraded = await fs.readFile(pkgPath, 'utf-8')
Expand Down Expand Up @@ -346,15 +365,18 @@ describe('doctor', function () {
let stderr = ''

try {
await ncu(['--doctor', '-u', '--doctorTest', `node ${echoPath} '123 456'`], {
cwd,
stdout: function (data: string) {
stdout += data
},
stderr: function (data: string) {
stderr += data
await ncu(
['--doctor', '-u', '--doctorTest', `node ${echoPath} '123 456'`],
{
stdout: function (data: string) {
stdout += data
},
stderr: function (data: string) {
stderr += data
},
},
})
{ cwd },
)
} catch (e) {}

const pkgUpgraded = await fs.readFile(pkgPath, 'utf-8')
Expand Down Expand Up @@ -421,15 +443,18 @@ else {
// explicitly set packageManager to avoid auto yarn detection
await spawn('npm', ['install'], {}, { cwd: tempDir })

await ncu(['--doctor', '-u', '-p', 'npm'], {
cwd: tempDir,
stdout: function (data: string) {
stdout += data
},
stderr: function (data: string) {
stderr += data
await ncu(
['--doctor', '-u', '-p', 'npm'],
{
stdout: function (data: string) {
stdout += data
},
stderr: function (data: string) {
stderr += data
},
},
})
{ cwd: tempDir },
)

pkgUpgraded = JSON.parse(await fs.readFile(pkgPath, 'utf-8'))
} finally {
Expand Down

0 comments on commit 1bcd1ef

Please sign in to comment.