-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
getProjectConfigFiles.test.ts
168 lines (131 loc) · 5.12 KB
/
getProjectConfigFiles.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import path from 'path';
import { ExpiringCache } from '../../src/parseSettings/ExpiringCache';
import { getProjectConfigFiles } from '../../src/parseSettings/getProjectConfigFiles';
const mockExistsSync = jest.fn<boolean, [string]>();
jest.mock('fs', () => ({
...jest.requireActual('fs'),
existsSync: (filePath: string): boolean => mockExistsSync(filePath),
}));
const parseSettings = {
filePath: './repos/repo/packages/package/file.ts',
tsconfigMatchCache: new ExpiringCache<string, string>(1),
tsconfigRootDir: './repos/repo',
};
beforeEach(() => {
parseSettings.tsconfigMatchCache.clear();
jest.clearAllMocks();
});
describe('getProjectConfigFiles', () => {
it('returns an array with just the project when given as a string', () => {
const project = './tsconfig.eslint.json';
const actual = getProjectConfigFiles(parseSettings, project);
expect(actual).toEqual([project]);
});
it('returns the project when given as a string array', () => {
const project = ['./tsconfig.eslint.json'];
const actual = getProjectConfigFiles(parseSettings, project);
expect(actual).toEqual(project);
});
it('returns the project when given as undefined', () => {
const project = undefined;
const actual = getProjectConfigFiles(parseSettings, project);
expect(actual).toEqual(project);
});
describe('when caching hits', () => {
it('returns a local tsconfig.json without calling existsSync a second time', () => {
mockExistsSync.mockReturnValue(true);
getProjectConfigFiles(parseSettings, true);
const actual = getProjectConfigFiles(parseSettings, true);
expect(actual).toEqual([
path.normalize('repos/repo/packages/package/tsconfig.json'),
]);
expect(mockExistsSync).toHaveBeenCalledTimes(1);
});
it('returns a nearby parent tsconfig.json when it was previously cached by a different directory search', () => {
mockExistsSync.mockImplementation(
input => input === path.normalize('a/tsconfig.json'),
);
const tsconfigMatchCache = new ExpiringCache<string, string>(1);
// This should call to fs.existsSync three times: c, b, a
getProjectConfigFiles(
{
filePath: './a/b/c/d.ts',
tsconfigRootDir: './a',
tsconfigMatchCache,
},
true,
);
// This should call to fs.existsSync once: e
// Then it should retrieve c from cache, pointing to a
const actual = getProjectConfigFiles(
{
filePath: './a/b/c/e/f.ts',
tsconfigRootDir: './a',
tsconfigMatchCache,
},
true,
);
expect(actual).toEqual([path.normalize('a/tsconfig.json')]);
expect(mockExistsSync).toHaveBeenCalledTimes(4);
});
it('returns a distant parent tsconfig.json when it was previously cached by a different directory search', () => {
mockExistsSync.mockImplementation(
input => input === path.normalize('a/tsconfig.json'),
);
const tsconfigMatchCache = new ExpiringCache<string, string>(1);
// This should call to fs.existsSync 4 times: d, c, b, a
getProjectConfigFiles(
{
filePath: './a/b/c/d/e.ts',
tsconfigRootDir: './a',
tsconfigMatchCache,
},
true,
);
// This should call to fs.existsSync 2: g, f
// Then it should retrieve b from cache, pointing to a
const actual = getProjectConfigFiles(
{
filePath: './a/b/f/g/h.ts',
tsconfigRootDir: './a',
tsconfigMatchCache,
},
true,
);
expect(actual).toEqual([path.normalize('a/tsconfig.json')]);
expect(mockExistsSync).toHaveBeenCalledTimes(6);
});
});
describe('when caching misses', () => {
it('returns a local tsconfig.json when matched', () => {
mockExistsSync.mockReturnValue(true);
const actual = getProjectConfigFiles(parseSettings, true);
expect(actual).toEqual([
path.normalize('repos/repo/packages/package/tsconfig.json'),
]);
});
it('returns a parent tsconfig.json when matched', () => {
mockExistsSync.mockImplementation(
filePath => filePath === path.normalize('repos/repo/tsconfig.json'),
);
const actual = getProjectConfigFiles(parseSettings, true);
expect(actual).toEqual([path.normalize('repos/repo/tsconfig.json')]);
});
it('throws when searching hits .', () => {
mockExistsSync.mockReturnValue(false);
expect(() =>
getProjectConfigFiles(parseSettings, true),
).toThrowErrorMatchingInlineSnapshot(
`"project was set to \`true\` but couldn't find any tsconfig.json relative to './repos/repo/packages/package/file.ts' within './repos/repo'."`,
);
});
it('throws when searching passes the tsconfigRootDir', () => {
mockExistsSync.mockReturnValue(false);
expect(() =>
getProjectConfigFiles({ ...parseSettings, tsconfigRootDir: '/' }, true),
).toThrowErrorMatchingInlineSnapshot(
`"project was set to \`true\` but couldn't find any tsconfig.json relative to './repos/repo/packages/package/file.ts' within '/'."`,
);
});
});
});