Skip to content

Commit 88c9a0c

Browse files
committed
Test tweaks
1 parent 338cc0f commit 88c9a0c

File tree

1 file changed

+110
-102
lines changed

1 file changed

+110
-102
lines changed

test/test.mjs

Lines changed: 110 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -14,80 +14,79 @@ import dependencyTree from '../index.js';
1414
const require = createRequire(import.meta.url);
1515
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1616

17-
describe('dependencyTree', () => {
18-
function testTreesForFormat(format, ext = '.js') {
19-
it('returns an object form of the dependency tree for a file', () => {
20-
const root = path.join(__dirname, `/fixtures/${format}`);
21-
const filename = path.normalize(`${root}/a${ext}`);
22-
23-
const tree = dependencyTree({ filename, root });
17+
let _directory;
2418

25-
assert.ok(tree instanceof Object);
19+
function testTreesForFormat(format, ext = '.js') {
20+
it('returns an object form of the dependency tree for a file', () => {
21+
const root = path.join(__dirname, `/fixtures/${format}`);
22+
const filename = path.normalize(`${root}/a${ext}`);
2623

27-
const aSubTree = tree[filename];
28-
29-
assert.ok(aSubTree instanceof Object);
30-
const filesInSubTree = Object.keys(aSubTree);
24+
const tree = dependencyTree({ filename, root });
25+
const aSubTree = tree[filename];
26+
const filesInSubTree = Object.keys(aSubTree);
3127

32-
assert.equal(filesInSubTree.length, 2);
33-
});
34-
}
28+
assert.ok(tree instanceof Object);
29+
assert.ok(aSubTree instanceof Object);
30+
assert.equal(filesInSubTree.length, 2);
31+
});
32+
}
3533

36-
function mockStylus() {
37-
mockfs({
38-
[path.join(__dirname, '/fixtures/stylus')]: {
39-
'a.styl': `
34+
function mockStylus() {
35+
mockfs({
36+
[path.join(__dirname, '/fixtures/stylus')]: {
37+
'a.styl': `
4038
@import "b"
4139
@require "c.styl"
4240
`,
43-
'b.styl': '@import "c"',
44-
'c.styl': ''
45-
}
46-
});
47-
}
41+
'b.styl': '@import "c"',
42+
'c.styl': ''
43+
}
44+
});
45+
}
4846

49-
function mockSass() {
50-
mockfs({
51-
[path.join(__dirname, '/fixtures/sass')]: {
52-
'a.scss': `
47+
function mockSass() {
48+
mockfs({
49+
[path.join(__dirname, '/fixtures/sass')]: {
50+
'a.scss': `
5351
@import "_b";
5452
@import "_c.scss";
5553
`,
56-
'_b.scss': 'body { color: blue; }',
57-
'_c.scss': 'body { color: pink; }'
58-
}
59-
});
60-
}
54+
'_b.scss': 'body { color: blue; }',
55+
'_c.scss': 'body { color: pink; }'
56+
}
57+
});
58+
}
6159

62-
function mockLess() {
63-
mockfs({
64-
[path.join(__dirname, '/fixtures/less')]: {
65-
'a.less': `
60+
function mockLess() {
61+
mockfs({
62+
[path.join(__dirname, '/fixtures/less')]: {
63+
'a.less': `
6664
@import "b.css";
6765
@import "c.less";
6866
`,
69-
'b.css': 'body { color: blue; }',
70-
'c.less': 'body { color: pink; }'
71-
}
72-
});
73-
}
67+
'b.css': 'body { color: blue; }',
68+
'c.less': 'body { color: pink; }'
69+
}
70+
});
71+
}
7472

75-
function mockes6() {
76-
mockfs({
77-
[path.join(__dirname, '/fixtures/es6')]: {
78-
'a.js': `
73+
function mockEs6() {
74+
mockfs({
75+
[path.join(__dirname, '/fixtures/es6')]: {
76+
'a.js': `
7977
import b from './b';
8078
import c from './c';
8179
`,
82-
'b.js': 'export default function() {};',
83-
'c.js': 'export default function() {};',
84-
'jsx.js': 'import c from "./c";\n export default <jsx />;',
85-
'foo.jsx': 'import React from "react";\n import b from "b";\n export default <jsx />;',
86-
'es7.js': 'import c from "./c";\n export default async function foo() {};'
87-
}
88-
});
89-
}
80+
'b.js': 'export default function() {};',
81+
'c.js': 'export default function() {};',
82+
'jsx.js': 'import c from "./c";\n export default <jsx />;',
83+
'foo.jsx': 'import React from "react";\n import b from "b";\n export default <jsx />;',
84+
'es7.js': 'import c from "./c";\n export default async function foo() {};'
85+
}
86+
});
87+
}
9088

89+
describe('dependencyTree', () => {
9190
afterEach(() => {
9291
mockfs.restore();
9392
});
@@ -327,36 +326,42 @@ describe('dependencyTree', () => {
327326
});
328327

329328
describe('throws', () => {
330-
beforeEach(function() {
331-
this._directory = path.join(__dirname, '/fixtures/commonjs');
329+
beforeEach(() => {
330+
_directory = path.join(__dirname, '/fixtures/commonjs');
332331
});
333332

334333
it('throws if the filename is missing', () => {
335-
assert.throws(function() {
334+
assert.throws(() => {
336335
dependencyTree({
337336
filename: undefined,
338-
directory: this._directory
337+
directory: _directory
339338
});
340-
});
339+
}, /^Error: filename not given$/);
341340
});
342341

343342
it('throws if the root is missing', () => {
344343
assert.throws(() => {
345344
dependencyTree({ undefined });
346-
});
345+
}, /^Error: filename not given$/);
347346
});
348347

349-
it('throws if a supplied filter is not a function', () => {
348+
it('throws if the directory is missing', () => {
350349
assert.throws(() => {
351-
const directory = path.join(__dirname, '/fixtures/onlyRealDeps');
352-
const filename = path.normalize(`${directory}/a.js`);
350+
dependencyTree({ filename: 'foo.js', directory: undefined });
351+
}, /^Error: directory not given$/);
352+
});
353+
354+
it('throws if a supplied filter is not a function', () => {
355+
const directory = path.join(__dirname, '/fixtures/onlyRealDeps');
356+
const filename = path.normalize(`${directory}/a.js`);
353357

358+
assert.throws(() => {
354359
dependencyTree({
355360
filename,
356361
directory,
357362
filter: 'foobar'
358363
});
359-
});
364+
}, /^Error: filter must be a function$/);
360365
});
361366

362367
it('does not throw on the legacy `root` option', () => {
@@ -373,21 +378,21 @@ describe('dependencyTree', () => {
373378
});
374379

375380
describe('on file error', () => {
376-
beforeEach(function() {
377-
this._directory = path.join(__dirname, '/fixtures/commonjs');
381+
beforeEach(() => {
382+
_directory = path.join(__dirname, '/fixtures/commonjs');
378383
});
379384

380-
it('does not throw', function() {
385+
it('does not throw', () => {
381386
assert.doesNotThrow(() => {
382387
dependencyTree({
383388
filename: 'foo',
384-
directory: this._directory
389+
directory: _directory
385390
});
386391
});
387392
});
388393

389-
it('returns no dependencies', function() {
390-
const tree = dependencyTree({ filename: 'foo', directory: this._directory });
394+
it('returns no dependencies', () => {
395+
const tree = dependencyTree({ filename: 'foo', directory: _directory });
391396
// eslint-disable-next-line unicorn/explicit-length-check
392397
assert.ok(!tree.length);
393398
});
@@ -418,15 +423,17 @@ describe('dependencyTree', () => {
418423
});
419424

420425
describe('memoization (#2)', () => {
421-
beforeEach(function() {
422-
this._spy = sinon.spy(dependencyTree, '_getDependencies');
426+
let _spy;
427+
428+
beforeEach(() => {
429+
_spy = sinon.spy(dependencyTree, '_getDependencies');
423430
});
424431

425432
afterEach(() => {
426433
dependencyTree._getDependencies.restore();
427434
});
428435

429-
it('accepts a cache object for memoization (#2)', function() {
436+
it('accepts a cache object for memoization (#2)', () => {
430437
const filename = path.join(__dirname, '/fixtures/amd/a.js');
431438
const directory = path.join(__dirname, '/fixtures/amd');
432439
const cache = {};
@@ -443,16 +450,15 @@ describe('dependencyTree', () => {
443450
});
444451

445452
assert.equal(Object.keys(tree[filename]).length, 2);
446-
assert.ok(this._spy.neverCalledWith(path.join(__dirname, '/fixtures/amd/b.js')));
453+
assert.ok(_spy.neverCalledWith(path.join(__dirname, '/fixtures/amd/b.js')));
447454
});
448455

449456
it('returns the precomputed list of a cached entry point', () => {
450457
const filename = path.join(__dirname, '/fixtures/amd/a.js');
451458
const directory = path.join(__dirname, '/fixtures/amd');
452459

453460
const cache = {
454-
// Shouldn't process the first file's tree
455-
[filename]: []
461+
[filename]: [] // Shouldn't process the first file's tree
456462
};
457463

458464
const tree = dependencyTree({
@@ -530,41 +536,41 @@ describe('dependencyTree', () => {
530536
});
531537

532538
describe('es6', () => {
533-
beforeEach(function() {
534-
this._directory = path.join(__dirname, '/fixtures/es6');
535-
mockes6();
539+
beforeEach(() => {
540+
_directory = path.join(__dirname, '/fixtures/es6');
541+
mockEs6();
536542
});
537543

538544
testTreesForFormat('es6');
539545

540-
it('resolves files that have jsx', function() {
541-
const filename = path.normalize(`${this._directory}/jsx.js`);
546+
it('resolves files that have jsx', () => {
547+
const filename = path.normalize(`${_directory}/jsx.js`);
542548
const { [filename]: tree } = dependencyTree({
543549
filename,
544-
directory: this._directory
550+
directory: _directory
545551
});
546552

547-
assert.ok(tree[path.normalize(`${this._directory}/c.js`)]);
553+
assert.ok(tree[path.normalize(`${_directory}/c.js`)]);
548554
});
549555

550-
it('resolves files with a jsx extension', function() {
551-
const filename = path.normalize(`${this._directory}/foo.jsx`);
556+
it('resolves files with a jsx extension', () => {
557+
const filename = path.normalize(`${_directory}/foo.jsx`);
552558
const { [filename]: tree } = dependencyTree({
553559
filename,
554-
directory: this._directory
560+
directory: _directory
555561
});
556562

557-
assert.ok(tree[path.normalize(`${this._directory}/b.js`)]);
563+
assert.ok(tree[path.normalize(`${_directory}/b.js`)]);
558564
});
559565

560-
it('resolves files that have es7', function() {
561-
const filename = path.normalize(`${this._directory}/es7.js`);
566+
it('resolves files that have es7', () => {
567+
const filename = path.normalize(`${_directory}/es7.js`);
562568
const { [filename]: tree } = dependencyTree({
563569
filename,
564-
directory: this._directory
570+
directory: _directory
565571
});
566572

567-
assert.ok(tree[path.normalize(`${this._directory}/c.js`)]);
573+
assert.ok(tree[path.normalize(`${_directory}/c.js`)]);
568574
});
569575

570576
describe('when given an es6 file using CJS lazy requires', () => {
@@ -795,7 +801,7 @@ describe('dependencyTree', () => {
795801

796802
describe('es6', () => {
797803
beforeEach(() => {
798-
mockes6();
804+
mockEs6();
799805
});
800806

801807
testToList('es6');
@@ -832,30 +838,32 @@ describe('dependencyTree', () => {
832838
});
833839

834840
describe('webpack', () => {
835-
beforeEach(function() {
841+
let testResolution;
842+
843+
beforeEach(() => {
836844
// Note: not mocking because webpack's resolver needs a real project with dependencies;
837845
// otherwise, we'd have to mock a ton of files.
838-
this._root = path.join(__dirname, '../');
839-
this._webpackConfig = `${this._root}/webpack.config.js`;
846+
const root = path.join(__dirname, '../');
847+
const webpackConfig = `${root}/webpack.config.js`;
840848

841-
this._testResolution = name => {
849+
testResolution = name => {
842850
const results = dependencyTree.toList({
843851
filename: path.join(__dirname, `/fixtures/webpack/${name}.js`),
844-
directory: this._root,
845-
webpackConfig: this._webpackConfig,
852+
directory: root,
853+
webpackConfig,
846854
filter: filename => filename.includes('filing-cabinet')
847855
});
848856

849857
assert.ok(results.some(filename => filename.includes(path.normalize('node_modules/filing-cabinet'))));
850858
};
851859
});
852860

853-
it('resolves aliased modules', function() {
854-
this._testResolution('aliased');
861+
it('resolves aliased modules', () => {
862+
testResolution('aliased');
855863
});
856864

857-
it('resolves unaliased modules', function() {
858-
this._testResolution('unaliased');
865+
it('resolves unaliased modules', () => {
866+
testResolution('unaliased');
859867
});
860868
});
861869

0 commit comments

Comments
 (0)