Skip to content

Commit a4acf85

Browse files
committed
types(jest-haste-map): Define/restrict public interfaces
1 parent 33a8a59 commit a4acf85

File tree

11 files changed

+68
-52
lines changed

11 files changed

+68
-52
lines changed

packages/jest-core/src/cli/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {AggregatedResult, TestContext} from '@jest/test-result';
1313
import type {Config} from '@jest/types';
1414
import type {ChangedFilesPromise} from 'jest-changed-files';
1515
import {readConfigs} from 'jest-config';
16-
import type HasteMap from 'jest-haste-map';
16+
import type {IHasteMap} from 'jest-haste-map';
1717
import Runtime from 'jest-runtime';
1818
import {createDirectory, preRunMessage} from 'jest-util';
1919
import {TestWatcher} from 'jest-watcher';
@@ -233,7 +233,7 @@ const runWatch = async (
233233
hasDeprecationWarnings: boolean,
234234
globalConfig: Config.GlobalConfig,
235235
outputStream: NodeJS.WriteStream,
236-
hasteMapInstances: Array<HasteMap>,
236+
hasteMapInstances: Array<IHasteMap>,
237237
filter?: Filter,
238238
) => {
239239
if (hasDeprecationWarnings) {

packages/jest-core/src/lib/createContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
import type {TestContext} from '@jest/test-result';
99
import type {Config} from '@jest/types';
10-
import type {HasteMapObject} from 'jest-haste-map';
10+
import type {IHasteFS, IModuleMap} from 'jest-haste-map';
1111
import Runtime from 'jest-runtime';
1212

1313
export default function createContext(
1414
config: Config.ProjectConfig,
15-
{hasteFS, moduleMap}: HasteMapObject,
15+
{hasteFS, moduleMap}: {hasteFS: IHasteFS; moduleMap: IModuleMap},
1616
): TestContext {
1717
return {
1818
config,

packages/jest-core/src/watch.ts

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ import exit = require('exit');
1212
import slash = require('slash');
1313
import type {TestContext} from '@jest/test-result';
1414
import type {Config} from '@jest/types';
15-
import type {
16-
ChangeEvent as HasteChangeEvent,
17-
default as HasteMap,
18-
} from 'jest-haste-map';
15+
import type {IHasteMap as HasteMap} from 'jest-haste-map';
1916
import {formatExecError} from 'jest-message-util';
2017
import {
2118
isInteractive,
@@ -243,31 +240,28 @@ export default async function watch(
243240
emitFileChange();
244241

245242
hasteMapInstances.forEach((hasteMapInstance, index) => {
246-
hasteMapInstance.on(
247-
'change',
248-
({eventsQueue, hasteFS, moduleMap}: HasteChangeEvent) => {
249-
const validPaths = eventsQueue.filter(({filePath}) =>
250-
isValidPath(globalConfig, filePath),
251-
);
243+
hasteMapInstance.on('change', ({eventsQueue, hasteFS, moduleMap}) => {
244+
const validPaths = eventsQueue.filter(({filePath}) =>
245+
isValidPath(globalConfig, filePath),
246+
);
252247

253-
if (validPaths.length) {
254-
const context = (contexts[index] = createContext(
255-
contexts[index].config,
256-
{hasteFS, moduleMap},
257-
));
258-
259-
activePlugin = null;
260-
261-
searchSources = searchSources.slice();
262-
searchSources[index] = {
263-
context,
264-
searchSource: new SearchSource(context),
265-
};
266-
emitFileChange();
267-
startRun(globalConfig);
268-
}
269-
},
270-
);
248+
if (validPaths.length) {
249+
const context = (contexts[index] = createContext(
250+
contexts[index].config,
251+
{hasteFS, moduleMap},
252+
));
253+
254+
activePlugin = null;
255+
256+
searchSources = searchSources.slice();
257+
searchSources[index] = {
258+
context,
259+
searchSource: new SearchSource(context),
260+
};
261+
emitFileChange();
262+
startRun(globalConfig);
263+
}
264+
});
271265
});
272266

273267
if (!hasExitListener) {

packages/jest-haste-map/src/HasteFS.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import {globsToMatcher, replacePathSepForGlob} from 'jest-util';
99
import H from './constants';
1010
import * as fastPath from './lib/fast_path';
11-
import type {FileData} from './types';
11+
import type {FileData, IHasteFS} from './types';
1212

13-
export default class HasteFS {
13+
export default class HasteFS implements IHasteFS {
1414
private readonly _rootDir: string;
1515
private readonly _files: FileData;
1616

packages/jest-haste-map/src/ModuleMap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type {
1919
const EMPTY_OBJ: Record<string, ModuleMetaData> = {};
2020
const EMPTY_MAP = new Map();
2121

22-
export default class ModuleMap implements IModuleMap<SerializableModuleMap> {
22+
export default class ModuleMap implements IModuleMap {
2323
static DuplicateHasteCandidatesError: typeof DuplicateHasteCandidatesError;
2424
private readonly _raw: RawModuleMap;
2525
private json: SerializableModuleMap | undefined;

packages/jest-haste-map/src/index.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import type {
3434
FileMetaData,
3535
HasteMapStatic,
3636
HasteRegExp,
37+
IHasteMap,
3738
InternalHasteMap,
3839
HasteMap as InternalHasteMapObject,
3940
MockData,
@@ -109,11 +110,9 @@ type Watcher = {
109110

110111
type HasteWorker = typeof import('./worker');
111112

112-
export type {default as FS} from './HasteFS';
113-
export {default as ModuleMap} from './ModuleMap';
114113
export type {
115-
ChangeEvent,
116-
HasteMap as HasteMapObject,
114+
IHasteFS,
115+
IHasteMap,
117116
IModuleMap,
118117
SerializableModuleMap,
119118
} from './types';
@@ -210,7 +209,7 @@ function invariant(condition: unknown, message?: string): asserts condition {
210209
* Worker processes can directly access the cache through `HasteMap.read()`.
211210
*
212211
*/
213-
export default class HasteMap extends EventEmitter {
212+
class HasteMap extends EventEmitter implements IHasteMap {
214213
private _buildPromise: Promise<InternalHasteMapObject> | null = null;
215214
private _cachePath = '';
216215
private _changeInterval?: ReturnType<typeof setInterval>;
@@ -227,7 +226,7 @@ export default class HasteMap extends EventEmitter {
227226
return HasteMap;
228227
}
229228

230-
static async create(options: Options): Promise<HasteMap> {
229+
static async create(options: Options): Promise<IHasteMap> {
231230
if (options.hasteMapModulePath) {
232231
const CustomHasteMap = require(options.hasteMapModulePath);
233232
return new CustomHasteMap(options);
@@ -1144,3 +1143,8 @@ function copy<T extends Record<string, unknown>>(object: T): T {
11441143
function copyMap<K, V>(input: Map<K, V>): Map<K, V> {
11451144
return new Map(input);
11461145
}
1146+
1147+
export default HasteMap as HasteMapStatic & {
1148+
create(options: Options): Promise<IHasteMap>;
1149+
getStatic(config: Config.ProjectConfig): HasteMapStatic;
1150+
};

packages/jest-haste-map/src/types.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@ export interface IModuleMap<S = SerializableModuleMap> {
3939
toJSON(): S;
4040
}
4141

42+
export interface IHasteFS {
43+
exists(path: string): boolean;
44+
getAbsoluteFileIterator(): Iterable<string>;
45+
getAllFiles(): Array<string>;
46+
getDependencies(file: string): Array<string> | null;
47+
getSize(path: string): number | null;
48+
matchFiles(pattern: RegExp | string): Array<string>;
49+
matchFilesWithGlob(
50+
globs: ReadonlyArray<string>,
51+
root: string | null,
52+
): Set<string>;
53+
}
54+
55+
export interface IHasteMap {
56+
on(eventType: 'change', handler: (event: ChangeEvent) => void): void;
57+
build(): Promise<{hasteFS: IHasteFS; moduleMap: IModuleMap}>;
58+
}
59+
4260
export type HasteMapStatic<S = SerializableModuleMap> = {
4361
getCacheFilePath(
4462
tmpdir: string,

packages/jest-resolve-dependencies/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import * as path from 'path';
9-
import type {FS as HasteFS} from 'jest-haste-map';
9+
import type {IHasteFS} from 'jest-haste-map';
1010
import type {ResolveModuleConfig, default as Resolver} from 'jest-resolve';
1111
import {SnapshotResolver, isSnapshotPath} from 'jest-snapshot';
1212

@@ -20,13 +20,13 @@ export type ResolvedModule = {
2020
* to retrieve a list of all transitive inverse dependencies.
2121
*/
2222
export class DependencyResolver {
23-
private _hasteFS: HasteFS;
23+
private _hasteFS: IHasteFS;
2424
private _resolver: Resolver;
2525
private _snapshotResolver: SnapshotResolver;
2626

2727
constructor(
2828
resolver: Resolver,
29-
hasteFS: HasteFS,
29+
hasteFS: IHasteFS,
3030
snapshotResolver: SnapshotResolver,
3131
) {
3232
this._resolver = resolver;

packages/jest-runtime/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import {
4949
shouldInstrument,
5050
} from '@jest/transform';
5151
import type {Config, Global} from '@jest/types';
52-
import type {IModuleMap} from 'jest-haste-map';
52+
import type {IHasteMap, IModuleMap} from 'jest-haste-map';
5353
import HasteMap from 'jest-haste-map';
5454
import {formatStackTrace, separateMessageFromStack} from 'jest-message-util';
5555
import type {MockFunctionMetadata, ModuleMocker} from 'jest-mock';
@@ -358,7 +358,7 @@ export default class Runtime {
358358
static createHasteMap(
359359
config: Config.ProjectConfig,
360360
options?: HasteMapOptions,
361-
): Promise<HasteMap> {
361+
): Promise<IHasteMap> {
362362
const ignorePatternParts = [
363363
...config.modulePathIgnorePatterns,
364364
...(options && options.watch ? config.watchPathIgnorePatterns : []),

packages/jest-snapshot/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import * as fs from 'graceful-fs';
99
import type {Config} from '@jest/types';
1010
import type {MatcherFunctionWithState} from 'expect';
11-
import type {FS as HasteFS} from 'jest-haste-map';
11+
import type {IHasteFS} from 'jest-haste-map';
1212
import {
1313
BOLD_WEIGHT,
1414
EXPECTED_COLOR,
@@ -110,11 +110,11 @@ function stripAddedIndentation(inlineSnapshot: string) {
110110
return inlineSnapshot;
111111
}
112112

113-
const fileExists = (filePath: string, hasteFS: HasteFS): boolean =>
113+
const fileExists = (filePath: string, hasteFS: IHasteFS): boolean =>
114114
hasteFS.exists(filePath) || fs.existsSync(filePath);
115115

116116
export const cleanup = (
117-
hasteFS: HasteFS,
117+
hasteFS: IHasteFS,
118118
update: Config.SnapshotUpdateState,
119119
snapshotResolver: SnapshotResolver,
120120
testPathIgnorePatterns?: Config.ProjectConfig['testPathIgnorePatterns'],

0 commit comments

Comments
 (0)