Skip to content

Commit

Permalink
fix: a few things I can't remember...
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds committed Sep 24, 2017
1 parent 39513ae commit a8c2cd1
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"add-contributor": "node src contributors add",
"test": "node src test",
"test:update": "node src test --updateSnapshot",
"build": "cross-env BUILD_PRESERVE=true node src build",
"build": "node src build",
"lint": "node src lint",
"format": "node src format",
"validate": "node src validate",
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/__tests__/__snapshots__/format.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ exports[`format --ignore-path arg can be used for a custom ignore file 1`] = `pr

exports[`format --no-write prevents --write argument from being added 1`] = `prettier --config ./src/config/prettierrc.js --ignore-path ./src/config/prettierignore **/*.+(js|json|less|css|ts) --no-write`;

exports[`format calls prettier CLI with args 1`] = `prettier --config ./src/config/prettierrc.js --ignore-path ./src/config/prettierignore --write "my-src/**/*.js"`;
exports[`format calls prettier CLI with args 1`] = `prettier --config ./src/config/prettierrc.js --ignore-path ./src/config/prettierignore --write my-src/**/*.js`;
21 changes: 21 additions & 0 deletions src/scripts/__tests__/__snapshots__/lint.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`format --no-cache will disable caching 1`] = `eslint --config ./src/config/eslintrc.js --ignore-path ./src/config/eslintignore --no-cache .`;

exports[`format calls eslint CLI with default args 1`] = `eslint --config ./src/config/eslintrc.js --ignore-path ./src/config/eslintignore --cache .`;

exports[`format does not use built-in config with .eslintrc file 1`] = `eslint --ignore-path ./src/config/eslintignore --cache .`;

exports[`format does not use built-in config with .eslintrc.js file 1`] = `eslint --ignore-path ./src/config/eslintignore --cache .`;

exports[`format does not use built-in config with --config 1`] = `eslint --ignore-path ./src/config/eslintignore --cache --config ./custom-config.js .`;

exports[`format does not use built-in config with eslintConfig pkg prop 1`] = `eslint --ignore-path ./src/config/eslintignore --cache .`;

exports[`format does not use built-in ignore with .eslintignore file 1`] = `eslint --config ./src/config/eslintrc.js --cache .`;

exports[`format does not use built-in ignore with --ignore-path 1`] = `eslint --config ./src/config/eslintrc.js --cache --ignore-path ./my-ignore .`;

exports[`format does not use built-in ignore with eslintIgnore pkg prop 1`] = `eslint --config ./src/config/eslintrc.js --cache .`;

exports[`format runs on given files, but only js files 1`] = `eslint --config ./src/config/eslintrc.js --ignore-path ./src/config/eslintignore --cache ./src/index.js ./src/component.js`;
21 changes: 21 additions & 0 deletions src/scripts/__tests__/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`format calls jest.run with default args 1`] = `--config {"builtInConfig":true} --watch`;

exports[`format does not watch --updateSnapshot 1`] = `--config {"builtInConfig":true} --updateSnapshot`;

exports[`format does not watch on CI 1`] = `--config {"builtInConfig":true}`;

exports[`format does not watch on SCRIPTS_PRECOMMIT 1`] = `--config {"builtInConfig":true}`;

exports[`format does not watch with --coverage 1`] = `--config {"builtInConfig":true} --coverage`;

exports[`format does not watch with --no-watch 1`] = `--config {"builtInConfig":true} --no-watch`;

exports[`format forwards args 1`] = `--config {"builtInConfig":true} --coverage --watch`;

exports[`format uses custom config with --config 1`] = `--watch --config ./my-config.js`;

exports[`format uses custom config with jest prop in pkg 1`] = `--watch`;

exports[`format uses custom config with jest.config.js file 1`] = `--watch`;
4 changes: 2 additions & 2 deletions src/scripts/__tests__/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ cases(
expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1)
const [firstCall] = crossSpawnSyncMock.mock.calls
const [script, calledArgs] = firstCall
expect(`${[script, ...calledArgs].join(' ')}`).toMatchSnapshot()
expect([script, ...calledArgs].join(' ')).toMatchSnapshot()

