Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to expose custom text on report page #32

Closed
4ekki opened this issue Dec 13, 2019 · 18 comments · Fixed by #33
Closed

Ability to expose custom text on report page #32

4ekki opened this issue Dec 13, 2019 · 18 comments · Fixed by #33
Assignees
Labels
enhancement New feature or request

Comments

@4ekki
Copy link
Contributor

4ekki commented Dec 13, 2019

I run tests with multiple versions of SUT and it would be great to be able to see inside the report, what software version was actually tested.

I'd like to be able to run jest with some environment variable, that will be taken into account by jest-html-reporters and text from this variable would be added to report page, something like this:
image

@4ekki 4ekki added the enhancement New feature or request label Dec 13, 2019
@Hazyzh
Copy link
Owner

Hazyzh commented Dec 19, 2019

Hi @4ekki , How about adding a reporter option?
Just like this
image
You can fill a list of values in this field.

@4ekki
Copy link
Contributor Author

4ekki commented Dec 19, 2019

@Hazyzh , yeah, this should work. Will it handle environment variables?

@Hazyzh
Copy link
Owner

Hazyzh commented Dec 19, 2019

@Hazyzh , yeah, this should work. Will it handle environment variables?

I can get environment variables, but hard to know which variables should show in the report.

@4ekki
Copy link
Contributor Author

4ekki commented Dec 19, 2019

What about taking a single specific variable, smth like $JEST_HTML_REPORTERS_CUSTOM_TEXT, and exposing its value to the report, if it's defined? Such variable can be initialized in globalTeardown script, allowing me to put there anything I want.

@Hazyzh
Copy link
Owner

Hazyzh commented Dec 19, 2019

If so, what's structure would the value like of this variable? for a single string, it's hard to display every item one by one.

@4ekki
Copy link
Contributor Author

4ekki commented Dec 19, 2019

For my specific case, I want to show this block in the report:

Environment:   		QA
Product branch:         feature/abc-4251-client-service-abc
Tests branch:  		master
Current time:  		2019-12-19T10:01:44.992Z

But I wanted to handle the formatting on my own (with all those \n and \t), since other people will definitely have their own structure.

However, I can put it into a json or an array of key-value pairs and you'll be able to parse it and put into a nice table-like structure.
So for this example, I'd define the following variable in globalTeardown:
process.env.JEST_HTML_REPORTERS_CUSTOM_TEXT_JSON="{'Environment':'QA','Product branch':'feature/abc-4251-client-service-abc','Tests branch':'master','Current time':'2019-12-19T10:01:44.992Z'}"

Hazyzh added a commit that referenced this issue Dec 24, 2019
env variable name is *JEST_HTML_REPORTERS_CUSTOM_INFOS*

Closes #32
@Hazyzh
Copy link
Owner

Hazyzh commented Dec 25, 2019

Hi @4ekki,
New version(1.2.1) will include this feature, even though it's an uncommon way to set a JSON object in the env variable, notice I change the specific variable name to JEST_HTML_REPORTERS_CUSTOM_INFOS.

A common way to use this feature is to set values in the config's customInfos key.

Hazyzh added a commit that referenced this issue Dec 25, 2019
* ✨ (main page) add a feature to display custom infos in main page

env variable name is *JEST_HTML_REPORTERS_CUSTOM_INFOS*

Closes #32

* Annotated

* update versions
@4ekki
Copy link
Contributor Author

4ekki commented Dec 25, 2019

@Hazyzh , many thanks!!!

@4ekki
Copy link
Contributor Author

4ekki commented Dec 25, 2019

Small note for anyone referring this in future. Since globalTeardown is actually run AFTER the report is being created, initializing JEST_HTML_REPORTERS_CUSTOM_INFOS should be done in globalSetup instead.

@poojachoudhary13
Copy link

poojachoudhary13 commented Jan 15, 2020

@4ekki if i want to attach the test case logs at the end of each test case then, can i use this?

@4ekki
Copy link
Contributor Author

4ekki commented Jan 15, 2020

@poojachoudhary13, this feature allows to define process.env.JEST_HTML_REPORTERS_CUSTOM_INFOS in globalSetup script and then read/write it in tests. On creating the report, object from this variable will be parsed to nice table in 'Custom information' section.
image

