Skip to content

Commit 1150842

Browse files
fix bit-javascript unit tests (teambit#2634)
* fix many tests * fix path and remove many ts-ignore * fix file paths calculation for dependency tree * add !fixtures/filing-cabinet/node_modules to avoid git ignoring it. * fix lint issues Co-authored-by: David First <[email protected]>
1 parent 2c1ca18 commit 1150842

File tree

10 files changed

+80
-94
lines changed

10 files changed

+80
-94
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ build/Release
2929
# Dependency directories
3030
node_modules
3131
jspm_packages
32+
!fixtures/filing-cabinet/node_modules
3233

3334
# Optional npm cache directory
3435
.npm

fixtures/filing-cabinet/node_modules/bootstrap/index.scss

Whitespace-only changes.

src/consumer/component/dependencies/files-dependency-builder/build-tree.spec.ts

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,62 @@ import path from 'path';
22
import { expect } from 'chai';
33
import * as buildTree from './build-tree';
44

5-
const fixtures = `${__dirname}/../../fixtures`;
5+
const fixtures = `${__dirname}/../../../../../fixtures`;
66
const precinctFixtures = path.join(fixtures, 'precinct');
77
const buildTreeFixtures = path.join(fixtures, 'build-tree');
88

99
describe('buildTree', () => {
1010
describe('getDependencyTree', () => {
11+
const filePaths: string[] = [];
12+
let visited: any;
1113
const dependencyTreeParams = {
1214
baseDir: '.',
1315
workspacePath: __dirname,
14-
filePaths: [],
16+
filePaths,
1517
bindingPrefix: '@bit',
18+
visited,
1619
resolveModulesConfig: undefined
1720
};
1821
it('when no files are passed should return an empty tree', async () => {
19-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
2022
const results = await buildTree.getDependencyTree(dependencyTreeParams);
2123
expect(results).to.deep.equal({ tree: {} });
2224
});
2325
it('when unsupported files are passed should return them with no dependencies', async () => {
24-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
2526
dependencyTreeParams.filePaths = [`${fixtures}/unsupported-file.pdf`];
26-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
2727
const results = await buildTree.getDependencyTree(dependencyTreeParams);
2828
expect(results.tree).to.deep.equal({ 'fixtures/unsupported-file.pdf': {} });
2929
});
3030
it('when supported and unsupported files are passed should return them all', async () => {
31-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
32-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
3331
dependencyTreeParams.filePaths = [`${fixtures}/unsupported-file.pdf`, `${precinctFixtures}/es6.js`];
34-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
3532
const results = await buildTree.getDependencyTree(dependencyTreeParams);
3633
expect(results.tree).to.have.property('fixtures/unsupported-file.pdf');
3734
expect(results.tree).to.have.property('fixtures/precinct/es6.js');
3835
});
3936
it('when a js file has parsing error it should add the file to the tree with the error instance', async () => {
40-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
4137
dependencyTreeParams.filePaths = [`${precinctFixtures}/unparseable.js`];
42-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
4338
const results = await buildTree.getDependencyTree(dependencyTreeParams);
4439
const unParsedFile = 'fixtures/precinct/unparseable.js';
4540
expect(results.tree).to.have.property(unParsedFile);
4641
expect(results.tree[unParsedFile]).to.have.property('error');
4742
expect(results.tree[unParsedFile].error).to.be.instanceof(Error);
4843
});
4944
it('when a js file has parsing error and it retrieved from the cache it should add the file to the tree with the error instance', async () => {
50-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
5145
dependencyTreeParams.filePaths = [`${precinctFixtures}/unparseable.js`];
52-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
5346
dependencyTreeParams.visited = {};
54-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
5547
const results = await buildTree.getDependencyTree(dependencyTreeParams);
5648
const unParsedFile = 'fixtures/precinct/unparseable.js';
5749
expect(results.tree).to.have.property(unParsedFile);
5850
expect(results.tree[unParsedFile]).to.have.property('error');
5951
expect(results.tree[unParsedFile].error).to.be.instanceof(Error);
6052

6153
// second time, this time it fetches from the cache (visited object)
62-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
6354
const resultsCached = await buildTree.getDependencyTree(dependencyTreeParams);
6455
expect(resultsCached.tree).to.have.property(unParsedFile);
6556
expect(resultsCached.tree[unParsedFile]).to.have.property('error');
6657
expect(resultsCached.tree[unParsedFile].error).to.be.instanceof(Error);
6758
});
6859
it.skip('when a css file has parsing error it should add the file to the tree with the error instance', async () => {
69-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
7060
dependencyTreeParams.filePaths = [`${buildTreeFixtures}/unparsed.css`];
71-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
7261
const results = await buildTree.getDependencyTree(dependencyTreeParams);
7362
const unParsedFile = 'fixtures/build-tree/unparsed.css';
7463
expect(results.tree).to.have.property(unParsedFile);
@@ -78,10 +67,7 @@ describe('buildTree', () => {
7867
describe('when a dependency of dependency has parsing error', () => {
7968
let results;
8069
before(async () => {
81-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
82-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
8370
dependencyTreeParams.filePaths = [`${buildTreeFixtures}/a.js`, `${buildTreeFixtures}/b.js`];
84-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
8571
results = await buildTree.getDependencyTree(dependencyTreeParams);
8672
});
8773
it('should add all the files to the tree', async () => {
@@ -102,9 +88,7 @@ describe('buildTree', () => {
10288
let results;
10389
const missingDepsFile = 'fixtures/missing-deps.js';
10490
before(async () => {
105-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
10691
dependencyTreeParams.filePaths = [`${fixtures}/missing-deps.js`];
107-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
10892
results = await buildTree.getDependencyTree(dependencyTreeParams);
10993
expect(results.tree).to.have.property(missingDepsFile);
11094
expect(results.tree[missingDepsFile]).to.have.property('missing');
@@ -122,9 +106,7 @@ describe('buildTree', () => {
122106
describe('when a file imports from itself', () => {
123107
let results;
124108
before(async () => {
125-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
126109
dependencyTreeParams.filePaths = [`${buildTreeFixtures}/tree-shaking-cycle/self-cycle.js`];
127-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
128110
results = await buildTree.getDependencyTree(dependencyTreeParams);
129111
});
130112
it('should not throw an error and should remove itself from the dependencies files', () => {
@@ -135,9 +117,7 @@ describe('buildTree', () => {
135117
describe('cycle with multiple files', () => {
136118
let results;
137119
before(async () => {
138-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
139120
dependencyTreeParams.filePaths = [`${buildTreeFixtures}/tree-shaking-cycle/foo.js`];
140-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
141121
results = await buildTree.getDependencyTree(dependencyTreeParams);
142122
});
143123
it('should not recognize the cycle dependencies as link files', () => {
@@ -155,9 +135,7 @@ describe('buildTree', () => {
155135
describe('fileA imports varX from fileB, fileB imports varX from fileC but not export it', () => {
156136
let results;
157137
before(async () => {
158-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
159138
dependencyTreeParams.filePaths = [`${buildTreeFixtures}/not-link-file/file-a.js`];
160-
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
161139
results = await buildTree.getDependencyTree(dependencyTreeParams);
162140
});
163141
it('should not mark fileB as a link file', () => {

src/consumer/component/dependencies/files-dependency-builder/build-tree.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ export async function getDependencyTree({
445445
if (filePath.startsWith(baseDir)) {
446446
return filePath;
447447
}
448-
return path.join(baseDir, filePath);
448+
return path.resolve(baseDir, filePath);
449449
});
450450
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
451451
const { madgeTree, skipped, pathMap, errors } = generateTree(fullPaths, config);

src/consumer/component/dependencies/files-dependency-builder/dependency-tree/index.spec.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import assert from 'assert';
22
import sinon from 'sinon';
3-
import mockfs from 'mock-fs';
43
import path from 'path';
4+
import mockfs from 'mock-fs';
55
import rewire from 'rewire';
6+
import Config from './Config';
7+
import precinct from '../precinct';
68

79
const expect = require('chai').expect;
8-
const precinct = require('../precinct');
9-
const Config = require('./Config');
1010

1111
// needed for the lazy loading.
1212
require('module-definition');
@@ -16,10 +16,18 @@ require('../../../../../utils');
1616
require('../../../../../utils/is-relative-import');
1717
require('../detectives/detective-css-and-preprocessors');
1818
require('../detectives/detective-typescript');
19+
require('../detectives/detective-css');
20+
require('../detectives/detective-sass');
21+
require('../detectives/detective-scss');
22+
require('../detectives/detective-less');
1923
require('../detectives/parser-helper');
24+
require('../dependency-tree/Config');
25+
require('../precinct');
26+
require('../filing-cabinet');
2027

21-
const dependencyTree = rewire('./');
22-
const fixtures = path.resolve(`${__dirname}/../../../fixtures/dependency-tree`);
28+
const dependencyTreeRewired = rewire('./');
29+
const dependencyTree = dependencyTreeRewired.default;
30+
const fixtures = path.resolve(`${__dirname}/../../../../../../fixtures/dependency-tree`);
2331

2432
describe('dependencyTree', function() {
2533
this.timeout(8000);
@@ -187,14 +195,14 @@ describe('dependencyTree', function() {
187195
const directory = `${__dirname}/cyclic`;
188196
const filename = path.normalize(`${directory}/a.js`);
189197

190-
const spy = sinon.spy(dependencyTree, '_getDependencies');
198+
const spy = sinon.spy(dependencyTreeRewired, '_getDependencies');
191199

192-
const tree = dependencyTree({ filename, directory });
200+
const tree = dependencyTreeRewired.default({ filename, directory });
193201

194202
assert(spy.callCount === 2);
195203
assert(Object.keys(tree[filename]).length);
196204

197-
dependencyTree._getDependencies.restore();
205+
dependencyTreeRewired._getDependencies.restore();
198206
});
199207

200208
it('excludes Nodejs core modules by default', () => {
@@ -337,7 +345,7 @@ describe('dependencyTree', function() {
337345
// @ts-ignore
338346
this._directory = `${fixtures}/commonjs`;
339347
// @ts-ignore
340-
this._revert = dependencyTree.__set__('traverse', () => []);
348+
this._revert = dependencyTreeRewired.__set__('traverse', () => []);
341349
});
342350

343351
afterEach(() => {
@@ -413,11 +421,11 @@ describe('dependencyTree', function() {
413421
describe('memoization (#2)', () => {
414422
beforeEach(() => {
415423
// @ts-ignore
416-
this._spy = sinon.spy(dependencyTree, '_getDependencies');
424+
this._spy = sinon.spy(dependencyTreeRewired, '_getDependencies');
417425
});
418426

419427
afterEach(() => {
420-
dependencyTree._getDependencies.restore();
428+
dependencyTreeRewired._getDependencies.restore();
421429
});
422430

423431
it('accepts a cache object for memoization (#2)', () => {

src/consumer/component/dependencies/files-dependency-builder/detectives/detective-es6/index.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
*/
44

55
import { expect } from 'chai';
6+
import detective from './';
67

78
const assert = require('assert');
8-
const detective = require('./');
99

1010
describe('detective-es6', () => {
1111
const ast = {
@@ -97,6 +97,7 @@ describe('detective-es6', () => {
9797
it('throws when content is not provided', function() {
9898
assert.throws(
9999
function() {
100+
// @ts-ignore
100101
detective();
101102
},
102103
Error,
@@ -132,44 +133,55 @@ describe('detective-es6', () => {
132133
it('should recognize default imports as default', () => {
133134
const deps = detective('import foo from "foo";');
134135
expect(deps).to.have.property('foo');
136+
// @ts-ignore
135137
expect(deps.foo).to.have.property('importSpecifiers');
138+
// @ts-ignore
136139
const importSpecifier = deps.foo.importSpecifiers[0];
137140
expect(importSpecifier.name).to.equal('foo');
138141
expect(importSpecifier.isDefault).to.be.true;
139142
});
140143
it('should recognize non-default imports as non-default', () => {
141144
const deps = detective('import { foo } from "foo";');
142145
expect(deps).to.have.property('foo');
146+
// @ts-ignore
143147
expect(deps.foo).to.have.property('importSpecifiers');
148+
// @ts-ignore
144149
const importSpecifier = deps.foo.importSpecifiers[0];
145150
expect(importSpecifier.name).to.equal('foo');
146151
expect(importSpecifier.isDefault).to.be.false;
147152
});
148153
it('should support export-default-as syntax', () => {
149154
const deps = detective('export { default as foo } from "foo";');
150155
expect(deps).to.have.property('foo');
156+
// @ts-ignore
151157
expect(deps.foo).to.have.property('importSpecifiers');
158+
// @ts-ignore
152159
const importSpecifier = deps.foo.importSpecifiers[0];
153160
expect(importSpecifier.name).to.equal('foo');
154161
expect(importSpecifier.isDefault).to.be.true;
155162
});
156163
it('should not be supported for CommonJS', () => {
157164
const deps = detective('const foo = require("foo");');
158165
expect(deps).to.have.property('foo');
166+
// @ts-ignore
159167
expect(deps.foo).to.not.have.property('importSpecifiers');
160168
});
161169
it('should add "exported": true if the same variable has been imported and exported', () => {
162170
const deps = detective('import { foo } from "foo"; export default foo;');
163171
expect(deps).to.have.property('foo');
172+
// @ts-ignore
164173
expect(deps.foo).to.have.property('importSpecifiers');
174+
// @ts-ignore
165175
const importSpecifier = deps.foo.importSpecifiers[0];
166176
expect(importSpecifier.name).to.equal('foo');
167177
expect(importSpecifier.exported).to.be.true;
168178
});
169179
it('should not add "exported" property if the variable has been imported but not exported', () => {
170180
const deps = detective('import { foo } from "foo";');
171181
expect(deps).to.have.property('foo');
182+
// @ts-ignore
172183
expect(deps.foo).to.have.property('importSpecifiers');
184+
// @ts-ignore
173185
const importSpecifier = deps.foo.importSpecifiers[0];
174186
expect(importSpecifier.name).to.equal('foo');
175187
expect(importSpecifier).to.not.have.property('exported');

0 commit comments

Comments
 (0)