From 07ecf376f40ab3d63f68d2f57fe999402e1a8fb6 Mon Sep 17 00:00:00 2001 From: Joel Kemp Date: Mon, 21 Jan 2019 12:44:25 -0500 Subject: [PATCH] Don't throw on an undefined partial Fixes https://github.com/dependents/node-detective-cjs/issues/10 Refs https://github.com/pahen/madge/issues/197 --- index.js | 6 ++++++ test/mockedJSFiles.js | 3 ++- test/test.js | 24 ++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 9c22383..8e5af5d 100644 --- a/index.js +++ b/index.js @@ -242,6 +242,12 @@ function commonJSLookup({dependency, filename, directory, nodeModulesConfig}) { if (!resolve) { resolve = require('resolve'); } + + if (!dependency) { + debug('blank dependency given. Returning early.'); + return ''; + } + // Need to resolve partials within the directory of the module, not filing-cabinet const moduleLookupDir = path.join(directory, 'node_modules'); diff --git a/test/mockedJSFiles.js b/test/mockedJSFiles.js index 30ebe47..715c3e1 100644 --- a/test/mockedJSFiles.js +++ b/test/mockedJSFiles.js @@ -3,7 +3,8 @@ module.exports = { 'es6': { 'foo.js': 'import bar from "./bar";', 'foo.jsx': 'import React from "react"; export default () => { return (
); }', - 'bar.js': 'export default function() {};' + 'bar.js': 'export default function() {};', + 'lazy.js': 'localizedModule(app, "modulename", locale => import(`modulename/locales/${locale}`));' }, 'cjs': { 'foo.js': 'module.exports = 1;', diff --git a/test/test.js b/test/test.js index cb7b634..3717fbc 100644 --- a/test/test.js +++ b/test/test.js @@ -153,6 +153,30 @@ describe('filing-cabinet', function() { assert.equal(result, expected); spy.restore(); }); + + describe('when given a lazy import with interpolation', function() { + it('does not throw', function() { + assert.doesNotThrow(() => { + cabinet({ + partial: '`modulename/locales/${locale}`', + filename: 'js/es6/lazy.js', + directory: 'js/es6/' + }); + }); + }); + }); + + describe('when given an undefined dependency', function() { + it('does not throw', function() { + assert.doesNotThrow(() => { + cabinet({ + partial: undefined, + filename: 'js/es6/lazy.js', + directory: 'js/es6/' + }); + }); + }); + }); }); describe('jsx', function() {