Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Aug 16, 2023
1 parent 1e21bab commit 1386870
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 12 deletions.
7 changes: 3 additions & 4 deletions packages/snapshot/src/port/inlineSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,10 @@ function prepareSnapString(snap: string, source: string, index: number) {
.split(/\n/g)

const isOneline = lines.length <= 1
const quote = isOneline ? '\'' : '`'
const quote = '`'
if (isOneline)
return `'${lines.join('\n').replace(/'/g, '\\\'')}'`
else
return `${quote}\n${lines.map(i => i ? indentNext + i : '').join('\n').replace(/`/g, '\\`').replace(/\${/g, '\\${')}\n${indent}${quote}`
return `${quote}${lines.join('\n').replace(/`/g, '\\`').replace(/\${/g, '\\${')}${quote}`
return `${quote}\n${lines.map(i => i ? indentNext + i : '').join('\n').replace(/`/g, '\\`').replace(/\${/g, '\\${')}\n${indent}${quote}`
}

const startRegex = /(?:toMatchInlineSnapshot|toThrowErrorMatchingInlineSnapshot)\s*\(\s*(?:\/\*[\S\s]*\*\/\s*|\/\/.*\s+)*\s*[\w_$]*(['"`\)])/m
Expand Down
14 changes: 14 additions & 0 deletions test/core/test/__snapshots__/snapshot.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ exports[`renders mock snapshot 2`] = `
}
`;
exports[`single line snapshot 1`] = `"some string"`;
exports[`single line snapshot 2`] = `"some "string""`;
exports[`single line snapshot 3`] = `"some "string"`;
exports[`single line snapshot 4`] = `"som\`e\` string"`;
exports[`single line snapshot 5`] = `"some string\`"`;
exports[`single line snapshot 6`] = `"some string'"`;
exports[`single line snapshot 7`] = `"some 'string'"`;
exports[`throwing 1`] = `"omega"`;
exports[`throwing 2`] = `"omega"`;
Expand Down
10 changes: 5 additions & 5 deletions test/core/test/inline-snap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ expect('foo').toMatchInlineSnapshot(\`{
replaceInlineSnap(code, s, 40, '"bar\nfoo"')
expect(s.toString()).toMatchInlineSnapshot(`
"
expect('foo').toMatchInlineSnapshot('"bar"')
expect('foo').toMatchInlineSnapshot(\`"bar"\`)
expect('foo').toMatchInlineSnapshot(\`
"bar
foo"
Expand All @@ -37,7 +37,7 @@ ${indent}}\`)
replaceInlineSnap(code, s, 60, '"bar\nfoo"')
expect(s.toString()).toMatchInlineSnapshot(`
"
expect('foo').toMatchInlineSnapshot('"bar"')
expect('foo').toMatchInlineSnapshot(\`"bar"\`)
expect('foo').toMatchInlineSnapshot(\`
"bar
foo"
Expand All @@ -54,7 +54,7 @@ ${indent}}\`)
replaceInlineSnap(code, s, 0, '"bar"')
expect(s.toString()).toMatchInlineSnapshot(`
"
expect('foo').toMatchInlineSnapshot(/* comment1 */'"bar"')
expect('foo').toMatchInlineSnapshot(/* comment1 */\`"bar"\`)
"
`)
})
Expand All @@ -77,7 +77,7 @@ ${indent}}\`)
comment2
*/
'"bar"')
\`"bar"\`)
"
`)
})
Expand All @@ -96,7 +96,7 @@ ${indent}}\`)
expect('foo').toMatchInlineSnapshot(
// comment1
// comment2
'"bar"')
\`"bar"\`)
"
`)
})
Expand Down
22 changes: 22 additions & 0 deletions test/core/test/snapshot.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/quotes */

import { expect, test, vi } from 'vitest'
import { testOutsideInlineSnapshot } from './snapshots-outside'

Expand All @@ -7,6 +9,26 @@ test('object', () => {
}).toMatchSnapshot()
})

test('single line inline', () => {
expect('some string').toMatchInlineSnapshot(`"some string"`)
expect('some "string"').toMatchInlineSnapshot(`"some "string""`)
expect('some "string').toMatchInlineSnapshot(`"some "string"`)
expect('som`e` string').toMatchInlineSnapshot(`"som\`e\` string"`)
expect('some string`').toMatchInlineSnapshot(`"some string\`"`)
expect("some string'").toMatchInlineSnapshot(`"some string'"`)
expect("some 'string'").toMatchInlineSnapshot(`"some 'string'"`)
})

test('single line snapshot', () => {
expect('some string').toMatchSnapshot()
expect('some "string"').toMatchSnapshot()
expect('some "string').toMatchSnapshot()
expect('som`e` string').toMatchSnapshot()
expect('some string`').toMatchSnapshot()
expect("some string'").toMatchSnapshot()
expect("some 'string'").toMatchSnapshot()
})

test('multiline', () => {
expect(`
Hello
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ exports[`custom json report 1`] = `
"initialized with context",
"resolveOptions",
"clean with force",
"onAfterSuiteRun with {\\"coverage\\":{\\"customCoverage\\":\\"Coverage report passed from workers to main thread\\"}}",
"reportCoverage with {\\"allTestsRun\\":true}",
"onAfterSuiteRun with {"coverage":{"customCoverage":"Coverage report passed from workers to main thread"}}",
"reportCoverage with {"allTestsRun":true}",
],
"transformedFiles": [
"<process-cwd>/src/Counter/Counter.component.ts",
Expand Down
2 changes: 1 addition & 1 deletion test/fails/test/__snapshots__/runner.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports[`should fail expect.test.ts > expect.test.ts 1`] = `"AssertionError: exp
exports[`should fail expect-soft.test.ts > expect-soft.test.ts 1`] = `"Error: expect.soft() can only be used inside a test"`;
exports[`should fail expect-unreachable.test.ts > expect-unreachable.test.ts 1`] = `"AssertionError: expected \\"hi\\" not to be reached"`;
exports[`should fail expect-unreachable.test.ts > expect-unreachable.test.ts 1`] = `"AssertionError: expected "hi" not to be reached"`;
exports[`should fail hook-timeout.test.ts > hook-timeout.test.ts 1`] = `"Error: Hook timed out in 10ms."`;
Expand Down

0 comments on commit 1386870

Please sign in to comment.