如何从Jest测试运行脚本



如何从测试中运行shell脚本(jest(?我试过这个:

var cp = require("child_process");
test("script", async () => {
const script = cp.exec("./scripts/foo.sh");
script.stdout.on("data", (data) => {
console.log("stdout: " + data);
});
script.stderr.on("data", (data) => {
console.error("stderr: " + data);
});
});

其中foo.sh:

#!/usr/bin/env bash
echo "fooooo"

这就给出了:

测试完成后无法登录。你忘了在测试中等待异步吗?

试图记录";stdout:fooooo">

此外,如果我删除它,它仍然不会显示console.log输出

有什么想法吗?

编辑:";真实的";脚本更复杂:

#!/usr/bin/env bash
echo "start script"
sandbox reset release -v

要使sandbox命令可用,请参阅:https://github.com/algorand/sandbox#getting-启动

目前,我通过在脚本完成所需的时间内安全地设置超时,并在显示预期的最后一个日志时调用done(),成功地使其工作起来:

test("script", (done) => {
//   const script = cp.exec("./scripts/foo.sh");
const script = cp.exec("./scripts/sandbox_dev_reset.sh");
script.stdout.on("data", (data) => {
console.log("stdout: " + data);
// this is a string that appears in the last log message
// don't know how to make the test stop (successfully) otherwise
if (
data.includes(
`Soon after sending the transaction it will appear in indexer`
)
) {
console.log("found the string! - done");
done();
}
});
script.stderr.on("data", (data) => {
console.error("stderr: " + data);
});
}, 100000);

附言:如果有任何帮助的话,最初这来自Rust测试,脚本正常完成(程序继续执行,没有黑客攻击(:


pub fn reset_network(net: &Network) -> Result<()> {
let mut cmd = Command::new("sh");
cmd
.current_dir(format!("/foo/bar/scripts"))
.arg("./sandbox_dev_reset.sh"),
let reset_res = cmd
.stdout(Stdio::piped())
.spawn()?
.stdout
.expect("Couldn't reset network");
log::debug!("Script cmd stdout: {:?}", reset_res);
for line in BufReader::new(reset_res)
.lines()
.filter_map(|line| line.ok())
{
log::debug!("{}", line);
}
log::debug!("Script finished");
Ok(())
}

请参阅测试异步代码:

在JavaScript中,异步运行代码是很常见的。当您有异步运行的代码时,Jest需要知道它正在测试的代码何时完成,然后才能进行另一个测试。Jest有几种方法来处理这个问题。

您可以使用回调。当派生的shell中的脚本被执行时,调用done()来通知jest测试用例已经完成用于测试回调,否则测试用例不会等待回调完成这意味着执行了测试用例,但没有执行回调。这与test()函数的timeout毫秒选项不同。

const cp = require('child_process');
const path = require('path');
test('script', (done) => {
const script = cp.exec(path.resolve(__dirname, './foo.sh'));
script.stdout.on('data', (data) => {
console.log('stdout: ' + data);
done();
});
script.stderr.on('data', (data) => {
console.error('stderr: ' + data);
done();
});
});

测试结果:

PASS  stackoverflow/74025882/index.test.js
✓ script (31 ms)
console.log
stdout: fooooo
at Socket.<anonymous> (stackoverflow/74025882/index.test.js:7:13)
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.175 s, estimated 10 s

更新:

测试的默认超时为5000毫秒。如果脚本执行时间大于此值,Jest将抛出超时错误。这就是它的工作方式。超时机制将中止那些执行时间过长的测试用例。

test()函数使用timeout选项是正确的方法。参见示例:

const cp = require('child_process');
const path = require('path');
describe('74025882', () => {
test('script', (done) => {
const script = cp.exec(path.resolve(__dirname, './foo.sh'));
script.stdout.on('data', (data) => {
console.log('stdout: ' + data);
done();
});
script.stderr.on('data', (data) => {
console.error('stderr: ' + data);
done();
});
}, 10 * 1_000);
});

foo.sh:

#!/usr/bin/env bash
sleep 6
echo "fooooo"

我使用sleep 6来模拟Rust脚本的执行时间。

相关内容

最新更新