Skip to content

Commit

Permalink
feat(interactive): add -i, --interactive option (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbardini authored Jun 20, 2020
1 parent 33b47f9 commit 56d2541
Show file tree
Hide file tree
Showing 16 changed files with 516 additions and 91 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ Usage: jest-it-up [options]

Options:
-m, --margin <margin> minimum threshold increase (default: 0)
-i, --interactive ask for confirmation before applying changes
-s, --silent do not output messages
-d, --dry-run process but do not write files
-d, --dry-run process but do not change files
-v, --version output the version number
-h, --help display help for command
```
3 changes: 2 additions & 1 deletion bin/jest-it-up
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ const jestItUp = require('../lib')

program
.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')
.option('-d, --dry-run', 'process but do not write files')
.option('-d, --dry-run', 'process but do not change files')
.version(version, '-v, --version')
.parse(process.argv)

Expand Down
Binary file modified demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
161 changes: 161 additions & 0 deletions demo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
config:
command: bash
cwd: /tmp/example
env:
recording: true
cols: 80
rows: 24
repeat: 0
quality: 100
frameDelay: auto
maxIdleTime: 2000
frameBox:
type: solid
title: null
style: []
watermark:
imagePath: null
style:
position: absolute
right: 15px
bottom: 15px
width: 100px
opacity: 0.9
cursorStyle: block
fontFamily: "Menlo, Monaco, Lucida Console, Ubuntu Mono, Monospace"
fontSize: 12
lineHeight: 1
letterSpacing: 0
theme:
background: "#1d1d1d"
foreground: "#afafaf"
cursor: "#c7c7c7"
black: "#232628"
red: "#fc4384"
green: "#b3e33b"
yellow: "#ffa727"
blue: "#75dff2"
magenta: "#ae89fe"
cyan: "#708387"
white: "#d5d5d0"
brightBlack: "#626566"
brightRed: "#ff7fac"
brightGreen: "#c8ed71"
brightYellow: "#ebdf86"
brightBlue: "#75dff2"
brightMagenta: "#ae89fe"
brightCyan: "#b1c6ca"
brightWhite: "#f9f9f4"
records:
- delay: 0
content: "$ "
- delay: 100
content: 'n'
- delay: 100
content: p
- delay: 100
content: m
- delay: 100
content: ' '
- delay: 100
content: t
- delay: 100
content: e
- delay: 100
content: s
- delay: 100
content: t
- delay: 500
content: "\r\n"
- delay: 344
content: "\r\n> [email protected] test /tmp/example\r\n> jest --coverage\r\n\r\n"
- delay: 1482
content: "\e[1m\e[2mDetermining test suites to run...\e[22m\e[22m"
- delay: 124
content: "\e[999D\e[K\r\n\r\e[K\r\e[1A\e[999D\e[K\r\n\r\e[K\r\e[1A"
- delay: 7
content: "\r\n\e[0m\e[7m\e[33m\e[1m RUNS \e[22m\e[39m\e[27m\e[0m \e[2mlib/\e[22m\e[1ma.test.js\e[22m\r\n"
- delay: 767
content: "\r\e[K\r\e[1A\r\e[K\r\e[1A\e[0m\e[7m\e[1m\e[32m PASS \e[39m\e[22m\e[27m\e[0m \e]8;;file:///tmp/example/lib/a.test.js\a\e[2mlib/\e[22m\e[1ma.test.js\e[22m\e]8;;\a\r\n\r\n\e[0m\e[7m\e[33m\e[1m RUNS \e[22m\e[39m\e[27m\e[0m \e[2mlib/\e[22m\e[1ma.test.js\e[22m\r\n\r\e[K\r\e[1A\r\e[K\r\e[1A\r\n\e[0m\e[7m\e[33m\e[1m RUNS \e[22m\e[39m\e[27m\e[0m \e[2mlib/\e[22m\e[1ma.test.js\e[22m\r\n\r\e[K\r\e[1A\r\e[K\r\e[1A\r\n\e[0m\e[7m\e[33m\e[1m RUNS \e[22m\e[39m\e[27m\e[0m \e[2mlib/\e[22m\e[1mb.test.js\e[22m\r\n"
- delay: 189
content: "\r\e[K\r\e[1A\r\e[K\r\e[1A\r\n\e[0m\e[7m\e[33m\e[1m RUNS \e[22m\e[39m\e[27m\e[0m \e[2mlib/\e[22m\e[1mb.test.js\e[22m\r\n"
- delay: 7
content: "\r\e[K\r\e[1A\r\e[K\r\e[1A\e[0m\e[7m\e[1m\e[32m PASS \e[39m\e[22m\e[27m\e[0m \e]8;;file:///tmp/example/lib/b.test.js\a\e[2mlib/\e[22m\e[1mb.test.js\e[22m\e]8;;\a\r\n\r\n\e[0m\e[7m\e[33m\e[1m RUNS \e[22m\e[39m\e[27m\e[0m \e[2mlib/\e[22m\e[1mb.test.js\e[22m\r\n\r\e[K\r\e[1A\r\e[K\r\e[1A\r\n\e[0m\e[7m\e[33m\e[1m RUNS \e[22m\e[39m\e[27m\e[0m \e[2mlib/\e[22m\e[1mc.test.js\e[22m\r\n"
- delay: 202
content: "\r\e[K\r\e[1A\r\e[K\r\e[1A\r\n\e[0m\e[7m\e[33m\e[1m RUNS \e[22m\e[39m\e[27m\e[0m \e[2mlib/\e[22m\e[1mc.test.js\e[22m\r\n\r\e[K\r\e[1A\r\e[K\r\e[1A\e[0m\e[7m\e[1m\e[32m PASS \e[39m\e[22m\e[27m\e[0m \e]8;;file:///tmp/example/lib/c.test.js\a\e[2mlib/\e[22m\e[1mc.test.js\e[22m\e]8;;\a\r\n\r\n\e[0m\e[7m\e[33m\e[1m RUNS \e[22m\e[39m\e[27m\e[0m \e[2mlib/\e[22m\e[1mc.test.js\e[22m\r\n\r\e[K\r\e[1A\r\e[K\r\e[1A\r\n\e[0m\e[7m\e[33m\e[1m RUNS \e[22m\e[39m\e[27m\e[0m \e[2mlib/\e[22m\e[1md.test.js\e[22m\r\n"
- delay: 189
content: "\r\e[K\r\e[1A\r\e[K\r\e[1A\r\n\e[0m\e[7m\e[33m\e[1m RUNS \e[22m\e[39m\e[27m\e[0m \e[2mlib/\e[22m\e[1md.test.js\e[22m\r\n\r\e[K\r\e[1A\r\e[K\r\e[1A\e[0m\e[7m\e[1m\e[32m PASS \e[39m\e[22m\e[27m\e[0m \e]8;;file:///tmp/example/lib/d.test.js\a\e[2mlib/\e[22m\e[1md.test.js\e[22m\e]8;;\a\r\n\r\n\e[0m\e[7m\e[33m\e[1m RUNS \e[22m\e[39m\e[27m\e[0m \e[2mlib/\e[22m\e[1md.test.js\e[22m\r\n\r\e[K\r\e[1A\r\e[K\r\e[1A\r\n\e[0m\e[7m\e[33m\e[1m RUNS \e[22m\e[39m\e[27m\e[0m \e[2mlib/\e[22m\e[1me.test.js\e[22m\r\n"
- delay: 183
content: "\r\e[K\r\e[1A\r\e[K\r\e[1A\r\n\e[0m\e[7m\e[33m\e[1m RUNS \e[22m\e[39m\e[27m\e[0m \e[2mlib/\e[22m\e[1me.test.js\e[22m\r\n\r\e[K\r\e[1A\r\e[K\r\e[1A\e[0m\e[7m\e[1m\e[32m PASS \e[39m\e[22m\e[27m\e[0m \e]8;;file:///tmp/example/lib/e.test.js\a\e[2mlib/\e[22m\e[1me.test.js\e[22m\e]8;;\a\r\n\r\n\e[0m\e[7m\e[33m\e[1m RUNS \e[22m\e[39m\e[27m\e[0m \e[2mlib/\e[22m\e[1me.test.js\e[22m\r\n\r\e[K\r\e[1A\r\e[K\r\e[1A\r\n\e[0m\e[7m\e[33m\e[1m RUNS \e[22m\e[39m\e[27m\e[0m \e[2mlib/\e[22m\e[1mf.test.js\e[22m\r\n"
- delay: 153
content: "\r\e[K\r\e[1A\r\e[K\r\e[1A\r\n\e[0m\e[7m\e[33m\e[1m RUNS \e[22m\e[39m\e[27m\e[0m \e[2mlib/\e[22m\e[1mf.test.js\e[22m\r\n\r\e[K\r\e[1A\r\e[K\r\e[1A\e[0m\e[7m\e[1m\e[32m PASS \e[39m\e[22m\e[27m\e[0m \e]8;;file:///tmp/example/lib/f.test.js\a\e[2mlib/\e[22m\e[1mf.test.js\e[22m\e]8;;\a\r\n\r\n\e[0m\e[7m\e[33m\e[1m RUNS \e[22m\e[39m\e[27m\e[0m \e[2mlib/\e[22m\e[1mf.test.js\e[22m\r\n\r\e[K\r\e[1A\r\e[K\r\e[1A\e[999D\e[K\e[1m\e[2mRunning coverage on untested files...\e[22m\e[22m"
- delay: 54
content: "\e[999D\e[K"
- delay: 145
content: "\r\n\e[1mTest Suites: \e[22m\e[1m\e[32m6 passed\e[39m\e[22m, 6 total\r\n\e[1mTests: \e[22m\e[1m\e[32m17 passed\e[39m\e[22m, 17 total\r\n\e[1mSnapshots: \e[22m\e[1m\e[32m5 passed\e[39m\e[22m, 5 total\r\n\e[1mTime:\e[22m 1.908s\r\n\e[2mRan all test suites\e[22m\e[2m.\e[22m\r\n"
- delay: 27
content: "\r\n> [email protected] posttest /tmp/example\r\n> ./bin/jest-it-up\r\n\r\n"
- delay: 82
content: "Your changes have improved code coverage. Nice work! ❤️\r\nThe following coverage thresholds will be updated:\r\n\r\n branches \e[2m85\e[22m → 100 \e[32m +15\e[39m\r\n functions \e[2m70\e[22m → 100 \e[32m +30\e[39m\r\n lines \e[2m90\e[22m → 100 \e[32m +10\e[39m\r\n statements \e[2m75\e[22m → 100 \e[32m +25\e[39m\r\n\r\nDone! Please record the changes to \e[33mjest.config.js\e[39m.\r\n"
- delay: 10
content: '$ '
- delay: 2000
content: g
- delay: 100
content: i
- delay: 100
content: t
- delay: 100
content: ' '
- delay: 100
content: d
- delay: 100
content: i
- delay: 100
content: f
- delay: 100
content: f
- delay: 100
content: ' '
- delay: 100
content: j
- delay: 100
content: e
- delay: 100
content: s
- delay: 100
content: t
- delay: 100
content: .
- delay: 100
content: c
- delay: 100
content: o
- delay: 100
content: 'n'
- delay: 100
content: f
- delay: 100
content: i
- delay: 100
content: g
- delay: 100
content: .
- delay: 100
content: j
- delay: 100
content: s
- delay: 500
content: "\r\n"
- delay: 24
content: "\e[?1h\e=\r"
- delay: 132
content: "\e[1m\e[0m\e[1m────────────────────────────────────────────────────────────────────────────────\e[0m\e[m\r\n\e[1mmodified: jest.config.js\e[m\r\n\e[1m────────────────────────────────────────────────────────────────────────────────\e[0m\e[m\r\n"
- delay: 33
content: "\e[36m\e[1m\e[38;5;13m@ jest.config.js:8 @\e[1m\e[1m\e[38;5;146m module.exports = {\e[0m\e[m\r\n coverageReporters: ['json-summary', 'lcovonly'],\e[m\e[m\r\n coverageThreshold: {\e[m\e[m\r\n global: {\e[m\e[m\r\n"
- delay: 20
content: "\e[1m\e[38;5;1m\e[1;31m branches: \e[m\e[1;31;48;5;52m85\e[m\e[1;31m,\e[m\e[m\r\n\e[0m\e[1m\e[38;5;1m\e[1;31m functions: \e[m\e[1;31;48;5;52m7\e[m\e[1;31m0,\e[m\e[m\r\n\e[0m\e[1m\e[38;5;1m\e[1;31m lines: \e[m\e[1;31;48;5;52m9\e[m\e[1;31m0,\e[m\e[m\r\n\e[0m\e[1m\e[38;5;1m\e[1;31m statements: \e[m\e[1;31;48;5;52m75\e[m\e[1;31m,\e[m\e[m\r\n"
- delay: 19
content: "\e[0m\e[1m\e[38;5;2m\e[1;32m branches: \e[m\e[1;32;48;5;22m100\e[m\e[1;32m,\e[m\e[m\r\n\e[0m\e[1m\e[38;5;2m\e[1;32m functions: \e[m\e[1;32;48;5;22m10\e[m\e[1;32m0,\e[m\e[m\r\n\e[0m\e[1m\e[38;5;2m\e[1;32m lines: \e[m\e[1;32;48;5;22m10\e[m\e[1;32m0,\e[m\e[m\r\n\e[0m\e[1m\e[38;5;2m\e[1;32m statements: \e[m\e[1;32;48;5;22m100\e[m\e[1;32m,\e[m\e[m\r\n\e[0m },\e[m\e[m\r\n },\e[m\e[m\r\n testEnvironment: 'node',\e[m\e[m\r\n\r\e[K\e[?1l\e>$ "
- delay: 2000
content: "\r\n"
7 changes: 6 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ module.exports = {
clearMocks: true,
collectCoverage: true,
collectCoverageFrom: ['lib/*.js'],
coverageReporters: ['json-summary', 'lcovonly'],
coverageReporters: [
'json-summary',
'lcovonly',
['text', { skipFull: true }],
'text-summary',
],
coverageThreshold: {
global: {
branches: 100,
Expand Down
14 changes: 2 additions & 12 deletions lib/__tests__/applyChanges.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,8 @@ const data = 'data'

jest.mock('fs')

it('does not write config file if there are no changes', () => {
const changes = []

applyChanges(configPath, changes, data)

expect(fs.writeFileSync).not.toHaveBeenCalled()
})

it('writes config file if there are changes', () => {
const changes = ['threshold → 1', 'threshold → 2', 'threshold → 3']

applyChanges(configPath, changes, data)
it('writes config file', () => {
applyChanges(configPath, data)

expect(fs.writeFileSync).toHaveBeenCalledTimes(1)
expect(fs.writeFileSync).toHaveBeenCalledWith(configPath, data)
Expand Down
80 changes: 63 additions & 17 deletions lib/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
const confirm = require('@inquirer/confirm')

const applyChanges = require('../applyChanges')
const getChanges = require('../getChanges')
const getData = require('../getData')
const getNewThresholds = require('../getNewThresholds')
const outputResults = require('../outputResults')
const outputChanges = require('../outputChanges')
const outputResult = require('../outputResult')

const jestItUp = require('..')

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

jest.mock('@inquirer/confirm')

jest.mock('../applyChanges')
jest.mock('../getChanges', () =>
jest.fn(() => ({ changes: 'changes', data: 'data' })),
jest.fn(() => ({ changes: ['changes'], data: 'data' })),
)
jest.mock('../getData', () =>
jest.fn(() => ({
Expand All @@ -19,10 +24,11 @@ jest.mock('../getData', () =>
})),
)
jest.mock('../getNewThresholds', () => jest.fn(() => 'newThresholds'))
jest.mock('../outputResults')
jest.mock('../outputChanges')
jest.mock('../outputResult')

it('runs with with default options', () => {
jestItUp()
it('runs with with default options', async () => {
await jestItUp()

expect(getData).toHaveBeenCalledTimes(1)
expect(getData).toHaveBeenCalledWith(
Expand All @@ -39,35 +45,75 @@ it('runs with with default options', () => {
'newThresholds',
)

expect(outputChanges).toHaveBeenCalledTimes(1)
expect(outputChanges).toHaveBeenCalledWith(['changes'], false)

expect(applyChanges).toHaveBeenCalledTimes(1)
expect(applyChanges).toHaveBeenCalledWith(
'/workingDir/jest.config.js',
'changes',
'data',
)

expect(outputResults).toHaveBeenCalledTimes(1)
expect(outputResults).toHaveBeenCalledWith('changes', false)
expect(outputResult).toHaveBeenCalledTimes(1)
expect(outputResult).toHaveBeenCalledWith(false)
})

it('returns early if there are no changes', async () => {
getChanges.mockReturnValueOnce({ changes: [] })

await jestItUp()

expect(outputChanges).toHaveBeenCalledTimes(1)
expect(outputChanges).toHaveBeenCalledWith([], false)

expect(applyChanges).not.toHaveBeenCalled()
expect(outputResult).not.toHaveBeenCalled()
})

it('runs with custom margin', () => {
jestItUp({ margin: 10 })
it('runs with custom margin', async () => {
await jestItUp({ margin: 10 })

expect(getNewThresholds).toHaveBeenCalledTimes(1)
expect(getNewThresholds).toHaveBeenCalledWith('thresholds', 'coverages', 10)
})

it('runs in silent mode', () => {
jestItUp({ silent: true })
it.each([true, false])(
'runs in interactive mode with %p confirmation',
async confirmed => {
confirm.mockResolvedValueOnce(confirmed)

await jestItUp({ interactive: true })

if (confirmed) {
expect(applyChanges).toHaveBeenCalledTimes(1)
expect(applyChanges).toHaveBeenCalledWith(
'/workingDir/jest.config.js',
'data',
)

expect(outputResult).toHaveBeenCalledTimes(1)
expect(outputResult).toHaveBeenCalledWith(false)
} else {
expect(applyChanges).not.toHaveBeenCalled()
expect(outputResult).not.toHaveBeenCalled()
}
},
)

it('runs in silent mode', async () => {
await jestItUp({ silent: true })

expect(outputResults).not.toHaveBeenCalled()
expect(outputChanges).not.toHaveBeenCalled()
})

it('runs in dry-run mode', () => {
jestItUp({ dryRun: true })
it('runs in dry-run mode', async () => {
await jestItUp({ dryRun: true })

expect(outputChanges).toHaveBeenCalledTimes(1)
expect(outputChanges).toHaveBeenCalledWith(['changes'], true)

expect(applyChanges).not.toHaveBeenCalled()

expect(outputResults).toHaveBeenCalledTimes(1)
expect(outputResults).toHaveBeenCalledWith('changes', true)
expect(outputResult).toHaveBeenCalledTimes(1)
expect(outputResult).toHaveBeenCalledWith(true)
})
Loading

0 comments on commit 56d2541

Please sign in to comment.