diff --git a/lib/index.mjs b/lib/index.mjs index 4358dab..3f91bac 100644 --- a/lib/index.mjs +++ b/lib/index.mjs @@ -108,7 +108,7 @@ export function createResolve (defaults) { } export function interopDefault (sourceModule) { - if (!(sourceModule && 'default' in sourceModule)) { + if (!isObject(sourceModule) || !('default' in sourceModule)) { return sourceModule } const newModule = sourceModule.default @@ -314,3 +314,7 @@ function _perr (_err) { Error.captureStackTrace(err, _pcall) return Promise.reject(err) } + +function isObject (val) { + return val !== null && typeof val === 'object' +} diff --git a/test/interop.test.mjs b/test/interop.test.mjs index 404e4de..2e13d93 100644 --- a/test/interop.test.mjs +++ b/test/interop.test.mjs @@ -4,6 +4,8 @@ import { interopDefault } from '../lib/index.mjs' const tests = [ [{}, {}], [{ default: {} }, {}], + [true, true], + [[1, 2, 3], [1, 2, 3]], [{ default: { x: 2 } }, { x: 2 }], [{ named: 2 }, { named: 2 }], [{ named: 2, default: {} }, { named: 2 }], @@ -15,8 +17,10 @@ describe('interopDefault', () => { it(JSON.stringify(input), () => { const interop = interopDefault(input) expect(interop).to.deep.equal(result) - if ('default' in input) { + if (typeof input === 'object' && 'default' in input) { expect(interop.default).to.deep.equal(result) + } else { + expect(interop).to.deep.equal(result) } }) }