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

Add resolver for custom snapshots paths #6143

Merged
merged 17 commits into from Sep 26, 2018
@@ -1,5 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`malformed custom resolver in project config inconsistent functions throws 1`] = `"<bold>Custom snapshot resolver functions must transform paths consistently, i.e. expects resolveTestPath(resolveSnapshotPath('some-path/__tests__/snapshot_resolver.test.js')) === some-path/__SPECS__/snapshot_resolver.test.js</>"`;

exports[`malformed custom resolver in project config missing resolveSnapshotPath throws 1`] = `
"<bold>Custom snapshot resolver must implement a \`resolveSnapshotPath\` function.</>
Documentation: https://facebook.github.io/jest/docs/en/configuration.html#snapshotResolver"
Expand Down
@@ -0,0 +1,9 @@
module.exports = {
resolveSnapshotPath: (testPath, snapshotExtension) =>
testPath.replace('__tests__', '__snapshots__') + snapshotExtension,

resolveTestPath: (snapshotFilePath, snapshotExtension) =>
snapshotFilePath
.replace('__snapshots__', '__SPECS__')
.slice(0, -snapshotExtension.length),
};
Expand Up @@ -96,4 +96,13 @@ describe('malformed custom resolver in project config', () => {
buildSnapshotResolver(projectConfig);
}).toThrowErrorMatchingSnapshot();
});

it('inconsistent functions throws ', () => {
const projectConfig = newProjectConfig(
'customSnapshotResolver-inconsistent-fns.js',
);
expect(() => {
buildSnapshotResolver(projectConfig);
}).toThrowErrorMatchingSnapshot();
});
});
24 changes: 23 additions & 1 deletion packages/jest-snapshot/src/snapshot_resolver.js
Expand Up @@ -51,12 +51,16 @@ function createCustomSnapshotResolver(
throw new TypeError(mustImplement('resolveTestPath'));
}

return {
const customResolver = {
resolveSnapshotPath: testPath =>
custom.resolveSnapshotPath(testPath, DOT_EXTENSION),
resolveTestPath: snapshotPath =>
custom.resolveTestPath(snapshotPath, DOT_EXTENSION),
};

verifyConsistentTransformations(customResolver);

return customResolver;
}

function mustImplement(functionName: string) {
Expand All @@ -67,3 +71,21 @@ function mustImplement(functionName: string) {
'\nDocumentation: https://facebook.github.io/jest/docs/en/configuration.html#snapshotResolver'
);
}

function verifyConsistentTransformations(custom: SnapshotResolver) {
const fakeTestPath = path.join(
'some-path',
'__tests__',
'snapshot_resolver.test.js',
);
const transformedPath = custom.resolveTestPath(
custom.resolveSnapshotPath(fakeTestPath),
);
if (transformedPath !== fakeTestPath) {
SimenB marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(
chalk.bold(
`Custom snapshot resolver functions must transform paths consistently, i.e. expects resolveTestPath(resolveSnapshotPath('${fakeTestPath}')) === ${transformedPath}`,
),
);
}
}