Skip to content

Commit

Permalink
feat(config): add -c, --config option (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterYinusa authored Jan 18, 2023
1 parent 524480e commit b69f172
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ $ jest-it-up --help
Usage: jest-it-up [options]

Options:
-c, --config <path> path to a Jest config file (default: 'jest.config.js')
-m, --margin <margin> minimum threshold increase (default: 0)
-i, --interactive ask for confirmation before applying changes
-s, --silent do not output messages
Expand Down
1 change: 1 addition & 0 deletions bin/jest-it-up
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { version } = require('../package.json')
const jestItUp = require('../lib')

program
.option('-c, --config <path>', 'path to a Jest config file', 'jest.config.js')
.option('-m, --margin <margin>', 'minimum threshold increase', parseFloat, 0)
.option('-i, --interactive', 'ask for confirmation before applying changes')
.option('-s, --silent', 'do not output messages')
Expand Down
7 changes: 5 additions & 2 deletions lib/__tests__/getData.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs')
const path = require('path')

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

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

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

beforeAll(() => fs.readFileSync.mockReturnValue(reportContents))
beforeEach(() => jest.resetModules())
beforeEach(() => {
jest.resetModules()
jest.spyOn(path, 'dirname').mockReturnValueOnce('/workingDir')
})

it('returns parsed config and report contents', async () => {
jest.mock('../jest.config.js', () => ({ coverageThreshold }), {
Expand Down
16 changes: 13 additions & 3 deletions lib/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ it('runs with with default options', async () => {
)

expect(outputResult).toHaveBeenCalledTimes(1)
expect(outputResult).toHaveBeenCalledWith(false)
expect(outputResult).toHaveBeenCalledWith('/workingDir/jest.config.js', false)
})

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

expect(outputResult).toHaveBeenCalledTimes(1)
expect(outputResult).toHaveBeenCalledWith(false)
expect(outputResult).toHaveBeenCalledWith(
'/workingDir/jest.config.js',
false,
)
} else {
expect(applyChanges).not.toHaveBeenCalled()
expect(outputResult).not.toHaveBeenCalled()
Expand All @@ -112,5 +115,12 @@ it('runs in dry-run mode', async () => {
expect(applyChanges).not.toHaveBeenCalled()

expect(outputResult).toHaveBeenCalledTimes(1)
expect(outputResult).toHaveBeenCalledWith(true)
expect(outputResult).toHaveBeenCalledWith('/workingDir/jest.config.js', true)
})

it('runs with custom config', async () => {
await jestItUp({ config: './customDir/jest.config.js' })

expect(getData).toHaveBeenCalledTimes(1)
expect(getData).toHaveBeenCalledWith('/workingDir/customDir/jest.config.js')
})
9 changes: 5 additions & 4 deletions lib/__tests__/outputResult.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require('ansi-colors').enabled = false

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

const configPath = './jest.config.js'
const lines = []

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

it('output results for updated coverage thresholds', () => {
outputResult(false)
outputResult(configPath, false)

expect(lines.join('\n')).toMatchInlineSnapshot(
`"Done! Please record the changes to jest.config.js."`,
`"Done! Please record the changes to ${configPath}."`,
)
})

it('output results in dry-run mode', () => {
outputResult(true)
outputResult(configPath, true)

expect(lines.join('\n')).toMatchInlineSnapshot(
`"No changes made to jest.config.js. (dry-run mode)"`,
`"No changes made to ${configPath}. (dry-run mode)"`,
)
})
2 changes: 1 addition & 1 deletion lib/getData.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const getData = async configPath => {
coverageDirectory = 'coverage',
} = typeof config === 'function' ? await config() : config
const reportPath = path.resolve(
process.cwd(),
path.dirname(configPath),
coverageDirectory,
'coverage-summary.json',
)
Expand Down
5 changes: 3 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ const outputChanges = require('./outputChanges')
const outputResult = require('./outputResult')

module.exports = async ({
config = 'jest.config.js',
dryRun = false,
interactive = false,
margin = 0,
silent = false,
} = {}) => {
const configPath = path.resolve(process.cwd(), 'jest.config.js')
const configPath = path.resolve(process.cwd(), config)

const { thresholds, coverages } = await getData(configPath)
const newThresholds = getNewThresholds(thresholds, coverages, margin)
Expand All @@ -42,5 +43,5 @@ module.exports = async ({
applyChanges(configPath, data)
}

outputResult(dryRun)
outputResult(configPath, dryRun)
}
6 changes: 3 additions & 3 deletions lib/outputResult.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { yellow } = require('ansi-colors')

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

Expand Down

0 comments on commit b69f172

Please sign in to comment.