question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
rickhanloniicommented, Oct 25, 2018

Yeah, @thymikee is right. So if you want to escape it you’ll need to opt-in by escaping the newline like:

So either:

const escape = string => {
  return string.replace('\n', '\\n');
};

it('should generate snapshot', () => {
  expect(escape(generateNewLine())).toMatchSnapshot();
});

Or if you wanted, you could create (and optionally publish) a matcher to do this:

const { toMatchSnapshot } = require('jest-snapshot');

expect.extend({
  toMatchEscapedSnapshot(received) {
    return toMatchSnapshot.call(
      this,
      received.replace('\n', '\\n'),
      'toMatchEscapedSnapshot',
    );
  },
});

it('should generate snapshot', () => {
  expect(generateNewLine()).toMatchEscapedSnapshot();
});
0reactions
github-actions[bot]commented, May 12, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

When adding "\n" to a Java String , the newline is not ...
The string holds names & addresses with a new line "\n" , but when I'm trying to present that on the screen ,...
Read more >
Snapshot Testing - Jest
Snapshot tests are a very useful tool whenever you want to make sure your UI does not change unexpectedly.
Read more >
restore-from-cluster-snapshot - AWS Documentation
Specifies the name of the HSM configuration that contains the information the Amazon Redshift cluster can use to retrieve and store keys in...
Read more >
Snapshot Management - Gaia R80.30 Administration Guide
Configures the name of the snapshot image. You must enter a string that does not contain spaces. desc "< Description of Snapshot >"....
Read more >
list-snapshots — AWS CLI 2.9.6 Command Reference
Do not use the NextToken response element directly outside of the AWS CLI. For usage examples, see Pagination in the AWS Command Line...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found