断言错误[ERR_ASSERTION]:子进程配置错误



我有一个test.js文件,该文件接收子进程并检查环境变量MY_ENV_VAR,该变量在我的index.js文件练习函数中设置。当我运行node test.js时,测试失败,并出现错误:AssertionError[ERR_ASSERTION]:子进程配置错误。我试过调试,但没有任何结果。我认为子进程没有接收到正确的输出。以下是本练习所需的文件:

其他信息

我为派生方法添加了其他命令,这些命令反映在下面的index.js文件中。这现在会导致另一个断言错误,因为我的环境变量的长度不等于child.js文件中断言.strictEqual指定的长度。子进程正在接收系统环境变量,而不是相对于该进程的环境变量。因此,如果我可以限制环境变量的范围,那么程序将按预期工作。

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

child.js

'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!')

test.js

'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)
})

欢迎所有帮助,并感谢

我已经更新了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
return spawn(process.execPath, ['child.js'], {
env: {MY_ENV_VAR: myEnvVar}
});
}
module.exports = exercise

我相信我和你在做同一个项目。请注意,从Node.js版本16起,我们无法在Windows上使用以下代码清理子进程的环境变量。(不过它在Linux上运行良好(

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}
});
}

相关内容

  • 没有找到相关文章

最新更新