Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js TestCafe:将测试从另一个文件导入当前设备_Node.js_Automated Tests_Integration Testing_E2e Testing_Testcafe - Fatal编程技术网

Node.js TestCafe:将测试从另一个文件导入当前设备

Node.js TestCafe:将测试从另一个文件导入当前设备,node.js,automated-tests,integration-testing,e2e-testing,testcafe,Node.js,Automated Tests,Integration Testing,E2e Testing,Testcafe,我有一个文件tests.js,其中包含一些test(…)定义。我希望跨多个fixture重用这些测试,最好不修改原始代码 因此,我编写了一个main.js,它定义了一个fixture并导入tests.js,从而“组装”了一个测试套件。(如果可以的话,我可以用不同的装置编写不同的驱动程序文件,从每个装置中导入相同的tests.js) 但是,我在尝试执行main.js时遇到了一个测试未定义的错误: C:\Windows\Temp\dummy>testcafe chrome main.js --

我有一个文件
tests.js
,其中包含一些
test(…)
定义。我希望跨多个fixture重用这些测试,最好不修改原始代码

因此,我编写了一个
main.js
,它定义了一个fixture并导入
tests.js
,从而“组装”了一个测试套件。(如果可以的话,我可以用不同的装置编写不同的驱动程序文件,从每个装置中导入相同的
tests.js

但是,我在尝试执行
main.js
时遇到了一个
测试未定义的错误:

C:\Windows\Temp\dummy>testcafe chrome main.js --debug-on-fail
ERROR Cannot prepare tests due to an error.

ReferenceError: test is not defined
    at Object.<anonymous> (C:\Windows\Temp\dummy\tests.js:1:1)
    at Object.<anonymous> (C:\Windows\Temp\dummy\main.js:7:1)

Type "testcafe -h" for help.
我已经试过了:

  • 删除block comment hack(
    test();
    )-这使得
    错误没有测试可运行。测试文件不包含任何测试,或者筛选功能限制过大。
  • tests.js
    导入移动到顶部-仍然会给出未定义的
    测试
  • main.js
    tests.js
    中导入
    testcafe
    -相同错误

是否有办法使testcafe入口点文件导入的其他文件“可见”test
函数?或者我真的需要修改我的
tests.js
文件才能工作吗?可能通过将测试定义添加到一个方法中,并从
main.js
中调用它,就像在?

的原始代码示例中一样,尝试在TestCafe命令行上添加选项
--禁用测试语法验证


(仅适用于最新的TestCafe版本)。

TestCafe不允许调用测试范围之外的
fixture
test
函数。您可以将
tests.js
文件中的测试包装到函数中,并在
main.js
文件中调用此函数:

//tests.js
导出默认函数(){
测试('test1',()=>{});
测试('test2',()=>{});
测试('test3',()=>{});
}
//main.js
从“/tests”导入定义测试;
定义测试();

另请参见:

谢谢-不幸的是,我使用的是
0.20.0
,但希望这能帮助其他人:)谢谢-我担心最终会归结为:(但有一件事我不明白,那就是为什么/如何导入的文件不属于“测试范围”;因为,AFAIU,
tests.js
main.js
导入并同步执行,它已经在TestCafe的“测试范围”中@Janaka,这是因为您的所有测试代码都被传输到vanilla JS。正在执行的是此传输产生的代码。谢谢@hdorgeval!这也澄清了我关于TestCafe的许多其他问题:)
// tests.js

test('wait', async t => {
    await t.wait(1);
});


// main.js

fixture `here goes the name`
    .page("http://localhost:3000")
    .beforeEach(async t => {
        // do stuff
    });

import "./tests";

/*
trick testcafe to scan the file;
based on https://github.com/DevExpress/testcafe/issues/2889#issuecomment-423859785

test();
*/