Skip to content

Commit

Permalink
fix(snapshot): correct snapshot comparison with carriage returns
Browse files Browse the repository at this point in the history
  • Loading branch information
leguellec committed Nov 27, 2019
1 parent 7b46a1c commit 74f92fb
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 126 deletions.
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ root = true
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
trim_trailing_whitespace = false
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
trim_trailing_whitespace = false
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -883,9 +883,9 @@ Then(/^I check something using file system$/, function() {
### Snapshot extension
#### Snapshot extension installation
#### Snapshot installation
The snapshot extension add capabilities to [api](#http-api-extension), [cli](#cli--extension) and [file](#file-system-extension) extensions,
The snapshot extension add capabilities to [api](#http-api-extension), [cli](#cli-extension) and [file](#file-system-extension) extensions,
so you will need these extensions if you want to use snapshot related gherkin definitions.
To install the extension, you should add the following snippet
Expand Down Expand Up @@ -996,13 +996,13 @@ yarn run examples -- --tags @offline

Due to public API rate limit (eg. GitHub API), this tag is used when running on CI.

[npm-image]: https://img.shields.io/npm/v/@ekino/veggies.svg?style=flat-square
[npm-image]: https://img.shields.io/npm/v/@ekino/veggies.svg?longCache=true&style=for-the-badge
[npm-url]: https://www.npmjs.com/package/@ekino/veggies
[travis-image]: https://img.shields.io/travis/ekino/veggies.svg?style=flat-square
[travis-image]: https://img.shields.io/travis/ekino/veggies.svg?longCache=true&style=for-the-badge
[travis-url]: https://travis-ci.org/ekino/veggies
[prettier-image]: https://img.shields.io/badge/styled_with-prettier-ff69b4.svg?style=flat-square
[prettier-image]: https://img.shields.io/badge/styled_with-prettier-ff69b4.svg?longCache=true&style=for-the-badge
[prettier-url]: https://github.com/prettier/prettier
[coverage-image]: https://img.shields.io/coveralls/ekino/veggies/master.svg?style=flat-square
[coverage-image]: https://img.shields.io/coveralls/ekino/veggies/master.svg?longCache=true&style=for-the-badge
[coverage-url]: https://coveralls.io/github/ekino/veggies?branch=master

[github-watch-badge]: https://img.shields.io/github/watchers/ekino/veggies.svg?style=social
Expand Down
12 changes: 6 additions & 6 deletions doc/README.tpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -821,9 +821,9 @@ Then(/^I check something using file system$/, function() {
### Snapshot extension
#### Snapshot extension installation
#### Snapshot installation
The snapshot extension add capabilities to [api](#http-api-extension), [cli](#cli--extension) and [file](#file-system-extension) extensions,
The snapshot extension add capabilities to [api](#http-api-extension), [cli](#cli-extension) and [file](#file-system-extension) extensions,
so you will need these extensions if you want to use snapshot related gherkin definitions.
To install the extension, you should add the following snippet
Expand Down Expand Up @@ -934,13 +934,13 @@ yarn run examples -- --tags @offline

Due to public API rate limit (eg. GitHub API), this tag is used when running on CI.

[npm-image]: https://img.shields.io/npm/v/@ekino/veggies.svg?style=flat-square
[npm-image]: https://img.shields.io/npm/v/@ekino/veggies.svg?longCache=true&style=for-the-badge
[npm-url]: https://www.npmjs.com/package/@ekino/veggies
[travis-image]: https://img.shields.io/travis/ekino/veggies.svg?style=flat-square
[travis-image]: https://img.shields.io/travis/ekino/veggies.svg?longCache=true&style=for-the-badge
[travis-url]: https://travis-ci.org/ekino/veggies
[prettier-image]: https://img.shields.io/badge/styled_with-prettier-ff69b4.svg?style=flat-square
[prettier-image]: https://img.shields.io/badge/styled_with-prettier-ff69b4.svg?longCache=true&style=for-the-badge
[prettier-url]: https://github.com/prettier/prettier
[coverage-image]: https://img.shields.io/coveralls/ekino/veggies/master.svg?style=flat-square
[coverage-image]: https://img.shields.io/coveralls/ekino/veggies/master.svg?longCache=true&style=for-the-badge
[coverage-url]: https://coveralls.io/github/ekino/veggies?branch=master

[github-watch-badge]: https://img.shields.io/github/watchers/ekino/veggies.svg?style=social
Expand Down
1 change: 1 addition & 0 deletions src/extensions/snapshot/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Snapshot {
*/
expectToMatch(expectedContent) {
expectedContent = prettyFormat(expectedContent)
expectedContent = snapshot.normalizeNewlines(expectedContent)
let snapshotsFile = snapshot.snapshotsPath(this.featureFile, this.options)

const scenarios = snapshot.extractScenarios(this.featureFile)
Expand Down
15 changes: 13 additions & 2 deletions tests/extensions/snapshot/extension.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ beforeAll(() => {
return fixtures.featureFileContent1
if (file === fixtures.featureFile1With3SnapshotsInAScenario)
return fixtures.featureFileContent1

if (file === fixtures.featureFileMultilineString) return fixtures.featureFileContent1
if (file === fixtures.snapshotFile1) return fixtures.snapshotFileContent1
if (file === fixtures.snapshotFile1WithPropertyMatchers)
return fixtures.snapshotFileContent1WithPropertyMatchers
Expand All @@ -32,7 +32,8 @@ beforeAll(() => {
return fixtures.snapshotFileContent1
if (file === fixtures.snapshotFile1With3SnapshotsInAScenario)
return fixtures.snapshotFileContent1With3SnapshotsInAScenario

if (file === fixtures.snapshotFileMultilineString)
return fixtures.snapshotFileContentMultilineString
throw new Error(`Unexpected call to readFileSync with file ${file}`)
})

Expand All @@ -45,6 +46,7 @@ beforeAll(() => {
if (file === fixtures.snapshotFile1And2) return {}
if (file === fixtures.snapshotFile1With2SnapshotsInAScenario) return {}
if (file === fixtures.snapshotFile1With3SnapshotsInAScenario) return {}
if (file === fixtures.snapshotFileMultilineString) return {}
throw new Error(`Unexpected call to statSync with file ${file}`)
})
})
Expand Down Expand Up @@ -239,3 +241,12 @@ test('expectToMatchJson should throw an error if a property matcher changes', ()
fixtures.diffErrorFile1WithPropertyMatchers
)
})

test('expectToMatch should handle multline content correctly', () => {
const snapshot = Snapshot({ cleanSnapshots: true })

snapshot.featureFile = fixtures.featureFileMultilineString
snapshot.scenarioLine = 3
expect(snapshot.expectToMatch(fixtures.multilineValue)).toBeUndefined()
clean.cleanSnapshots()
})
29 changes: 27 additions & 2 deletions tests/extensions/snapshot/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ exports.featureFileContent1 = dedent`
Scenario: scenario 1
When I do something...
Scenario: scenario 2
When I do something...
Scenario: scenario 1
When I do something...
"""
Expand Down Expand Up @@ -55,6 +55,19 @@ exports.snapshotContent1WithPropertyMatchers = dedent`
}
`

exports.snapshotContentMultilineString = dedent`
Object {
"content": Object {
"long text": "I
am
a
long
text",
"text": "i am a text",
},
}
`

exports.snapshotFileContent1 = `
exports[\`scenario 1 1.1\`] = \`${exports.snapshotContent1}\`;
Expand Down Expand Up @@ -102,6 +115,12 @@ exports.snapshotFileContent1WithValue2 = `
exports[\`scenario 1 1.1\`] = \`${exports.snapshotContent2}\`;
`

exports.snapshotFileContentMultilineString = `
exports[\`scenario 1 1.1\`] = \`${exports.snapshotContentMultilineString}\`;
`

exports.diffErrorValue1VsValue2 = dedent`
\u001b[32m- Snapshot\u001b[39m
\u001b[31m+ Received\u001b[39m
Expand Down Expand Up @@ -141,13 +160,17 @@ exports.value1WithError = {
}
exports.value2 = { key1: 'value1', key2: 'value2', key3: 'value8', key4: 'value4', key5: 'value5' }
exports.value3 = { key1: 'value1', key2: 'value2', key3: 'value9', key4: 'value4', key5: 'value5' }
exports.multilineValue = {
content: { text: 'i am a text', 'long text': 'I\r\n am \r\n a\r\n long\r\n text' }
}

exports.featureFile1 = './snapshot1.feature'
exports.featureFile1WithPropertyMatchers = './snapshot1WithPropertyMatchers.feature'
exports.featureFile1And2 = './snapshot1And2.feature'
exports.featureFile1NotExists = './snapshot1NotExists.feature'
exports.featureFile1With2SnapshotsInAScenario = './snapshot1With2SnapshotsInAScenario.feature'
exports.featureFile1With3SnapshotsInAScenario = './snapshot1With3SnapshotsInAScenario.feature'
exports.featureFileMultilineString = './snapshotMultilineString.feature'

exports.snapshotFile1 = '__snapshots__/snapshot1.feature.snap'
exports.snapshotFile1WithPropertyMatchers =
Expand All @@ -158,3 +181,5 @@ exports.snapshotFile1With2SnapshotsInAScenario =
'__snapshots__/snapshot1With2SnapshotsInAScenario.feature.snap'
exports.snapshotFile1With3SnapshotsInAScenario =
'__snapshots__/snapshot1With3SnapshotsInAScenario.feature.snap'

exports.snapshotFileMultilineString = '__snapshots__/snapshotMultilineString.feature.snap'

0 comments on commit 74f92fb

Please sign in to comment.