Skip to content

Commit

Permalink
Escape backticks when injecting expect.unindent...
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Jun 15, 2019
1 parent 76dda50 commit 5f62694
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/ensureAfterHookIsRegistered.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const detectIndent = require('detect-indent');
const indentString = require('./indentString');
function stringify(obj, indentationWidth, inspect) {
if (obj.includes('\n')) {
return `expect.unindent\`${indentString(obj, indentationWidth)}\``;
return `expect.unindent\`${indentString(
obj.replace(/`/g, '\\`'),
indentationWidth
)}\``;
} else {
return inspect(obj, indentationWidth);
}
Expand Down
27 changes: 27 additions & 0 deletions test/unexpected-snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,33 @@ it('should foo', function() {
);
});

it('should escape backticks in injected multiline snapshot', function() {
return expect(
`
it('should foo', function() {
expect(
'foo\`\\nbar\`',
'to equal snapshot',
'bar'
);
});
`,
'to come out as exactly',
`
it('should foo', function() {
expect(
'foo\`\\nbar\`',
'to equal snapshot',
expect.unindent\`
foo\\\`
bar\\\`
\`
);
});
`
);
});

describe('with a multi line string', function() {
it('should inject a template string with indentation', function() {
return expect(
Expand Down

0 comments on commit 5f62694

Please sign in to comment.