Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(jest-snapshot): Remove hard dependency on jest-haste-map #13977

Merged
merged 4 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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