diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..ff3f391 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,3 @@ +module.exports = { + "reporters": ["/json-reporter.js"] +} diff --git a/json-reporter.js b/json-reporter.js new file mode 100644 index 0000000..708ddf5 --- /dev/null +++ b/json-reporter.js @@ -0,0 +1,35 @@ +class MyCustomReporter { + constructor(globalConfig, options) { + this._globalConfig = globalConfig; + this._options = options; + } + + onRunComplete(contexts, results) { + results.testResults.forEach((testResultItem) => { + const testFilePath = testResultItem.testFilePath; + + testResultItem.testResults.forEach((result) => { + if (result.status !== "failed") { + return; + } + + result.failureMessages.forEach(failureMessages => { + const newLine = "%0A"; + const message = failureMessages.replace(/\n/g, newLine); + const captureGroup = message.match(/:([0-9]+):([0-9]+)/); + + if (!captureGroup) { + console.log('Unable to extract line number from call stack') + return; + } + + const [, line, col] = captureGroup; + console.log(`::error file=${testFilePath},line=${line},col=${col}::${message}`); + }) + + }); + }) + } +} + +module.exports = MyCustomReporter; diff --git a/some.test.js b/some.test.js index 3549285..299b4fb 100644 --- a/some.test.js +++ b/some.test.js @@ -2,4 +2,14 @@ describe("foo", function() { it("passes", function() { expect("a").toBe("a"); }); + + it("fails", function() { + expect("b").toBe("b"); + expect("c").toBe("d"); + expect("d").toBe("c"); + }); + + it("fails II", function() { + expect(1).toBe(2); + }); });