Skip to content

Commit 752f55e

Browse files
committed
tests: minor deduplication
1 parent d220b31 commit 752f55e

4 files changed

Lines changed: 50 additions & 52 deletions

File tree

test/config.test.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,21 @@ import dependencyTree from '../index.js';
55
import { fixtures } from './helpers.js';
66

77
describe('Config', () => {
8-
it('pre-parses tsconfig for performance', () => {
8+
describe('with a tsConfig path', () => {
99
const tsConfigPath = fixtures('ts', '.tsconfig');
1010
const config = new Config({
1111
filename: 'foo',
1212
directory: 'bar',
1313
tsConfig: tsConfigPath
1414
});
1515

16-
expect(config.tsConfig).toBeTypeOf('object');
17-
});
18-
19-
it('includes tsConfigPath so filing-cabinet can resolve compilerOptions.paths', () => {
20-
const tsConfigPath = fixtures('ts', '.tsconfig');
21-
const config = new Config({
22-
filename: 'foo',
23-
directory: 'bar',
24-
tsConfig: tsConfigPath
16+
it('pre-parses tsconfig for performance', () => {
17+
expect(config.tsConfig).toBeTypeOf('object');
2518
});
2619

27-
expect(config.tsConfigPath).toBe(tsConfigPath);
20+
it('includes tsConfigPath so filing-cabinet can resolve compilerOptions.paths', () => {
21+
expect(config.tsConfigPath).toBe(tsConfigPath);
22+
});
2823
});
2924

3025
it('retains detective config in the clone', () => {

test/formats.test.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -216,25 +216,28 @@ describe('module formats', () => {
216216
expect(results).toHaveLength(2);
217217
});
218218

219-
it('recognizes ts file import from js file when allowJs is on (#104)', () => {
219+
describe('mixedTsJs', () => {
220220
const directory = fixtures('ts', 'mixedTsJs');
221-
const filename = path.join(directory, 'a.js');
222-
const tsConfigPath = path.join(directory, '.tsconfig');
223221

224-
const options = {
225-
filename,
226-
directory,
227-
tsConfig: tsConfigPath
228-
};
229-
const parsedTsConfig = new Config(options).tsConfig;
222+
it('recognizes ts file import from js file when allowJs is on (#104)', () => {
223+
const filename = path.join(directory, 'a.js');
224+
const tsConfigPath = path.join(directory, '.tsconfig');
230225

231-
expect(parsedTsConfig.compilerOptions.allowJs).toBe(true);
226+
const options = {
227+
filename,
228+
directory,
229+
tsConfig: tsConfigPath
230+
};
231+
const parsedTsConfig = new Config(options).tsConfig;
232232

233-
const results = dependencyTree.toList(options);
233+
expect(parsedTsConfig.compilerOptions.allowJs).toBe(true);
234234

235-
const depB = path.join(directory, 'b.ts');
235+
const results = dependencyTree.toList(options);
236236

237-
expect(results[0]).toBe(depB);
237+
const depB = path.join(directory, 'b.ts');
238+
239+
expect(results[0]).toBe(depB);
240+
});
238241
});
239242
});
240243
});

test/index.test.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,10 @@ describe('dependencyTree', () => {
250250
});
251251

252252
it('does not throw on the legacy `root` option', () => {
253-
expect(() => {
254-
const directory = fixtures('onlyRealDeps');
255-
const filename = path.normalize(`${directory}/a.js`);
253+
const directory = fixtures('onlyRealDeps');
254+
const filename = path.normalize(`${directory}/a.js`);
256255

256+
expect(() => {
257257
dependencyTree({ filename, root: directory });
258258
}).not.toThrow();
259259
});
@@ -287,9 +287,10 @@ describe('dependencyTree', () => {
287287
});
288288

289289
describe('memoization (#2)', () => {
290+
const directory = fixtures('amd');
291+
const filename = path.join(directory, 'a.js');
292+
290293
it('accepts a cache object for memoization (#2)', () => {
291-
const directory = fixtures('amd');
292-
const filename = path.join(directory, 'a.js');
293294
const bFile = path.join(directory, 'b.js');
294295
const cFile = path.join(directory, 'c.js');
295296
const cache = {
@@ -307,9 +308,6 @@ describe('dependencyTree', () => {
307308
});
308309

309310
it('returns the precomputed list of a cached entry point', () => {
310-
const directory = fixtures('amd');
311-
const filename = path.join(directory, 'a.js');
312-
313311
const cache = {
314312
[filename]: [] // Shouldn't process the first file's tree
315313
};

test/node-modules.test.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,32 @@ import dependencyTree from '../index.js';
44
import { fixtures } from './helpers.js';
55

66
describe('package-specific node_modules resolution', () => {
7-
const directory = fixtures('es6', 'parentChild');
8-
const filename = path.normalize(`${directory}/module.entry.js`);
9-
const rootChildPath = path.normalize(`${directory}/node_modules/child_node_module/index.main.js`);
10-
const nestedChildPath = path.normalize(`${directory}/node_modules/parent_module_a/node_modules/child_node_module/index.main.js`);
11-
12-
it('finds sub package in node module package', () => {
13-
const treeList = dependencyTree({
14-
filename,
15-
directory,
16-
isListForm: true
7+
describe('with a nested parent/child package layout', () => {
8+
const directory = fixtures('es6', 'parentChild');
9+
const filename = path.normalize(`${directory}/module.entry.js`);
10+
const rootChildPath = path.normalize(`${directory}/node_modules/child_node_module/index.main.js`);
11+
const nestedChildPath = path.normalize(`${directory}/node_modules/parent_module_a/node_modules/child_node_module/index.main.js`);
12+
13+
it('finds sub package in node module package', () => {
14+
const treeList = dependencyTree({
15+
filename,
16+
directory,
17+
isListForm: true
18+
});
19+
20+
expect(treeList).toContain(nestedChildPath);
1721
});
1822

19-
expect(treeList).toContain(nestedChildPath);
20-
});
23+
it('uses correct version of sub package in node module package', () => {
24+
const treeList = dependencyTree({
25+
filename,
26+
directory,
27+
isListForm: true
28+
});
2129

22-
it('uses correct version of sub package in node module package', () => {
23-
const treeList = dependencyTree({
24-
filename,
25-
directory,
26-
isListForm: true
30+
expect(treeList).not.toContain(rootChildPath);
31+
expect(treeList).toContain(nestedChildPath);
2732
});
28-
29-
expect(treeList).not.toContain(rootChildPath);
30-
expect(treeList).toContain(nestedChildPath);
3133
});
3234

3335
it('falls back to entry directory when a node_modules file has no package subpath', () => {

0 commit comments

Comments
 (0)