From ff657a0fc9254ddf38f3b1808218f45a3b463cfd Mon Sep 17 00:00:00 2001 From: vvo Date: Sat, 30 Sep 2017 13:55:54 +0200 Subject: [PATCH] feat(jest-editor-support): Add Snapshot metadata Following #2629, I rebased the code on current master. I still have to actually test the feature with orta/vscode-jest#73. What i would like to add ultimately is the ability to: - preview the snapshot - have the snapshot diff - have a button to accept diff per snapshot (would update it) WDYT? --- .eslintrc.js | 2 ++ packages/jest-editor-support/package.json | 2 +- packages/jest-editor-support/src/Snapshot.js | 17 ++++++++++------- .../src/__tests__/Snapshot-test.js | 4 ++-- packages/jest-editor-support/src/index.js | 4 ++-- .../src/parsers/babylon_parser.js | 11 ++++++----- packages/jest-snapshot/src/index.js | 8 ++++---- yarn.lock | 2 +- 8 files changed, 28 insertions(+), 22 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 053fc383039e..fc737bf0a8f2 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -63,6 +63,8 @@ module.exports = { 'packages/jest-editor-support/src/Process.js', 'packages/jest-editor-support/src/Runner.js', 'packages/jest-editor-support/src/Settings.js', + 'packages/jest-editor-support/src/Snapshot.js', + 'packages/jest-editor-support/src/__tests__/Snapshot-test.js', 'packages/jest-jasmine2/src/jasmine/Env.js', 'packages/jest-jasmine2/src/jasmine/Spec.js', 'packages/jest-jasmine2/src/jasmine/Suite.js', diff --git a/packages/jest-editor-support/package.json b/packages/jest-editor-support/package.json index 960ee70990a9..79415dfbdd73 100644 --- a/packages/jest-editor-support/package.json +++ b/packages/jest-editor-support/package.json @@ -10,7 +10,7 @@ "dependencies": { "babylon": "^6.14.1", "babel-traverse": "^6.14.1", - "jest-snapshot": "^19.0.2" + "jest-snapshot": "^21.1.0" }, "typings": "index.d.ts" } diff --git a/packages/jest-editor-support/src/Snapshot.js b/packages/jest-editor-support/src/Snapshot.js index 825ea8d1b894..8b445a88a028 100644 --- a/packages/jest-editor-support/src/Snapshot.js +++ b/packages/jest-editor-support/src/Snapshot.js @@ -10,9 +10,9 @@ 'use strict'; -const traverse = require('babel-traverse').default; -const {getASTfor} = require('./parsers/BabylonParser'); -const {utils} = require('jest-snapshot'); +import traverse from 'babel-traverse'; +import {getASTfor} from './parsers/babylon_parser'; +import {utils} from 'jest-snapshot'; type Node = any; @@ -93,7 +93,7 @@ const buildName: ( return utils.testNameToKey(describeLess + fullName, position); }; -module.exports = class Snapshot { +export default class Snapshot { _parser: Function; _matchers: Array; constructor(parser: any, customMatchers?: Array) { @@ -111,7 +111,10 @@ module.exports = class Snapshot { const Visitors = { Identifier(path, state, matchers) { if (matchers.includes(path.node.name)) { - state.found.push({node: path.node, parents: getArrayOfParents(path)}); + state.found.push({ + node: path.node, + parents: getArrayOfParents(path), + }); } }, }; @@ -126,7 +129,7 @@ module.exports = class Snapshot { }); const snapshotPath = utils.getSnapshotPath(filePath); - const snapshots = utils.getSnapshotData(snapshotPath, false).data; + const snapshots = utils.getSnapshotData(snapshotPath, 'none').data; let lastParent = null; let count = 1; @@ -161,4 +164,4 @@ module.exports = class Snapshot { return result; }); } -}; +} diff --git a/packages/jest-editor-support/src/__tests__/Snapshot-test.js b/packages/jest-editor-support/src/__tests__/Snapshot-test.js index dcb71e21d0f5..ca076a7ae800 100644 --- a/packages/jest-editor-support/src/__tests__/Snapshot-test.js +++ b/packages/jest-editor-support/src/__tests__/Snapshot-test.js @@ -98,8 +98,8 @@ test('describe.example', () => { results.forEach(result => { const check = expectations[result.name]; - check.checked = result.content === - `${check.number} ${check.assertion} ${check.describe}`; + check.checked = + result.content === `${check.number} ${check.assertion} ${check.describe}`; }); expect( Object.keys(expectations) diff --git a/packages/jest-editor-support/src/index.js b/packages/jest-editor-support/src/index.js index 2b404e2af219..146f57f90b6e 100644 --- a/packages/jest-editor-support/src/index.js +++ b/packages/jest-editor-support/src/index.js @@ -13,8 +13,8 @@ import ProjectWorkspace from './project_workspace'; import Runner from './Runner'; import Settings from './Settings'; import Snapshot from './Snapshot'; -import { Expect, ItBlock, Node } from './parsers/parser_nodes'; -import { parse } from './parsers/babylon_parser'; +import {Expect, ItBlock, Node} from './parsers/parser_nodes'; +import {parse} from './parsers/babylon_parser'; import TestReconciler from './test_reconciler'; module.exports = { diff --git a/packages/jest-editor-support/src/parsers/babylon_parser.js b/packages/jest-editor-support/src/parsers/babylon_parser.js index 7788e2ebb3ee..9e3c38fe982d 100644 --- a/packages/jest-editor-support/src/parsers/babylon_parser.js +++ b/packages/jest-editor-support/src/parsers/babylon_parser.js @@ -7,19 +7,20 @@ * @flow */ -import { readFileSync } from 'fs'; +import {readFileSync} from 'fs'; -import { parse as babylonParse } from 'babylon'; -import { Expect, ItBlock } from './parser_nodes'; +import {parse as babylonParse} from 'babylon'; +import {Expect, ItBlock} from './parser_nodes'; +import type {File as BabylonFile} from 'babylon'; export type BabylonParserResult = { expects: Array, itBlocks: Array, }; -export const getASTfor = (file: string): BabylonParserResult => { +export const getASTfor = (file: string): BabylonFile => { const data = readFileSync(file).toString(); - const config = { plugins: ['*'], sourceType: 'module' }; + const config = {plugins: ['*'], sourceType: 'module'}; return babylonParse(data, config); }; diff --git a/packages/jest-snapshot/src/index.js b/packages/jest-snapshot/src/index.js index 10252b817713..bd59c992baa5 100644 --- a/packages/jest-snapshot/src/index.js +++ b/packages/jest-snapshot/src/index.js @@ -22,13 +22,13 @@ import { } from 'jest-matcher-utils'; import SnapshotState from './State'; import {addSerializer, getSerializers} from './plugins'; -import utils, {SNAPSHOT_EXTENSION} from './utils'; +import * as utils from './utils'; const fileExists = (filePath: Path, hasteFS: HasteFS): boolean => hasteFS.exists(filePath) || fs.existsSync(filePath); const cleanup = (hasteFS: HasteFS, update: SnapshotUpdateState) => { - const pattern = '\\.' + SNAPSHOT_EXTENSION + '$'; + const pattern = '\\.' + utils.SNAPSHOT_EXTENSION + '$'; const files = hasteFS.matchFiles(pattern); const filesRemoved = files .filter( @@ -37,7 +37,7 @@ const cleanup = (hasteFS: HasteFS, update: SnapshotUpdateState) => { path.resolve( path.dirname(snapshotFile), '..', - path.basename(snapshotFile, '.' + SNAPSHOT_EXTENSION), + path.basename(snapshotFile, '.' + utils.SNAPSHOT_EXTENSION), ), hasteFS, ), @@ -148,7 +148,7 @@ const toThrowErrorMatchingSnapshot = function(received: any, expected: void) { }; module.exports = { - EXTENSION: SNAPSHOT_EXTENSION, + EXTENSION: utils.SNAPSHOT_EXTENSION, SnapshotState, addSerializer, cleanup, diff --git a/yarn.lock b/yarn.lock index 59fcae6047ef..1f68c5565c1f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -908,7 +908,7 @@ babel-template@^6.16.0, babel-template@^6.24.1, babel-template@^6.26.0: babylon "^6.18.0" lodash "^4.17.4" -babel-traverse@^6.18.0, babel-traverse@^6.23.1, babel-traverse@^6.24.1, babel-traverse@^6.26.0: +babel-traverse@^6.14.1, babel-traverse@^6.18.0, babel-traverse@^6.23.1, babel-traverse@^6.24.1, babel-traverse@^6.26.0: version "6.26.0" resolved "https://registry.yarnpkg.com/babel-traverse/-/babel-traverse-6.26.0.tgz#46a9cbd7edcc62c8e5c064e2d2d8d0f4035766ee" dependencies: