如何使用WebStorm在TypeScript中而不是JavaScript中创建Cucumber步骤定义文件? [英] How can I use WebStorm to create a Cucumber step definition file in TypeScript instead of JavaScript?
问题描述
我正在使用Cucumber.js构建新的e2e测试套件,并且希望将TypeScript用于我的步骤文件。当我创建一个新步骤时,按 Alt + Enter
可使WebStorm生成一个新的步骤文件,唯一出现的选项是创建一个JavaScript文件。 = https://i.stack.imgur.com/RbXXs.png rel = nofollow noreferrer>
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,然后将示例 中的功能代码粘贴到名为例如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.
这篇关于如何使用WebStorm在TypeScript中而不是JavaScript中创建Cucumber步骤定义文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!