Skip to content

Commit

Permalink
feat: implement support for transforming snapshot resolvers (#8829)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Apr 22, 2021
1 parent b86e48a commit c8b1835
Show file tree
Hide file tree
Showing 21 changed files with 1,181 additions and 106 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -30,6 +30,7 @@
- `[jest-runtime]` Support for async code transformations ([#11191](https://github.com/facebook/jest/pull/11191) & [#11220](https://github.com/facebook/jest/pull/11220))
- `[jest-reporters]` Add static filepath property to all reporters ([#11015](https://github.com/facebook/jest/pull/11015))
- `[jest-snapshot]` [**BREAKING**] Make prettier optional for inline snapshots - fall back to string replacement ([#7792](https://github.com/facebook/jest/pull/7792))
- `[jest-snapshot]` [**BREAKING**] Run transforms over `snapshotResolver` ([#8751](https://github.com/facebook/jest/pull/8829))
- `[jest-transform]` Pass config options defined in Jest's config to transformer's `process` and `getCacheKey` functions ([#10926](https://github.com/facebook/jest/pull/10926))
- `[jest-transform]` Add support for transformers written in ESM ([#11163](https://github.com/facebook/jest/pull/11163))
- `[jest-transform]` [**BREAKING**] Do not export `ScriptTransformer` class, instead export the async function `createScriptTransformer` ([#11163](https://github.com/facebook/jest/pull/11163))
Expand Down
34 changes: 34 additions & 0 deletions e2e/__tests__/transform.test.ts
Expand Up @@ -7,6 +7,7 @@

import {tmpdir} from 'os';
import * as path from 'path';
import * as fs from 'graceful-fs';
import {wrap} from 'jest-snapshot-serializer-raw';
import {onNodeVersions} from '@jest/test-utils';
import {
Expand Down Expand Up @@ -207,6 +208,39 @@ describe('transformer caching', () => {
});
});

describe('transform-snapshotResolver', () => {
const dir = path.resolve(
__dirname,
'..',
'transform/transform-snapshotResolver',
);
const snapshotDir = path.resolve(dir, '__snapshots__');
const snapshotFile = path.resolve(snapshotDir, 'snapshot.test.js.snap');

const cleanupTest = () => {
if (fs.existsSync(snapshotFile)) {
fs.unlinkSync(snapshotFile);
}
if (fs.existsSync(snapshotDir)) {
fs.rmdirSync(snapshotDir);
}
};

beforeEach(cleanupTest);
afterAll(cleanupTest);

it('should transform the snapshotResolver', () => {
const result = runJest(dir, ['-w=1', '--no-cache', '--ci=false']);

expect(result.stderr).toMatch('1 snapshot written from 1 test suite');

const contents = require(snapshotFile);
expect(contents).toHaveProperty(
'snapshots are written to custom location 1',
);
});
});

describe('transform-environment', () => {
const dir = path.resolve(__dirname, '../transform/transform-environment');

Expand Down
@@ -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.
*/

test('snapshots are written to custom location', () => {
expect('foobar').toMatchSnapshot();
});
18 changes: 18 additions & 0 deletions e2e/transform/transform-snapshotResolver/babel.config.js
@@ -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'},
},
],
],
};
22 changes: 22 additions & 0 deletions e2e/transform/transform-snapshotResolver/customSnapshotResolver.ts
@@ -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: 'foo/__tests__/bar.test.js',
};

export default snapshotResolver;
10 changes: 10 additions & 0 deletions e2e/transform/transform-snapshotResolver/package.json
@@ -0,0 +1,10 @@
{
"jest": {
"testEnvironment": "node",
"snapshotResolver": "<rootDir>/customSnapshotResolver.ts"
},
"dependencies": {
"@babel/preset-env": "^7.0.0",
"@babel/preset-typescript": "^7.0.0"
}
}
5 changes: 5 additions & 0 deletions e2e/transform/transform-snapshotResolver/tsconfig.json
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"module": "commonjs"
}
}

0 comments on commit c8b1835

Please sign in to comment.