我有一个 tests.js
文件,其中包含一些 test(...)
定义。我想在多个装置中重复使用这些测试,最好不要对原始代码进行任何修改。
所以我写了一个 main.js
定义了一个夹具并导入了 tests.js
,从而“组装”了一个测试套件。 (如果可行,我可以用不同的装置编写不同的驱动程序文件,从每个文件中导入相同的 tests.js
。)
但是,我在尝试执行 main.js
时遇到了一个test is not defined
错误:
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();
*/
我已经试过了:
- 删除 block 注释 hack (
test();
) - 这会导致ERROR No tests to run。要么是测试文件不包含测试,要么是过滤函数限制太多。
- 将
tests.js
导入移动到顶部 - 仍然给出test is not defined
- 从
main.js
和tests.js
中导入testcafe
- 同样的错误
有没有办法让 test
函数对 testcafe 入口点文件导入的其他文件“可见”?或者我真的需要修改我的 tests.js
文件才能让它工作吗?也许通过将测试定义添加到一个方法中,并从 main.js
中调用它——就像在 this issue 的原始代码示例中一样?
最佳答案
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();
另请参阅:Organize Tests
关于node.js - TestCafe:将测试从另一个文件导入到当前夹具中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55389523/