Skip to content

Commit

Permalink
types(jest-haste-map): Define/restrict public interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
robhogan committed Jul 13, 2022
1 parent 33a8a59 commit a4acf85
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 52 deletions.
4 changes: 2 additions & 2 deletions packages/jest-core/src/cli/index.ts
Expand Up @@ -13,7 +13,7 @@ import type {AggregatedResult, TestContext} from '@jest/test-result';
import type {Config} from '@jest/types';
import type {ChangedFilesPromise} from 'jest-changed-files';
import {readConfigs} from 'jest-config';
import type HasteMap from 'jest-haste-map';
import type {IHasteMap} from 'jest-haste-map';
import Runtime from 'jest-runtime';
import {createDirectory, preRunMessage} from 'jest-util';
import {TestWatcher} from 'jest-watcher';
Expand Down Expand Up @@ -233,7 +233,7 @@ const runWatch = async (
hasDeprecationWarnings: boolean,
globalConfig: Config.GlobalConfig,
outputStream: NodeJS.WriteStream,
hasteMapInstances: Array<HasteMap>,
hasteMapInstances: Array<IHasteMap>,
filter?: Filter,
) => {
if (hasDeprecationWarnings) {
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-core/src/lib/createContext.ts
Expand Up @@ -7,12 +7,12 @@

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

export default function createContext(
config: Config.ProjectConfig,
{hasteFS, moduleMap}: HasteMapObject,
{hasteFS, moduleMap}: {hasteFS: IHasteFS; moduleMap: IModuleMap},
): TestContext {
return {
config,
Expand Down
50 changes: 22 additions & 28 deletions packages/jest-core/src/watch.ts
Expand Up @@ -12,10 +12,7 @@ import exit = require('exit');
import slash = require('slash');
import type {TestContext} from '@jest/test-result';
import type {Config} from '@jest/types';
import type {
ChangeEvent as HasteChangeEvent,
default as HasteMap,
} from 'jest-haste-map';
import type {IHasteMap as HasteMap} from 'jest-haste-map';
import {formatExecError} from 'jest-message-util';
import {
isInteractive,
Expand Down Expand Up @@ -243,31 +240,28 @@ export default async function watch(
emitFileChange();

hasteMapInstances.forEach((hasteMapInstance, index) => {
hasteMapInstance.on(
'change',
({eventsQueue, hasteFS, moduleMap}: HasteChangeEvent) => {
const validPaths = eventsQueue.filter(({filePath}) =>
isValidPath(globalConfig, filePath),
);
hasteMapInstance.on('change', ({eventsQueue, hasteFS, moduleMap}) => {
const validPaths = eventsQueue.filter(({filePath}) =>
isValidPath(globalConfig, filePath),
);

if (validPaths.length) {
const context = (contexts[index] = createContext(
contexts[index].config,
{hasteFS, moduleMap},
));

activePlugin = null;

searchSources = searchSources.slice();
searchSources[index] = {
context,
searchSource: new SearchSource(context),
};
emitFileChange();
startRun(globalConfig);
}
},
);
if (validPaths.length) {
const context = (contexts[index] = createContext(
contexts[index].config,
{hasteFS, moduleMap},
));

activePlugin = null;

searchSources = searchSources.slice();
searchSources[index] = {
context,
searchSource: new SearchSource(context),
};
emitFileChange();
startRun(globalConfig);
}
});
});

if (!hasExitListener) {
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-haste-map/src/HasteFS.ts
Expand Up @@ -8,9 +8,9 @@
import {globsToMatcher, replacePathSepForGlob} from 'jest-util';
import H from './constants';
import * as fastPath from './lib/fast_path';
import type {FileData} from './types';
import type {FileData, IHasteFS} from './types';

export default class HasteFS {
export default class HasteFS implements IHasteFS {
private readonly _rootDir: string;
private readonly _files: FileData;

Expand Down
2 changes: 1 addition & 1 deletion packages/jest-haste-map/src/ModuleMap.ts
Expand Up @@ -19,7 +19,7 @@ import type {
const EMPTY_OBJ: Record<string, ModuleMetaData> = {};
const EMPTY_MAP = new Map();

export default class ModuleMap implements IModuleMap<SerializableModuleMap> {
export default class ModuleMap implements IModuleMap {
static DuplicateHasteCandidatesError: typeof DuplicateHasteCandidatesError;
private readonly _raw: RawModuleMap;
private json: SerializableModuleMap | undefined;
Expand Down
16 changes: 10 additions & 6 deletions packages/jest-haste-map/src/index.ts
Expand Up @@ -34,6 +34,7 @@ import type {
FileMetaData,
HasteMapStatic,
HasteRegExp,
IHasteMap,
InternalHasteMap,
HasteMap as InternalHasteMapObject,
MockData,
Expand Down Expand Up @@ -109,11 +110,9 @@ type Watcher = {

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

export type {default as FS} from './HasteFS';
export {default as ModuleMap} from './ModuleMap';
export type {
ChangeEvent,
HasteMap as HasteMapObject,
IHasteFS,
IHasteMap,
IModuleMap,
SerializableModuleMap,
} from './types';
Expand Down Expand Up @@ -210,7 +209,7 @@ function invariant(condition: unknown, message?: string): asserts condition {
* Worker processes can directly access the cache through `HasteMap.read()`.
*
*/
export default class HasteMap extends EventEmitter {
class HasteMap extends EventEmitter implements IHasteMap {
private _buildPromise: Promise<InternalHasteMapObject> | null = null;
private _cachePath = '';
private _changeInterval?: ReturnType<typeof setInterval>;
Expand All @@ -227,7 +226,7 @@ export default class HasteMap extends EventEmitter {
return HasteMap;
}

static async create(options: Options): Promise<HasteMap> {
static async create(options: Options): Promise<IHasteMap> {
if (options.hasteMapModulePath) {
const CustomHasteMap = require(options.hasteMapModulePath);
return new CustomHasteMap(options);
Expand Down Expand Up @@ -1144,3 +1143,8 @@ function copy<T extends Record<string, unknown>>(object: T): T {
function copyMap<K, V>(input: Map<K, V>): Map<K, V> {
return new Map(input);
}

export default HasteMap as HasteMapStatic & {
create(options: Options): Promise<IHasteMap>;
getStatic(config: Config.ProjectConfig): HasteMapStatic;
};
18 changes: 18 additions & 0 deletions packages/jest-haste-map/src/types.ts
Expand Up @@ -39,6 +39,24 @@ export interface IModuleMap<S = SerializableModuleMap> {
toJSON(): S;
}

export interface IHasteFS {
exists(path: string): boolean;
getAbsoluteFileIterator(): Iterable<string>;
getAllFiles(): Array<string>;
getDependencies(file: string): Array<string> | null;
getSize(path: string): number | null;
matchFiles(pattern: RegExp | string): Array<string>;
matchFilesWithGlob(
globs: ReadonlyArray<string>,
root: string | null,
): Set<string>;
}

export interface IHasteMap {
on(eventType: 'change', handler: (event: ChangeEvent) => void): void;
build(): Promise<{hasteFS: IHasteFS; moduleMap: IModuleMap}>;
}

export type HasteMapStatic<S = SerializableModuleMap> = {
getCacheFilePath(
tmpdir: string,
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-resolve-dependencies/src/index.ts
Expand Up @@ -6,7 +6,7 @@
*/

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

Expand All @@ -20,13 +20,13 @@ export type ResolvedModule = {
* to retrieve a list of all transitive inverse dependencies.
*/
export class DependencyResolver {
private _hasteFS: HasteFS;
private _hasteFS: IHasteFS;
private _resolver: Resolver;
private _snapshotResolver: SnapshotResolver;

constructor(
resolver: Resolver,
hasteFS: HasteFS,
hasteFS: IHasteFS,
snapshotResolver: SnapshotResolver,
) {
this._resolver = resolver;
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-runtime/src/index.ts
Expand Up @@ -49,7 +49,7 @@ import {
shouldInstrument,
} from '@jest/transform';
import type {Config, Global} from '@jest/types';
import type {IModuleMap} from 'jest-haste-map';
import type {IHasteMap, IModuleMap} from 'jest-haste-map';
import HasteMap from 'jest-haste-map';
import {formatStackTrace, separateMessageFromStack} from 'jest-message-util';
import type {MockFunctionMetadata, ModuleMocker} from 'jest-mock';
Expand Down Expand Up @@ -358,7 +358,7 @@ export default class Runtime {
static createHasteMap(
config: Config.ProjectConfig,
options?: HasteMapOptions,
): Promise<HasteMap> {
): Promise<IHasteMap> {
const ignorePatternParts = [
...config.modulePathIgnorePatterns,
...(options && options.watch ? config.watchPathIgnorePatterns : []),
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-snapshot/src/index.ts
Expand Up @@ -8,7 +8,7 @@
import * as fs from 'graceful-fs';
import type {Config} from '@jest/types';
import type {MatcherFunctionWithState} from 'expect';
import type {FS as HasteFS} from 'jest-haste-map';
import type {IHasteFS} from 'jest-haste-map';
import {
BOLD_WEIGHT,
EXPECTED_COLOR,
Expand Down Expand Up @@ -110,11 +110,11 @@ function stripAddedIndentation(inlineSnapshot: string) {
return inlineSnapshot;
}

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

export const cleanup = (
hasteFS: HasteFS,
hasteFS: IHasteFS,
update: Config.SnapshotUpdateState,
snapshotResolver: SnapshotResolver,
testPathIgnorePatterns?: Config.ProjectConfig['testPathIgnorePatterns'],
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-test-result/src/types.ts
Expand Up @@ -9,7 +9,7 @@ import type {V8Coverage} from 'collect-v8-coverage';
import type {CoverageMap, CoverageMapData} from 'istanbul-lib-coverage';
import type {ConsoleBuffer} from '@jest/console';
import type {Config, TestResult, TransformTypes} from '@jest/types';
import type {FS as HasteFS, ModuleMap} from 'jest-haste-map';
import type {IHasteFS, IModuleMap} from 'jest-haste-map';
import type Resolver from 'jest-resolve';

export interface RuntimeTransformResult extends TransformTypes.TransformResult {
Expand Down Expand Up @@ -190,8 +190,8 @@ export type Test = {

export type TestContext = {
config: Config.ProjectConfig;
hasteFS: HasteFS;
moduleMap: ModuleMap;
hasteFS: IHasteFS;
moduleMap: IModuleMap;
resolver: Resolver;
};

Expand Down

0 comments on commit a4acf85

Please sign in to comment.