Skip to content

Commit

Permalink
test(stryker): Replace line numbers by error index
Browse files Browse the repository at this point in the history
so the snapshots match even when stryker mutates the files.
  • Loading branch information
karfau committed Mar 21, 2021
1 parent a681852 commit e28e57b
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 46 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@

/node_modules
/reports
/test/error/reported.json
86 changes: 43 additions & 43 deletions test/error/__snapshots__/reported-levels.test.js.snap
Expand Up @@ -10,7 +10,7 @@ Array [
exports[`SYNTAX_AttributeEqualMissingValue with mimeType text/html should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] element parse error: Error: attribute value missed!!||@#[line:1,col:1]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -24,7 +24,7 @@ Array [
exports[`SYNTAX_AttributeEqualMissingValue with mimeType text/xml should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] element parse error: Error: attribute value missed!!||@#[line:1,col:1]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -38,7 +38,7 @@ Array [
exports[`SYNTAX_AttributeMissingEndingQuote with mimeType text/html should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] element parse error: Error: attribute value no end '\\"' match||@#[line:1,col:1]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -52,7 +52,7 @@ Array [
exports[`SYNTAX_AttributeMissingEndingQuote with mimeType text/xml should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] element parse error: Error: attribute value no end '\\"' match||@#[line:1,col:1]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -66,7 +66,7 @@ Array [
exports[`SYNTAX_ElementClosingNotConnected with mimeType text/html should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] element parse error: Error: elements closed character '/' and '>' must be connected to||@#[line:1,col:6]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -80,7 +80,7 @@ Array [
exports[`SYNTAX_ElementClosingNotConnected with mimeType text/xml should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] element parse error: Error: elements closed character '/' and '>' must be connected to||@#[line:1,col:6]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -94,9 +94,9 @@ Array [
exports[`SYNTAX_EndTagMaybeNotComplete with mimeType text/html should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] end tag name: inner maybe not complete||@#[line:1,col:6]
at parse (lib/sax.js:128)",
at parse (lib/sax.js:#2)",
"[xmldom error] element parse error: Error: [xmldom error] end tag name: inner maybe not complete||@#[line:1,col:6]||@#[line:1,col:6]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -110,9 +110,9 @@ Array [
exports[`SYNTAX_EndTagMaybeNotComplete with mimeType text/xml should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] end tag name: inner maybe not complete||@#[line:1,col:6]
at parse (lib/sax.js:128)",
at parse (lib/sax.js:#2)",
"[xmldom error] element parse error: Error: [xmldom error] end tag name: inner maybe not complete||@#[line:1,col:6]||@#[line:1,col:6]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -126,9 +126,9 @@ Array [
exports[`SYNTAX_EndTagNotComplete with mimeType text/html should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] end tag name: xml is not complete:xml||@#[line:1,col:1]
at parse (lib/sax.js:124)",
at parse (lib/sax.js:#1)",
"[xmldom error] element parse error: Error: [xmldom error] end tag name: xml is not complete:xml||@#[line:1,col:1]||@#[line:1,col:1]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -142,9 +142,9 @@ Array [
exports[`SYNTAX_EndTagNotComplete with mimeType text/xml should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] end tag name: xml is not complete:xml||@#[line:1,col:1]
at parse (lib/sax.js:124)",
at parse (lib/sax.js:#1)",
"[xmldom error] element parse error: Error: [xmldom error] end tag name: xml is not complete:xml||@#[line:1,col:1]||@#[line:1,col:1]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -158,7 +158,7 @@ Array [
exports[`SYNTAX_InvalidAttributeName with mimeType text/html should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] element parse error: Error: invalid attribute:123||@#[line:1,col:1]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -172,7 +172,7 @@ Array [
exports[`SYNTAX_InvalidAttributeName with mimeType text/xml should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] element parse error: Error: invalid attribute:123||@#[line:1,col:1]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -186,7 +186,7 @@ Array [
exports[`SYNTAX_InvalidTagName with mimeType text/html should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] element parse error: Error: invalid tagName:123||@#[line:1,col:1]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -200,7 +200,7 @@ Array [
exports[`SYNTAX_InvalidTagName with mimeType text/xml should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] element parse error: Error: invalid tagName:123||@#[line:1,col:1]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -214,9 +214,9 @@ Array [
exports[`SYNTAX_UnclosedComment with mimeType text/html should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] Unclosed comment||@#[line:1,col:1]
at parseDCC (lib/sax.js:538)",
at parseDCC (lib/sax.js:#21)",
"[xmldom error] element parse error: Error: [xmldom error] Unclosed comment||@#[line:1,col:1]||@#[line:1,col:1]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -230,9 +230,9 @@ Array [
exports[`SYNTAX_UnclosedComment with mimeType text/xml should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] Unclosed comment||@#[line:1,col:1]
at parseDCC (lib/sax.js:538)",
at parseDCC (lib/sax.js:#21)",
"[xmldom error] element parse error: Error: [xmldom error] Unclosed comment||@#[line:1,col:1]||@#[line:1,col:1]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -246,9 +246,9 @@ Array [
exports[`SYNTAX_UnexpectedEndOfInput with mimeType text/html should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] unexpected end of input||@#[line:1,col:1]
at parseElementStartPart (lib/sax.js:308)",
at parseElementStartPart (lib/sax.js:#13)",
"[xmldom error] element parse error: Error: [xmldom error] unexpected end of input||@#[line:1,col:1]||@#[line:1,col:1]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -262,9 +262,9 @@ Array [
exports[`SYNTAX_UnexpectedEndOfInput with mimeType text/xml should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] unexpected end of input||@#[line:1,col:1]
at parseElementStartPart (lib/sax.js:308)",
at parseElementStartPart (lib/sax.js:#13)",
"[xmldom error] element parse error: Error: [xmldom error] unexpected end of input||@#[line:1,col:1]||@#[line:1,col:1]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -278,7 +278,7 @@ Array [
exports[`WF_AttributeMissingQuote with mimeType text/html should escalate Error thrown in errorHandler.warning to errorHandler.error 1`] = `
Array [
"[xmldom warning] attribute \\"value\\" missed quot(\\")!||@#[line:1,col:1]
at parseElementStartPart (lib/sax.js:333)",
at parseElementStartPart (lib/sax.js:#14)",
]
`;

Expand All @@ -292,7 +292,7 @@ Array [
exports[`WF_AttributeMissingQuote with mimeType text/xml should escalate Error thrown in errorHandler.warning to errorHandler.error 1`] = `
Array [
"[xmldom warning] attribute \\"value\\" missed quot(\\")!||@#[line:1,col:1]
at parseElementStartPart (lib/sax.js:333)",
at parseElementStartPart (lib/sax.js:#14)",
]
`;

Expand All @@ -306,7 +306,7 @@ Array [
exports[`WF_AttributeMissingQuote2 with mimeType text/html should escalate Error thrown in errorHandler.warning to errorHandler.error 1`] = `
Array [
"[xmldom warning] attribute \\"&\\" missed quot(\\")!!||@#[line:1,col:1]
at parseElementStartPart (lib/sax.js:363)",
at parseElementStartPart (lib/sax.js:#17)",
]
`;

Expand All @@ -320,7 +320,7 @@ Array [
exports[`WF_AttributeMissingQuote2 with mimeType text/xml should escalate Error thrown in errorHandler.warning to errorHandler.error 1`] = `
Array [
"[xmldom warning] attribute \\"&\\" missed quot(\\")!!||@#[line:1,col:1]
at parseElementStartPart (lib/sax.js:363)",
at parseElementStartPart (lib/sax.js:#17)",
]
`;

Expand All @@ -334,7 +334,7 @@ Array [
exports[`WF_AttributeMissingStartingQuote with mimeType text/html should escalate Error thrown in errorHandler.warning to errorHandler.error 1`] = `
Array [
"[xmldom warning] attribute \\"attr\\" missed start quot(\\")!!||@#[line:1,col:1]
at parseElementStartPart (lib/sax.js:281)",
at parseElementStartPart (lib/sax.js:#10)",
]
`;

Expand All @@ -348,7 +348,7 @@ Array [
exports[`WF_AttributeMissingStartingQuote with mimeType text/xml should escalate Error thrown in errorHandler.warning to errorHandler.error 1`] = `
Array [
"[xmldom warning] attribute \\"attr\\" missed start quot(\\")!!||@#[line:1,col:1]
at parseElementStartPart (lib/sax.js:281)",
at parseElementStartPart (lib/sax.js:#10)",
]
`;

Expand All @@ -362,7 +362,7 @@ Array [
exports[`WF_AttributeMissingValue with mimeType text/html should escalate Error thrown in errorHandler.warning to errorHandler.error 1`] = `
Array [
"[xmldom warning] attribute \\"attr\\" missed value!! \\"attr\\" instead!!||@#[line:1,col:1]
at parseElementStartPart (lib/sax.js:337)",
at parseElementStartPart (lib/sax.js:#15)",
]
`;

Expand All @@ -376,7 +376,7 @@ Array [
exports[`WF_AttributeMissingValue with mimeType text/xml should escalate Error thrown in errorHandler.warning to errorHandler.error 1`] = `
Array [
"[xmldom warning] attribute \\"attr\\" missed value!! \\"attr\\" instead!!||@#[line:1,col:1]
at parseElementStartPart (lib/sax.js:337)",
at parseElementStartPart (lib/sax.js:#15)",
]
`;

Expand All @@ -392,7 +392,7 @@ Array [
exports[`WF_AttributeMissingValue2 with mimeType text/html should escalate Error thrown in errorHandler.warning to errorHandler.error 1`] = `
Array [
"[xmldom warning] attribute \\"attr\\" missed value!! \\"attr\\" instead2!!||@#[line:1,col:1]
at parseElementStartPart (lib/sax.js:385)",
at parseElementStartPart (lib/sax.js:#18)",
]
`;

Expand All @@ -408,7 +408,7 @@ Array [
exports[`WF_AttributeMissingValue2 with mimeType text/xml should escalate Error thrown in errorHandler.warning to errorHandler.error 1`] = `
Array [
"[xmldom warning] attribute \\"attr\\" missed value!! \\"attr\\" instead2!!||@#[line:1,col:1]
at parseElementStartPart (lib/sax.js:385)",
at parseElementStartPart (lib/sax.js:#18)",
]
`;

Expand All @@ -422,7 +422,7 @@ Array [
exports[`WF_AttributeValueMustAfterEqual with mimeType text/html should escalate Error thrown in errorHandler.warning to errorHandler.error 1`] = `
Array [
"[xmldom warning] attribute value must after \\"=\\"||@#[line:1,col:1]
at parseElementStartPart (lib/sax.js:263)",
at parseElementStartPart (lib/sax.js:#8)",
]
`;

Expand All @@ -436,7 +436,7 @@ Array [
exports[`WF_AttributeValueMustAfterEqual with mimeType text/xml should escalate Error thrown in errorHandler.warning to errorHandler.error 1`] = `
Array [
"[xmldom warning] attribute value must after \\"=\\"||@#[line:1,col:1]
at parseElementStartPart (lib/sax.js:263)",
at parseElementStartPart (lib/sax.js:#8)",
]
`;

Expand Down Expand Up @@ -464,9 +464,9 @@ Array [
exports[`WF_EntityDeclared with mimeType text/html should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] entity not found:&e;||@#[line:1,col:1]
at replace (lib/sax.js:71)",
at replace (lib/sax.js:#0)",
"[xmldom error] element parse error: Error: [xmldom error] entity not found:&e;||@#[line:1,col:1]||@#[line:1,col:1]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -480,9 +480,9 @@ Array [
exports[`WF_EntityDeclared with mimeType text/xml should not catch Error thrown in errorHandler.error 1`] = `
Array [
"[xmldom error] entity not found:&e;||@#[line:1,col:1]
at replace (lib/sax.js:71)",
at replace (lib/sax.js:#0)",
"[xmldom error] element parse error: Error: [xmldom error] entity not found:&e;||@#[line:1,col:1]||@#[line:1,col:1]
at parse (lib/sax.js:208)",
at parse (lib/sax.js:#5)",
]
`;

Expand All @@ -496,6 +496,6 @@ Array [
exports[`WF_UnclosedXmlAttribute with mimeType text/xml should escalate Error thrown in errorHandler.warning to errorHandler.error 1`] = `
Array [
"[xmldom warning] unclosed xml attribute||@#[line:1,col:1]
at parse (lib/sax.js:173)",
at parse (lib/sax.js:#4)",
]
`;
20 changes: 17 additions & 3 deletions test/error/reported-levels.test.js
@@ -1,6 +1,6 @@
'use strict'

const { REPORTED } = require('./reported')
const { LINE_TO_ERROR_INDEX, REPORTED } = require('./reported')
const { getTestParser } = require('../get-test-parser')
const { ParseError } = require('../../lib/sax')
const { DOMParser } = require('../../lib/dom-parser')
Expand Down Expand Up @@ -90,8 +90,10 @@ describe.each(Object.entries(REPORTED))(
* - put's the message on one line as first line
* - picks the first line in the stack trace that is in `lib/sax.js`,
* and strips absolute paths and character position from that stack entry
* as second line
* as second line. the line number in the stack is converted to the error index
* (to make snapshot testing possible even with stryker).
* @param {Error} error
* @returns {string}
*/
function toErrorSnapshot(error) {
const libSaxMatch = /\/.*\/(lib\/sax\.js)/
Expand All @@ -102,5 +104,17 @@ function toErrorSnapshot(error) {
// strip of absolute path
.replace(libSaxMatch, '$1')
// strip of position of character in line
.replace(/:\d+\)$/, ')')}`
.replace(/:\d+\)$/, ')')
// since stryker mutates the code, line numbers in the stack trace will change
// and no longer match the ones in the snapshots.
// So we map line numbers to an "error index" meaning "the nth thing reported by lib/sax.js"
// `./reported.js` creates that index on every test run
// and writes it `./report.json` on every run, for later inspection.
.replace(/(lib\/sax\.js:)\d+/, (fileAndLine, file) => {
return `${file}#${
fileAndLine in LINE_TO_ERROR_INDEX
? LINE_TO_ERROR_INDEX[fileAndLine].index
: -1
}`
})}`
}

0 comments on commit e28e57b

Please sign in to comment.