Skip to content

Commit

Permalink
Export Cucumber functions through @wdio/cucumber-framework package (#…
Browse files Browse the repository at this point in the history
…7398)

* Export Cucumber functions through @wdio/cucumber-framework package

* reduce treshold

* fix smoke tests
  • Loading branch information
christian-bromann committed Sep 7, 2021
1 parent 5c0579d commit 072c3b3
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 7 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Expand Up @@ -26,7 +26,7 @@ module.exports = {
coverageThreshold: {
global: {
branches: 90,
functions: 97,
functions: 96,
lines: 97,
statements: 97
}
Expand Down
@@ -1,6 +1,6 @@
<%- isUsingTypeScript || isUsingBabel
? "import { Given, When, Then } from '@cucumber/cucumber';"
: "const { Given, When, Then } = require('@cucumber/cucumber');" %>
? "import { Given, When, Then } from '@wdio/cucumber-framework';"
: "const { Given, When, Then } = require('@wdio/cucumber-framework');" %>
<%
/**
* step definition without page objects
Expand Down
35 changes: 34 additions & 1 deletion packages/wdio-cucumber-framework/src/index.ts
Expand Up @@ -6,6 +6,22 @@ import isGlob from 'is-glob'
import glob from 'glob'

import * as Cucumber from '@cucumber/cucumber'
import {
After,
AfterAll,
AfterStep,
Before,
BeforeAll,
BeforeStep,
defineParameterType,
defineStep,
Given,
setDefaultTimeout,
setDefinitionFunctionWrapper,
setWorldConstructor,
Then,
When
} from '@cucumber/cucumber'
import { GherkinStreams } from '@cucumber/gherkin-streams'
import EventDataCollector from '@cucumber/cucumber/lib/formatter/helpers/event_data_collector'
import { ITestCaseHookParameter } from '@cucumber/cucumber/lib/support_code_library_builder/types'
Expand Down Expand Up @@ -374,7 +390,24 @@ adapterFactory.init = async function (...args: any[]) {
}

export default adapterFactory
export { CucumberAdapter, adapterFactory }
export {
CucumberAdapter,
adapterFactory,
After,
AfterAll,
AfterStep,
Before,
BeforeAll,
BeforeStep,
defineParameterType,
defineStep,
Given,
setDefaultTimeout,
setDefinitionFunctionWrapper,
setWorldConstructor,
Then,
When
}

declare global {
namespace WebdriverIO {
Expand Down
14 changes: 14 additions & 0 deletions packages/wdio-cucumber-framework/tests/adapter.test.ts
Expand Up @@ -5,6 +5,7 @@ import mockery from 'mockery'

import CucumberAdapter from '../src'
import { setUserHookNames } from '../src/utils'
import * as packageExports from '../src'

jest.mock('../src/reporter', () => class CucumberReporter {
eventListener = {
Expand Down Expand Up @@ -45,6 +46,19 @@ describe('CucumberAdapter', () => {
;(Cucumber.AfterStep as jest.Mock).mockClear()
})

it('exports Cucumber exports', () => {
expect(Object.keys(packageExports))
.toContain('Given')
expect(Object.keys(packageExports))
.toContain('When')
expect(Object.keys(packageExports))
.toContain('Then')
expect(Object.keys(packageExports))
.toContain('Before')
expect(Object.keys(packageExports))
.toContain('After')
})

it('can be initiated with tests', async () => {
const adapter = await CucumberAdapter.init('0-0', {
waitforTimeout: 1,
Expand Down
2 changes: 1 addition & 1 deletion tests/cucumber/reporter/reporter.given.js
@@ -1,4 +1,4 @@
import { Given, BeforeAll, Before, After, AfterAll } from '@cucumber/cucumber'
import { Given, BeforeAll, Before, After, AfterAll } from '../../../packages/wdio-cucumber-framework'

BeforeAll(() => {})
Before(function () {})
Expand Down
2 changes: 1 addition & 1 deletion tests/cucumber/step-definitions/given.js
@@ -1,6 +1,6 @@
// eslint-disable-next-line
import assert from 'assert'
import { Given, BeforeAll, Before, After, AfterAll } from '@cucumber/cucumber'
import { Given, BeforeAll, Before, After, AfterAll } from '../../../packages/wdio-cucumber-framework'

browser.addCommand('rootLevel', () => {
return true
Expand Down
2 changes: 1 addition & 1 deletion tests/cucumber/step-definitions/then.js
@@ -1,6 +1,6 @@
import assert from 'assert'

import { Then } from '@cucumber/cucumber'
import { Then } from '../../../packages/wdio-cucumber-framework'

Then(/^the title of the page should be:$/, (expectedTitle) => {
const actualTitle = browser.getTitle()
Expand Down
16 changes: 16 additions & 0 deletions website/docs/Frameworks.md
Expand Up @@ -401,3 +401,19 @@ Here you have some examples of this syntax:
- `@skip(browserName="firefox";platformName="linux")`: will skip the test in firefox over linux executions.
- `@skip(browserName=["chrome","firefox"])`: tagged items will be skipped for both chrome and firefox browsers.
- `@skip(browserName=/i.*explorer/`: capabilities with browsers matching the regexp will be skipped (like `iexplorer`, `internet explorer`, `internet-explorer`, ...).

### Import Step Definition Helper

In order to use step definition helper like `Given`, `When` or `Then` or hooks, you are suppose to import then from `@cucumber/cucumber`, e.g. like this:

```js
import { Given, When, Then } from '@cucumber/cucumber'
```

Now, if you use Cucumber already for other types of tests unrelated to WebdriverIO for which you use a specific version you need to import these helpers in your e2e tests from the WebdriverIO Cucumber package, e.g.:

```js
import { Given, When, Then } from '@wdio/cucumber-framework'
```

This ensures that you use the right helpers within the WebdriverIO framework and allows you to use an independant Cucumber version for other types of testing.

0 comments on commit 072c3b3

Please sign in to comment.