Skip to content

Commit

Permalink
moar PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jeysal committed Feb 19, 2019
1 parent 363baf6 commit bc96e8b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
6 changes: 3 additions & 3 deletions packages/jest-resolve-dependencies/src/index.ts
Expand Up @@ -15,9 +15,9 @@ import {isSnapshotPath} from 'jest-snapshot';
* to retrieve a list of all transitive inverse dependencies.
*/
class DependencyResolver {
_hasteFS: HasteFS;
_resolver: Resolver;
_snapshotResolver: Snapshot.SnapshotResolver;
private _resolver: Resolver;
private _hasteFS: HasteFS;
private _snapshotResolver: Snapshot.SnapshotResolver;

constructor(
resolver: Resolver,
Expand Down
5 changes: 2 additions & 3 deletions packages/jest-snapshot/src/index.ts
Expand Up @@ -6,15 +6,14 @@
*/

import fs from 'fs';
import {Config, Matchers} from '@jest/types';
import {Config, Matchers, Snapshot} 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,
EXTENSION,
} from './snapshot_resolver';
import SnapshotState from './State';
Expand All @@ -31,7 +30,7 @@ const fileExists = (filePath: Config.Path, hasteFS: HasteFS): boolean =>
const cleanup = (
hasteFS: HasteFS,
update: Config.SnapshotUpdateState,
snapshotResolver: SnapshotResolver,
snapshotResolver: Snapshot.SnapshotResolver,
) => {
const pattern = '\\.' + EXTENSION + '$';
const files = hasteFS.matchFiles(pattern);
Expand Down
24 changes: 9 additions & 15 deletions packages/jest-snapshot/src/snapshot_resolver.ts
Expand Up @@ -6,25 +6,19 @@
*/

import path from 'path';
import {Config} from '@jest/types';
import {Config, Snapshot} 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, SnapshotResolver> = new Map();
const cache: Map<Config.Path, Snapshot.SnapshotResolver> = new Map();
export const buildSnapshotResolver = (
config: Config.ProjectConfig,
): SnapshotResolver => {
): Snapshot.SnapshotResolver => {
const key = config.rootDir;
if (!cache.has(key)) {
cache.set(key, createSnapshotResolver(config.snapshotResolver));
Expand All @@ -34,13 +28,13 @@ export const buildSnapshotResolver = (

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

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

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

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

function verifyConsistentTransformations(custom: SnapshotResolver) {
function verifyConsistentTransformations(custom: Snapshot.SnapshotResolver) {
const resolvedSnapshotPath = custom.resolveSnapshotPath(
custom.testPathForConsistencyCheck,
);
Expand Down

0 comments on commit bc96e8b

Please sign in to comment.