Skip to content

Commit

Permalink
eslint-module-utils: Add tests for resolver versions
Browse files Browse the repository at this point in the history
  • Loading branch information
sompylasar committed Jun 4, 2017
1 parent 17d2ee9 commit a0012f8
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 16 deletions.
17 changes: 17 additions & 0 deletions tests/files/foo-bar-resolver-v1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var path = require('path')

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

exports.interfaceVersion = 1
17 changes: 17 additions & 0 deletions tests/files/foo-bar-resolver-v2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var path = require('path')

exports.resolve = function (modulePath, sourceFile, config) {
var fooPathSuffix = '/files/foo.js'
var exceptionPathSuffix = '/files/exception.js'
if (sourceFile.indexOf(fooPathSuffix) === sourceFile.length - fooPathSuffix.length) {
return { found: true, path: path.join(__dirname, 'bar.jsx') }
}
else if (sourceFile.indexOf(exceptionPathSuffix) === sourceFile.length - exceptionPathSuffix.length) {
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.

52 changes: 43 additions & 9 deletions tests/src/core/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,54 @@ describe('resolve', function () {
expect(resolve.bind(null, null, null)).to.throw(Error)
})

it('loads a custom resolver path', function () {
var file = resolve( '../files/foo'
, utils.testContext({ 'import/resolver': './foo-bar-resolver'})
)
it('resolves via a custom resolver with interface version 1', function () {
const testContext = utils.testContext({ 'import/resolver': './foo-bar-resolver-v1'})

expect(file).to.equal(utils.testFilePath('./bar.jsx'))
expect(resolve( '../files/foo'
, Object.assign({}, testContext, { getFilename: function () { return utils.getFilename('foo.js') } })
)).to.equal(utils.testFilePath('./bar.jsx'))

expect(resolve( '../files/exception'
, Object.assign({}, testContext, { getFilename: function () { return utils.getFilename('exception.js') } })
)).to.equal(undefined)

expect(resolve( '../files/not-found'
, Object.assign({}, testContext, { getFilename: function () { return utils.getFilename('not-found.js') } })
)).to.equal(undefined)
})

it('resolves via a custom resolver with interface version 2', function () {
const testContext = utils.testContext({ 'import/resolver': './foo-bar-resolver-v2'})
const testContextReports = []
testContext.report = function (reportInfo) {
testContextReports.push(reportInfo)
}

expect(resolve( '../files/foo'
, Object.assign({}, testContext, { getFilename: function () { return utils.getFilename('foo.js') } })
)).to.equal(utils.testFilePath('./bar.jsx'))

testContextReports.length = 0
expect(resolve( '../files/exception'
, Object.assign({}, testContext, { getFilename: function () { return utils.getFilename('exception.js') } })
)).to.equal(undefined)
expect(testContextReports[0]).to.be.an('object')
expect(testContextReports[0].message).to.equal('Resolve error: foo-bar-resolver-v2 resolve test exception')
expect(testContextReports[0].loc).to.eql({ line: 1, column: 0 })

testContextReports.length = 0
expect(resolve( '../files/not-found'
, Object.assign({}, testContext, { getFilename: function () { return utils.getFilename('not-found.js') } })
)).to.equal(undefined)
expect(testContextReports.length).to.equal(0)
})

it('respects import/resolve extensions', function () {
var file = resolve( './jsx/MyCoolComponent'
, utils.testContext({ 'import/resolve': { 'extensions': ['.jsx'] }})
)
const testContext = utils.testContext({ 'import/resolve': { 'extensions': ['.jsx'] }})

expect(file).to.equal(utils.testFilePath('./jsx/MyCoolComponent.jsx'))
expect(resolve( './jsx/MyCoolComponent'
, testContext
)).to.equal(utils.testFilePath('./jsx/MyCoolComponent.jsx'))
})

const caseDescribe = (!CASE_SENSITIVE_FS ? describe : describe.skip)
Expand Down

0 comments on commit a0012f8

Please sign in to comment.