|
| 1 | +import { expect } from 'chai' |
| 2 | +import * as path from 'path' |
| 3 | + |
| 4 | +import importType from 'core/importType' |
| 5 | + |
| 6 | +import { testContext } from '../utils' |
| 7 | + |
| 8 | +describe('importType(name)', function () { |
| 9 | + const context = testContext() |
| 10 | + |
| 11 | + it("should return 'builtin' for node.js modules", function() { |
| 12 | + expect(importType('fs', context)).to.equal('builtin') |
| 13 | + expect(importType('path', context)).to.equal('builtin') |
| 14 | + }) |
| 15 | + |
| 16 | + it("should return 'external' for non-builtin modules without a relative path", function() { |
| 17 | + expect(importType('lodash', context)).to.equal('external') |
| 18 | + expect(importType('async', context)).to.equal('external') |
| 19 | + expect(importType('chalk', context)).to.equal('external') |
| 20 | + expect(importType('foo', context)).to.equal('external') |
| 21 | + expect(importType('lodash.find', context)).to.equal('external') |
| 22 | + expect(importType('lodash/fp', context)).to.equal('external') |
| 23 | + }) |
| 24 | + |
| 25 | + it("should return 'project' for non-builtins resolved outside of node_modules", function () { |
| 26 | + const pathContext = testContext({ "import/resolver": { node: { paths: [ path.join(__dirname, '..', '..', 'files') ] } } }) |
| 27 | + expect(importType('importType', pathContext)).to.equal('project') |
| 28 | + }) |
| 29 | + |
| 30 | + it("should return 'parent' for internal modules that go through the parent", function() { |
| 31 | + expect(importType('../foo', context)).to.equal('parent') |
| 32 | + expect(importType('../../foo', context)).to.equal('parent') |
| 33 | + expect(importType('../bar/foo', context)).to.equal('parent') |
| 34 | + }) |
| 35 | + |
| 36 | + it("should return 'sibling' for internal modules that are connected to one of the siblings", function() { |
| 37 | + expect(importType('./foo', context)).to.equal('sibling') |
| 38 | + expect(importType('./foo/bar', context)).to.equal('sibling') |
| 39 | + }) |
| 40 | + |
| 41 | + describe("should return 'index' for sibling index file when", function() { |
| 42 | + it("resolves", function() { |
| 43 | + expect(importType('./importType', context)).to.equal('index') |
| 44 | + expect(importType('./importType/', context)).to.equal('index') |
| 45 | + expect(importType('./importType/index', context)).to.equal('index') |
| 46 | + expect(importType('./importType/index.js', context)).to.equal('index') |
| 47 | + }) |
| 48 | + it("doesn't resolve", function() { |
| 49 | + expect(importType('.', context)).to.equal('index') |
| 50 | + expect(importType('./', context)).to.equal('index') |
| 51 | + expect(importType('./index', context)).to.equal('index') |
| 52 | + expect(importType('./index.js', context)).to.equal('index') |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + it("should return 'unknown' for any unhandled cases", function() { |
| 57 | + expect(importType('/malformed', context)).to.equal('unknown') |
| 58 | + expect(importType(' foo', context)).to.equal('unknown') |
| 59 | + }) |
| 60 | +}) |
0 commit comments