【问题标题】:Using Cucumber.js with Jest将 Cucumber.js 与 Jest 一起使用
【发布时间】:2018-04-04 13:05:30
【问题描述】:

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

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

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

另一种选择是使用 Jest 测试运行程序以某种方式执行 Gherkin 规范。我也愿意接受这个选项。目前,我通过与我的 Jest 测试运行程序分开调用 cucumber.js 来运行它们。

【问题讨论】:

    标签: javascript testing bdd jestjs cucumberjs


    【解决方案1】:

    另一种方法是使用 jest-cucumber

    https://www.npmjs.com/package/jest-cucumber.

    让您可以灵活地使用这两个框架

    【讨论】:

    • 我很想知道使用 jest-cucumber 与原版 cucumber.js 相比有什么优势。一定是生态系统,但不是很明显?
    【解决方案2】:

    我的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();
      }
    });
    

    希望对您有所帮助。

    【讨论】:

      【解决方案3】:

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

      这是一个使用 jest 文档中的 DOM-testing example 的功能:

      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)
      })
      


      我发布的这个issue 给出了一个配置示例。

      【讨论】:

        猜你喜欢
        • 2017-10-31
        • 1970-01-01
        • 2018-08-09
        • 2019-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-08
        • 2019-05-19
        相关资源
        最近更新 更多