Skip to content

Commit

Permalink
Merge pull request #865 from sompylasar/eslint-module-utils_tests
Browse files Browse the repository at this point in the history
eslint-module-utils tests
  • Loading branch information
benmosher committed Jun 22, 2017
2 parents c41ed06 + 117717f commit 14c501e
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 30 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Expand Up @@ -4,7 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).

## [Unreleased]

### Added
- Add `filePath` into `parserOptions` passed to `parser` ([#839], thanks [@sompylasar])

## [2.5.0] - 2017-06-22

Expand Down Expand Up @@ -152,8 +153,7 @@ Yanked due to critical issue in eslint-module-utils with cache key resulting fro
- Something horrible happened during `npm prepublish` of 1.10.1.
Several `rm -rf node_modules && npm i` and `gulp clean && npm prepublish`s later, it is rebuilt and republished as 1.10.2. Thanks [@rhettlivingston] for noticing and reporting!

## [1.10.1] - 2016-07-02 [
ED]
## [1.10.1] - 2016-07-02 [YANKED]
### Added
- Officially support ESLint 3.x. (peerDependencies updated to `2.x - 3.x`)

Expand Down
14 changes: 14 additions & 0 deletions tests/files/foo-bar-resolver-no-version.js
@@ -0,0 +1,14 @@
var path = require('path')

exports.resolveImport = function (modulePath, sourceFile, config) {
var sourceFileName = path.basename(sourceFile)
if (sourceFileName === 'foo.js') {
return path.join(__dirname, 'bar.jsx')
}
else if (sourceFileName === 'exception.js') {
throw new Error('foo-bar-resolver-v1 resolveImport test exception')
}
else {
return undefined
}
}
16 changes: 16 additions & 0 deletions tests/files/foo-bar-resolver-v1.js
@@ -0,0 +1,16 @@
var path = require('path')

exports.resolveImport = function (modulePath, sourceFile, config) {
var sourceFileName = path.basename(sourceFile)
if (sourceFileName === 'foo.js') {
return path.join(__dirname, 'bar.jsx')
}
else if (sourceFileName === 'exception.js') {
throw new Error('foo-bar-resolver-v1 resolveImport test exception')
}
else {
return undefined
}
}

exports.interfaceVersion = 1
16 changes: 16 additions & 0 deletions tests/files/foo-bar-resolver-v2.js
@@ -0,0 +1,16 @@
var path = require('path')

exports.resolve = function (modulePath, sourceFile, config) {
var sourceFileName = path.basename(sourceFile)
if (sourceFileName === 'foo.js') {
return { found: true, path: path.join(__dirname, 'bar.jsx') }
}
else if (sourceFileName === 'exception.js') {
throw new Error('foo-bar-resolver-v2 resolve test exception')
}
else {
return { found: false }
}
}

exports.interfaceVersion = 2
7 changes: 0 additions & 7 deletions tests/files/foo-bar-resolver.js

This file was deleted.

Empty file.
12 changes: 6 additions & 6 deletions tests/src/core/getExports.js
Expand Up @@ -13,7 +13,7 @@ describe('ExportMap', function () {
parserPath: 'babel-eslint',
}

it('should handle ExportAllDeclaration', function () {
it('handles ExportAllDeclaration', function () {
var imports
expect(function () {
imports = ExportMap.get('./export-all', fakeContext)
Expand All @@ -24,12 +24,12 @@ describe('ExportMap', function () {

})

it('should return a cached copy on subsequent requests', function () {
it('returns a cached copy on subsequent requests', function () {
expect(ExportMap.get('./named-exports', fakeContext))
.to.exist.and.equal(ExportMap.get('./named-exports', fakeContext))
})

it('should not return a cached copy after modification', (done) => {
it('does not return a cached copy after modification', (done) => {
const firstAccess = ExportMap.get('./mutator', fakeContext)
expect(firstAccess).to.exist

Expand All @@ -42,7 +42,7 @@ describe('ExportMap', function () {
})
})

it('should not return a cached copy with different settings', () => {
it('does not return a cached copy with different settings', () => {
const firstAccess = ExportMap.get('./named-exports', fakeContext)
expect(firstAccess).to.exist

Expand All @@ -56,7 +56,7 @@ describe('ExportMap', function () {
.not.to.equal(firstAccess)
})

it('should not throw for a missing file', function () {
it('does not throw for a missing file', function () {
var imports
expect(function () {
imports = ExportMap.get('./does-not-exist', fakeContext)
Expand All @@ -66,7 +66,7 @@ describe('ExportMap', function () {

})

it('should export explicit names for a missing file in exports', function () {
it('exports explicit names for a missing file in exports', function () {
var imports
expect(function () {
imports = ExportMap.get('./exports-missing', fakeContext)
Expand Down
76 changes: 76 additions & 0 deletions tests/src/core/hash.js
@@ -0,0 +1,76 @@
import { expect } from 'chai'

import hashify, { hashArray, hashObject } from 'eslint-module-utils/hash'

const createHash = require('crypto').createHash

function expectHash(actualHash, expectedString) {
const expectedHash = createHash('sha256')
expectedHash.update(expectedString)
expect(actualHash.digest('hex'), 'to be a hex digest of sha256 hash of string <' + expectedString + '>').to.equal(expectedHash.digest('hex'))
}

describe('hash', function () {
describe('hashify', function () {
it('handles null', function () {
expectHash(hashify(null), 'null')
})

it('handles undefined', function () {
expectHash(hashify(undefined), 'undefined')
})

it('handles numbers', function () {
expectHash(hashify(123.456), '123.456')
})

it('handles strings', function () {
expectHash(hashify('a string'), '"a string"')
})

it('handles Array instances', function () {
expectHash(hashify([ 'a string' ]), '["a string",]')
})

it('handles empty Array instances', function () {
expectHash(hashify([]), '[]')
})

it('handles Object instances', function () {
expectHash(hashify({ foo: 123.456, 'a key': 'a value' }), '{"a key":"a value","foo":123.456,}')
})

it('handles nested Object instances', function () {
expectHash(hashify({ foo: 123.456, 'a key': 'a value', obj: { abc: { def: 'ghi' } } }), '{"a key":"a value","foo":123.456,"obj":{"abc":{"def":"ghi",},},}')
})

it('handles nested Object and Array instances', function () {
expectHash(hashify({ foo: 123.456, 'a key': 'a value', obj: { arr: [ { def: 'ghi' } ] } }), '{"a key":"a value","foo":123.456,"obj":{"arr":[{"def":"ghi",},],},}')
})
})

describe('hashArray', function () {
it('handles Array instances', function () {
expectHash(hashArray([ 'a string' ]), '["a string",]')
})

it('handles empty Array instances', function () {
expectHash(hashArray([]), '[]')
})
})

describe('hashObject', function () {
it('handles Object instances', function () {
expectHash(hashObject({ foo: 123.456, 'a key': 'a value' }), '{"a key":"a value","foo":123.456,}')
})

it('handles nested Object instances', function () {
expectHash(hashObject({ foo: 123.456, 'a key': 'a value', obj: { abc: { def: 'ghi' } } }), '{"a key":"a value","foo":123.456,"obj":{"abc":{"def":"ghi",},},}')
})

it('handles nested Object and Array instances', function () {
expectHash(hashObject({ foo: 123.456, 'a key': 'a value', obj: { arr: [ { def: 'ghi' } ] } }), '{"a key":"a value","foo":123.456,"obj":{"arr":[{"def":"ghi",},],},}')
})
})

})
58 changes: 58 additions & 0 deletions tests/src/core/ignore.js
@@ -0,0 +1,58 @@
import { expect } from 'chai'

import isIgnored, { hasValidExtension } from 'eslint-module-utils/ignore'

import * as utils from '../utils'

describe('ignore', function () {
describe('isIgnored', function () {
it('ignores paths with extensions other than .js', function () {
const testContext = utils.testContext({})

expect(isIgnored('../files/foo.js', testContext)).to.equal(false)

expect(isIgnored('../files/bar.jsx', testContext)).to.equal(true)

expect(isIgnored('../files/typescript.ts', testContext)).to.equal(true)

expect(isIgnored('../files/ignore.invalid.extension', testContext)).to.equal(true)
})

it('ignores paths with invalid extensions when configured with import/extensions', function () {
const testContext = utils.testContext({ 'import/extensions': [ '.js', '.jsx', '.ts' ] })

expect(isIgnored('../files/foo.js', testContext)).to.equal(false)

expect(isIgnored('../files/bar.jsx', testContext)).to.equal(false)

expect(isIgnored('../files/typescript.ts', testContext)).to.equal(false)

expect(isIgnored('../files/ignore.invalid.extension', testContext)).to.equal(true)
})
})

describe('hasValidExtension', function () {
it('assumes only .js as valid by default', function () {
const testContext = utils.testContext({})

expect(hasValidExtension('../files/foo.js', testContext)).to.equal(true)

expect(hasValidExtension('../files/foo.jsx', testContext)).to.equal(false)

expect(hasValidExtension('../files/foo.css', testContext)).to.equal(false)

expect(hasValidExtension('../files/foo.invalid.extension', testContext)).to.equal(false)
})

it('can be configured with import/extensions', function () {
const testContext = utils.testContext({ 'import/extensions': [ '.foo', '.bar' ] })

expect(hasValidExtension('../files/foo.foo', testContext)).to.equal(true)

expect(hasValidExtension('../files/foo.bar', testContext)).to.equal(true)

expect(hasValidExtension('../files/foo.js', testContext)).to.equal(false)
})
})

})
6 changes: 3 additions & 3 deletions tests/src/core/parse.js
Expand Up @@ -41,15 +41,15 @@ describe('parse(content, { settings, ecmaFeatures })', function () {
expect(parseSpy.args[0][1], 'custom parser to get parserOptions.filePath equal to the full path of the source file').to.have.property('filePath', path)
})

it('should throw on context == null', function () {
it('throws on context == null', function () {
expect(parse.bind(null, path, content, null)).to.throw(Error)
})

it('should throw on unable to resolve parserPath', function () {
it('throws on unable to resolve parserPath', function () {
expect(parse.bind(null, path, content, { settings: {}, parserPath: null })).to.throw(Error)
})

it('should take the alternate parser specified in settings', function () {
it('takes the alternate parser specified in settings', function () {
const parseSpy = sinon.spy()
const parserOptions = { ecmaFeatures: { jsx: true } }
parseStubParser.parse = parseSpy
Expand Down

0 comments on commit 14c501e

Please sign in to comment.