Skip to content

Commit

Permalink
test: Use jest mock instead of sinon spy (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
sudo-suhas authored and Kent C. Dodds committed Sep 3, 2017
1 parent 49a56a2 commit a4ce471
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 106 deletions.
18 changes: 13 additions & 5 deletions package.json
Expand Up @@ -12,7 +12,9 @@
"bin": {
"nps": "./dist/bin/nps.js"
},
"files": ["dist"],
"files": [
"dist"
],
"keywords": [],
"author": "Kent C. Dodds <kent@doddsfamily.us> (http://kentcdodds.com/)",
"license": "MIT",
Expand Down Expand Up @@ -51,17 +53,23 @@
"nps": "^5.4.0",
"nps-utils": "^1.2.0",
"opt-cli": "^1.5.1",
"prettier-eslint-cli": "^4.1.1",
"sinon": "^3.2.1"
"prettier-eslint-cli": "^4.1.1"
},
"eslintConfig": {
"extends": ["kentcdodds", "kentcdodds/jest", "kentcdodds/prettier"],
"extends": [
"kentcdodds",
"kentcdodds/jest",
"kentcdodds/prettier"
],
"rules": {
"max-len": "off"
}
},
"lint-staged": {
"*.js": ["prettier-eslint --write", "git add"]
"*.js": [
"prettier-eslint --write",
"git add"
]
},
"repository": {
"type": "git",
Expand Down
8 changes: 3 additions & 5 deletions src/__tests__/get-scripts-from-config.js
@@ -1,4 +1,3 @@
import {spy} from 'sinon'
import getScriptsFromConfig from '../get-scripts-from-config'

test('returns empty object by default', () => {
Expand All @@ -7,11 +6,10 @@ test('returns empty object by default', () => {

test('passes input to the scripts if it is a function', () => {
const input = 'hello'
const scripts = spy()
const scripts = jest.fn()
getScriptsFromConfig(scripts, input)
expect(scripts.calledOnce).toBe(true)
const [firstArg] = scripts.firstCall.args
expect(firstArg).toBe(input)
expect(scripts).toHaveBeenCalledTimes(1)
expect(scripts).toHaveBeenCalledWith(input)
})

test('just uses the scripts object if it is an object', () => {
Expand Down
49 changes: 23 additions & 26 deletions src/__tests__/index.js
@@ -1,6 +1,5 @@
/* eslint import/newline-after-import:0, global-require:0 */
import {oneLine} from 'common-tags'
import {spy} from 'sinon'
import chalk from 'chalk'
import managePath from 'manage-path'

Expand Down Expand Up @@ -48,9 +47,10 @@ test('spawn called and appends options to default', () => {

test('spawn.on called with "close" and "error"', () => {
return testSpawnCallWithDefaults().then(({onSpy}) => {
expect(onSpy.calledTwice).toBe(true)
expect(onSpy.calledWith('close')).toBe(true)
expect(onSpy.calledWith('error')).toBe(true)
expect(onSpy).toHaveBeenCalledTimes(2)
const [[arg1], [arg2]] = onSpy.mock.calls
expect(arg1).toEqual('error')
expect(arg2).toEqual('close')
})
})

Expand All @@ -74,16 +74,16 @@ test('returns a log object when no script is found', () => {
test('options: silent sets the logLevel to disable', () => {
const options = {silent: true}
return testSpawnCallWithDefaults(options).then(({mockGetLogger}) => {
expect(mockGetLogger.calledOnce).toBe(true)
expect(mockGetLogger.calledWith('disable')).toBe(true)
expect(mockGetLogger).toHaveBeenCalledTimes(1)
expect(mockGetLogger).toHaveBeenCalledWith('disable')
})
})

test('options: logLevel sets the log level', () => {
const options = {logLevel: 'warn'}
return testSpawnCallWithDefaults(options).then(({mockGetLogger}) => {
expect(mockGetLogger.calledOnce).toBe(true)
expect(mockGetLogger.calledWith('warn')).toBe(true)
expect(mockGetLogger).toHaveBeenCalledTimes(1)
expect(mockGetLogger).toHaveBeenCalledWith('warn')
})
})

Expand Down Expand Up @@ -120,10 +120,9 @@ test('runs scripts serially if given an array of input', () => {
const scripts = ['lint src/ scripts/', 'build src/ scripts/']
const scriptConfig = {build: buildCommand, lint: lintCommand}
const options = {}
runPackageScript({scriptConfig, scripts, options}).then(() => {
expect(mockSpawnStubSpy.calledTwice).toBe(true)
const [command1] = mockSpawnStubSpy.firstCall.args
const [command2] = mockSpawnStubSpy.secondCall.args
return runPackageScript({scriptConfig, scripts, options}).then(() => {
expect(mockSpawnStubSpy).toHaveBeenCalledTimes(2)
const [[command1], [command2]] = mockSpawnStubSpy.mock.calls
expect(command1).toBe('eslint src/ scripts/')
expect(command2).toBe('babel src/ scripts/')
})
Expand Down Expand Up @@ -152,7 +151,7 @@ test('stops running scripts when running serially if any given script fails', ()
kill() {},
}
}
const mockSpawnStubSpy = spy(spawnStub)
const mockSpawnStubSpy = jest.fn(spawnStub)
const {runPackageScript} = setup(mockSpawnStubSpy)
return runPackageScript({scriptConfig, scripts}).then(
() => {
Expand All @@ -163,9 +162,9 @@ test('stops running scripts when running serially if any given script fails', ()
expect(typeof ref === 'string').toBe(true)
expect(typeof message === 'string').toBe(true)
// only called with the bad script, not for the good one
expect(mockSpawnStubSpy.calledOnce).toBe(true)
const [command1] = mockSpawnStubSpy.firstCall.args
expect(command1).toBe('bad')
expect(mockSpawnStubSpy).toHaveBeenCalledTimes(1)
const [[command1]] = mockSpawnStubSpy.mock.calls
expect(command1).toBe(badCommand)
},
)
})
Expand Down Expand Up @@ -247,8 +246,8 @@ function testSpawnCall(
scripts,
}).then(
result => {
expect(mockSpawnStubSpy.calledOnce).toBe(true)
const [command, options] = mockSpawnStubSpy.firstCall.args
expect(mockSpawnStubSpy).toHaveBeenCalledTimes(1)
const [[command, options]] = mockSpawnStubSpy.mock.calls
return Promise.resolve({
result,
command,
Expand All @@ -258,8 +257,8 @@ function testSpawnCall(
})
},
error => {
expect(mockSpawnStubSpy.calledOnce).toBe(true)
const [command, options] = mockSpawnStubSpy.firstCall.args
expect(mockSpawnStubSpy).toHaveBeenCalledTimes(1)
const [[command, options]] = mockSpawnStubSpy.mock.calls
return Promise.reject({
error,
command,
Expand All @@ -272,18 +271,17 @@ function testSpawnCall(
}

function setup(mockSpawnStubSpy) {
const killSpy = spy()
const onSpy = spy((event, cb) => {
const onSpy = jest.fn((event, cb) => {
if (event === 'close') {
cb(0)
}
})
const infoSpy = jest.fn()
const mockGetLogger = spy(() => ({info: infoSpy}))
const mockGetLogger = jest.fn(() => ({info: infoSpy}))
mockGetLogger.getLogLevel = require.requireActual(
'../get-logger',
).getLogLevel
mockSpawnStubSpy = mockSpawnStubSpy || spy(spawnStub)
mockSpawnStubSpy = mockSpawnStubSpy || jest.fn(spawnStub)
jest.resetModules()
jest.mock('spawn-command-with-kill', () => mockSpawnStubSpy)
jest.mock('../get-logger', () => mockGetLogger)
Expand All @@ -293,10 +291,9 @@ function setup(mockSpawnStubSpy) {
infoSpy,
mockGetLogger,
onSpy,
killSpy,
runPackageScript,
}
function spawnStub() {
return {on: onSpy, kill: killSpy}
return {on: onSpy, kill() {}}
}
}
19 changes: 9 additions & 10 deletions src/bin-utils/__tests__/index.js
@@ -1,7 +1,6 @@
/* eslint import/newline-after-import:0, global-require:0 */
import path from 'path'
import chalk from 'chalk'
import {spy} from 'sinon'
import {oneLine} from 'common-tags'
import {help, preloadModule, loadConfig, specificHelpScript} from '../'

Expand All @@ -25,14 +24,14 @@ test('preloadModule: resolves a node_module', () => {
})

test('preloadModule: logs a warning when the module cannot be required', () => {
const mockWarn = spy()
const mockWarn = jest.fn()
jest.resetModules()
jest.mock('../../get-logger', () => () => ({warn: mockWarn}))
const {preloadModule: proxiedPreloadModule} = require('../')
const val = proxiedPreloadModule('./module-that-does-exist')
expect(val).toBeUndefined()
expect(mockWarn.calledOnce).toBe(true)
const [{message}] = mockWarn.firstCall.args
expect(mockWarn).toHaveBeenCalledTimes(1)
const [[{message}]] = mockWarn.mock.calls
expect(message).toMatch(/Unable to preload "\.\/module-that-does-exist"/)
})

Expand Down Expand Up @@ -147,14 +146,14 @@ test(
)

test('loadConfig: logs a warning when the JS module cannot be required', () => {
const mockError = spy()
const mockError = jest.fn()
jest.resetModules()
jest.mock('../../get-logger', () => () => ({error: mockError}))
const {loadConfig: proxiedReloadConfig} = require('../')
const val = proxiedReloadConfig('./config-that-does-exist')
expect(val).toBeUndefined()
expect(mockError.calledOnce).toBe(true)
const [{message}] = mockError.firstCall.args
expect(mockError).toHaveBeenCalledTimes(1)
const [[{message}]] = mockError.mock.calls
expect(message).toMatch(
/Unable to find JS config at "\.\/config-that-does-exist"/,
)
Expand Down Expand Up @@ -197,14 +196,14 @@ test('loadConfig: does not swallow YAML syntax errors', () => {
})

test('loadConfig: logs a warning when the YAML file cannot be located', () => {
const mockError = spy()
const mockError = jest.fn()
jest.resetModules()
jest.mock('../../get-logger', () => () => ({error: mockError}))
const {loadConfig: proxiedReloadConfig} = require('../')
const val = proxiedReloadConfig('./config-that-does-not-exist.yml')
expect(val).toBeUndefined()
expect(mockError.calledOnce).toBe(true)
const [{message}] = mockError.firstCall.args
expect(mockError).toHaveBeenCalledTimes(1)
const [[{message}]] = mockError.mock.calls
expect(message).toMatch(
/Unable to find YML config at "\.\/config-that-does-not-exist.yml"/,
)
Expand Down

0 comments on commit a4ce471

Please sign in to comment.