如何使用 WebStorm 在 TypeScript 而不是 JavaScript 中创建 Cucumber 步骤定义文件? [英] How can I use WebStorm to create a Cucumber step definition file in TypeScript instead of JavaScript?

查看:36
本文介绍了如何使用 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.

这篇关于如何使用 WebStorm 在 TypeScript 而不是 JavaScript 中创建 Cucumber 步骤定义文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