Skip to content

Commit

Permalink
Improves the terse messages in jest-editor (jestjs#3615)
Browse files Browse the repository at this point in the history
* Improves the terse messages in jest-editor

* Adds a note about wording for jest-editor inside a test that matches the error it is implicity relying on

* Adds a note about wording for jest-editor inside a test that matches the error it is implicity relying on
  • Loading branch information
orta authored and cpojer committed May 22, 2017
1 parent a0ae458 commit 526c00c
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 13 deletions.
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"jest.pathToJest": "npm run jest --",
"jest.pathToJest": "yarn jest --",
"editor.rulers": [
80
],
Expand All @@ -12,5 +12,6 @@
"prettier.printWidth": 80,
"prettier.singleQuote": true,
"prettier.trailingComma": "all",
"prettier.semi": true
"prettier.semi": true,
"editor.formatOnSave": true
}
1 change: 1 addition & 0 deletions fixtures/failing_jsons/failing_expects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"numFailedTestSuites":1,"numFailedTests":7,"numPassedTestSuites":5,"numPassedTests":8,"numPendingTestSuites":0,"numPendingTests":0,"numRuntimeErrorTestSuites":0,"numTotalTestSuites":6,"numTotalTests":15,"snapshot":{"added":0,"didUpdate":false,"failure":true,"filesAdded":0,"filesRemoved":0,"filesUnmatched":1,"filesUpdated":0,"matched":5,"total":6,"unchecked":0,"unmatched":1,"updated":0},"startTime":1495204247174,"success":false,"testResults":[{"assertionResults":[{"failureMessages":["Error: expect(received).toEqual(expected)\n\nExpected value to equal:\n 2\nReceived:\n 1\n at Object.<anonymous>.it (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/src/_tests/example.test.ts:3:13)\n at Object.asyncFn (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/jasmine-async.js:68:30)\n at resolve (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/queueRunner.js:38:12)\n at tryCallTwo (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:45:5)\n at doResolve (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:200:13)\n at new Promise (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:66:3)\n at mapper (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/queueRunner.js:31:21)\n at Promise.resolve.then.el (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/p-map/index.js:42:16)\n at tryCallOne (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:37:12)\n at /Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:123:15"],"status":"failed","title":"numbers"},{"failureMessages":["Error: expect(received).toEqual(expected)\n\nExpected value to equal:\n 2\nReceived:\n \"1\"\n\nDifference:\n\n Comparing two different types of values. Expected number but received string.\n at Object.<anonymous>.it (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/src/_tests/example.test.ts:7:15)\n at Object.asyncFn (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/jasmine-async.js:68:30)\n at resolve (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/queueRunner.js:38:12)\n at tryCallTwo (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:45:5)\n at doResolve (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:200:13)\n at new Promise (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:66:3)\n at mapper (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/queueRunner.js:31:21)\n at Promise.resolve.then.el (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/p-map/index.js:42:16)\n at tryCallOne (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:37:12)\n at /Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:123:15"],"status":"failed","title":"string to numbers: numbers"},{"failureMessages":["Error: expect(received).toEqual(expected)\n\nExpected value to equal:\n {\"a\": 2}\nReceived:\n {}\n\nDifference:\n\n- Expected\n+ Received\n\n-Object {\n- \"a\": 2,\n-}\n+Object {}\n at Object.<anonymous>.it (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/src/_tests/example.test.ts:11:14)\n at Object.asyncFn (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/jasmine-async.js:68:30)\n at resolve (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/queueRunner.js:38:12)\n at tryCallTwo (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:45:5)\n at doResolve (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:200:13)\n at new Promise (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:66:3)\n at mapper (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/queueRunner.js:31:21)\n at Promise.resolve.then.el (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/p-map/index.js:42:16)\n at tryCallOne (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:37:12)\n at /Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:123:15"],"status":"failed","title":"objects"},{"failureMessages":["Error: expect(value).toMatchSnapshot()\n\nReceived value does not match stored snapshot 1.\n\n- Snapshot\n+ Received\n\nObject {\n- \"asd\": 3,\n+ \"asd\": 2,\n }\n at Object.<anonymous>.it (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/src/_tests/example.test.ts:15:20)\n at Object.asyncFn (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/jasmine-async.js:68:30)\n at resolve (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/queueRunner.js:38:12)\n at tryCallTwo (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:45:5)\n at doResolve (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:200:13)\n at new Promise (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:66:3)\n at mapper (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/queueRunner.js:31:21)\n at Promise.resolve.then.el (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/p-map/index.js:42:16)\n at tryCallOne (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:37:12)\n at /Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:123:15"],"status":"failed","title":"snapshots"},{"failureMessages":[],"status":"passed","title":"making snapshot"},{"failureMessages":["Error: expect(received).toBeGreaterThan(expected)\n\nExpected value to be greater than:\n 3\nReceived:\n 2\n at Object.<anonymous>.it (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/src/_tests/example.test.ts:23:13)\n at Object.asyncFn (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/jasmine-async.js:68:30)\n at resolve (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/queueRunner.js:38:12)\n at tryCallTwo (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:45:5)\n at doResolve (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:200:13)\n at new Promise (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:66:3)\n at mapper (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/queueRunner.js:31:21)\n at Promise.resolve.then.el (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/p-map/index.js:42:16)\n at tryCallOne (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:37:12)\n at /Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:123:15"],"status":"failed","title":"greater than"},{"failureMessages":["Error: expect(received).toBeFalsy()\n\nExpected value to be falsy, instead received\n 2\n at Object.<anonymous>.it (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/src/_tests/example.test.ts:27:13)\n at Object.asyncFn (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/jasmine-async.js:68:30)\n at resolve (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/queueRunner.js:38:12)\n at tryCallTwo (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:45:5)\n at doResolve (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:200:13)\n at new Promise (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:66:3)\n at mapper (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/queueRunner.js:31:21)\n at Promise.resolve.then.el (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/p-map/index.js:42:16)\n at tryCallOne (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:37:12)\n at /Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:123:15"],"status":"failed","title":"falsy"},{"failureMessages":["Error: expect(received).toBeTruthy()\n\nExpected value to be truthy, instead received\n null\n at Object.<anonymous>.it (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/src/_tests/example.test.ts:31:16)\n at Object.asyncFn (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/jasmine-async.js:68:30)\n at resolve (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/queueRunner.js:38:12)\n at tryCallTwo (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:45:5)\n at doResolve (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:200:13)\n at new Promise (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:66:3)\n at mapper (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/jest-jasmine2/build/queueRunner.js:31:21)\n at Promise.resolve.then.el (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/p-map/index.js:42:16)\n at tryCallOne (/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:37:12)\n at /Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/node_modules/promise/lib/core.js:123:15"],"status":"failed","title":"truthy"}],"endTime":1495204249679,"message":" ● numbers\n\n expect(received).toEqual(expected)\n \n Expected value to equal:\n 2\n Received:\n 1\n \n at Object.<anonymous>.it (src/_tests/example.test.ts:3:13)\n\n ● string to numbers: numbers\n\n expect(received).toEqual(expected)\n \n Expected value to equal:\n 2\n Received:\n \"1\"\n \n Difference:\n \n Comparing two different types of values. Expected number but received string.\n \n at Object.<anonymous>.it (src/_tests/example.test.ts:7:15)\n\n ● objects\n\n expect(received).toEqual(expected)\n \n Expected value to equal:\n {\"a\": 2}\n Received:\n {}\n \n Difference:\n \n - Expected\n + Received\n \n -Object {\n - \"a\": 2,\n -}\n +Object {}\n \n at Object.<anonymous>.it (src/_tests/example.test.ts:11:14)\n\n ● snapshots\n\n expect(value).toMatchSnapshot()\n \n Received value does not match stored snapshot 1.\n \n - Snapshot\n + Received\n \n Object {\n - \"asd\": 3,\n + \"asd\": 2,\n }\n \n at Object.<anonymous>.it (src/_tests/example.test.ts:15:20)\n\n ● greater than\n\n expect(received).toBeGreaterThan(expected)\n \n Expected value to be greater than:\n 3\n Received:\n 2\n \n at Object.<anonymous>.it (src/_tests/example.test.ts:23:13)\n\n ● falsy\n\n expect(received).toBeFalsy()\n \n Expected value to be falsy, instead received\n 2\n \n at Object.<anonymous>.it (src/_tests/example.test.ts:27:13)\n\n ● truthy\n\n expect(received).toBeTruthy()\n \n Expected value to be truthy, instead received\n null\n \n at Object.<anonymous>.it (src/_tests/example.test.ts:31:16)\n","name":"/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/src/_tests/example.test.ts","startTime":1495204247355,"status":"failed","summary":""},{"assertionResults":[{"failureMessages":[],"status":"passed","title":"handles some simple JSX"}],"endTime":1495204250922,"message":"","name":"/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/src/_tests/render.test.tsx","startTime":1495204249689,"status":"passed","summary":""},{"assertionResults":[{"failureMessages":[],"status":"passed","title":"generates the width for a simple component"}],"endTime":1495204251093,"message":"","name":"/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/src/_tests/_component-to-node.test.ts","startTime":1495204250950,"status":"passed","summary":""},{"assertionResults":[{"failureMessages":[],"status":"passed","title":"wraps whatever text you pass into it with an SVG schema"}],"endTime":1495204251200,"message":"","name":"/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/src/_tests/_tree-to-svg.test.ts","startTime":1495204251099,"status":"passed","summary":""},{"assertionResults":[{"failureMessages":[],"status":"passed","title":"wraps whatever text you pass into it with an SVG schema"},{"failureMessages":[],"status":"passed","title":"Calls nodeToSVG for it's first node"},{"failureMessages":[],"status":"passed","title":"Calls nodeToSVG for it's children nodes"}],"endTime":1495204251312,"message":"","name":"/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/src/_tests/_tree-to-svg-sub-funcs.test.ts","startTime":1495204251206,"status":"passed","summary":""},{"assertionResults":[{"failureMessages":[],"status":"passed","title":"handles a simple square"}],"endTime":1495204251434,"message":"","name":"/Users/orta/dev/projects/artsy/js/libs/jest-snapshots-svg/src/_tests/_node-to-svg.test.ts","startTime":1495204251318,"status":"passed","summary":""}],"wasInterrupted":false}
15 changes: 12 additions & 3 deletions integration_tests/__tests__/toMatchSnapshot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ test('basic support', () => {
expect(status).toBe(0);
}

// This test below also covers how jest-editor-support creates terse messages
// for letting a Snapshot update, so if the wording is updated, please edit
// /packages/jest-editor-support/src/TestReconciler.js
{
writeFiles(TESTS_DIR, {[filename]: template(['{apple: "updated value"}'])});
writeFiles(TESTS_DIR, {
[filename]: template(['{apple: "updated value"}']),
});
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('Received value does not match stored snapshot');
expect(status).toBe(1);
Expand All @@ -65,7 +70,9 @@ test('error thrown before snapshot', () => {
});`);

{
writeFiles(TESTS_DIR, {[filename]: template(['true', '{a: "original"}'])});
writeFiles(TESTS_DIR, {
[filename]: template(['true', '{a: "original"}']),
});
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).toMatch('1 snapshot written in 1 test suite.');
expect(status).toBe(0);
Expand All @@ -78,7 +85,9 @@ test('error thrown before snapshot', () => {
}

{
writeFiles(TESTS_DIR, {[filename]: template(['false', '{a: "original"}'])});
writeFiles(TESTS_DIR, {
[filename]: template(['false', '{a: "original"}']),
});
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);
expect(stderr).not.toMatch('1 obsolete snapshot found');
expect(status).toBe(1);
Expand Down
6 changes: 4 additions & 2 deletions packages/jest-editor-support/src/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ module.exports = class Settings extends EventEmitter {
}

getConfig(completed: any) {
this.getConfigProcess = this._createProcess(this.workspace, ['--showConfig']);
this.getConfigProcess = this._createProcess(this.workspace, [
'--showConfig',
]);

this.getConfigProcess.stdout.on('data', (data: Buffer) => {
const {config, version} = JSON.parse(data.toString());
Expand All @@ -68,7 +70,7 @@ module.exports = class Settings extends EventEmitter {
this.settings = config;
});

// They could have an older build of Jest which
// They could have an older build of Jest which
// would error with `--showConfig`
this.getConfigProcess.on('close', () => {
completed();
Expand Down
14 changes: 11 additions & 3 deletions packages/jest-editor-support/src/TestReconciler.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,21 @@ module.exports = class TestReconciler {

// Do everything we can to try make a one-liner from the error report
sanitizeShortErrorMessage(string: string): string {
if (string.includes('does not match stored snapshot')) {
return 'Snapshot has changed';
}

if (string.includes('New snapshot was not written')) {
return 'New snapshot is ready to write';
}

return string
.split('\n')
.splice(2)
.join('')
.replace(' ', ' ')
.replace('Received:', ' Received:')
.replace('Difference:', ' Difference:');
.replace(/\s\s+/g, ' ')
.replace('Received:', ', Received:')
.split('Difference:')[0];
}

// Pull the line out from the stack trace
Expand Down
4 changes: 1 addition & 3 deletions packages/jest-editor-support/src/__tests__/Settings-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ describe('Settings', () => {
it('calls callback even if no data is sent', () => {
const workspace = new ProjectWorkspace('root_path', 'path_to_jest');
const completed = jest.fn();
const config = {cacheDirectory: '/tmp/jest', name: '[md5 hash]'};


const mockProcess: any = new EventEmitter();
mockProcess.stdout = new EventEmitter();
const createProcess = () => mockProcess;
Expand All @@ -59,7 +58,6 @@ describe('Settings', () => {

expect(completed).toHaveBeenCalled();
});

});

const makeBuffer = (content: string) => {
Expand Down
39 changes: 39 additions & 0 deletions packages/jest-editor-support/src/__tests__/TestReconciler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,42 @@ Expected value to be falsy, instead received
});
});
});

describe('Terse Messages', () => {
it('handles shrinking a snapshot message', () => {
const parser = reconcilerWithFile('failing_expects.json');
const file =
'/Users/orta/dev/projects/artsy/js/' +
'libs/jest-snapshots-svg/src/_tests/example.test.ts';

const terseForTest = name => parser.stateForTestAssertion(file, name);

let message = 'Expected value to equal: 2, Received: 1';
let testName = 'numbers';
expect(terseForTest(testName).terseMessage).toEqual(message);

message = 'Expected value to equal: 2, Received: "1"';
testName = 'string to numbers: numbers';
expect(terseForTest(testName).terseMessage).toEqual(message);

message = 'Expected value to equal: {"a": 2}, Received: {}';
testName = 'objects';
expect(terseForTest(testName).terseMessage).toEqual(message);

message = 'Snapshot has changed';
testName = 'snapshots';
expect(terseForTest(testName).terseMessage).toEqual(message);

message = 'Expected value to be greater than: 3, Received: 2';
testName = 'greater than';
expect(terseForTest(testName).terseMessage).toEqual(message);

message = 'Expected value to be falsy, instead received 2';
testName = 'falsy';
expect(terseForTest(testName).terseMessage).toEqual(message);

message = 'Expected value to be truthy, instead received null';
testName = 'truthy';
expect(terseForTest(testName).terseMessage).toEqual(message);
});
});

0 comments on commit 526c00c

Please sign in to comment.