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

decentralize jest-snapshot types #7973

Merged
merged 1 commit into from
Feb 24, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/jest-resolve-dependencies/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* LICENSE file in the root directory of this source tree.
*/

import {Config, Snapshot} from '@jest/types';
import {Config} from '@jest/types';
import {FS as HasteFS} from 'jest-haste-map';
import Resolver from 'jest-resolve';
import {isSnapshotPath} from 'jest-snapshot';
import {isSnapshotPath, SnapshotResolver} from 'jest-snapshot';

namespace DependencyResolver {
export type ResolvedModule = {
Expand All @@ -25,12 +25,12 @@ namespace DependencyResolver {
class DependencyResolver {
private _hasteFS: HasteFS;
private _resolver: Resolver;
private _snapshotResolver: Snapshot.SnapshotResolver;
private _snapshotResolver: SnapshotResolver;

constructor(
resolver: Resolver,
hasteFS: HasteFS,
snapshotResolver: Snapshot.SnapshotResolver,
snapshotResolver: SnapshotResolver,
) {
this._resolver = resolver;
this._hasteFS = hasteFS;
Expand Down
13 changes: 10 additions & 3 deletions packages/jest-snapshot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
*/

import fs from 'fs';
import {Config, Matchers, Snapshot} from '@jest/types';
import {Config, Matchers} from '@jest/types';
import {FS as HasteFS} from 'jest-haste-map';

import diff from 'jest-diff';
import {EXPECTED_COLOR, matcherHint, RECEIVED_COLOR} from 'jest-matcher-utils';
import {
buildSnapshotResolver,
isSnapshotPath,
SnapshotResolver as JestSnapshotResolver,
EXTENSION,
} from './snapshot_resolver';
import SnapshotState from './State';
Expand All @@ -31,7 +32,7 @@ const fileExists = (filePath: Config.Path, hasteFS: HasteFS): boolean =>
const cleanup = (
hasteFS: HasteFS,
update: Config.SnapshotUpdateState,
snapshotResolver: Snapshot.SnapshotResolver,
snapshotResolver: JestSnapshotResolver,
) => {
const pattern = '\\.' + EXTENSION + '$';
const files = hasteFS.matchFiles(pattern);
Expand Down Expand Up @@ -296,7 +297,7 @@ const _toThrowErrorMatchingSnapshot = ({
});
};

export = {
const JestSnapshot = {
EXTENSION,
SnapshotState,
addSerializer,
Expand All @@ -310,3 +311,9 @@ export = {
toThrowErrorMatchingSnapshot,
utils,
};
/* eslint-disable-next-line no-redeclare */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be cool to teach this rule about namespaces. Maybe worth an issue to https://github.com/typescript-eslint/typescript-eslint?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

namespace JestSnapshot {
export type SnapshotResolver = JestSnapshotResolver;
}

export = JestSnapshot;
24 changes: 15 additions & 9 deletions packages/jest-snapshot/src/snapshot_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@
*/

import path from 'path';
import {Config, Snapshot} from '@jest/types';
import {Config} from '@jest/types';
import chalk from 'chalk';

export type SnapshotResolver = {
testPathForConsistencyCheck: string;
resolveSnapshotPath(testPath: Config.Path, extension?: string): Config.Path;
resolveTestPath(snapshotPath: Config.Path, extension?: string): Config.Path;
};

export const EXTENSION = 'snap';
export const DOT_EXTENSION = '.' + EXTENSION;

export const isSnapshotPath = (path: string): boolean =>
path.endsWith(DOT_EXTENSION);

const cache: Map<Config.Path, Snapshot.SnapshotResolver> = new Map();
const cache: Map<Config.Path, SnapshotResolver> = new Map();
export const buildSnapshotResolver = (
config: Config.ProjectConfig,
): Snapshot.SnapshotResolver => {
): SnapshotResolver => {
const key = config.rootDir;
if (!cache.has(key)) {
cache.set(key, createSnapshotResolver(config.snapshotResolver));
Expand All @@ -28,13 +34,13 @@ export const buildSnapshotResolver = (

function createSnapshotResolver(
snapshotResolverPath?: Config.Path | null,
): Snapshot.SnapshotResolver {
): SnapshotResolver {
return typeof snapshotResolverPath === 'string'
? createCustomSnapshotResolver(snapshotResolverPath)
: createDefaultSnapshotResolver();
}

function createDefaultSnapshotResolver(): Snapshot.SnapshotResolver {
function createDefaultSnapshotResolver(): SnapshotResolver {
return {
resolveSnapshotPath: (testPath: Config.Path) =>
path.join(
Expand All @@ -59,10 +65,10 @@ function createDefaultSnapshotResolver(): Snapshot.SnapshotResolver {

function createCustomSnapshotResolver(
snapshotResolverPath: Config.Path,
): Snapshot.SnapshotResolver {
const custom: Snapshot.SnapshotResolver = require(snapshotResolverPath);
): SnapshotResolver {
const custom: SnapshotResolver = require(snapshotResolverPath);

const keys: [keyof Snapshot.SnapshotResolver, string][] = [
const keys: [keyof SnapshotResolver, string][] = [
['resolveSnapshotPath', 'function'],
['resolveTestPath', 'function'],
['testPathForConsistencyCheck', 'string'],
Expand Down Expand Up @@ -95,7 +101,7 @@ function mustImplement(propName: string, requiredType: string) {
);
}

function verifyConsistentTransformations(custom: Snapshot.SnapshotResolver) {
function verifyConsistentTransformations(custom: SnapshotResolver) {
const resolvedSnapshotPath = custom.resolveSnapshotPath(
custom.testPathForConsistencyCheck,
);
Expand Down
14 changes: 0 additions & 14 deletions packages/jest-types/src/Snapshot.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/jest-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import * as Console from './Console';
import * as Matchers from './Matchers';
import * as Mocks from './Mocks';
import * as PrettyFormat from './PrettyFormat';
import * as Snapshot from './Snapshot';
import * as SourceMaps from './SourceMaps';
import * as TestResult from './TestResult';
import * as Global from './Global';
Expand All @@ -22,7 +21,6 @@ export {
Matchers,
Mocks,
PrettyFormat,
Snapshot,
SourceMaps,
TestResult,
Global,
Expand Down