Skip to content

Commit

Permalink
Implement support for transforming snapshot resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Aug 17, 2019
1 parent d610c9a commit ef7000c
Show file tree
Hide file tree
Showing 13 changed files with 1,032 additions and 5 deletions.
13 changes: 13 additions & 0 deletions e2e/__tests__/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,16 @@ describe('transformer-config', () => {
expect(stdout).toMatchSnapshot();
});
});

describe('transform-snapshotResolver', () => {
const dir = path.resolve(
__dirname,
'../transform/transform-snapshotResolver',
);
it('should transform the snapshotResolver', () => {
const {json, stderr} = runWithJson(dir, ['--no-cache']);
expect(stderr).toMatch(/PASS/);
expect(json.success).toBe(true);
expect(json.numPassedTests).toBe(1);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should add two numbers 1`] = `"hello world"`;
10 changes: 10 additions & 0 deletions e2e/transform/transform-snapshotResolver/__tests__/add.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

it('should add two numbers', () => {
expect('hello world').toMatchSnapshot();
});
18 changes: 18 additions & 0 deletions e2e/transform/transform-snapshotResolver/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = {
presets: [
'@babel/preset-typescript',
[
'@babel/preset-env',
{
targets: {node: 'current'},
},
],
],
};
9 changes: 9 additions & 0 deletions e2e/transform/transform-snapshotResolver/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"jest": {
"snapshotResolver": "<rootDir>/snapshotResolver.ts"
},
"dependencies": {
"@babel/preset-env": "^7.0.0",
"@babel/preset-typescript": "^7.0.0"
}
}
22 changes: 22 additions & 0 deletions e2e/transform/transform-snapshotResolver/snapshotResolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {SnapshotResolver} from 'jest-snapshot';

const snapshotResolver: SnapshotResolver = {
resolveSnapshotPath: (testPath, snapshotExtension) =>
testPath.replace('__tests__', '__snapshots__') + snapshotExtension,

resolveTestPath: (snapshotFilePath, snapshotExtension) =>
snapshotFilePath
.replace('__snapshots__', '__tests__')
.slice(0, -(snapshotExtension || '').length),

testPathForConsistencyCheck: 'some/__tests__/example.test.js',
};

export default snapshotResolver;
5 changes: 5 additions & 0 deletions e2e/transform/transform-snapshotResolver/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"module": "commonjs"
}
}
926 changes: 926 additions & 0 deletions e2e/transform/transform-snapshotResolver/yarn.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const initialize = ({
});

const {expand, updateSnapshot} = globalConfig;
const snapshotResolver = buildSnapshotResolver(config);
const snapshotResolver = buildSnapshotResolver(config, localRequire);
const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath);
const snapshotState = new SnapshotState(snapshotPath, {
expand,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/src/setup_jest_globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default ({

patchJasmine();
const {expand, updateSnapshot} = globalConfig;
const snapshotResolver = buildSnapshotResolver(config);
const snapshotResolver = buildSnapshotResolver(config, localRequire);
const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath);
const snapshotState = new SnapshotState(snapshotPath, {
expand,
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-snapshot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"types": "build/index.d.ts",
"dependencies": {
"@babel/types": "^7.0.0",
"@jest/transform": "^24.8.0",
"@jest/types": "^24.8.0",
"chalk": "^2.0.1",
"expect": "^24.8.0",
Expand All @@ -19,6 +20,7 @@
"jest-matcher-utils": "^24.8.0",
"jest-message-util": "^24.8.0",
"jest-resolve": "^24.8.0",
"jest-util": "^24.8.0",
"mkdirp": "^0.5.1",
"natural-compare": "^1.4.0",
"pretty-format": "^24.8.0",
Expand Down
23 changes: 20 additions & 3 deletions packages/jest-snapshot/src/snapshot_resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/

import path from 'path';
import {ScriptTransformer} from '@jest/transform';
import {interopRequireDefault} from 'jest-util';
import {Config} from '@jest/types';
import chalk from 'chalk';

Expand All @@ -22,21 +24,33 @@ export const isSnapshotPath = (path: string): boolean =>
path.endsWith(DOT_EXTENSION);

const cache: Map<Config.Path, SnapshotResolver> = new Map();
const buildDefaultResolverRequire = (config: Config.ProjectConfig) => (
snapshotResolverPath: string,
) =>
new ScriptTransformer(config).requireAndTranspileModule<SnapshotResolver>(
snapshotResolverPath,
);

export const buildSnapshotResolver = (
config: Config.ProjectConfig,
localRequire: (module: string) => any = buildDefaultResolverRequire(config),
): SnapshotResolver => {
const key = config.rootDir;
if (!cache.has(key)) {
cache.set(key, createSnapshotResolver(config.snapshotResolver));
cache.set(
key,
createSnapshotResolver(localRequire, config.snapshotResolver),
);
}
return cache.get(key)!;
};

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

Expand Down Expand Up @@ -65,8 +79,11 @@ function createDefaultSnapshotResolver(): SnapshotResolver {

function createCustomSnapshotResolver(
snapshotResolverPath: Config.Path,
localRequire: (moduleName: string) => SnapshotResolver,
): SnapshotResolver {
const custom: SnapshotResolver = require(snapshotResolverPath);
const custom: SnapshotResolver = interopRequireDefault(
localRequire(snapshotResolverPath),
).default;

const keys: Array<[keyof SnapshotResolver, string]> = [
['resolveSnapshotPath', 'function'],
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-snapshot/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
{"path": "../jest-matcher-utils"},
{"path": "../jest-message-util"},
{"path": "../jest-resolve"},
{"path": "../jest-util"},
{"path": "../jest-transform" },
{"path": "../jest-types"},
{"path": "../pretty-format"}
]
Expand Down

0 comments on commit ef7000c

Please sign in to comment.