Skip to content

Commit

Permalink
feat: make prettier optional for inline snapshots (#7792)
Browse files Browse the repository at this point in the history
Co-authored-by: Simen Bekkhus <sbekkhus91@gmail.com>
Co-authored-by: Tim Seckinger <seckinger.tim@gmail.com>
  • Loading branch information
3 people committed Nov 15, 2020
1 parent a752ba1 commit c8418df
Show file tree
Hide file tree
Showing 13 changed files with 469 additions and 251 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

- `[jest-config]` [**BREAKING**] Default to Node testing environment instead of browser (JSDOM) ([#9874](https://github.com/facebook/jest/pull/9874))
- `[jest-runner]` [**BREAKING**] set exit code to 1 if test logs after teardown ([#10728](https://github.com/facebook/jest/pull/10728))
- `[jest-snapshot]`: [**BREAKING**] Make prettier optional for inline snapshots - fall back to string replacement ([#7792](https://github.com/facebook/jest/pull/7792))

### Fixes

Expand Down
Expand Up @@ -12,14 +12,14 @@ _getJestObj().mock("./App", () => () =>
/*#__PURE__*/ _jsxDEV(
"div",
{
children: "Hello world"
children: "Hello world",
},
void 0,
false,
{
fileName: _jsxFileName,
lineNumber: 1,
columnNumber: 32
columnNumber: 32,
},
this
)
Expand Down
3 changes: 0 additions & 3 deletions packages/jest-circus/package.json
Expand Up @@ -15,11 +15,9 @@
"./runner": "./runner.js"
},
"dependencies": {
"@babel/traverse": "^7.1.0",
"@jest/environment": "^26.6.2",
"@jest/test-result": "^26.6.2",
"@jest/types": "^26.6.2",
"@types/babel__traverse": "^7.0.4",
"@types/node": "*",
"chalk": "^4.0.0",
"co": "^4.6.0",
Expand All @@ -41,7 +39,6 @@
"@babel/core": "^7.1.0",
"@babel/register": "^7.0.0",
"@jest/test-utils": "^26.6.2",
"@types/babel__traverse": "^7.0.4",
"@types/co": "^4.6.0",
"@types/dedent": "^0.7.0",
"@types/graceful-fs": "^4.1.3",
Expand Down
Expand Up @@ -30,15 +30,9 @@ const jestAdapter = async (
FRAMEWORK_INITIALIZER,
);

const getPrettier = () =>
config.prettierPath ? require(config.prettierPath) : null;
const getBabelTraverse = () => require('@babel/traverse').default;

const {globals, snapshotState} = await initialize({
config,
environment,
getBabelTraverse,
getPrettier,
globalConfig,
localRequire: runtime.requireModule.bind(runtime),
parentProcess: process,
Expand Down
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

import type BabelTraverse from '@babel/traverse';
import throat from 'throat';
import type {JestEnvironment} from '@jest/environment';
import {
Expand Down Expand Up @@ -46,8 +45,6 @@ interface JestGlobals extends Global.TestFrameworkGlobals {
export const initialize = async ({
config,
environment,
getPrettier,
getBabelTraverse,
globalConfig,
localRequire,
parentProcess,
Expand All @@ -57,8 +54,6 @@ export const initialize = async ({
}: {
config: Config.ProjectConfig;
environment: JestEnvironment;
getPrettier: () => null | any;
getBabelTraverse: () => typeof BabelTraverse;
globalConfig: Config.GlobalConfig;
localRequire: <T = unknown>(path: Config.Path) => T;
testPath: Config.Path;
Expand Down Expand Up @@ -162,8 +157,7 @@ export const initialize = async ({
const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath);
const snapshotState = new SnapshotState(snapshotPath, {
expand,
getBabelTraverse,
getPrettier,
prettierPath: config.prettierPath,
updateSnapshot,
});
// @ts-expect-error: snapshotState is a jest extension of `expect`
Expand Down
5 changes: 2 additions & 3 deletions packages/jest-jasmine2/src/setup_jest_globals.ts
Expand Up @@ -106,13 +106,12 @@ export default ({

patchJasmine();
const {expand, updateSnapshot} = globalConfig;
const {prettierPath} = config;
const snapshotResolver = buildSnapshotResolver(config);
const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath);
const snapshotState = new SnapshotState(snapshotPath, {
expand,
getBabelTraverse: () => require('@babel/traverse').default,
getPrettier: () =>
config.prettierPath ? require(config.prettierPath) : null,
prettierPath,
updateSnapshot,
});
// @ts-expect-error: snapshotState is a jest extension of `expect`
Expand Down
20 changes: 18 additions & 2 deletions packages/jest-snapshot/package.json
Expand Up @@ -14,10 +14,15 @@
"./package.json": "./package.json"
},
"dependencies": {
"@babel/generator": "^7.7.2",
"@babel/parser": "^7.7.2",
"@babel/plugin-syntax-typescript": "^7.7.2",
"@babel/traverse": "^7.7.2",
"@babel/types": "^7.0.0",
"@jest/types": "^26.6.2",
"@types/babel__traverse": "^7.0.4",
"@types/prettier": "^2.0.0",
"@types/prettier": "^2.1.5",
"babel-preset-current-node-syntax": "^1.0.0",
"chalk": "^4.0.0",
"expect": "^26.6.2",
"graceful-fs": "^4.2.4",
Expand All @@ -32,14 +37,25 @@
"semver": "^7.3.2"
},
"devDependencies": {
"@babel/core": "^7.7.2",
"@babel/preset-flow": "^7.7.2",
"@babel/preset-react": "^7.7.2",
"@babel/traverse": "^7.3.4",
"@jest/test-utils": "^26.6.2",
"@types/graceful-fs": "^4.1.3",
"@types/natural-compare": "^1.4.0",
"@types/semver": "^7.1.0",
"ansi-regex": "^5.0.0",
"ansi-styles": "^4.2.0",
"prettier": "^1.19.1"
"prettier": "^2.0.0"
},
"peerDependencies": {
"@babel/core": "^7.7.2"
},
"peerDependenciesMeta": {
"@babel/core": {
"optional": true
}
},
"engines": {
"node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0"
Expand Down

0 comments on commit c8418df

Please sign in to comment.