Commit 9af862b

Anton Golub <antongolub@antongolub.com>
2024-09-21 09:00:14
test: improve proc hand detection (#909)
1 parent d06216d
Changed files (2)
src/core.ts
@@ -293,7 +293,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
       cwd: $.cwd ?? $[CWD],
       ac: $.ac,
       signal: $.signal,
-      shell: typeof $.shell === 'string' ? $.shell : true,
+      shell: isString($.shell) ? $.shell : true,
       env: $.env,
       spawn: $.spawn,
       spawnSync: $.spawnSync,
test/cli.test.js
@@ -13,29 +13,34 @@
 // limitations under the License.
 
 import assert from 'node:assert'
-import { test, describe, beforeEach } from 'node:test'
+import { test, describe, before, after } from 'node:test'
 import { fileURLToPath } from 'node:url'
 import '../build/globals.js'
 import { isMain } from '../build/cli.js'
 
 const __filename = fileURLToPath(import.meta.url)
-
+const spawn = $.spawn
 describe('cli', () => {
   // Helps detect unresolved ProcessPromise.
-  let promiseResolved = false
-
-  beforeEach(() => {
+  before(() => {
+    const spawned = []
+    $.spawn = (...args) => {
+      const proc = spawn(...args)
+      const done = () => (proc._done = true)
+      spawned.push(proc)
+      return proc.once('close', done).once('error', done)
+    }
     process.on('exit', () => {
-      if (!promiseResolved) {
+      if (spawned.some((p) => p._done !== true)) {
         console.error('Error: ProcessPromise never resolved.')
         process.exitCode = 1
       }
     })
   })
+  after(() => ($.spawn = spawn))
 
   test('promise resolved', async () => {
     await $`echo`
-    promiseResolved = true
   })
 
   test('prints version', async () => {