Skip to content

Commit ac7cc63

Browse files
committed
Exit with status 1 on tests failure
1 parent c37f8b0 commit ac7cc63

File tree

5 files changed

+59
-19
lines changed

5 files changed

+59
-19
lines changed

__mocks__/child_process.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export const spawn = jest.fn()
2-
export const spawnSync = jest.fn()
2+
export const spawnSync = jest.fn().mockReturnValue({ status: 0 })
33

44
export default { spawn, spawnSync }

__test__/runner/index.test.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,36 @@ describe('start', () => {
4646
})
4747

4848
describe('test', () => {
49-
it('calls test runner without options', () => {
50-
test()
49+
it('calls test runner without options', async () => {
50+
await test()
5151
expect(TestRunner.run).toHaveBeenCalled()
5252
})
5353

54-
it('calls test runner with path', () => {
55-
test('test/index.test.js')
54+
it('calls test runner with path', async () => {
55+
await test('test/index.test.js')
5656
expect(TestRunner.run).toHaveBeenCalledWith('test/index.test.js')
5757
})
5858

59-
it('restores cursor', () => {
60-
test()
59+
it('restores cursor', async () => {
60+
await test()
6161
expect(restoreCursorOnExit).toHaveBeenCalled()
6262
})
63+
64+
it('exits with success status', async () => {
65+
await test()
66+
expect(process.exit).toHaveBeenCalledWith(0)
67+
})
68+
69+
describe('running test fails', () => {
70+
beforeEach(() => {
71+
TestRunner.__setError(Error)
72+
})
73+
74+
it('exits with error status', async () => {
75+
await test()
76+
expect(process.exit).toHaveBeenCalledWith(1)
77+
})
78+
})
6379
})
6480

6581
describe('package', () => {

src/__mocks__/test_runner.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
export const TestRunner = { run: jest.fn().mockResolvedValue(true) }
1+
let testError = null
2+
3+
export const TestRunner = {
4+
__setError: error => {
5+
testError = error
6+
},
7+
run: jest.fn(() => {
8+
if (testError) {
9+
return Promise.reject(testError)
10+
} else {
11+
return Promise.resolve()
12+
}
13+
})
14+
}

src/runner/index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,22 @@ export const start = command => {
2323
} else if (command.inspectBrk) {
2424
params.options = ['--inspect-brk=' + command.inspectBrk]
2525
}
26-
Starter.run(params)
26+
return Starter.run(params)
2727
}
2828

2929
export const pack = (platform, command) => {
3030
restoreCursorOnExit()
31-
new Packager(platform, 'production', !!command.publish).build()
31+
return new Packager(platform, 'production', !!command.publish).build()
3232
}
3333

3434
export const test = (path) => {
3535
restoreCursorOnExit()
36-
TestRunner.run(path)
36+
return TestRunner.run(path)
37+
.then(() => process.exit(0))
38+
.catch(() => process.exit(1))
3739
}
3840

3941
export const clear = () => {
4042
restoreCursorOnExit()
41-
Cleaner.run()
43+
return Cleaner.run()
4244
}

src/test_runner/index.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import { log } from 'utils/logger'
88
const run = path => {
99
Checker.ensure()
1010
if (!path || path.match(/features/)) {
11-
buildAndRun(path)
11+
return buildAndRun(path)
1212
} else {
1313
log(chalk.bold('Running test suite...'))
14-
runUnitTests(path)
14+
return runUnitTests(path)
1515
}
1616
}
1717

@@ -26,25 +26,34 @@ const buildAndRun = path => {
2626
}
2727

2828
const runFeatureTests = path => {
29-
spawnSync('npx', ['jest', '-i', path], {
29+
const result = spawnSync('npx', ['jest', '-i', path], {
3030
env: nodeEnv('test'),
3131
shell: true,
3232
stdio: 'inherit'
3333
})
34+
return buildPromise(result.status)
3435
}
3536

3637
const runUnitTests = path => {
37-
spawnSync('npx', ['jest', path], {
38+
const result = spawnSync('npx', ['jest', path], {
3839
env: nodeEnv('test'),
3940
shell: true,
4041
stdio: 'inherit'
4142
})
43+
return buildPromise(result.status)
4244
}
4345

4446
const runAllTests = () => {
45-
runUnitTests('./test/units')
46-
runFeatureTests('./test/features')
47-
return Promise.resolve()
47+
return Promise.all([
48+
runUnitTests('./test/units'),
49+
runFeatureTests('./test/features')
50+
])
51+
}
52+
53+
const buildPromise = status => {
54+
return (status === 0)
55+
? Promise.resolve()
56+
: Promise.reject(Error('Some tests failed'))
4857
}
4958

5059
export const TestRunner = { run }

0 commit comments

Comments
 (0)