Skip to content

Commit

Permalink
feat(jest-editor-support): Add Snapshot metadata
Browse files Browse the repository at this point in the history
Following jestjs#2629, I rebased the code on current master. I still have to
actually test the feature with jest-community/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?
  • Loading branch information
vvo committed Sep 30, 2017
1 parent b78c0ee commit ff657a0
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-editor-support/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
17 changes: 10 additions & 7 deletions packages/jest-editor-support/src/Snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -93,7 +93,7 @@ const buildName: (
return utils.testNameToKey(describeLess + fullName, position);
};

module.exports = class Snapshot {
export default class Snapshot {
_parser: Function;
_matchers: Array<string>;
constructor(parser: any, customMatchers?: Array<string>) {
Expand All @@ -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),
});
}
},
};
Expand All @@ -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;

Expand Down Expand Up @@ -161,4 +164,4 @@ module.exports = class Snapshot {
return result;
});
}
};
}
4 changes: 2 additions & 2 deletions packages/jest-editor-support/src/__tests__/Snapshot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-editor-support/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
11 changes: 6 additions & 5 deletions packages/jest-editor-support/src/parsers/babylon_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Expect>,
itBlocks: Array<ItBlock>,
};

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);
};

Expand Down
8 changes: 4 additions & 4 deletions packages/jest-snapshot/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
),
Expand Down Expand Up @@ -148,7 +148,7 @@ const toThrowErrorMatchingSnapshot = function(received: any, expected: void) {
};

module.exports = {
EXTENSION: SNAPSHOT_EXTENSION,
EXTENSION: utils.SNAPSHOT_EXTENSION,
SnapshotState,
addSerializer,
cleanup,
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit ff657a0

Please sign in to comment.