Skip to content

Commit 6530a73

Browse files
committed
Add tests for cleaner
1 parent b3c1da5 commit 6530a73

File tree

9 files changed

+64
-18
lines changed

9 files changed

+64
-18
lines changed

__mocks__/chalk.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const cyan = jest.fn(value => value)
2+
3+
export default { cyan }

__mocks__/del.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const del = jest.fn()
2+
3+
export default del

__mocks__/ora.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const start = jest.fn()
2+
const succeed = jest.fn()
3+
4+
const ora = jest.fn(() => {
5+
return {
6+
start: start,
7+
succeed: succeed
8+
}
9+
})
10+
ora.start = start
11+
ora.succeed = succeed
12+
13+
export default ora

__test__/cleaner/index.test.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import Cleaner from 'cleaner'
2+
3+
import del from 'del'
4+
import chalk from 'chalk'
5+
import ora from 'ora'
6+
7+
jest.unmock('cleaner')
8+
9+
describe('Cleaner', () => {
10+
beforeEach(() => {
11+
const cleaner = new Cleaner()
12+
cleaner.run()
13+
})
14+
15+
it('shows spinner', () => {
16+
expect(ora).toHaveBeenCalledWith({
17+
color: 'cyan',
18+
text: 'Cleaning app directory'
19+
})
20+
expect(ora.start).toHaveBeenCalled()
21+
})
22+
23+
it('clears directories', () => {
24+
expect(del).toHaveBeenNthCalledWith(1, [
25+
'/test/home/builds/**',
26+
'/test/home/packages/**',
27+
'/test/home/.tmp/**'
28+
])
29+
})
30+
31+
it('stops spinner with success message', () => {
32+
expect(chalk.cyan).toHaveBeenCalledWith('Cleaned app directory')
33+
expect(ora.succeed).toHaveBeenCalledWith('Cleaned app directory')
34+
})
35+
})

__test__/index.test.js

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import commander from 'commander'
22

33
import { perform } from 'index'
44

5-
jest.mock('commander')
6-
75
describe('bozon cli', () => {
86
beforeEach(() => {
97
perform()

__test__/runner/index.test.js

-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ import Cleaner from 'cleaner'
66
import Packager from 'packager'
77
import TestRunner from 'test_runner'
88

9-
jest.mock('generator')
10-
jest.mock('starter')
11-
jest.mock('cleaner')
12-
jest.mock('packager')
13-
jest.mock('test_runner')
14-
159
describe('create', () => {
1610
it('passes app name to generator', () => {
1711
create('myapp')

__test__/setup.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
process.cwd = jest.fn().mockReturnValue('/test/home')

jest.config.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module.exports = {
55
// All imported modules in your tests should be mocked automatically
6-
// automock: true,
6+
automock: false,
77

88
// Stop running tests after `n` failures
99
// bail: 0,
@@ -64,10 +64,7 @@ module.exports = {
6464
// maxWorkers: "50%",
6565

6666
// An array of directory names to be searched recursively up from the requiring module's location
67-
moduleDirectories: [
68-
'node_modules',
69-
'src'
70-
],
67+
moduleDirectories: ['node_modules', 'src'],
7168

7269
// An array of file extensions your modules use
7370
// moduleFileExtensions: [
@@ -124,7 +121,7 @@ module.exports = {
124121
// runner: "jest-runner",
125122

126123
// The paths to modules that run some code to configure or set up the testing environment before each test
127-
// setupFiles: [],
124+
setupFiles: ['<rootDir>/__test__/setup.js'],
128125

129126
// A list of paths to modules that run some code to configure or set up the testing framework before each test
130127
// setupFilesAfterEnv: [],
@@ -186,4 +183,4 @@ module.exports = {
186183

187184
// Whether to use watchman for file crawling
188185
// watchman: true,
189-
};
186+
}

src/cleaner/index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ export default class Cleaner {
1313

1414
run() {
1515
this.spinner.start()
16-
del([path.join(process.cwd(), 'builds', '**')])
17-
del([path.join(process.cwd(), 'packages', '**')])
18-
del([path.join(process.cwd(), '.tmp', '**')])
16+
del([
17+
path.join(process.cwd(), 'builds', '**'),
18+
path.join(process.cwd(), 'packages', '**'),
19+
path.join(process.cwd(), '.tmp', '**')
20+
])
1921
this.spinner.succeed(chalk.cyan('Cleaned app directory'))
2022
}
2123
}

0 commit comments

Comments
 (0)