String with a new line does not show up on snapshot
See original GitHub issue🐛 Bug Report
I am currently creating a snapshot test for an object where one of the properties is a string that has a new line (‘\n’). Like this:
{
description: 'this is a test with \n a new \n line'
}
Upon generating a snapshot for testing one of these objects, the generated snapshot shows this:
exports[`generateNewLine should generate snapshot 1`] = `
Object {
"description": "this is a test with
a new
line",
};
The next line characters are lost and the generated snapshot does not include them.
I was not able to find any issue related to this to see if there is something I am doing wrong, or if it is not supported 😃
To Reproduce
Steps to reproduce the behavior: Create a simple function that returns a text with a new line character.
function generateNewLine() {
return {
description: 'this is a test with \n a new \n line'
};
}
Execute the function and generate a snapshot:
describe('generateNewLine', () => {
it('should generate snapshot', () => {
expect(generateNewLine()).toMatchSnapshot();
});
});
Expected behavior
It should keep the new line characters on the generated snapshot, something like this:
exports[`generateBreakLine should generate snapshot 1`] = `
Object {
"description": "this is a test with \n a new \n line",
};
Link to repl or repo (highly encouraged)
https://repl.it/@JuaniGalan/new-line-snapshot-possible-bug
Issues without a reproduction link are likely to stall.
Run npx envinfo --preset jest
Paste the results here:
System:
OS: macOS High Sierra 10.13.6
CPU: x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
Binaries:
Node: 8.9.4 - /usr/local/bin/node
npm: 6.4.1 - /usr/local/bin/npm
npmPackages:
jest: 22.0.5 => 22.0.5
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:7 (1 by maintainers)
Yeah, @thymikee is right. So if you want to escape it you’ll need to opt-in by escaping the newline like:
So either:
Or if you wanted, you could create (and optionally publish) a matcher to do this:
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.