Javascript jest-将测试分离到另一个文件中
是否可以将jest的测试函数分离到另一个文件中,或者它们必须位于执行文件中 例如: 测试文件.jsJavascript jest-将测试分离到另一个文件中,javascript,node.js,jestjs,Javascript,Node.js,Jestjs,是否可以将jest的测试函数分离到另一个文件中,或者它们必须位于执行文件中 例如: 测试文件.js function tests (input) { it( "should pass", function () { expect(input).toBeTrue() } ) // more test functions } module.exports = tests const te
function tests (input)
{
it(
"should pass",
function ()
{
expect(input).toBeTrue()
}
)
// more test functions
}
module.exports = tests
const tests = require("./test_file.js")
describe(
"test block 1",
function ()
{
let input
beforeAll( () => input = true )
tests(input)
}
)
main.js
function tests (input)
{
it(
"should pass",
function ()
{
expect(input).toBeTrue()
}
)
// more test functions
}
module.exports = tests
const tests = require("./test_file.js")
describe(
"test block 1",
function ()
{
let input
beforeAll( () => input = true )
tests(input)
}
)
现在,
input
始终是未定义的让我们添加一些日志并查看此代码的执行顺序
main.test.js
:
const tests=require('./test_file.js');
控制台日志('000');
描述('测试块1',功能(){
让输入;
控制台日志('111');
以前(()=>{
console.log('222');
输入=真;
});
测试(输入);
});
test_file.js
:
console.log('333');
功能测试(输入){
控制台日志('444');
它('should pass',function(){
控制台日志('555');
expect(input.toBeTruthy();
});
}
module.exports=测试;
测试结果:
src/stackoverflow/59520741/main.test.js失败(9.747s)
试块1
✕ 应通过(7毫秒)
● 试块1›应通过测试
expect(已收到)。toBeTruthy()
收到:未定义
4 |它('should pass',function(){
5 |控制台日志('555');
>6 | expect(input).toBeTruthy();
| ^
7 | });
8 | }
9 | module.exports=测试;
反对。(src/stackoverflow/59520741/test_file.js:6:19)
console.log src/stackoverflow/59520741/test_file.js:1
333
console.log src/stackoverflow/59520741/main.test.js:2
000
console.log src/stackoverflow/59520741/main.test.js:5
111
console.log src/stackoverflow/59520741/test_file.js:3
444
console.log src/stackoverflow/59520741/main.test.js:7
222
console.log src/stackoverflow/59520741/test_file.js:5
555
测试套件:1个失败,共1个
测试:1次失败,共1次
快照:共0个
时间:11.015s
现在,我们知道了。tests
函数将在jest的测试运行程序收集测试用例之前执行。这意味着您的tests
函数只声明了测试用例('111'和'444'短语)。此时,beforeAll
hook尚未执行。输入
变量的值仍然是未定义的
,并传递到测试
函数中
执行
测试
函数后,将声明测试用例。测试运行者将收集这些测试用例,并以正常方式运行它们。beforeAll
hook将首先执行('222'短语) 让我们添加一些日志,并查看此代码的执行顺序
main.test.js
:
const tests=require('./test_file.js');
控制台日志('000');
描述('测试块1',功能(){
让输入;
控制台日志('111');
以前(()=>{
console.log('222');
输入=真;
});
测试(输入);
});
test_file.js
:
console.log('333');
功能测试(输入){
控制台日志('444');
它('should pass',function(){
控制台日志('555');
expect(input.toBeTruthy();
});
}
module.exports=测试;
测试结果:
src/stackoverflow/59520741/main.test.js失败(9.747s)
试块1
✕ 应通过(7毫秒)
● 试块1›应通过测试
expect(已收到)。toBeTruthy()
收到:未定义
4 |它('should pass',function(){
5 |控制台日志('555');
>6 | expect(input).toBeTruthy();
| ^
7 | });
8 | }
9 | module.exports=测试;
反对。(src/stackoverflow/59520741/test_file.js:6:19)
console.log src/stackoverflow/59520741/test_file.js:1
333
console.log src/stackoverflow/59520741/main.test.js:2
000
console.log src/stackoverflow/59520741/main.test.js:5
111
console.log src/stackoverflow/59520741/test_file.js:3
444
console.log src/stackoverflow/59520741/main.test.js:7
222
console.log src/stackoverflow/59520741/test_file.js:5
555
测试套件:1个失败,共1个
测试:1次失败,共1次
快照:共0个
时间:11.015s
现在,我们知道了。tests
函数将在jest的测试运行程序收集测试用例之前执行。这意味着您的tests
函数只声明了测试用例('111'和'444'短语)。此时,beforeAll
hook尚未执行。输入
变量的值仍然是未定义的
,并传递到测试
函数中
执行
测试
函数后,将声明测试用例。测试运行者将收集这些测试用例,并以正常方式运行它们。beforeAll
hook将首先执行('222'短语) 它可以像你写的那样完成。@felixmosh它似乎没有正确接收输入。我总是没有定义。它可以像你写的那样完成。@felixmosh它似乎没有正确接收输入。我总是没有定义。