This repository has been archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for Feature Contexts
- Loading branch information
1 parent
7cfcdb6
commit ef33827
Showing
7 changed files
with
197 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ContextualizedFeature, SkippableFeature } from './load-features' | ||
import { parseContexts } from './parseContexts' | ||
|
||
/** | ||
* If a "Contexts" table is present, run the feature for every context | ||
*/ | ||
export const contextualizeFeature = ( | ||
feature: SkippableFeature, | ||
): ContextualizedFeature[] => { | ||
const { description } = feature | ||
const contexts = parseContexts(description ?? '') | ||
if (contexts.length === 0) | ||
return [ | ||
{ | ||
...feature, | ||
context: {}, | ||
}, | ||
] | ||
return contexts.map((context) => ({ | ||
...feature, | ||
context, | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { parseContexts } from './parseContexts' | ||
|
||
const descriptionWithContexts = `If a feature has a contexts table, | ||
it should be repeated for all the entries. | ||
This allows to use contexts over multiple scenarios which is useful when | ||
scenarios need to be split up so that a retries do not trigger execution | ||
of a previous scenario. | ||
Note: "Context" has been chosen to distinguish it from the Gherkin "Scenario Outline + Examples" | ||
which is very similar, but is applied per scenario. | ||
Contexts: | ||
| arg1 | arg2 | | ||
| Hello | World | | ||
| ¡Hola! | Mundo |` | ||
|
||
const descriptionWithoutContexts = `No contexts here.` | ||
|
||
describe('parseContextss', () => { | ||
it('should parse contexts', () => { | ||
expect(parseContexts(descriptionWithContexts)).toEqual([ | ||
{ | ||
arg1: 'Hello', | ||
arg2: 'World', | ||
}, | ||
{ | ||
arg1: '¡Hola!', | ||
arg2: 'Mundo', | ||
}, | ||
]) | ||
}) | ||
it('should ignore if contexts do not exist', () => { | ||
expect(parseContexts(descriptionWithoutContexts)).toEqual([]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const tableMatch = /^\|([^|]+\|)+$/ | ||
|
||
/** | ||
* Parse a Contexts definition into a table | ||
*/ | ||
export const parseContexts = ( | ||
description: string, | ||
): Record<string, string>[] => { | ||
const lines = description.split('\n').map((s) => s.trim()) | ||
if (lines.find((s) => s === 'Contexts:') === undefined) return [] | ||
|
||
const table = [] | ||
let contextsFound = false | ||
// only pick up table lines immediately following the Contexts: token | ||
for (let line = 0; line < lines.length; line++) { | ||
if (lines[line] === 'Contexts:' && lines[line + 1] === '') { | ||
contextsFound = true | ||
continue | ||
} | ||
if (!contextsFound) continue | ||
if (lines[line] === '') continue | ||
if (tableMatch.exec(lines[line]) === null) { | ||
break | ||
} | ||
table.push( | ||
lines[line] | ||
.substr(1, lines[line].length - 2) | ||
.split('|') | ||
.map((s) => s.trim()), | ||
) | ||
} | ||
const [headers, ...rest] = table | ||
return rest.map((line) => | ||
headers.reduce( | ||
(row, header, k) => ({ ...row, [header]: line[k] }), | ||
{} as Record<string, string>, | ||
), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Feature: Feature Contexts | ||
|
||
If a feature has a contexts table, | ||
it should be repeated for all the entries. | ||
This allows to use contexts over multiple scenarios which is useful when | ||
scenarios need to be split up so that a retries do not trigger execution | ||
of a previous scenario. | ||
|
||
Note: "Context" has been chosen to distinguish it from the Gherkin "Scenario Outline + Examples" | ||
which is very similar, but is applied per scenario. | ||
|
||
Contexts: | ||
|
||
| arg1 | arg2 | | ||
| Hello | World | | ||
| ¡Hola! | Mundo | | ||
|
||
Scenario: | ||
|
||
Given I log "<arg1> <arg2>" |