javascript - Jasmine:如果异步,循环测试将无法运行
问题描述
我正在实现一个从给定目录读取文件列表的测试。Map
然后对照使用文件名作为键的对象检查该列表。如果找到匹配项,我想执行一些测试,如果没有,则跳过文件并继续下一个。
如果我只从地图中抓取一个对象,我的测试就可以工作,但是当我尝试循环时它会失败。我想通过循环多个文件来运行测试。
如果我的测试中有异步调用,这似乎只是一个问题。使用完全同步的循环,我可以让它运行得很好。这是同步循环的示例(我意识到这是一个非常人为的示例,但它按预期工作):
describe('a test', () => {
const list = ['a', 'b', 'c'];
const test = (x) => {
describe(`should run ${x}`, () => {
beforeAll(async () => {
console.log('beforeAll');
});
beforeEach(async () => {
console.log('beforeEach');
});
for(let i = 0; i < 2; i++) {
it(`should log iteration ${i}`, async () => {
console.log(x, i);
expect(i).toBeTruthy();
});
}
});
}
for (let i = 0; i < list.length; i++) {
test(list[i]);
}
});
但是,如果它是异步的,则相同的模式不起作用。事实上,test
据我所知,嵌套的描述甚至没有运行。我将project
变量记录到控制台,所以我可以看到它被调用了,但是里面的logging in...
语句beforeAll
根本没有被执行。似乎它只是跳过它。我该如何使用async/await
?
describe('Import Project', () => {
const _page = new PageFactory();
const utils = new Utils();
const projPath = browser.params.projPath;
let projectGuid: string;
const test = (project) => {
describe(`importing ${project.name}`, () => {
beforeAll(async () => {
console.log('logging in...'); //not getting executed for some reason
await _page.loginPage.login(browser.params.username, browser.params.password);
// import
await _page.projectsPage.importProject(projectPath + project.filename)
.withImportType(project.importType)
.withProjectName(project.projectName)
.inEnvironment(project.environment)
.import();
projectGuid = await utils.extractGuid();
});
afterEach(async () => {
await _page.designerPage.navigateTo(projectGuid);
});
for(let i = 0; i < project.operations.length; i++) {
const operation = project.operations[i];
it(`should run ${operation.name} successfully`, async () => {
await runOperation(operation.name);
const status = await _page.operationLogsPage.getStatus(operation.outcomeTarget);
expect(status).toEqual(operation.expectedStatus);
});
}
});
}
utils.getProjects().then(projects => {
// projects is a string[] of filenames
// that are returned using fs.readdir()
for (let i = 0; i < projects.length; i++) {
const myProject = projectMap.get(projects[i]);
if (myProject) {
test(myProject);
}
}
});
});
这个的输出只是Executed 0 of 0 specs SUCCESS in 0.003 sec.
我不明白这与手动写出所有这些嵌套的描述块有何不同。如果我把每一个都写出来,它会工作得很好,但由于某种原因,它在循环中不想工作。有任何想法吗?
解决方案
我无法弄清楚如何让它以我想要的方式工作。相反,我决定使用节点child_process.execSync
并以这种方式遍历每个文件。这并不理想,但它有效。由于我要运行测试的每个文件都会创建一个新会话,因此需要更长的时间,但我厌倦了尝试使其以另一种方式工作。
我正在使用运行脚本node ./path/to/test.js --baseUrl=http://<myUrl>
这是代码的相关部分:
fs.readdir(projectsPath, (err, files) => {
if (err) {
throw new Error('Unable to scan directory: ' + err);
}
let i = 0;
const next = () => {
if (i < files.length) {
const file = files[i];
console.log('running tests on:', file);
const runCmd = `nodenv --exec "yarn run e2e:all ${baseUrl} --params.projectToImport=${file} --suite import_projects"`
// exec
try {
execSync(runCmd, { cwd: workingDir, stdio: 'inherit' },
(error, stdout, stderr) => {
if (error) {
console.error(error);
return;
}
console.log(stdout);
});
} catch (err) {
if (err.stderr) {
console.log('stderr:', err.stderr.toString());
}
}
// next iteration
i++;
next();
}
}
next();
});
推荐阅读
- python - TypeError: __init__() 得到了一个意外的关键字参数“大小”
- node.js - 当对象名称之一是数字时,如何获取从 API 获取的 JSON 文件中的数据
- node.js - 在 Bot Framework 如何使用 node.js 从自适应卡片 [图像和选择] 返回选定的值
- python - OR-Tools 具有多次行程、多次接送和有限容量的车辆路线
- docker - “Install-PackageProvider ContainerImage -Force”在 Windows Server 2016 中引发错误
- sql - 将字符串列转换为字符行
- python - 如果帖子包含“好”字,则排除帖子
- python - 创建新环境并从 Anaconda 命令提示符 (Windows) 安装 Notebook 后,Jupyter Notebook 不起作用
- python-3.x - 计算 gcp 监控中无数据持续时间超过 1 天
- java - Jackson @JsonAnySetter 在与 Jackson ObjectMapper treeToValue 方法一起使用时忽略重复键的值