Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Add custom reporter to produce Problem Matcher output #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
"reporters": ["<rootDir>/json-reporter.js"]
}
35 changes: 35 additions & 0 deletions json-reporter.js
Original file line number Diff line number Diff line change
@@ -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}`);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the magic which turns a console.log into a GitHub annotation.

})

});
})
}
}

module.exports = MyCustomReporter;
10 changes: 10 additions & 0 deletions some.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});