Skip to content

Commit

Permalink
tests: Switch some tests to inline snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Okonetchnikov committed Oct 21, 2018
1 parent 7192141 commit 815a2ab
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 98 deletions.
21 changes: 0 additions & 21 deletions test/__snapshots__/resolveTaskFn-chunked.spec.js.snap

This file was deleted.

16 changes: 0 additions & 16 deletions test/__snapshots__/resolveTaskFn.spec.js.snap

This file was deleted.

140 changes: 84 additions & 56 deletions test/gitStash.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,64 +98,74 @@ describe('git', () => {

describe('gitStashSave/gitStashPop', () => {
it('should stash and restore WC state without a commit', async () => {
expect(await gitStatus()).toContain(' M test.css')
expect(await gitStatus()).toContain(' M test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
M test.js"
`)

// Add test.js to index
await gitflow.execGit(['add', 'test.js'], gitOpts)
expect(await gitStatus()).toContain(' M test.css')
expect(await gitStatus()).toContain('M test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
M test.js"
`)

// Stashing files
await gitflow.gitStashSave(gitOpts)
expect(await gitStatus()).not.toContain(' M test.css')
expect(await gitStatus()).toContain('M test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`"M test.js"`)

// Restoring state
await gitflow.gitStashPop(gitOpts)
expect(await gitStatus()).toContain(' M test.css')
expect(await gitStatus()).toContain('M test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
M test.js"
`)
})

it('should not restore deleted files', async () => {
// Delete test.js
await gitflow.execGit(['checkout', 'test.js'], gitOpts)
await gitflow.execGit(['rm', 'test.js'], gitOpts)
expect(await gitStatus()).toContain(' M test.css')
expect(await gitStatus()).toContain('D test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
D test.js"
`)

// Stashing files
await gitflow.gitStashSave(gitOpts)
expect(await gitStatus()).not.toContain(' M test.css')
expect(await gitStatus()).toContain('D test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`"D test.js"`)

// Restoring state
await gitflow.gitStashPop(gitOpts)
expect(await gitStatus()).toContain(' M test.css')
expect(await gitStatus()).toContain('D test.js')
expect(await gitStatus()).not.toContain('?? test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
D test.js"
`)
})

it('should drop hooks fixes when aborted', async () => {
expect(await gitStatus()).toContain(' M test.css')
expect(await gitStatus()).toContain(' M test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
M test.js"
`)

// Add test.js to index
await gitflow.execGit(['add', 'test.js'], gitOpts)
// Save diff for the reference
const initialIndex = await gitflow.execGit(['diff', '--cached'], gitOpts)

// Expect test.js is in index
expect(await gitStatus()).toContain(' M test.css')
expect(await gitStatus()).toContain('M test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
M test.js"
`)
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)

// Stashing state
await gitflow.gitStashSave(gitOpts)

// Only index should remain
expect(await gitStatus()).not.toContain(' M test.css')
expect(await gitStatus()).toContain('M test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`"M test.js"`)
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)

// Do additional edits (imitate eslint --fix)
Expand All @@ -168,25 +178,28 @@ describe('git', () => {
await fsp.writeFile(path.join(wcDirPath, 'test.js'), eslintContent)

// Expect both indexed and modified state on one file
expect(await gitStatus()).not.toContain(' M test.css')
expect(await gitStatus()).toContain('MM test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`"MM test.js"`)
// and index isn't modified
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)

// Restoring state
await gitflow.gitStashPop(gitOpts)
// Expect stashed files to be back
expect(await gitStatus()).toContain(' M test.css')
expect(await gitStatus()).toContain('M test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
M test.js"
`)
// and modification are gone
expect(await readFile('test.js')).toEqual(initialContent)
// Expect no modifications in index
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)
})

it('should drop hooks fixes and revert to user modifications when aborted', async () => {
expect(await gitStatus()).toContain(' M test.css')
expect(await gitStatus()).toContain(' M test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
M test.js"
`)

// Add test.js to index
await gitflow.execGit(['add', 'test.js'], gitOpts)
Expand All @@ -201,16 +214,17 @@ describe('git', () => {
await fsp.writeFile(path.join(wcDirPath, 'test.js'), userContent)

// Expect test.js is in both index and modified
expect(await gitStatus()).toContain(' M test.css')
expect(await gitStatus()).toContain('MM test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
MM test.js"
`)
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)

// Stashing state
await gitflow.gitStashSave(gitOpts)

// Only index should remain
expect(await gitStatus()).not.toContain(' M test.css')
expect(await gitStatus()).toContain('M test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`"M test.js"`)

// Do additional edits (imitate eslint --fix)
await fsp.writeFile(
Expand All @@ -221,41 +235,45 @@ describe('git', () => {
)

// Expect both indexed and modified state on one file
expect(await gitStatus()).toContain('MM test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`"MM test.js"`)
// and index isn't modified
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)

// Restoring state
await gitflow.gitStashPop(gitOpts)

// Expect stashed files to be back
expect(await gitStatus()).toContain(' M test.css')
expect(await gitStatus()).toContain('MM test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
MM test.js"
`)
// and content is back to user modifications
expect(await readFile('test.js')).toEqual(userContent)
// Expect no modifications in index
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)
})

it('should add hooks fixes to index when not aborted', async () => {
expect(await gitStatus()).toContain(' M test.css')
expect(await gitStatus()).toContain(' M test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
M test.js"
`)

// Add test.js to index
await gitflow.execGit(['add', 'test.js'], gitOpts)
// Save diff for the reference
const initialIndex = await gitflow.execGit(['diff', '--cached'], gitOpts)

expect(await gitStatus()).toContain(' M test.css')
expect(await gitStatus()).toContain('M test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
M test.js"
`)
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)

// Stashing state
await gitflow.gitStashSave(gitOpts)

// Only index should remain
expect(await gitStatus()).not.toContain(' M test.css')
expect(await gitStatus()).toContain('M test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`"M test.js"`)

// Do additional edits (imitate eslint --fix)
const newContent = `module.exports = {
Expand All @@ -268,7 +286,7 @@ describe('git', () => {
const newIndex = await gitflow.execGit(['diff', '--cached'], gitOpts)

// Expect only index changes
expect(await gitStatus()).toContain('M test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`"M test.js"`)
// and index is modified
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).not.toEqual(initialIndex)
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(newIndex)
Expand All @@ -277,17 +295,21 @@ describe('git', () => {
await gitflow.gitStashPop(gitOpts)

// Expect stashed files to be back
expect(await gitStatus()).toContain(' M test.css')
expect(await gitStatus()).toContain('M test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
M test.js"
`)
// and content keeps linter modifications
expect(await readFile('test.js')).toEqual(newContent)
// Expect modifications in index
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(newIndex)
})

it('should add hooks fixes to index and keep user modifications when not aborted', async () => {
expect(await gitStatus()).toContain(' M test.css')
expect(await gitStatus()).toContain(' M test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
M test.js"
`)

// Add test.js to index
await gitflow.execGit(['add', 'test.js'], gitOpts)
Expand All @@ -302,16 +324,17 @@ describe('git', () => {
await fsp.writeFile(path.join(wcDirPath, 'test.js'), userContent)

// Expect test.js is in both index and modified
expect(await gitStatus()).toContain(' M test.css')
expect(await gitStatus()).toContain('MM test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
MM test.js"
`)
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)

// Stashing state
await gitflow.gitStashSave(gitOpts)

// Only index should remain
expect(await gitStatus()).not.toContain(' M test.css')
expect(await gitStatus()).toContain('M test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`"M test.js"`)
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)

// Do additional edits (imitate eslint --fix)
Expand All @@ -334,8 +357,11 @@ describe('git', () => {
await gitflow.gitStashPop(gitOpts)

// Expect stashed files to be back
expect(await gitStatus()).toContain(' M test.css')
expect(await gitStatus()).toContain('MM test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`
" M test.css
MM test.js
?? test.js.rej"
`)
// and content is back to user modifications
expect(await readFile('test.js')).toEqual(userContent)
// Expect formatting changes in the index
Expand Down Expand Up @@ -381,16 +407,15 @@ describe('git', () => {
}`
)

expect(await gitStatus()).toContain('MM test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`"MM test.js"`)
// Save diff for the reference
const initialIndex = await gitflow.execGit(['diff', '--cached'], gitOpts)

// Stashing state
await gitflow.gitStashSave(gitOpts)

// Only index should remain
expect(await gitStatus()).not.toContain(' M test.css')
expect(await gitStatus()).toContain('M test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`"M test.js"`)
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(initialIndex)

// Imitate running prettier on the version from the index
Expand All @@ -415,7 +440,10 @@ describe('git', () => {
await gitflow.gitStashPop(gitOpts)

// Expect stashed files to be back
expect(await gitStatus()).toContain('MM test.js')
expect(await gitStatus()).toMatchInlineSnapshot(`
"MM test.js
?? test.js.rej"
`)
// and all lint-staged modifications to be gone
expect(await gitflow.execGit(['diff', '--cached'], gitOpts)).toEqual(indexAfterEslint)
expect(await readFile('test.js')).toEqual(`module.exports = {
Expand Down
20 changes: 17 additions & 3 deletions test/resolveTaskFn-chunked.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ describe('resolveTaskFn', () => {
try {
await taskFn()
} catch (err) {
expect(err.privateMsg).toMatchSnapshot()
expect(err.privateMsg).toMatchInlineSnapshot(`
"
× mock-fail-linter found some errors. Please fix them and try committing again.
Mock error"
`)
}
})

Expand All @@ -125,7 +131,12 @@ describe('resolveTaskFn', () => {
try {
await taskFn()
} catch (err) {
expect(err.privateMsg).toMatchSnapshot()
expect(err.privateMsg).toMatchInlineSnapshot(`
"
‼ mock-killed-linter was terminated with SIGINT"
`)
}
})

Expand All @@ -137,7 +148,10 @@ describe('resolveTaskFn', () => {
try {
await taskFn()
} catch (err) {
expect(err.message).toMatchSnapshot()
expect(err.message).toMatchInlineSnapshot(`
"× test got an unexpected error.
Unexpected Error"
`)
}
})
})
Expand Down

0 comments on commit 815a2ab

Please sign in to comment.