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 2 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"]
}
30 changes: 30 additions & 0 deletions json-reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class MyCustomReporter {
constructor(globalConfig, options) {
this._globalConfig = globalConfig;
this._options = options;
}

onRunComplete(contexts, results) {
results.testResults[0].testResults.forEach((item) => {
if (item.status !== "failed") {
return;
}
const newLine = "%0A";
Copy link
Owner Author

Choose a reason for hiding this comment

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

We have to url encode new lines in order to produce a matcher message that can spawn over multiple lines

const name = results.testResults[0].testFilePath;

if (!item.location) {
throw new Error(
"You need to invoked jest with argument --testLocationInResults"
stefanbuck marked this conversation as resolved.
Show resolved Hide resolved
);
}

const line = item.location.line + 1;
Copy link

Choose a reason for hiding this comment

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

+ 1? testLocation is for the it, not the expect. You're probably better of looking at the stack trace in the error

Copy link
Owner Author

Choose a reason for hiding this comment

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

I misread the documentation

Note that column is 0-indexed while line is not.
https://jestjs.io/docs/en/cli.html#--testlocationinresults

Anyway, thanks for the hint about the it this wasn't clear from the documentation. Will parse the error message as you suggested.

Copy link
Owner Author

Choose a reason for hiding this comment

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

I updated the implementation according to your comment. Should I try to get a Jest PR up? If you could point me to a location / package where I should make changes, that would be amazing. From a quick look it feels like it should be part of https://github.com/facebook/jest/tree/master/packages/jest-reporters/

Copy link

Choose a reason for hiding this comment

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

Part of reporters, yeah.

Auto-adding it would go somewhere here: https://github.com/facebook/jest/blob/faa52673f4d02ee1d273103ada9382a0805b54a4/packages/jest-core/src/TestScheduler.ts#L267-L298

We should probably wait a bit with that until we're happy, though

Copy link
Owner Author

Choose a reason for hiding this comment

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

I'm already super happy with the result 😉

I want to get this feature in so I can deprecate a Danger.js plugin I wrote to present failing tests on a Pull Request. Also I think the Jest community will love this integration which works out of the box with every GitHub Action setup.


The image below shows a comment on a Pull Request generated by a custom Danger.js](https://github.com/danger/danger-js) plugin

const col = item.location.column;
const message = item.failureMessages[0].replace(/\n/g, newLine);
Copy link
Owner Author

Choose a reason for hiding this comment

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

We might need to iterate over failureMessages, but for this MVP its should be enough


console.log(`::error file=${name},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.

The GitHub runner has some build in matchers and therefore all we have to do is producing output in this format actions/toolkit#260 (comment)

});
}
}

module.exports = MyCustomReporter;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "jest ."
"test": "jest . --testLocationInResults"
},
"keywords": [],
"author": "",
Expand Down
4 changes: 4 additions & 0 deletions some.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ describe("foo", function() {
it("passes", function() {
expect("a").toBe("a");
});

it("fails", function() {
expect("b").toBe("a");
});
});