// afterEach
process.exit = originalExit
Expand All @@ -33,7 +33,7 @@ cases(
},
{
'calls prettier CLI with args': {
args: ['"my-src/**/*.js"'],
args: ['my-src/**/*.js'],
},
'--no-write prevents --write argument from being added': {
args: ['--no-write'],
Expand Down
88 changes: 88 additions & 0 deletions src/scripts/__tests__/lint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import cases from 'jest-in-case'

jest.mock('jest', () => ({run: jest.fn()}))
jest.mock('../../config/jest.config', () => ({builtInConfig: true}))

// this removes the quotes around strings...
expect.addSnapshotSerializer({
print: val => val,
test: val => typeof val === 'string',
})

cases(
'format',
({
args = [],
utils = require('../../utils'),
hasPkgProp = () => false,
hasFile = () => false,
setup = () => () => {},
}) => {
// beforeEach
const {sync: crossSpawnSyncMock} = require('cross-spawn')
const originalArgv = process.argv
const originalExit = process.exit
Object.assign(utils, {
hasPkgProp,
hasFile,
resolveBin: (modName, {executable = modName} = {}) => executable,
})
process.exit = jest.fn()
const teardown = setup()

process.argv = ['node', '../lint', ...args]
crossSpawnSyncMock.mockClear()

try {
// tests
require('../lint')
expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1)
const [firstCall] = crossSpawnSyncMock.mock.calls
const [script, calledArgs] = firstCall
expect([script, ...calledArgs].join(' ')).toMatchSnapshot()
} catch (error) {
throw error
} finally {
teardown()
// afterEach
process.exit = originalExit
process.argv = originalArgv
jest.resetModules()
}
},
{
'calls eslint CLI with default args': {},
'does not use built-in config with --config': {
args: ['--config', './custom-config.js'],
},
'does not use built-in config with .eslintrc file': {
hasFile: filename => filename === '.eslintrc',
},
'does not use built-in config with .eslintrc.js file': {
hasFile: filename => filename === '.eslintrc.js',
},
'does not use built-in config with eslintConfig pkg prop': {
hasPkgProp: prop => prop === 'eslintConfig',
},
'does not use built-in ignore with --ignore-path': {
args: ['--ignore-path', './my-ignore'],
},
'does not use built-in ignore with .eslintignore file': {
hasFile: filename => filename === '.eslintignore',
},
'does not use built-in ignore with eslintIgnore pkg prop': {
hasPkgProp: prop => prop === 'eslintIgnore',
},
'--no-cache will disable caching': {
args: ['--no-cache'],
},
'runs on given files, but only js files': {
args: [
'./src/index.js',
'./package.json',
'./src/index.css',
'./src/component.js',
],
},
},
)
88 changes: 88 additions & 0 deletions src/scripts/__tests__/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import cases from 'jest-in-case'

jest.mock('jest', () => ({run: jest.fn()}))
jest.mock('../../config/jest.config', () => ({builtInConfig: true}))

// this removes the quotes around strings...
expect.addSnapshotSerializer({
print: val => val,
test: val => typeof val === 'string',
})

cases(
'format',
({
args = [],
utils = require('../../utils'),
pkgHasJestProp = false,
hasJestConfigFile = false,
setup = () => () => {},
ci = 'false',
precommit = 'false',
}) => {
// beforeEach
const {run: jestRunMock} = require('jest')
const originalArgv = process.argv
const prevCI = process.env.CI
const prevPrecommit = process.env.SCRIPTS_PRECOMMIT
process.env.CI = ci
process.env.SCRIPTS_PRECOMMIT = precommit
Object.assign(utils, {
hasPkgProp: () => pkgHasJestProp,
hasFile: () => hasJestConfigFile,
})
process.exit = jest.fn()
const teardown = setup()

process.argv = ['node', '../test', ...args]
jestRunMock.mockClear()

try {
// tests
require('../test')
expect(jestRunMock).toHaveBeenCalledTimes(1)
const [firstCall] = jestRunMock.mock.calls
const [jestArgs] = firstCall
expect(jestArgs.join(' ')).toMatchSnapshot()
} catch (error) {
throw error
} finally {
teardown()
// afterEach
process.argv = originalArgv
process.env.CI = prevCI
process.env.SCRIPTS_PRECOMMIT = prevPrecommit
jest.resetModules()
}
},
{
'calls jest.run with default args': {},
'does not watch on CI': {
ci: 'true',
},
'does not watch on SCRIPTS_PRECOMMIT': {
precommit: 'true',
},
'does not watch with --no-watch': {
args: ['--no-watch'],
},
'does not watch with --coverage': {
args: ['--coverage'],
},
'does not watch --updateSnapshot': {
args: ['--updateSnapshot'],
},
'uses custom config with --config': {
args: ['--config', './my-config.js'],
},
'uses custom config with jest prop in pkg': {
pkgHasJestProp: true,
},
'uses custom config with jest.config.js file': {
hasJestConfigFile: true,
},
'forwards args': {
args: ['--coverage', '--watch'],
},
},
)
2 changes: 1 addition & 1 deletion src/scripts/__tests__/travis-after-success.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ cases(
expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1)
const [firstCall] = crossSpawnSyncMock.mock.calls
const [script, calledArgs] = firstCall
expect(`${[script, ...calledArgs].join(' ')}`).toMatchSnapshot()
expect([script, ...calledArgs].join(' ')).toMatchSnapshot()

// afterEach
process.exit = originalExit
Expand Down
4 changes: 3 additions & 1 deletion src/scripts/__tests__/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ cases(
expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1)
const [firstCall] = crossSpawnSyncMock.mock.calls
const [script, calledArgs] = firstCall
expect(`${[script, ...calledArgs].join(' ')}`).toMatchSnapshot()
expect([script, ...calledArgs].join(' ')).toMatchSnapshot()
} catch (error) {
throw error
} finally {
teardown()
}

