Node.js AssertionError[ERR_ASSERTION]:子进程配置错误

Node.js AssertionError[ERR_ASSERTION]:子进程配置错误,node.js,process,child-process,assertion,Node.js,Process,Child Process,Assertion,我有一个test.js文件,它接受一个child\u进程并检查环境变量MY\u ENV\u VAR,该变量在我的index.js文件练习函数中设置。当我运行node test.js时,测试失败,错误为AssertionError[ERR_ASSERTION]:子进程配置错误。我试过调试,但没有得出任何可靠的结论。我认为子进程没有收到正确的输出。下面是此练习所需的文件:其他信息 我在spawn方法中添加了其他命令,这些命令反映在index.js文件中。这会导致另一个断言错误,因为my enviro

我有一个
test.js
文件,它接受一个child\u进程并检查环境变量MY\u ENV\u VAR,该变量在我的
index.js
文件练习函数中设置。当我运行
node test.js
时,测试失败,错误为AssertionError[ERR_ASSERTION]:子进程配置错误。我试过调试,但没有得出任何可靠的结论。我认为子进程没有收到正确的输出。下面是此练习所需的文件:
其他信息 我在spawn方法中添加了其他命令,这些命令反映在
index.js
文件中。这会导致另一个断言错误,因为my environment变量的长度不等于
child.js
文件中的断言.striefqual指定的长度。子进程正在接收系统环境变量,而不是与此进程相关的环境变量。因此,如果我可以限制环境变量的范围,那么程序将按预期工作

index.js child.js test.js
欢迎提供所有帮助,谢谢

我已更新了
index.js
中的函数,代码将按参与方式执行。正确的解决方案如下:

index.js
'use strict'
const { spawn } = require('child_process')

function exercise (myEnvVar) {
  // TODO return a child process with
  // a single environment variable set 
  // named MY_ENV_VAR. The MY_ENV_VAR 
  // environment variable's value should 
  // be the value of the myEnvVar parameter 
  // passed to this exercise function
  process.env.MY_ENV_VAR = myEnvVar
  return spawn(process.execPath, 
['child.js', '-e','process.env',
 '-e', 'process.stdout.pipe(process.stdout)', '-e', 'process.exit(0)']);
}
module.exports = exercise
'use strict'
const assert = require('assert')
const clean = (env) => Object.fromEntries(
  Object.entries(env).filter(([k]) => !/^(_.*|pwd|shlvl)/i.test(k))
)
const env = clean(process.env)

assert.strictEqual(env.MY_ENV_VAR, 'is set')
assert.strictEqual(
  Object.keys(env).length,
  1,
  'child process should have only one env var'
)
console.log('passed!')
'use strict'
const assert = require('assert')
const { equal } = assert.strict
const exercise = require('.')

let sp = null
try {
  sp = exercise('is set')
  assert(sp, 'exercise function should return a child process instance')
  if (Buffer.isBuffer(sp)) {
    equal(sp.toString().trim(), 'passed!', 'child process misconfigured')
    process.stdout.write(sp)
    return
  }
} catch (err) { 
  const { status} = err
  if (status == null) throw err
  equal(status, 0, 'exit code should be 0')
  return
}

if (!sp.on) {
  const { stdout, stderr } = sp
  if (stderr.length > 0) process.stderr.write(stderr)
  if (stdout.length > 0) process.stdout.write(stdout)
  equal(sp.status, 0, 'exit code should be 0')
  equal(stdout.toString().trim(), 'passed!', 'child process misconfigured')
  return
}

let out = ''
if (sp.stderr) sp.stderr.pipe(process.stderr)
if (sp.stdout) {
  sp.stdout.once('data', (data) => { out = data })
  sp.stdout.pipe(process.stdout)
} else {
  // stdio may be misconfigured, or fork method may be used,
  // allow benefit of the doubt since in either case
  // exit code check will still fail:
  out = 'passed!'
}
const timeout = setTimeout(() => {
  equal(out.toString().trim(), 'passed!', 'child process misconfigured')
}, 1000)

sp.once('exit', (status) => {
  equal(status, 0, 'exit code should be 0')
  equal(out.toString().trim(), 'passed!', 'child process misconfigured')
  clearTimeout(timeout)
})

'use strict'
const { spawn } = require('child_process')

function exercise (myEnvVar) {
  // TODO return a child process with
  // a single environment variable set 
  // named MY_ENV_VAR. The MY_ENV_VAR 
  // environment variable's value should 
  // be the value of the myEnvVar parameter 
  // passed to this exercise function
  return spawn(process.execPath, ['child.js'], {
    env: {MY_ENV_VAR: myEnvVar}
  });
}
module.exports = exercise