I don't understand how do you want to attach test case logs with this. Please share additional details.

@poojachoudhary13
Copy link

@4ekki Ohh okay, i wanted to attach console.log for each testcase. So that in case of failure we have some logs to look for. I thought JEST_HTML_REPORTERS_CUSTOM_INFOS will help with that.IS there any way to do what i am trying to achieve ?

@4ekki
Copy link
Contributor Author

4ekki commented Jan 16, 2020

@poojachoudhary13 , I don't think that you'll be able to put logs into this report. Furthermore, I don't think it's a good idea, since this report will be very huge if it contains all the logs.
In my tests, I use graylog for logging (the same, as my SUT) and send messages there. In the beginning of each test file, I take file name and use it as an identifier in graylog, so after tests complete I'm able to find every message that was sent to graylog from specific test file.
If you don't have an external logger (like graylog) in your project, you can try to create new *.log file in the beginning of the testrun and adjust your logger in tests to add messages with some unique prefix (matching test file name) to this file.

@kewinshah25
Copy link

Hi, I don't seem to get the environment variable (JEST_HTML_REPORTERS_CUSTOM_INFOS).
I initialized it in the globalSetup.ts and globalteardown.ts
But I still don't seem to find this custom info on my html reports, I think that I have configured the customInfos wrongly, can you please help?
"customInfos":"JEST_HTML_REPORTERS_CUSTOM_INFOS"
This is how I have initialised my customInfos and

module.exports = async () => {
    process.env.JEST_HTML_REPORTERS_CUSTOM_INFOS="{'Environment':'QA','Product branch':'feature/abc-4251-client-service-abc','Tests branch':'master','Current time':'2019-12-19T10:01:44.992Z'}"
    };

this is how I've initialised my globalSetup.ts.
Please help, thanks!

@4ekki
Copy link
Contributor Author

4ekki commented Jul 1, 2020

@kewinshah25 , this property should be a stringified JSON, which needs double quotes instead of single ones. Change to
process.env.JEST_HTML_REPORTERS_CUSTOM_INFOS='{"Environment":"QA","Product branch":"feature/abc-4251-client-service-abc","Tests branch":"master","Current time":"2019-12-19T10:01:44.992Z"}' and it will be fine.

It's even easier to define it as object and use JSON.stringify:

const jsonForReporter = {
       Environment: "QA",
      "Product branch": "feature/abc-4251-client-service-abc",
      "Tests branch": "master",
      "Current time":"2019-12-19T10:01:44.992Z"
};
process.env.JEST_HTML_REPORTERS_CUSTOM_INFOS = JSON.stringify(jsonForReporter);

@kewinshah25
Copy link

Thanks, that helped a lot!!

@mushtaque87
Copy link

mushtaque87 commented Apr 6, 2023

I tried the same thing on my detox project using jest test runner . I cant see it in the reporter.
beforeAll(async () => { const jsonForReporter = { Device: "iPhone 13", "OS": "iOS", }; console.log('jsonForReporter', JSON.stringify(jsonForReporter)); process.env.JEST_HTML_REPORTERS_CUSTOM_INFOS = JSON.stringify(jsonForReporter); })

But when I try to add customInfos in jest.config.js it works fine.
customInfos:[ {title : 'Device' , value: 'iPhone 13'}, {title : 'OS' , value: 'iOS'}, ]

I want to print these information on the fly to fetch the device.name and device.getPlatform() , after the app is launched in a emulator / simulator. Any idea how can I get these information printed on my reporter .. I use default globalSetup from jest and dont create my own.

@4ekki
Copy link
Contributor Author

4ekki commented Apr 6, 2023

@mushtaque87 , from your beforeAll I guess that you do this in test. In order to use process.env.JEST_HTML_REPORTERS_CUSTOM_INFOS, you need to define it in globalSetup or globalTeardown scripts. Tests have isolated context that is destroyed after they're over, so their process.env is different, than globalSetup/Teardown.

Unfortunately, AFAIK there's no way to share data from tests to globalSetup in Jest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants