在Jest中使用Cucumber.js

Joh*_*rug 10 javascript testing bdd cucumberjs jestjs

我正在使用Jest进行单元测试,而我正在整合Cucumber.js以运行用Gherkin编写的规范.

我已经完成所有设置并且它正在运行,但我遇到了一个问题:我如何使用Jest expect?我可以使用chai's,但我想expect在我的单元测试和我的步骤定义之间保持相同的语法(我不希望to.equal在我的步骤定义和toEqual单元测试中).

我怎样才能做到这一点?经过一番挖掘后,似乎Jest依赖于expectnpm包.我可以在我的内容中明确依赖该包package.json,但我更倾向于使用我现有的Jest依赖项.也许那是不可能的,但我希望是这样.

另一个选择是以某种方式用Jest测试运行器执行Gherkin规范.我也对这个选项持开放态度.目前我正在通过cucumber.js与我的Jest测试运行器分开调用来运行它们.

Mik*_* S. 7

我的react-native环境:

"cucumber": "^4.1.0",
"jest": "22.4.2",
Run Code Online (Sandbox Code Playgroud)

在我的steps definition文件中,我只需要这样

const { Given, Then, When } = require('cucumber');
const expect = require('expect');
Run Code Online (Sandbox Code Playgroud)

Expect是Jest的一部分,因此您可以将其作为自己的对象导入.然后我可以在任何需要断言的地方使用它.注意:newMember在其他地方声明并填充.

Given('Sara has provided account details', function() {
  for (const prop in newMember) {
    expect(newMember[prop]).toBeTruthy();
  }
});
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.


wor*_*len 4

expect是 jest 运行时的全局范围。因此,只要您运行 jest,它就可用。我正在使用这个包(需要一些配置才能正确转换为您的 babel 配置):gherkin-jest

以下是使用jest 文档中的DOM 测试示例的功能:

Feature: Using feature files in jest and cucumber
  As a developer
  I want to write tests in cucumber and jest
  So that businesspeople understand tests and I can test React

  Scenario: Emoji toggles upon checking and unchecking the checkbox
    Given I did not check the checkbox, so the label is ""
    When I check the box and the emoji toggles to be ""
Run Code Online (Sandbox Code Playgroud)


Feature: Using feature files in jest and cucumber
  As a developer
  I want to write tests in cucumber and jest
  So that businesspeople understand tests and I can test React

  Scenario: Emoji toggles upon checking and unchecking the checkbox
    Given I did not check the checkbox, so the label is ""
    When I check the box and the emoji toggles to be ""
Run Code Online (Sandbox Code Playgroud)


我发布的这个问题提供了一个配置示例。