Skip to content

Commit

Permalink
chore(jest-snapshot): Remove hard dependency on jest-haste-map (#13977
Browse files Browse the repository at this point in the history
)
  • Loading branch information
robhogan authored Mar 2, 2023
1 parent e7eb64c commit df6ddd8
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion packages/jest-snapshot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:^",
Expand Down
13 changes: 6 additions & 7 deletions packages/jest-snapshot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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';
Expand Down Expand Up @@ -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'],
Expand All @@ -123,7 +122,7 @@ export const cleanup = (
filesRemovedList: Array<string>;
} => {
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('|'));
Expand All @@ -137,7 +136,7 @@ export const cleanup = (
return false;
}

if (!fileExists(testPath, hasteFS)) {
if (!fileExists(testPath, fileSystem)) {
if (update === 'all') {
fs.unlinkSync(snapshotFile);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/jest-snapshot/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;
}

export type MatchSnapshotConfig = {
context: Context;
hint?: string;
Expand Down
1 change: 0 additions & 1 deletion packages/jest-snapshot/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:^"
Expand Down

0 comments on commit df6ddd8

Please sign in to comment.