Skip to content

Commit

Permalink
Improve toWarnDev matcher DX for unexpected warnings (facebook#12082)
Browse files Browse the repository at this point in the history
Use jest-diff to format the warnings in a way that makes it easier to spot the differences.
  • Loading branch information
bvaughn authored Jan 23, 2018
1 parent 098745b commit d0e75dc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"gzip-size": "^3.0.0",
"jasmine-check": "^1.0.0-rc.0",
"jest": "^22.0.6",
"jest-diff": "^22.1.0",
"merge-stream": "^1.0.0",
"minimatch": "^3.0.4",
"minimist": "^1.2.0",
Expand Down
22 changes: 15 additions & 7 deletions scripts/jest/matchers/toWarnDev.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const jestDiff = require('jest-diff');

function normalizeCodeLocInfo(str) {
return str && str.replace(/at .+?:\d+/g, 'at **');
}
Expand Down Expand Up @@ -39,13 +41,19 @@ const createMatcherFor = consoleMethod =>
}
}

let errorMessage = `Unexpected warning recorded:\n${this.utils.printReceived(
message
)}`;
if (expectedMessages.length > 0) {
errorMessage += `\n\nThe following expected warnings were not yet seen:\n${expectedMessages
.map(unformatted => this.utils.printExpected(unformatted))
.join('\n')}`;
let errorMessage;
if (expectedMessages.length === 0) {
errorMessage =
'Unexpected warning recorded: ' +
this.utils.printReceived(normalizedMessage);
} else if (expectedMessages.length === 1) {
errorMessage =
'Unexpected warning recorded: ' +
jestDiff(normalizedMessage, expectedMessages[0]);
} else {
errorMessage =
'Unexpected warning recorded: ' +
jestDiff([normalizedMessage], expectedMessages);
}

// Record the call stack for unexpected warnings.
Expand Down
20 changes: 20 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3213,6 +3213,15 @@ jest-diff@^22.0.6:
jest-get-type "^22.0.6"
pretty-format "^22.0.6"

jest-diff@^22.1.0:
version "22.1.0"
resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-22.1.0.tgz#0fad9d96c87b453896bf939df3dc8aac6919ac38"
dependencies:
chalk "^2.0.1"
diff "^3.2.0"
jest-get-type "^22.1.0"
pretty-format "^22.1.0"

jest-docblock@^22.0.6:
version "22.0.6"
resolved "https://registry.yarnpkg.com/jest-docblock/-/jest-docblock-22.0.6.tgz#f187fb2c67eec0999e569d563092125283084f9e"
Expand All @@ -3238,6 +3247,10 @@ jest-get-type@^22.0.6:
version "22.0.6"
resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-22.0.6.tgz#301fbc0760779fdbad37b6e3239a3c1811aa75cb"

jest-get-type@^22.1.0:
version "22.1.0"
resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-22.1.0.tgz#4e90af298ed6181edc85d2da500dbd2753e0d5a9"

jest-haste-map@^22.0.6:
version "22.0.6"
resolved "https://registry.yarnpkg.com/jest-haste-map/-/jest-haste-map-22.0.6.tgz#198d665f65e1c484fef106a3c970c5b47903647e"
Expand Down Expand Up @@ -4273,6 +4286,13 @@ pretty-format@^22.0.6:
ansi-regex "^3.0.0"
ansi-styles "^3.2.0"

pretty-format@^22.1.0:
version "22.1.0"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-22.1.0.tgz#2277605b40ed4529ae4db51ff62f4be817647914"
dependencies:
ansi-regex "^3.0.0"
ansi-styles "^3.2.0"

private@^0.1.6, private@~0.1.5:
version "0.1.7"
resolved "https://registry.yarnpkg.com/private/-/private-0.1.7.tgz#68ce5e8a1ef0a23bb570cc28537b5332aba63ef1"
Expand Down

0 comments on commit d0e75dc

Please sign in to comment.