如何使用 WebStorm 在 TypeScript 而不是 JavaScript 中创建 Cucumber 步骤定义文件?
我正在使用 Cucumber.js 构建一个新的 e2e 测试套件,并且我想将 TypeScript 用于我的步骤文件.当我创建一个新步骤并按 Alt+Enter
让 WebStorm 生成一个新步骤文件时,我看到的唯一选项是创建一个 JavaScript 文件.
I'm building a new e2e test suite using Cucumber.js and I'd like to use TypeScript for my step files. When I create a new step and I press Alt+Enter
to have WebStorm generate a new step file the only option I am presented with is to create a JavaScript file.
有谁知道如何在 TypeScript 中创建一个新的步骤文件?
Does anyone know how I can make this create a new step file in TypeScript?
推荐答案
Webstorm 似乎没有为文件类型TypeScript"提供向导,因此您可能需要手动创建步骤定义文件.
Webstorm doesn't seem to provide a wizard for file type "TypeScript" so you might want to create your step definition file manually.
对于 简单数学示例,可能的 TS 步骤定义文件可能如下所示:
For the Simple Math Example a possible TS step definition file might look like this:
import * as cucumber from "cucumber";
module.exports = function () {
// Assign this to a typed variable so we have type-safe access
let sd: cucumber.StepDefinitions = this;
// The common variable is simply kept in function scope here
let variable: number = 0;
sd.Given(/^a variable set to (d+)$/, (value: string) => {
variable = parseInt(value);
});
sd.When(/^I increment the variable by (d+)$/, (value: string) => {
variable += parseInt(value);
});
sd.Then(/^the variable should contain (d+)$/, (value: string) => {
if (variable != parseInt(value))
throw new Error('Variable should contain '+value
+ ' but it contains ' + variable + '.');
});
};
将此内容放入例如features/step_definitions/mathSteps.ts 并将 示例 中的功能代码粘贴到名为 e.g.features/math.feature 你应该有一个运行的例子.
Put this content in e.g. features/step_definitions/mathSteps.ts and paste the feature code from the example into a file called e.g. features/math.feature and you should have a running example.
相关文章