forked from jestjs/jest
/
dependency_resolver.test.ts
108 lines (97 loc) 路 3.45 KB
/
dependency_resolver.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {tmpdir} from 'os';
import path from 'path';
import {Config} from '@jest/types';
import {buildSnapshotResolver} from 'jest-snapshot';
import {makeProjectConfig} from '../../../../TestUtils';
import DependencyResolver from '../index';
const maxWorkers = 1;
let dependencyResolver: DependencyResolver;
let Runtime;
let config: Config.ProjectConfig;
const cases: {[key: string]: jest.Mock} = {
fancyCondition: jest.fn(path => path.length > 10),
testRegex: jest.fn(path => /.test.js$/.test(path)),
};
const filter = (path: Config.Path) =>
Object.keys(cases).every(key => cases[key](path));
beforeEach(() => {
Runtime = require('jest-runtime');
config = makeProjectConfig({
cacheDirectory: path.resolve(tmpdir(), 'jest-resolve-dependencies-test'),
moduleDirectories: ['node_modules'],
rootDir: '.',
roots: ['./packages/jest-resolve-dependencies'],
});
return Runtime.createContext(config, {maxWorkers, watchman: false}).then(
(runtimeContext: any) => {
dependencyResolver = new DependencyResolver(
runtimeContext.resolver,
runtimeContext.hasteFS,
buildSnapshotResolver(config),
);
},
);
});
test('resolves no dependencies for non-existent path', () => {
const resolved = dependencyResolver.resolve('/non/existent/path');
expect(resolved.length).toEqual(0);
});
test('resolves dependencies for existing path', () => {
const resolved = dependencyResolver.resolve(
path.resolve(__dirname, '__fixtures__', 'file.js'),
);
expect(resolved).toEqual([
expect.stringContaining('jest-resolve-dependencies'),
expect.stringContaining('jest-regex-util'),
]);
});
test('resolves dependencies for scoped packages', () => {
const resolved = dependencyResolver.resolve(
path.resolve(__dirname, '__fixtures__', 'scoped.js'),
);
expect(resolved).toEqual([
expect.stringContaining(path.join('@myorg', 'pkg')),
]);
});
test('resolves no inverse dependencies for empty paths set', () => {
const paths = new Set();
const resolved = dependencyResolver.resolveInverse(paths, filter);
expect(resolved.length).toEqual(0);
});
test('resolves no inverse dependencies for set of non-existent paths', () => {
const paths = new Set(['/non/existent/path', '/another/one']);
const resolved = dependencyResolver.resolveInverse(paths, filter);
expect(resolved.length).toEqual(0);
});
test('resolves inverse dependencies for existing path', () => {
const paths = new Set([path.resolve(__dirname, '__fixtures__/file.js')]);
const resolved = dependencyResolver.resolveInverse(paths, filter);
expect(resolved).toEqual([
expect.stringContaining(
path.join('__tests__', '__fixtures__', 'file.test.js'),
),
]);
});
test('resolves inverse dependencies from available snapshot', () => {
const paths = new Set([
path.resolve(__dirname, '__fixtures__/file.js'),
path.resolve(__dirname, '__fixtures__/__snapshots__/related.test.js.snap'),
]);
const resolved = dependencyResolver.resolveInverse(paths, filter);
expect(resolved).toEqual(
expect.arrayContaining([
expect.stringContaining(
path.join('__tests__', '__fixtures__', 'file.test.js'),
),
expect.stringContaining(
path.join('__tests__', '__fixtures__', 'related.test.js'),
),
]),
);
});