将 Cucumber.js 与 Jest 一起使用

新手上路,请多包涵

我正在使用 Jest 进行单元测试,并且正在集成 Cucumber.js 以运行用 Gherkin 编写的规范。

我已经全部设置好并且可以正常工作,但我遇到了一个问题:如何使用 Jest 的 expect ?我可以使用 chai 的,但我想在我的单元测试和我的步骤定义之间保持 expect 语法相同(我不想要 to.equal 在我的步骤定义和 toEqual 在我的单元测试中)。

我怎样才能做到这一点?经过一些挖掘后,Jest 似乎依赖于 expect npm 包。我可以在我的 package.json 中明确依赖该包,但我更愿意使用我现有的 Jest 依赖项。也许这是不可能的,但我希望是这样。

另一种选择是通过 Jest 测试运行器以某种方式执行 Gherkin 规范。我也会接受那个选项。目前,我正在通过调用 cucumber.js 与我的 Jest 测试运行器分开运行它们。

原文由 Johannes Fahrenkrug 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 563
2 个回答

expect 是玩笑运行时的全局范围。所以只要你在运行 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 "😎"

 import {cucumber as c} from 'gherkin-jest'
import React from 'react'
import {mount} from 'enzyme'
import {Checkbox} from '../src/components'

c.defineCreateWorld(() => ({
  checkbox:null
}))

c.defineRule('I did not check the checkbox so the label is {string}', (world, off) => {
  world.checkbox = mount(<Checkbox labelOff={off} />)
  expect(world.checkbox.text()).toBe(off)
})

c.defineRule('I checked the box and the emoji toggles to be {string}', (world, on) =>{
  world.checkbox = mount(<Checkbox labelOn={on}/>)
  world.checkbox.find('TouchableOpacity').props().onPress()
  expect(world.checkbox.text()).toBe(on)
})

我发布的这个 问题 给出了配置示例。

原文由 wordyallen 发布,翻译遵循 CC BY-SA 3.0 许可协议

我的 react-native 环境:

 "cucumber": "^4.1.0",
"jest": "22.4.2",

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

const { Given, Then, When } = require('cucumber');
const expect = require('expect');

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

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

希望有所帮助。

原文由 Mike S. 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题