bit-bin
Version:
<a href="https://opensource.org/licenses/Apache-2.0"><img alt="apache" src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"></a> <a href="https://github.com/teambit/bit/blob/master/CONTRIBUTING.md"><img alt="prs" src="https://img.shields.io/b
189 lines (168 loc) • 6.92 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
function _chai() {
const data = require("chai");
_chai = function () {
return data;
};
return data;
}
function _() {
const data = _interopRequireDefault(require("./"));
_ = function () {
return data;
};
return data;
}
/**
* this file had been forked (and changed since then) from https://github.com/dependents/node-detective-es6
*/
const assert = require('assert');
describe('detective-es6', () => {
const ast = {
type: 'Program',
body: [{
type: 'VariableDeclaration',
declarations: [{
type: 'VariableDeclarator',
id: {
type: 'Identifier',
name: 'x'
},
init: {
type: 'Literal',
value: 4,
raw: '4'
}
}],
kind: 'let'
}]
};
it('accepts an ast', () => {
const deps = (0, _().default)(ast);
const depsKeys = Object.keys(deps);
assert(!depsKeys.length);
});
it('retrieves the dependencies of es6 modules', () => {
const deps = (0, _().default)('import {foo, bar} from "mylib";');
const depsKeys = Object.keys(deps);
assert(depsKeys.length === 1);
assert(depsKeys[0] === 'mylib');
});
it('retrieves the re-export dependencies of es6 modules', () => {
const deps = (0, _().default)('export {foo, bar} from "mylib";');
const depsKeys = Object.keys(deps);
assert(depsKeys.length === 1);
assert(depsKeys[0] === 'mylib');
});
it('retrieves the re-export * dependencies of es6 modules', () => {
const deps = (0, _().default)('export * from "mylib";');
const depsKeys = Object.keys(deps);
assert(depsKeys.length === 1);
assert(depsKeys[0] === 'mylib');
});
it('handles multiple imports', () => {
const deps = (0, _().default)('import {foo, bar} from "mylib";\nimport "mylib2"');
const depsKeys = Object.keys(deps);
assert(depsKeys.length === 2);
assert(depsKeys[0] === 'mylib');
assert(depsKeys[1] === 'mylib2');
});
it('handles default imports', () => {
const deps = (0, _().default)('import foo from "foo";');
const depsKeys = Object.keys(deps);
assert(depsKeys.length === 1);
assert(depsKeys[0] === 'foo');
});
it('handles dynamic imports', function () {
const deps = (0, _().default)('import("foo").then(foo => foo());');
const depsKeys = Object.keys(deps);
assert(depsKeys.length === 1);
assert(depsKeys[0] === 'foo');
});
it('should support commonJS syntax', function () {
const deps = (0, _().default)('var foo = require("foo");');
const depsKeys = Object.keys(deps);
assert(depsKeys.length === 1);
assert(depsKeys[0] === 'foo');
});
it('returns an empty list for empty files', function () {
const deps = (0, _().default)('');
const depsKeys = Object.keys(deps);
assert.equal(depsKeys.length, 0);
});
it('throws when content is not provided', function () {
assert.throws(function () {
// @ts-ignore
(0, _().default)();
}, Error, 'src not given');
});
it('does not throw with jsx in a module', function () {
assert.doesNotThrow(function () {
(0, _().default)("import foo from 'foo'; var templ = <jsx />;");
});
});
it('does not throw on an async ES7 function', function () {
assert.doesNotThrow(function () {
(0, _().default)("import foo from 'foo'; export default async function bar() {}");
});
});
describe('string in apostrophes', () => {
it('should recognize when using require statement', () => {
const deps = (0, _().default)('const foo = require(`foo`);'); // eslint-disable-line
const depsKeys = Object.keys(deps);
assert.equal(depsKeys.length, 1);
assert.equal(depsKeys[0], 'foo');
});
it('should throw when using import syntax', () => {
(0, _chai().expect)(() => (0, _().default)('import foo from `foo`;')).to.throw(); // eslint-disable-line
});
});
describe('import-specifiers detection (for tree shaking)', () => {
it('should recognize default imports as default', () => {
const deps = (0, _().default)('import foo from "foo";');
(0, _chai().expect)(deps).to.have.property('foo'); // @ts-ignore
(0, _chai().expect)(deps.foo).to.have.property('importSpecifiers'); // @ts-ignore
const importSpecifier = deps.foo.importSpecifiers[0];
(0, _chai().expect)(importSpecifier.name).to.equal('foo');
(0, _chai().expect)(importSpecifier.isDefault).to.be.true;
});
it('should recognize non-default imports as non-default', () => {
const deps = (0, _().default)('import { foo } from "foo";');
(0, _chai().expect)(deps).to.have.property('foo'); // @ts-ignore
(0, _chai().expect)(deps.foo).to.have.property('importSpecifiers'); // @ts-ignore
const importSpecifier = deps.foo.importSpecifiers[0];
(0, _chai().expect)(importSpecifier.name).to.equal('foo');
(0, _chai().expect)(importSpecifier.isDefault).to.be.false;
});
it('should support export-default-as syntax', () => {
const deps = (0, _().default)('export { default as foo } from "foo";');
(0, _chai().expect)(deps).to.have.property('foo'); // @ts-ignore
(0, _chai().expect)(deps.foo).to.have.property('importSpecifiers'); // @ts-ignore
const importSpecifier = deps.foo.importSpecifiers[0];
(0, _chai().expect)(importSpecifier.name).to.equal('foo');
(0, _chai().expect)(importSpecifier.isDefault).to.be.true;
});
it('should not be supported for CommonJS', () => {
const deps = (0, _().default)('const foo = require("foo");');
(0, _chai().expect)(deps).to.have.property('foo'); // @ts-ignore
(0, _chai().expect)(deps.foo).to.not.have.property('importSpecifiers');
});
it('should add "exported": true if the same variable has been imported and exported', () => {
const deps = (0, _().default)('import { foo } from "foo"; export default foo;');
(0, _chai().expect)(deps).to.have.property('foo'); // @ts-ignore
(0, _chai().expect)(deps.foo).to.have.property('importSpecifiers'); // @ts-ignore
const importSpecifier = deps.foo.importSpecifiers[0];
(0, _chai().expect)(importSpecifier.name).to.equal('foo');
(0, _chai().expect)(importSpecifier.exported).to.be.true;
});
it('should not add "exported" property if the variable has been imported but not exported', () => {
const deps = (0, _().default)('import { foo } from "foo";');
(0, _chai().expect)(deps).to.have.property('foo'); // @ts-ignore
(0, _chai().expect)(deps.foo).to.have.property('importSpecifiers'); // @ts-ignore
const importSpecifier = deps.foo.importSpecifiers[0];
(0, _chai().expect)(importSpecifier.name).to.equal('foo');
(0, _chai().expect)(importSpecifier).to.not.have.property('exported');
});
});
});
;