Skip to content

Commit 74f92fb

Browse files
committed
fix(snapshot): correct snapshot comparison with carriage returns
1 parent 7b46a1c commit 74f92fb

File tree

7 files changed

+74
-126
lines changed

7 files changed

+74
-126
lines changed

.editorconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ root = true
44
indent_style = space
55
indent_size = 4
66
charset = utf-8
7-
trim_trailing_whitespace = true
7+
trim_trailing_whitespace = false
88
insert_final_newline = true
99

1010
[*.md]
11-
trim_trailing_whitespace = false
11+
trim_trailing_whitespace = false

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -883,9 +883,9 @@ Then(/^I check something using file system$/, function() {
883883
884884
### Snapshot extension
885885
886-
#### Snapshot extension installation
886+
#### Snapshot installation
887887
888-
The snapshot extension add capabilities to [api](#http-api-extension), [cli](#cli--extension) and [file](#file-system-extension) extensions,
888+
The snapshot extension add capabilities to [api](#http-api-extension), [cli](#cli-extension) and [file](#file-system-extension) extensions,
889889
so you will need these extensions if you want to use snapshot related gherkin definitions.
890890
891891
To install the extension, you should add the following snippet
@@ -996,13 +996,13 @@ yarn run examples -- --tags @offline
996996

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

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

10081008
[github-watch-badge]: https://img.shields.io/github/watchers/ekino/veggies.svg?style=social

doc/README.tpl.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -821,9 +821,9 @@ Then(/^I check something using file system$/, function() {
821821
822822
### Snapshot extension
823823
824-
#### Snapshot extension installation
824+
#### Snapshot installation
825825
826-
The snapshot extension add capabilities to [api](#http-api-extension), [cli](#cli--extension) and [file](#file-system-extension) extensions,
826+
The snapshot extension add capabilities to [api](#http-api-extension), [cli](#cli-extension) and [file](#file-system-extension) extensions,
827827
so you will need these extensions if you want to use snapshot related gherkin definitions.
828828
829829
To install the extension, you should add the following snippet
@@ -934,13 +934,13 @@ yarn run examples -- --tags @offline
934934

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

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

946946
[github-watch-badge]: https://img.shields.io/github/watchers/ekino/veggies.svg?style=social

src/extensions/snapshot/extension.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class Snapshot {
7171
*/
7272
expectToMatch(expectedContent) {
7373
expectedContent = prettyFormat(expectedContent)
74+
expectedContent = snapshot.normalizeNewlines(expectedContent)
7475
let snapshotsFile = snapshot.snapshotsPath(this.featureFile, this.options)
7576

7677
const scenarios = snapshot.extractScenarios(this.featureFile)

tests/extensions/snapshot/extension.test.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ beforeAll(() => {
2323
return fixtures.featureFileContent1
2424
if (file === fixtures.featureFile1With3SnapshotsInAScenario)
2525
return fixtures.featureFileContent1
26-
26+
if (file === fixtures.featureFileMultilineString) return fixtures.featureFileContent1
2727
if (file === fixtures.snapshotFile1) return fixtures.snapshotFileContent1
2828
if (file === fixtures.snapshotFile1WithPropertyMatchers)
2929
return fixtures.snapshotFileContent1WithPropertyMatchers
@@ -32,7 +32,8 @@ beforeAll(() => {
3232
return fixtures.snapshotFileContent1
3333
if (file === fixtures.snapshotFile1With3SnapshotsInAScenario)
3434
return fixtures.snapshotFileContent1With3SnapshotsInAScenario
35-
35+
if (file === fixtures.snapshotFileMultilineString)
36+
return fixtures.snapshotFileContentMultilineString
3637
throw new Error(`Unexpected call to readFileSync with file ${file}`)
3738
})
3839

@@ -45,6 +46,7 @@ beforeAll(() => {
4546
if (file === fixtures.snapshotFile1And2) return {}
4647
if (file === fixtures.snapshotFile1With2SnapshotsInAScenario) return {}
4748
if (file === fixtures.snapshotFile1With3SnapshotsInAScenario) return {}
49+
if (file === fixtures.snapshotFileMultilineString) return {}
4850
throw new Error(`Unexpected call to statSync with file ${file}`)
4951
})
5052
})
@@ -239,3 +241,12 @@ test('expectToMatchJson should throw an error if a property matcher changes', ()
239241
fixtures.diffErrorFile1WithPropertyMatchers
240242
)
241243
})
244+
245+
test('expectToMatch should handle multline content correctly', () => {
246+
const snapshot = Snapshot({ cleanSnapshots: true })
247+
248+
snapshot.featureFile = fixtures.featureFileMultilineString
249+
snapshot.scenarioLine = 3
250+
expect(snapshot.expectToMatch(fixtures.multilineValue)).toBeUndefined()
251+
clean.cleanSnapshots()
252+
})

tests/extensions/snapshot/fixtures.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ exports.featureFileContent1 = dedent`
66
77
Scenario: scenario 1
88
When I do something...
9-
9+
1010
Scenario: scenario 2
1111
When I do something...
12-
12+
1313
Scenario: scenario 1
1414
When I do something...
1515
"""
@@ -55,6 +55,19 @@ exports.snapshotContent1WithPropertyMatchers = dedent`
5555
}
5656
`
5757

58+
exports.snapshotContentMultilineString = dedent`
59+
Object {
60+
"content": Object {
61+
"long text": "I
62+
am
63+
a
64+
long
65+
text",
66+
"text": "i am a text",
67+
},
68+
}
69+
`
70+
5871
exports.snapshotFileContent1 = `
5972
6073
exports[\`scenario 1 1.1\`] = \`${exports.snapshotContent1}\`;
@@ -102,6 +115,12 @@ exports.snapshotFileContent1WithValue2 = `
102115
exports[\`scenario 1 1.1\`] = \`${exports.snapshotContent2}\`;
103116
`
104117

118+
exports.snapshotFileContentMultilineString = `
119+
120+
exports[\`scenario 1 1.1\`] = \`${exports.snapshotContentMultilineString}\`;
121+
122+
`
123+
105124
exports.diffErrorValue1VsValue2 = dedent`
106125
\u001b[32m- Snapshot\u001b[39m
107126
\u001b[31m+ Received\u001b[39m
@@ -141,13 +160,17 @@ exports.value1WithError = {
141160
}
142161
exports.value2 = { key1: 'value1', key2: 'value2', key3: 'value8', key4: 'value4', key5: 'value5' }
143162
exports.value3 = { key1: 'value1', key2: 'value2', key3: 'value9', key4: 'value4', key5: 'value5' }
163+
exports.multilineValue = {
164+
content: { text: 'i am a text', 'long text': 'I\r\n am \r\n a\r\n long\r\n text' }
165+
}
144166

145167
exports.featureFile1 = './snapshot1.feature'
146168
exports.featureFile1WithPropertyMatchers = './snapshot1WithPropertyMatchers.feature'
147169
exports.featureFile1And2 = './snapshot1And2.feature'
148170
exports.featureFile1NotExists = './snapshot1NotExists.feature'
149171
exports.featureFile1With2SnapshotsInAScenario = './snapshot1With2SnapshotsInAScenario.feature'
150172
exports.featureFile1With3SnapshotsInAScenario = './snapshot1With3SnapshotsInAScenario.feature'
173+
exports.featureFileMultilineString = './snapshotMultilineString.feature'
151174

152175
exports.snapshotFile1 = '__snapshots__/snapshot1.feature.snap'
153176
exports.snapshotFile1WithPropertyMatchers =
@@ -158,3 +181,5 @@ exports.snapshotFile1With2SnapshotsInAScenario =
158181
'__snapshots__/snapshot1With2SnapshotsInAScenario.feature.snap'
159182
exports.snapshotFile1With3SnapshotsInAScenario =
160183
'__snapshots__/snapshot1With3SnapshotsInAScenario.feature.snap'
184+
185+
exports.snapshotFileMultilineString = '__snapshots__/snapshotMultilineString.feature.snap'

0 commit comments

Comments
 (0)