Skip to content

Commit 14c501e

Browse files
authored
Merge pull request #865 from sompylasar/eslint-module-utils_tests
eslint-module-utils tests
2 parents c41ed06 + 117717f commit 14c501e

12 files changed

+299
-30
lines changed

CHANGELOG.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).
55

66
## [Unreleased]
7-
7+
### Added
8+
- Add `filePath` into `parserOptions` passed to `parser` ([#839], thanks [@sompylasar])
89

910
## [2.5.0] - 2017-06-22
1011

@@ -152,8 +153,7 @@ Yanked due to critical issue in eslint-module-utils with cache key resulting fro
152153
- Something horrible happened during `npm prepublish` of 1.10.1.
153154
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!
154155

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

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var path = require('path')
2+
3+
exports.resolveImport = function (modulePath, sourceFile, config) {
4+
var sourceFileName = path.basename(sourceFile)
5+
if (sourceFileName === 'foo.js') {
6+
return path.join(__dirname, 'bar.jsx')
7+
}
8+
else if (sourceFileName === 'exception.js') {
9+
throw new Error('foo-bar-resolver-v1 resolveImport test exception')
10+
}
11+
else {
12+
return undefined
13+
}
14+
}

tests/files/foo-bar-resolver-v1.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var path = require('path')
2+
3+
exports.resolveImport = function (modulePath, sourceFile, config) {
4+
var sourceFileName = path.basename(sourceFile)
5+
if (sourceFileName === 'foo.js') {
6+
return path.join(__dirname, 'bar.jsx')
7+
}
8+
else if (sourceFileName === 'exception.js') {
9+
throw new Error('foo-bar-resolver-v1 resolveImport test exception')
10+
}
11+
else {
12+
return undefined
13+
}
14+
}
15+
16+
exports.interfaceVersion = 1

tests/files/foo-bar-resolver-v2.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var path = require('path')
2+
3+
exports.resolve = function (modulePath, sourceFile, config) {
4+
var sourceFileName = path.basename(sourceFile)
5+
if (sourceFileName === 'foo.js') {
6+
return { found: true, path: path.join(__dirname, 'bar.jsx') }
7+
}
8+
else if (sourceFileName === 'exception.js') {
9+
throw new Error('foo-bar-resolver-v2 resolve test exception')
10+
}
11+
else {
12+
return { found: false }
13+
}
14+
}
15+
16+
exports.interfaceVersion = 2

tests/files/foo-bar-resolver.js

-7
This file was deleted.

tests/files/ignore.invalid.extension

Whitespace-only changes.

tests/src/core/getExports.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('ExportMap', function () {
1313
parserPath: 'babel-eslint',
1414
}
1515

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

2525
})
2626

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

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

@@ -42,7 +42,7 @@ describe('ExportMap', function () {
4242
})
4343
})
4444

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

@@ -56,7 +56,7 @@ describe('ExportMap', function () {
5656
.not.to.equal(firstAccess)
5757
})
5858

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

6767
})
6868

69-
it('should export explicit names for a missing file in exports', function () {
69+
it('exports explicit names for a missing file in exports', function () {
7070
var imports
7171
expect(function () {
7272
imports = ExportMap.get('./exports-missing', fakeContext)

tests/src/core/hash.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { expect } from 'chai'
2+
3+
import hashify, { hashArray, hashObject } from 'eslint-module-utils/hash'
4+
5+
const createHash = require('crypto').createHash
6+
7+
function expectHash(actualHash, expectedString) {
8+
const expectedHash = createHash('sha256')
9+
expectedHash.update(expectedString)
10+
expect(actualHash.digest('hex'), 'to be a hex digest of sha256 hash of string <' + expectedString + '>').to.equal(expectedHash.digest('hex'))
11+
}
12+
13+
describe('hash', function () {
14+
describe('hashify', function () {
15+
it('handles null', function () {
16+
expectHash(hashify(null), 'null')
17+
})
18+
19+
it('handles undefined', function () {
20+
expectHash(hashify(undefined), 'undefined')
21+
})
22+
23+
it('handles numbers', function () {
24+
expectHash(hashify(123.456), '123.456')
25+
})
26+
27+
it('handles strings', function () {
28+
expectHash(hashify('a string'), '"a string"')
29+
})
30+
31+
it('handles Array instances', function () {
32+
expectHash(hashify([ 'a string' ]), '["a string",]')
33+
})
34+
35+
it('handles empty Array instances', function () {
36+
expectHash(hashify([]), '[]')
37+
})
38+
39+
it('handles Object instances', function () {
40+
expectHash(hashify({ foo: 123.456, 'a key': 'a value' }), '{"a key":"a value","foo":123.456,}')
41+
})
42+
43+
it('handles nested Object instances', function () {
44+
expectHash(hashify({ foo: 123.456, 'a key': 'a value', obj: { abc: { def: 'ghi' } } }), '{"a key":"a value","foo":123.456,"obj":{"abc":{"def":"ghi",},},}')
45+
})
46+
47+
it('handles nested Object and Array instances', function () {
48+
expectHash(hashify({ foo: 123.456, 'a key': 'a value', obj: { arr: [ { def: 'ghi' } ] } }), '{"a key":"a value","foo":123.456,"obj":{"arr":[{"def":"ghi",},],},}')
49+
})
50+
})
51+
52+
describe('hashArray', function () {
53+
it('handles Array instances', function () {
54+
expectHash(hashArray([ 'a string' ]), '["a string",]')
55+
})
56+
57+
it('handles empty Array instances', function () {
58+
expectHash(hashArray([]), '[]')
59+
})
60+
})
61+
62+
describe('hashObject', function () {
63+
it('handles Object instances', function () {
64+
expectHash(hashObject({ foo: 123.456, 'a key': 'a value' }), '{"a key":"a value","foo":123.456,}')
65+
})
66+
67+
it('handles nested Object instances', function () {
68+
expectHash(hashObject({ foo: 123.456, 'a key': 'a value', obj: { abc: { def: 'ghi' } } }), '{"a key":"a value","foo":123.456,"obj":{"abc":{"def":"ghi",},},}')
69+
})
70+
71+
it('handles nested Object and Array instances', function () {
72+
expectHash(hashObject({ foo: 123.456, 'a key': 'a value', obj: { arr: [ { def: 'ghi' } ] } }), '{"a key":"a value","foo":123.456,"obj":{"arr":[{"def":"ghi",},],},}')
73+
})
74+
})
75+
76+
})

tests/src/core/ignore.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { expect } from 'chai'
2+
3+
import isIgnored, { hasValidExtension } from 'eslint-module-utils/ignore'
4+
5+
import * as utils from '../utils'
6+
7+
describe('ignore', function () {
8+
describe('isIgnored', function () {
9+
it('ignores paths with extensions other than .js', function () {
10+
const testContext = utils.testContext({})
11+
12+
expect(isIgnored('../files/foo.js', testContext)).to.equal(false)
13+
14+
expect(isIgnored('../files/bar.jsx', testContext)).to.equal(true)
15+
16+
expect(isIgnored('../files/typescript.ts', testContext)).to.equal(true)
17+
18+
expect(isIgnored('../files/ignore.invalid.extension', testContext)).to.equal(true)
19+
})
20+
21+
it('ignores paths with invalid extensions when configured with import/extensions', function () {
22+
const testContext = utils.testContext({ 'import/extensions': [ '.js', '.jsx', '.ts' ] })
23+
24+
expect(isIgnored('../files/foo.js', testContext)).to.equal(false)
25+
26+
expect(isIgnored('../files/bar.jsx', testContext)).to.equal(false)
27+
28+
expect(isIgnored('../files/typescript.ts', testContext)).to.equal(false)
29+
30+
expect(isIgnored('../files/ignore.invalid.extension', testContext)).to.equal(true)
31+
})
32+
})
33+
34+
describe('hasValidExtension', function () {
35+
it('assumes only .js as valid by default', function () {
36+
const testContext = utils.testContext({})
37+
38+
expect(hasValidExtension('../files/foo.js', testContext)).to.equal(true)
39+
40+
expect(hasValidExtension('../files/foo.jsx', testContext)).to.equal(false)
41+
42+
expect(hasValidExtension('../files/foo.css', testContext)).to.equal(false)
43+
44+
expect(hasValidExtension('../files/foo.invalid.extension', testContext)).to.equal(false)
45+
})
46+
47+
it('can be configured with import/extensions', function () {
48+
const testContext = utils.testContext({ 'import/extensions': [ '.foo', '.bar' ] })
49+
50+
expect(hasValidExtension('../files/foo.foo', testContext)).to.equal(true)
51+
52+
expect(hasValidExtension('../files/foo.bar', testContext)).to.equal(true)
53+
54+
expect(hasValidExtension('../files/foo.js', testContext)).to.equal(false)
55+
})
56+
})
57+
58+
})

tests/src/core/parse.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ describe('parse(content, { settings, ecmaFeatures })', function () {
4141
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)
4242
})
4343

44-
it('should throw on context == null', function () {
44+
it('throws on context == null', function () {
4545
expect(parse.bind(null, path, content, null)).to.throw(Error)
4646
})
4747

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

52-
it('should take the alternate parser specified in settings', function () {
52+
it('takes the alternate parser specified in settings', function () {
5353
const parseSpy = sinon.spy()
5454
const parserOptions = { ecmaFeatures: { jsx: true } }
5555
parseStubParser.parse = parseSpy

0 commit comments

Comments
 (0)