From df6ddd8104c764d6062c0d2031054e2ede109d37 Mon Sep 17 00:00:00 2001 From: Rob Hogan <2590098+robhogan@users.noreply.github.com> Date: Thu, 2 Mar 2023 18:24:30 +0000 Subject: [PATCH] chore(jest-snapshot): Remove hard dependency on `jest-haste-map` (#13977) --- CHANGELOG.md | 2 ++ packages/jest-snapshot/package.json | 1 - packages/jest-snapshot/src/index.ts | 13 ++++++------- packages/jest-snapshot/src/types.ts | 8 ++++++++ packages/jest-snapshot/tsconfig.json | 1 - yarn.lock | 1 - 6 files changed, 16 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddfafa992689..5e4dd61ba875 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,8 @@ ### Chore & Maintenance +- `[jest-snapshot]` Remove dependency on `jest-haste-map` ([#13977](https://github.com/facebook/jest/pull/13977)) + ### Performance ## 29.4.3 diff --git a/packages/jest-snapshot/package.json b/packages/jest-snapshot/package.json index dfcfe9959774..f6fed9c56e90 100644 --- a/packages/jest-snapshot/package.json +++ b/packages/jest-snapshot/package.json @@ -34,7 +34,6 @@ "graceful-fs": "^4.2.9", "jest-diff": "workspace:^", "jest-get-type": "workspace:^", - "jest-haste-map": "workspace:^", "jest-matcher-utils": "workspace:^", "jest-message-util": "workspace:^", "jest-util": "workspace:^", diff --git a/packages/jest-snapshot/src/index.ts b/packages/jest-snapshot/src/index.ts index 5e58f1c7f22c..13e22a9f6605 100644 --- a/packages/jest-snapshot/src/index.ts +++ b/packages/jest-snapshot/src/index.ts @@ -8,7 +8,6 @@ import * as fs from 'graceful-fs'; import type {Config} from '@jest/types'; import type {MatcherFunctionWithContext} from 'expect'; -import type {IHasteFS} from 'jest-haste-map'; import { BOLD_WEIGHT, EXPECTED_COLOR, @@ -31,7 +30,7 @@ import { printReceived, printSnapshotAndReceived, } from './printSnapshot'; -import type {Context, MatchSnapshotConfig} from './types'; +import type {Context, FileSystem, MatchSnapshotConfig} from './types'; import {deepMerge, escapeBacktickString, serialize} from './utils'; export {addSerializer, getSerializers} from './plugins'; @@ -110,11 +109,11 @@ function stripAddedIndentation(inlineSnapshot: string) { return inlineSnapshot; } -const fileExists = (filePath: string, hasteFS: IHasteFS): boolean => - hasteFS.exists(filePath) || fs.existsSync(filePath); +const fileExists = (filePath: string, fileSystem: FileSystem): boolean => + fileSystem.exists(filePath) || fs.existsSync(filePath); export const cleanup = ( - hasteFS: IHasteFS, + fileSystem: FileSystem, update: Config.SnapshotUpdateState, snapshotResolver: SnapshotResolver, testPathIgnorePatterns?: Config.ProjectConfig['testPathIgnorePatterns'], @@ -123,7 +122,7 @@ export const cleanup = ( filesRemovedList: Array; } => { const pattern = `\\.${EXTENSION}$`; - const files = hasteFS.matchFiles(pattern); + const files = fileSystem.matchFiles(pattern); let testIgnorePatternsRegex: RegExp | null = null; if (testPathIgnorePatterns && testPathIgnorePatterns.length > 0) { testIgnorePatternsRegex = new RegExp(testPathIgnorePatterns.join('|')); @@ -137,7 +136,7 @@ export const cleanup = ( return false; } - if (!fileExists(testPath, hasteFS)) { + if (!fileExists(testPath, fileSystem)) { if (update === 'all') { fs.unlinkSync(snapshotFile); } diff --git a/packages/jest-snapshot/src/types.ts b/packages/jest-snapshot/src/types.ts index ee0da9659c03..541c191193a7 100644 --- a/packages/jest-snapshot/src/types.ts +++ b/packages/jest-snapshot/src/types.ts @@ -13,6 +13,14 @@ export interface Context extends MatcherContext { snapshotState: SnapshotState; } +// This is typically implemented by `jest-haste-map`'s `HasteFS`, but we +// partially reproduce the interface here to avoid a dependency. + +export interface FileSystem { + exists(path: string): boolean; + matchFiles(pattern: RegExp | string): Array; +} + export type MatchSnapshotConfig = { context: Context; hint?: string; diff --git a/packages/jest-snapshot/tsconfig.json b/packages/jest-snapshot/tsconfig.json index c9d79155bd00..af04ef522255 100644 --- a/packages/jest-snapshot/tsconfig.json +++ b/packages/jest-snapshot/tsconfig.json @@ -11,7 +11,6 @@ {"path": "../expect-utils"}, {"path": "../jest-diff"}, {"path": "../jest-get-type"}, - {"path": "../jest-haste-map"}, {"path": "../jest-matcher-utils"}, {"path": "../jest-message-util"}, {"path": "../jest-transform"}, diff --git a/yarn.lock b/yarn.lock index 6ca858c85871..e31b46e5a082 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13116,7 +13116,6 @@ __metadata: graceful-fs: ^4.2.9 jest-diff: "workspace:^" jest-get-type: "workspace:^" - jest-haste-map: "workspace:^" jest-matcher-utils: "workspace:^" jest-message-util: "workspace:^" jest-util: "workspace:^"