Skip to content

Commit b69f172

Browse files
authored
feat(config): add -c, --config option (#12)
1 parent 524480e commit b69f172

File tree

8 files changed

+32
-15
lines changed

8 files changed

+32
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ $ jest-it-up --help
6363
Usage: jest-it-up [options]
6464

6565
Options:
66+
-c, --config <path> path to a Jest config file (default: 'jest.config.js')
6667
-m, --margin <margin> minimum threshold increase (default: 0)
6768
-i, --interactive ask for confirmation before applying changes
6869
-s, --silent do not output messages

bin/jest-it-up

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { version } = require('../package.json')
55
const jestItUp = require('../lib')
66

77
program
8+
.option('-c, --config <path>', 'path to a Jest config file', 'jest.config.js')
89
.option('-m, --margin <margin>', 'minimum threshold increase', parseFloat, 0)
910
.option('-i, --interactive', 'ask for confirmation before applying changes')
1011
.option('-s, --silent', 'do not output messages')

lib/__tests__/getData.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const fs = require('fs')
2+
const path = require('path')
23

34
const getData = require('../getData')
45

@@ -21,10 +22,12 @@ const reportContents = JSON.stringify({
2122
})
2223

2324
jest.mock('fs')
24-
jest.spyOn(process, 'cwd').mockImplementation(() => '/workingDir')
2525

2626
beforeAll(() => fs.readFileSync.mockReturnValue(reportContents))
27-
beforeEach(() => jest.resetModules())
27+
beforeEach(() => {
28+
jest.resetModules()
29+
jest.spyOn(path, 'dirname').mockReturnValueOnce('/workingDir')
30+
})
2831

2932
it('returns parsed config and report contents', async () => {
3033
jest.mock('../jest.config.js', () => ({ coverageThreshold }), {

lib/__tests__/index.test.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ it('runs with with default options', async () => {
5252
)
5353

5454
expect(outputResult).toHaveBeenCalledTimes(1)
55-
expect(outputResult).toHaveBeenCalledWith(false)
55+
expect(outputResult).toHaveBeenCalledWith('/workingDir/jest.config.js', false)
5656
})
5757

5858
it('returns early if there are no changes', async () => {
@@ -89,7 +89,10 @@ it.each([true, false])(
8989
)
9090

9191
expect(outputResult).toHaveBeenCalledTimes(1)
92-
expect(outputResult).toHaveBeenCalledWith(false)
92+
expect(outputResult).toHaveBeenCalledWith(
93+
'/workingDir/jest.config.js',
94+
false,
95+
)
9396
} else {
9497
expect(applyChanges).not.toHaveBeenCalled()
9598
expect(outputResult).not.toHaveBeenCalled()
@@ -112,5 +115,12 @@ it('runs in dry-run mode', async () => {
112115
expect(applyChanges).not.toHaveBeenCalled()
113116

114117
expect(outputResult).toHaveBeenCalledTimes(1)
115-
expect(outputResult).toHaveBeenCalledWith(true)
118+
expect(outputResult).toHaveBeenCalledWith('/workingDir/jest.config.js', true)
119+
})
120+
121+
it('runs with custom config', async () => {
122+
await jestItUp({ config: './customDir/jest.config.js' })
123+
124+
expect(getData).toHaveBeenCalledTimes(1)
125+
expect(getData).toHaveBeenCalledWith('/workingDir/customDir/jest.config.js')
116126
})

lib/__tests__/outputResult.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ require('ansi-colors').enabled = false
22

33
const outputResult = require('../outputResult')
44

5+
const configPath = './jest.config.js'
56
const lines = []
67

78
jest.spyOn(console, 'log').mockImplementation(message => lines.push(message))
@@ -11,17 +12,17 @@ afterEach(() => {
1112
})
1213

1314
it('output results for updated coverage thresholds', () => {
14-
outputResult(false)
15+
outputResult(configPath, false)
1516

1617
expect(lines.join('\n')).toMatchInlineSnapshot(
17-
`"Done! Please record the changes to jest.config.js."`,
18+
`"Done! Please record the changes to ${configPath}."`,
1819
)
1920
})
2021

2122
it('output results in dry-run mode', () => {
22-
outputResult(true)
23+
outputResult(configPath, true)
2324

2425
expect(lines.join('\n')).toMatchInlineSnapshot(
25-
`"No changes made to jest.config.js. (dry-run mode)"`,
26+
`"No changes made to ${configPath}. (dry-run mode)"`,
2627
)
2728
})

lib/getData.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const getData = async configPath => {
88
coverageDirectory = 'coverage',
99
} = typeof config === 'function' ? await config() : config
1010
const reportPath = path.resolve(
11-
process.cwd(),
11+
path.dirname(configPath),
1212
coverageDirectory,
1313
'coverage-summary.json',
1414
)

lib/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ const outputChanges = require('./outputChanges')
1010
const outputResult = require('./outputResult')
1111

1212
module.exports = async ({
13+
config = 'jest.config.js',
1314
dryRun = false,
1415
interactive = false,
1516
margin = 0,
1617
silent = false,
1718
} = {}) => {
18-
const configPath = path.resolve(process.cwd(), 'jest.config.js')
19+
const configPath = path.resolve(process.cwd(), config)
1920

2021
const { thresholds, coverages } = await getData(configPath)
2122
const newThresholds = getNewThresholds(thresholds, coverages, margin)
@@ -42,5 +43,5 @@ module.exports = async ({
4243
applyChanges(configPath, data)
4344
}
4445

45-
outputResult(dryRun)
46+
outputResult(configPath, dryRun)
4647
}

lib/outputResult.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const { yellow } = require('ansi-colors')
22

3-
const outputResult = dryRun => {
3+
const outputResult = (configPath, dryRun) => {
44
console.log(
55
dryRun
6-
? `No changes made to ${yellow('jest.config.js')}. (dry-run mode)`
7-
: `Done! Please record the changes to ${yellow('jest.config.js')}.`,
6+
? `No changes made to ${yellow(configPath)}. (dry-run mode)`
7+
: `Done! Please record the changes to ${yellow(configPath)}.`,
88
)
99
}
1010

0 commit comments

Comments
 (0)