Expand Down
4 changes: 3 additions & 1 deletion src/scripts/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const parsedArgs = yargsParser(args)
const useBuiltinConfig =
!args.includes('--config') &&
!hasFile('.eslintrc') &&
!hasFile('.eslintrc.js') &&
!hasPkgProp('eslintConfig')

const config = useBuiltinConfig
? ['--config', hereRelative('../config/eslintrc.js')]
: []
Expand Down Expand Up @@ -40,7 +42,7 @@ if (filesGiven) {

const result = spawn.sync(
resolveBin('eslint'),
[...config, ...ignore, ...cache, ...filesToApply, ...args],
[...config, ...ignore, ...cache, ...args, ...filesToApply],
{stdio: 'inherit'},
)

Expand Down
14 changes: 6 additions & 8 deletions src/scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@
process.env.BABEL_ENV = 'test'
process.env.NODE_ENV = 'test'

const jest = require('jest')
const {hasPkgProp, parseEnv, hasFile} = require('../utils')

const args = process.argv.slice(2)

const watch =
!process.env.CI &&
!parseEnv('CI', false) &&
!parseEnv('SCRIPTS_PRECOMMIT', false) &&
!args.includes('--no-watch') &&
!args.includes('--coverage') &&
!args.includes('--updateSnapshot')
? ['--watch']
: []

const useBuiltinConfig =
const config =
!args.includes('--config') &&
!hasFile('jest.config.js') &&
!hasPkgProp('jest')
? ['--config', JSON.stringify(require('../config/jest.config'))]
: []

const config = useBuiltinConfig
? ['--config', JSON.stringify(require('../config/jest.config'))]
: []

jest.run([...config, ...watch, ...args])
require('jest').run([...config, ...watch, ...args])
6 changes: 5 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ const ifAnyDep = (deps, t, f) => (hasAnyDep(deps) ? t : f)
const ifScript = ifPkgSubProp('scripts')

function parseEnv(name, def) {
if (process.env.hasOwnProperty(name)) {
if (
process.env.hasOwnProperty(name) &&
process.env[name] &&
process.env[name] !== 'undefined'
) {
return JSON.parse(process.env[name])
}
return def
Expand Down

0 comments on commit a8c2cd1

Please sign in to comment.