How To Generate Mocha Test Reports
On This Page What is Mocha?How to Generate
How To Generate Mocha Test Reports
Mocha Test Report provides a comprehensive perceptivity into various build test during Mocha test mechanisation. It is useful in identifying bugs, analyzing them, and decide and improving test coverage.
In this article, you will explore how to generate apply default reporter, Mochawesome, and Test Reporting and Analytics.
What is Mocha?
Mocha is a simple and extensible JavaScript test fabric that runs on Node.js and in thebrowser. It makes it easier to write, and for APIs, web UI and.
Mocha do asynchronous testing simple. Mocha tryout have a high rate of accurate reports as it maps undetected exceptions to the correct tryout cause. Popular test frameworks like WebDriverIO, and support Mocha as their standard way of writing trial.
Read More:
How to Generate Test Reports with Default Mocha Reporter
Mocha has a built-in default newsman called Spec.Specgenerates reports as hierarchic sight nested based on the test cases. Defaut Mocha account are adjusted for the Windows terminal.
The following is a step-by-step guide to yield a report using the default report:
Step 1: Start a Node.js projection by running this commandnpm initin your project root directory.
Step 2: Install mocha as a development addiction with this bid:
npm install –save-dev mocha
Step 3: Create a.js file (test.js).
Step 4: Write and save the next sample examination code in your test.js file.
`` ` js const assert = require ('assert '); describe ('Array ', map () {describe (' # indexOf () ', function () {it ('should return -1 when the value is not present ', mapping () {assert.strictEqual ([1, 2, 3] .indexOf (4), -1);}); it ('should return the right index when the value is present ', function () {assert.strictEqual ([1, 2, 3] .indexOf (2), 1);});});}); describe ('Math ', purpose () {it ('should retrovert 9 when multiplying 3 by 3 ', use () {assert.strictEqual (3 * 3, 9);}); it ('should return -8 when breed -2 by 4 ', use () {assert.strictEqual (-2 * 4, -8);});}); `` `Execute your test using this command:npx mocha –spec test.js
Your resultant should look like what you have in the image below
The –spec flagspecifies the use of Spec as the nonpayment reporter.
Read More:
Limitation of Default Mocha Reporter
While the default reporter is useful and has a simple mocha examination reporter approach, it lacks sufficient data for tests. When running a large set or complex set of tests, the default test newsman is not always the best option.
Reports are expected to visualize the test details, failures, screenshots, time for execution, logging details, and more. However, the default Mocha test reporter does not meet these crucial needs.
Here are some of the Default Mocha Reporter limitations:
- Reports are limited to terminal window
- It lacks detailed analytics
- It lack ocular representation
- It is inefficient in reporting complex test cases
Read More:
What is Mochawesome?
Mochawesome is a custom mocha reporter. It runs on and works in coaction withMargeto return standalone HTML/CSS reports to visualize mocha exam. It is indorse on all the leading test fabric that support the Mocha style of indite exam.
Test reports from Mochawesome hold a visual appeal that is lacking in the default. Mochawesome useschartJsas its visualization library.
Developers using Mochawesome can translate the flow of their test as it displays lifecycle hooks(before (), beforeEach (), after (), and afterAll ()).
Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.
Using Mochawesome, developer can access heap trace logs and error messages associated with test cases. This give developers decent information to handle examination cases. Mochawesome is contrive to be responsive and mobile-friendly
Read More:
How to Generate Mocha Test Reports with Mochawesome
Generating a report with Mochawesome involves creating a undertaking and lend Mochawesome to it. To use Mochawesome in your project, follow the steps below:
Start a Node.js task by running the dictationnpm initin your project root directory.
Step 1: Install mocha as a ontogenesis dependency with this command:
npm install –save-dev mocha
Step 2: Install mochawesome as growth dependency using this command:
npm install –save-dev mochawesome
Step 3: Create a.js file(test.js).
Step 4: Write and save the undermentioned sample test code in your test.js file.
`` ` js const assert = require ('assert '); describe ('Array ', part () {describe (' # indexOf () ', function () {it ('should return -1 when the value is not present ', function () {assert.strictEqual ([1, 2, 3] .indexOf (4), -1);}); it ('should return the correct index when the value is present ', function () {assert.strictEqual ([1, 2, 3] .indexOf (2), 1);});});}); describe ('Math ', function () {it ('should return 9 when multiplying 3 by 3 ', function () {assert.strictEqual (3 * 3, 9);}); it ('should regress -8 when multiplying -2 by 4 ', function () {assert.strictEqual (-2 * 4, -8);});}); `` `Run this code using the–reporter mochawesome fleur-de-lis,as present below:
npx mocha test.js –reporter mochawesome
Your result should look like what you experience in the ikon below
Using the –reporter mochawesome flag specifies that you are using the reporter pick and Mochawesome describe source instead of the nonpayment Mocha reporter.
Running the above command likewise created a mochawesome-report folder in your project.
As shown in the image above, mochawesome generated two study (JSON and HTML).
Mochawesome Report in HTML
Mochawesome Report in JSON
Read More:
How to Get Mocha Test Report with Test Reporting and Analytics
enhances test reports by providing a centralized dashboard for elaborated penetration, analytics, and debugging capabilities. It mix seamlessly with Mocha and fetch report for every build run with elaborated insights and custom view, get it easier to analyze tryout resultant, identify failures, and improve test coverage.
To integrate with BrowserStack Test Reporting and Analytics, you need to get a BrowserStack account and a MochaJs trial rooms.
The following steps illustrate how to get Mocha test reports with BrowserStack Test Reporting and Analytics
Step 1: Install the modish version of the browserstack-node-sdk-npm package. Run this command to install the sdk package:
npm install browserstack-node-sdk @ latest
Step 2: Create abrowserstack.ymlfile in the root pamphlet of your examination suit if it doesn ’ t live. The yaml file should look something like this:
userName: YOUR_USERNAME accessKey: YOUR_ACCESS_KEY buildName: `` Your static build/job name goes hither '' projectName: `` Your static project name travel hither '' CUSTOM_TAG_1: `` You can set a custom Build Tag here '' # Use CUSTOM_TAG_ & lt; N & gt; and set more build tags as you demand. …
Step 3: Run your test with BrowserStack Test Reporting and Analytics. Prependbrowserstack-node-sdkto the existing command you use to trigger a run, and data will automatically commence getting sent to BrowserStack Test Reporting and Analytics. Your command should look like this:
npx browserstack-node-sdk `` Your existing command for running test retinue. E.g. & lt; mocha mocha/specs/e2e/ * /bdd/ * .e2e.js -- timeout=60000 & gt; ''
Note:Do not use double quotes (“ “) in your dictation.
After the build run completion, you will see the physique URL for Test Reporting and Analytics on your console.
Why Use Test Reporting and Analytics for Mocha Test Reports?
Using Test Reporting and Analytics helps developers keep track of every Mocha Automation build run by creating a detailed Mocha Test Report on the Test Reporting and Analytics Dashboard with all historical data.
The following are some of the reasons developer use:
- Detailed Test Build Insight: With Test Reporting and Analytics, you can admission comprehensive test build insight like execution, build stability, run succinct, and more.
- Historical data andTrends: Test Reporting and Analytics allows developer to get comprehensive historical data and place drift for re-run and merged footrace of exam build for a best understanding of recurring issues.
- Support End-to-End Visibility: It provides consummate end-to-end visibleness into automation metric for all teams.
- Track Performance and Failure Rate: It aid developer know the performance of each test build and track the failure pace of tests, craziness, and average failure pace to proceed an eye on the health of the test suite.
- Team Collaboration: Test Reporting and Analytics allows for better team collaboration, by facilitating the seamless download and sharing of exam reports.
Conclusion
Generating tryout reports is an important aspect of test debugging and analysis. Mocha default test reporters and custom reporters like Mochawesome provide basic capabilities. By integrating with creature like BrowserStack Test Reporting and Analytics, you can keep track of every Mocha Automation make run by make detailed, reliable, and well shareable Mocha Test Reports.
On This Page
# Ask-and-Contributeabout this topic with our Discord community.
Related Guides
Automate This With SUSA
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.
Try SUSA FreeTest Your App Autonomously
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.
Try SUSA Free