From a0012f8f9b17d6d6afd9ade7e608b3ad94b542e9 Mon Sep 17 00:00:00 2001 From: Ivan Babak Date: Sun, 4 Jun 2017 16:43:08 -0700 Subject: [PATCH] eslint-module-utils: Add tests for resolver versions --- tests/files/foo-bar-resolver-v1.js | 17 ++++++++++ tests/files/foo-bar-resolver-v2.js | 17 ++++++++++ tests/files/foo-bar-resolver.js | 7 ---- tests/src/core/resolve.js | 52 ++++++++++++++++++++++++------ 4 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 tests/files/foo-bar-resolver-v1.js create mode 100644 tests/files/foo-bar-resolver-v2.js delete mode 100644 tests/files/foo-bar-resolver.js diff --git a/tests/files/foo-bar-resolver-v1.js b/tests/files/foo-bar-resolver-v1.js new file mode 100644 index 000000000..decde2e56 --- /dev/null +++ b/tests/files/foo-bar-resolver-v1.js @@ -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 diff --git a/tests/files/foo-bar-resolver-v2.js b/tests/files/foo-bar-resolver-v2.js new file mode 100644 index 000000000..adb764838 --- /dev/null +++ b/tests/files/foo-bar-resolver-v2.js @@ -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 diff --git a/tests/files/foo-bar-resolver.js b/tests/files/foo-bar-resolver.js deleted file mode 100644 index 92421ba26..000000000 --- a/tests/files/foo-bar-resolver.js +++ /dev/null @@ -1,7 +0,0 @@ -var path = require('path'); - -exports.resolve = function(source, file) { - return { found: true, path: path.join(__dirname, 'bar.jsx') }; -}; - -exports.interfaceVersion = 2; diff --git a/tests/src/core/resolve.js b/tests/src/core/resolve.js index e8f255f34..32eea3037 100644 --- a/tests/src/core/resolve.js +++ b/tests/src/core/resolve.js @@ -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)