Skip to content

Commit

Permalink
Add sinon, replace eslint-module-utils test spy with sinon.spy
Browse files Browse the repository at this point in the history
  • Loading branch information
sompylasar committed May 26, 2017
1 parent d4246fb commit d0007f2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"nyc": "^8.3.0",
"redux": "^3.0.4",
"rimraf": "2.5.2",
"sinon": "^2.3.2",
"typescript": "^2.0.3",
"typescript-eslint-parser": "^2.1.0"
},
Expand Down
19 changes: 10 additions & 9 deletions tests/src/core/parse.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as fs from 'fs'
import { expect } from 'chai'
import sinon from 'sinon'
import parse from 'eslint-module-utils/parse'

import { getFilename, makeNaiveSpy } from '../utils'
import { getFilename } from '../utils'

describe('parse(content, { settings, ecmaFeatures })', function () {
const path = getFilename('jsx.js')
Expand All @@ -22,20 +23,20 @@ describe('parse(content, { settings, ecmaFeatures })', function () {
})

it('passes expected parserOptions to custom parser', function () {
const parseSpy = makeNaiveSpy()
const parseSpy = sinon.spy()
const parserOptions = { ecmaFeatures: { jsx: true } }
require('./parseStubParser').parse = parseSpy
parse(path, content, { settings: {}, parserPath: require.resolve('./parseStubParser'), parserOptions: parserOptions })
expect(parseSpy.callCount).to.equal(1)
expect(parseSpy.lastCallArguments[0]).to.equal(content)
expect(parseSpy.lastCallArguments[1]).to.be.an('object')
expect(parseSpy.lastCallArguments[1]).to.not.equal(parserOptions)
expect(parseSpy.lastCallArguments[1])
expect(parseSpy.callCount, 'parse to be called once').to.equal(1)
expect(parseSpy.args[0][0], 'parse to get content as its first argument').to.equal(content)
expect(parseSpy.args[0][1], 'parse to get an object as its second argument').to.be.an('object')
expect(parseSpy.args[0][1], 'parse to clone the parserOptions object').to.not.equal(parserOptions)
expect(parseSpy.args[0][1], 'parse to get ecmaFeatures in parserOptions which is a clone of ecmaFeatures passed in')
.to.have.property('ecmaFeatures')
.that.is.eql(parserOptions.ecmaFeatures)
.and.is.not.equal(parserOptions.ecmaFeatures)
expect(parseSpy.lastCallArguments[1]).to.have.property('attachComment', true)
expect(parseSpy.lastCallArguments[1]).to.have.property('filePath', path)
expect(parseSpy.args[0][1], 'parse to get parserOptions.attachComment equal to true').to.have.property('attachComment', true)
expect(parseSpy.args[0][1], 'parse to get parserOptions.filePath equal to the full path of the source file').to.have.property('filePath', path)
})

})
14 changes: 0 additions & 14 deletions tests/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,6 @@ export function getFilename(file) {
return path.join(__dirname, '..', 'files', file || 'foo.js')
}

/**
* naive implementation of a function spy
* for more robust spy, consider replacing with sinon or chai-spies
* @return {function}
*/
export function makeNaiveSpy() {
const spy = function () {
spy.callCount += 1
spy.lastCallArguments = arguments
}
spy.callCount = 0
return spy
}

/**
* to be added as valid cases just to ensure no nullable fields are going
* to crash at runtinme
Expand Down

0 comments on commit d0007f2

Please sign in to comment.