node.js - TestCafe:将另一个文件中的测试导入当前的夹具
问题描述
我有一个tests.js
包含一些test(...)
定义的文件。我想在多个夹具中重用这些测试,最好不对原始代码进行任何修改。
所以我写了一个main.js
定义一个夹具和导入tests.js
,从而“组装”一个测试套件。(如果可行,我可以使用不同的设备编写不同的驱动程序文件,tests.js
从每个文件中导入相同的驱动程序文件。)
但是,test is not defined
尝试执行时出现错误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.
最小样本:
// 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();
*/
我已经尝试过:
- 删除块注释 hack (
test();
) - 它给出ERROR No tests to run. Either the test files contain no tests or the filter function is too restrictive.
- 将
tests.js
导入移到顶部 - 仍然给出test is not defined
testcafe
从内部导入main.js
-tests.js
相同的错误
有没有办法让test
testcafe 入口点文件导入的其他文件“可见”该功能?或者我真的需要修改我的tests.js
文件才能让它工作吗?也许通过将测试定义添加到方法中,并从内部调用它- 就像在这个问题main.js
的原始代码示例中一样?
解决方案
TestCafe 不允许在测试范围之外调用fixture
和函数。test
您可以将tests.js
文件中的测试包装在一个函数中,并在文件中调用此函数main.js
:
// tests.js
export default function () {
test('Test 1', () => {});
test('Test 2', () => {});
test('Test 3', () => {});
}
// main.js
import defineTests from './tests';
defineTests();
另请参阅:组织测试
推荐阅读
- google-cast - 如何隐藏或更改下一个队列项目的倒计时外观?
- electron - 无法在 Windows 10 上使用 ffi 运行电子重建 (src\ffi.cc(228): error C2065: 'Handle': undeclared identifier)
- html - 根据组件值在角度组件之间画线的最佳做法是什么?
- excel - 如何在 VBA 中将 If 语句变成 For 循环
- python - 如何使用python从下拉列表中提取数据
- r - 比较数据帧中的单词并计算每个对的最大单词长度的矩阵
- regex - Ansible 正则表达式匹配字符串并提取行
- java - JDK 9.0.4 和 Derby 10.14.2 找不到创建的表
- angular - i18n 关于 Angular 7 中具有复数/选择的属性
- tfs - 如何启动由 TFS Release Agent 执行后不关闭的控制台程序