Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(snapshot): add snapshot support for Scenario Outline #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ It's also the perfect companion for testing CLI applications built with commande
- [**http API**](#http-api-extension) [install](#http-api-installation) | [gherkin expressions](#http-api-gherkin-expressions) | [low level API](#http-api-low-level-api)
- [**CLI**](#cli-extension) [install](#cli-installation) | [gherkin expressions](#cli-gherkin-expressions) | [low level API](#cli-low-level-api)
- [**fileSystem**](#file-system-extension) [install](#file-system-installation) | [gherkin expressions](#file-system-gherkin-expressions) | [low level API](#file-system-low-level-api)
- [**snapshot**](#snapshot-extension) [install](#snapshot-installation) | [low level API](#snapshot-low-level-api)
- [**snapshot**](#snapshot-extension) [install](#snapshot-extension-installation) | [low level API](#snapshot-low-level-api)
- [Helpers](#helpers)
- [**cast**](#cast-helper) [usage](#cast-usage) | [add a type](#add-a-type)
- [Examples](#examples)
Expand Down
14 changes: 14 additions & 0 deletions examples/features/snapshot/__snapshots__/snapshot.feature.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@


exports[`Scenario Outline 1.1`] = `undefined`;

exports[`Scenario Outline 2.1`] = `undefined`;

exports[`Scenario Outline 3.1`] = `undefined`;

exports[`Scenario Outline 4.1`] = `undefined`;

exports[`Setting json body from .yaml fixture file 1.1`] = `Object {
"first_name": "Raphaël",
"gender": "male",
Expand All @@ -22,3 +30,9 @@ first_name: Raphaël
last_name: Benitte
gender: male
"`;

exports[`Snapshot testing on files 2.1`] = `"id: 1
first_name: Raphaël
last_name: Benitte
gender: male
"`;
20 changes: 20 additions & 0 deletions examples/features/snapshot/snapshot.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,23 @@ Feature: Using snapshot definitions
Scenario: Snapshot testing on files
Given I set cwd to examples/features/snapshot/fixtures
Then file file.yaml should match snapshot

Scenario Outline: I get all posts filtered using '<filter>=<value>' directive
Given I mock http call to forward request body for path /users/yaml
When I POST http://fake.io/users/yaml
Then response status code should be 200
And response body should match snapshot

Examples:
| filter | value |

| title | car industry |

| anotherTitle | antother value |
# this comment and the empty lines above and below shouldn't be taken into account


| tester | tesfdkngjsfk |
Scenario: Snapshot testing on files
Given I set cwd to examples/features/snapshot/fixtures
Then file file.yaml should match snapshot
56 changes: 55 additions & 1 deletion src/extensions/snapshot/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const EXPECTED_COLOR = chalk.green
const RECEIVED_COLOR = chalk.red

exports.scenarioRegex = /^[\s]*Scenario:[\s]*(.*[^\s])[\s]*$/

exports.examplesRegex = /^[\s]*Examples:[\s]*$/
exports.oneExampleRegex = /\|(.*)\|/
/**
* Extract scenarios from a feature file
* @param {string} file - Feature file path
Expand All @@ -32,15 +33,68 @@ exports.extractScenarios = file => {
const linesContent = _.split(content, '\n')

let result = []
let skipToIndex = -1

_.forEach(linesContent, (lineContent, idx) => {
//In the case of a Scenario Outline, skip to its end after it has been processed
if (skipToIndex > 0) {
if (skipToIndex === idx) {
skipToIndex = -1
} else {
return
}
}

const line = idx + 1
const scenarioInfos = this.scenarioRegex.exec(lineContent)
if (scenarioInfos) result.push({ line, name: scenarioInfos[1] })

const scenarioOutlineInfo = this.examplesRegex.exec(lineContent)
if (scenarioOutlineInfo) {
const examples = extractExampleLines(
_.slice(linesContent, idx + 1),
idx + 2,
scenarioOutlineInfo[1]
)
result.push(...examples.results)
}
})

return result
}

/**
* A function to extract the lines of examples in the Scenario outline
*
* @param {Array<String>} examplesLinesContent - The remaining lines to be examined and parsed
* @param {number} offset - The index of the first line in the original text
* @param {String} name - the name for the scenarios
* @returns {{results: Array, endIndex: *}}
*/
const extractExampleLines = (examplesLinesContent, offset, name = 'Scenario Outline') => {
const results = []
let endIndex = offset
let headersSkipped = false

_.forEach(examplesLinesContent, (lineContent, idx) => {
if (lineContent.match(/^\s*$/) || lineContent.match(/[\s]*#.*/)) return //skip empty lines and comments
const index = offset + idx
endIndex = index
const example = this.oneExampleRegex.exec(lineContent)
if (example) {
if (headersSkipped) {
results.push({ line: index, name })
} else {
headersSkipped = true
}
} else {
return false //break out of the loop
}
})

return { results, endIndex }
}

/**
* Create snapshots prefix that will be used for each snapshot step of a scenario
* For example if the scenario name is 'Scenario 1', then prefix will be 'Scenario 1 1'
Expand Down