Skip to content

Commit ebf1767

Browse files
Andrey Lozhkinokonet
Andrey Lozhkin
authored andcommitted
test: Add full coverage for runAll.js (#309)
* Add full coverage for runAll.js * Add console mock to runAll * Add required changes * Fix the "no files" test * Update listr SamVerschueren/listr-verbose-renderer#4 (comment) * Disable dates in console output * Update snapshot * Clear the console mock after the tests are run * Remove clearing implementation after each test * Refactor the no-files case * Refactor the not-skip case
1 parent 331d3c5 commit ebf1767

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"execa": "^0.8.0",
3535
"is-glob": "^4.0.0",
3636
"jest-validate": "^21.1.0",
37-
"listr": "^0.12.0",
37+
"listr": "^0.13.0",
3838
"lodash": "^4.17.4",
3939
"log-symbols": "^2.0.0",
4040
"minimatch": "^3.0.0",

src/runAll.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module.exports = function runAll(scripts, config) {
3838
new Listr(runScript(task.commands, task.fileList, scripts, config), {
3939
// In sub-tasks we don't want to run concurrently
4040
// and we want to abort on errors
41+
dateFormat: false,
4142
concurrent: false,
4243
exitOnError: true
4344
}),
@@ -51,6 +52,7 @@ module.exports = function runAll(scripts, config) {
5152

5253
if (tasks.length) {
5354
return new Listr(tasks, {
55+
dateFormat: false,
5456
concurrent,
5557
renderer,
5658
exitOnError: !concurrent // Wait for all errors when running concurrently
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`runAll should not skip tasks if there are files 1`] = `
4+
"
5+
LOG Running tasks for *.js [started]
6+
LOG echo \\"sample\\" [started]
7+
LOG echo \\"sample\\" [completed]
8+
LOG Running tasks for *.js [completed]"
9+
`;
10+
11+
exports[`runAll should resolve the promise with no files 1`] = `
12+
"
13+
LOG Running tasks for *.js [started]
14+
LOG Running tasks for *.js [skipped]
15+
LOG → No staged files match *.js"
16+
`;
17+
318
exports[`runAll should throw when invalid config is provided 1`] = `"Invalid config provided to runAll! Use getConfig instead."`;
419

520
exports[`runAll should throw when invalid config is provided 2`] = `"Invalid config provided to runAll! Use getConfig instead."`;

test/runAll.spec.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { makeConsoleMock } from 'consolemock'
12
import sgfMock from 'staged-git-files'
23
import { getConfig } from '../src/getConfig'
34
import runAll from '../src/runAll'
@@ -9,10 +10,14 @@ sgfMock.mockImplementation((params, callback) => {
910
})
1011

1112
const scripts = { mytask: 'echo "Running task"' }
13+
const globalConsoleTemp = global.console
1214

1315
describe('runAll', () => {
14-
afterEach(() => {
15-
sgfMock.mockClear()
16+
beforeEach(() => {
17+
global.console = makeConsoleMock()
18+
})
19+
afterAll(() => {
20+
global.console = globalConsoleTemp
1621
})
1722
it('should throw when invalid config is provided', () => {
1823
expect(() => runAll(scripts, {})).toThrowErrorMatchingSnapshot()
@@ -35,8 +40,21 @@ describe('runAll', () => {
3540
return expect(runAll(scripts, getConfig({}))).resolves.toEqual('No tasks to run.')
3641
})
3742

43+
it('should resolve the promise with no files', async () => {
44+
await runAll(scripts, getConfig({ linters: { '*.js': ['echo "sample"'] } }))
45+
expect(console.printHistory()).toMatchSnapshot()
46+
})
47+
48+
it('should not skip tasks if there are files', async () => {
49+
sgfMock.mockImplementationOnce((params, callback) => {
50+
callback(null, [{ filename: 'sample.js', status: 'sample' }])
51+
})
52+
await runAll(scripts, getConfig({ linters: { '*.js': ['echo "sample"'] } }))
53+
expect(console.printHistory()).toMatchSnapshot()
54+
})
55+
3856
it('should reject the promise when staged-git-files errors', () => {
39-
sgfMock.mockImplementation((params, callback) => {
57+
sgfMock.mockImplementationOnce((params, callback) => {
4058
callback('test', undefined)
4159
})
4260
expect.assertions(1)

0 commit comments

Comments
 (0)