Skip to content

Commit

Permalink
test: add some runAll tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Nov 14, 2019
1 parent 357934f commit bbfdbce
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
78 changes: 78 additions & 0 deletions test/__snapshots__/runAll.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,10 +1,88 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`runAll should not skip tasks if there are files 1`] = `
"
LOG Preparing... [started]
LOG Preparing... [completed]
LOG Running tasks... [started]
LOG Running tasks for *.js [started]
LOG echo \\"sample\\" [started]
LOG echo \\"sample\\" [completed]
LOG Running tasks for *.js [completed]
LOG Running tasks... [completed]
LOG Applying unstaged changes... [started]
LOG Applying unstaged changes... [completed]
LOG Cleaning up... [started]
LOG Cleaning up... [completed]"
`;

exports[`runAll should resolve the promise with no files 1`] = `
"
LOG No staged files match any of provided globs."
`;

exports[`runAll should skip applying unstaged modifications if there are errors during linting 1`] = `
"
LOG Preparing... [started]
LOG Preparing... [completed]
LOG Running tasks... [started]
LOG Running tasks for *.js [started]
LOG echo \\"sample\\" [started]
LOG echo \\"sample\\" [failed]
LOG →
LOG Running tasks for *.js [failed]
LOG →
LOG Running tasks... [failed]
LOG Applying unstaged changes... [started]
LOG Applying unstaged changes... [skipped]
LOG → Skipped because of errors from tasks
LOG Restoring original state due to errors... [started]
LOG Restoring original state due to errors... [completed]
LOG Cleaning up... [started]
LOG Cleaning up... [completed]
LOG {
name: 'ListrError',
errors: [
{
privateMsg: '\\\\n\\\\n\\\\n× echo \\"sample\\" found some errors. Please fix them and try committing again.\\\\n\\\\nLinter finished with error',
context: {hasErrors: true}
}
],
context: {hasErrors: true}
}"
`;

exports[`runAll should skip tasks and restore state if terminated 1`] = `
"
LOG Preparing... [started]
LOG Preparing... [completed]
LOG Running tasks... [started]
LOG Running tasks for *.js [started]
LOG echo \\"sample\\" [started]
LOG echo \\"sample\\" [failed]
LOG →
LOG Running tasks for *.js [failed]
LOG →
LOG Running tasks... [failed]
LOG Applying unstaged changes... [started]
LOG Applying unstaged changes... [skipped]
LOG → Skipped because of errors from tasks
LOG Restoring original state due to errors... [started]
LOG Restoring original state due to errors... [completed]
LOG Cleaning up... [started]
LOG Cleaning up... [completed]
LOG {
name: 'ListrError',
errors: [
{
privateMsg: '\\\\n\\\\n\\\\n‼ echo \\"sample\\" was terminated with SIGINT',
context: {hasErrors: true}
}
],
context: {hasErrors: true}
}"
`;

exports[`runAll should use an injected logger 1`] = `
"
LOG No staged files match any of provided globs."
Expand Down
52 changes: 52 additions & 0 deletions test/runAll.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import makeConsoleMock from 'consolemock'
import execa from 'execa'
import normalize from 'normalize-path'

import resolveGitDir from '../src/resolveGitDir'
Expand Down Expand Up @@ -63,4 +64,55 @@ describe('runAll', () => {
await runAll({ config: { '*.js': ['echo "sample"'] }, debug: true }, logger)
expect(logger.printHistory()).toMatchSnapshot()
})

it('should not skip tasks if there are files', async () => {
expect.assertions(1)
getStagedFiles.mockImplementationOnce(async () => ['sample.js'])
await runAll({ config: { '*.js': ['echo "sample"'] } })
expect(console.printHistory()).toMatchSnapshot()
})

it('should skip applying unstaged modifications if there are errors during linting', async () => {
expect.assertions(1)
getStagedFiles.mockImplementationOnce(async () => ['sample.js'])
execa.mockImplementation(() =>
Promise.resolve({
stdout: '',
stderr: 'Linter finished with error',
code: 1,
failed: true,
cmd: 'mock cmd'
})
)

try {
await runAll({ config: { '*.js': ['echo "sample"'] } })
} catch (err) {
console.log(err)
}
expect(console.printHistory()).toMatchSnapshot()
})

it('should skip tasks and restore state if terminated', async () => {
expect.assertions(1)
getStagedFiles.mockImplementationOnce(async () => ['sample.js'])
execa.mockImplementation(() =>
Promise.resolve({
stdout: '',
stderr: '',
code: 0,
failed: false,
killed: true,
signal: 'SIGINT',
cmd: 'mock cmd'
})
)

try {
await runAll({ config: { '*.js': ['echo "sample"'] } })
} catch (err) {
console.log(err)
}
expect(console.printHistory()).toMatchSnapshot()
})
})

0 comments on commit bbfdbce

Please sign in to comment.