Package Exports
- pekel
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pekel) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
< pekel >
This is a runner for executing tests using the Cucumber-Js within Jest (version 25+).
npm i pekelCucumber Features Supported
- After
- AfterAll
- And
- Attachments
- Background
- Before
- BeforeAll
- But
- Comments
- Data Table
- DocString
- Given
- Rule
- Scenario
- Scenario Outline
- setDefaultTimeout
- setDefinitionFunctionWrapper
- setWorldConstructor
- Tags
- Then
- When
| Feature | Notes |
|---|---|
| After | called after each scenario in a feature file |
| AfterAll | called after the feature file is completed |
| Attachments | not sure how we can support this |
| Before | called before each scenario in a feature file |
| BeforeAll | called before the feature file is started |
| Rule | I've yet to come across a working example of this for cucumber-js |
| Scenario Outline | passing example table properties as part of description are not supported |
| setDefaultTimeout | please use jest.setTimeout or set the timeout property in your jest config |
| Tags | need to identify a way to pass tags through jest |
Setup
Install Peer Dependencies
npm i cucumber -Dnpm i jest -D* if not already installedCreate A Feature File
path/to/my/features/button.featureFeature: Button Given I go to home When I click the login button Then the login button is not visibleWrite Your Hooks
path/to/my/hooks.tsximport React from 'react'; import ReactDOM from 'react-dom'; import { AfterAll, BeforeAll } from 'cucumber'; import App from './path/to/my/app'; BeforeAll(function () { ReactDOM.render( <App/>, document.body ) }); AfterAll(function () { ReactDOM.unmountComponentAtNode( document.body ) });
You can choose to use the hooks to render/unmount your component before/after each feature file like above, or you can add a path to your application entry point to your jest configuration's setupFiles property. The latter is more performant.
Write Your Steps
path/to/my/steps.tsimport { Given, When, Then } from 'cucumber'; import expect from 'expect'; Given(/I go to (.*)$/, function(link) { window.location.hash = `#/${link}`; }); When(/I click the (\S+) button$/, function(name) { document.querySelector(`[data-test-id="${name}"]`).click(); }); Then(/the (\S+) button is (visible|not visible)$/, function(name, state) { expect(!!document.querySelector(`[data-test-id="${name}"]`)) .toEqual(state === 'visible') });
Setup Your World
setWorldConstuctor allows you to set the context of "this" for your steps/hooks definitions. This can be helpful when you want to maintain state between steps/hooks or want your steps/hooks to have access to some predefined data
path/to/my/world.tsimport { setWorldConstructor } from 'cucumber'; setWorldConstructor( class MyWorld { pages = []; } );
Create/Modify your jest configuration:
{ "runner": "pekel", "setupFilesAfterEnv": [ "path/to/my/hooks.ts", "path/to/my/steps.ts", "path/to/my/world.ts" ], "testMatch": [ "path/to/my/features/*.feature" ] }
If you have existing jest test cases that do not use Cucumber, create a separate configuration. You can use the Jest CLI to run against specific configurations:
jest --config=path/to/my/config.jsonProperty Note restoreMocks There is an open bug for jest to fix the implementation of this where it does not unset manual mocks that are defined using the __mock__ folders. However, if this is set true, pekel will perform a scan of all __mock__ folders and files and manually unmock them for you. runner tells jest which runner to use while executing the tests setupFiles A spot where you should add your window polyfills, eg. jest-canvas-mock. It can also be used as the spot where you render your application's entry component into the dom setupFilesAfterEnv Add your steps, hooks, and world files here testMatch Add a path pattern to your feature files here
Why?!
Maintaining two separate testing frameworks is a burden.
Cucumber is great, but the runner it provides is not nearly as well documented or as polished as Jest.
Other existing solutions failed to support:
Step Definitions that can be used in every feature file
IDE Intellisense for feature files and steps