Mocha: run browsers tests in child process and feed TAP output to parent process

Created on 30 Mar 2017  路  5Comments  路  Source: mochajs/mocha

I am missing some know-how here - I am looking for a way to run Mocha tests running in the browser in child processes.

Is this possible? How do people run their front-end unit tests and collect the results in an automated way?

question stale

Most helpful comment

@the1mills Sorry I only saw your question just now -- didn't get a notification about the edit.

I don't know much about Karma competitors off the top of my head, although it's possible some of the multiple-browser cloud-based testing services I alluded to can be used directly, without Karma. Karma's main feature is allowing you to run JS tests in a browser -- any browser for which there is a Karma runner plugin -- but initiating from and receiving the results on the commandline. Thus, essentially, it brings together automation and browser environments for JS tests, whereas with something like Mocha alone you'd typically have to open a page manually to run tests in a browser environment. (But it doesn't replace Mocha, rather allowing just about any JS test library to be run inside the browser -- so they work together in a complementary fashion.) This sort of feature is useful just any time you want automated tests for JS code that the code is supposed to work in a browser.

(In contrast, if you want to test from a level of effects that the code has on a webpage -- basically functional/integration-testing site behavior instead of unit-testing the underlying JS -- you'd want Selenium or some such WebDriver implementation, which runs in server-side code such as Node JS but gives you functions to open a browser and simulate user interactions within it. Like Karma, Selenium is complementary to Mocha; but the two tackle completely different aspects of "browser testing": Karma tests JS code in a browser, Selenium provides code to test the page as a UI.)

All 5 comments

I usually use Karma, if that's what you're thinking of? Steps go something like this:

  • npm install mocha karma karma-mocha
  • Karma needs a little boilerplate configuration, I think the one supplied by its init command is sufficient to start.
  • for each browser, run Karma and specify the browser; Karma will take Mocha and your tests and run them inside a newly spawned browser instance, but report the results programmatically on the commandline pretty much like Mocha would have (although you may also have to find a Karma version of whichever reporter you want for reading the result output; but it'll also use the return code to programmatically signal success/failure)

On a CI server, the utility of this depends on the browser you want to be able to test and the browser available on the server; sometimes it's more helpful to use a service such as SauceLabs, Testling or... what's the other one, BrowserStack or something like that?... that basically does what Karma does only they farm it out to browsers on their server so you don't need the browsers installed where the tests are actually running (this is also handy for running many different browser versions en masse).

[EDITTED TO ADD:] Just saw the mention of TAP and a parent process in the title; I haven't tried quite this, but I would imagine there's a TAP reporter for Karma somewhere, and that you can use the usual Node child-process methods to get the output from Karma? (I know I've succeeded in running Mocha as a child process and piping the output elsewhere before.)

Thanks let me digest this may ask a followup question.
Karma seems to be the name of the game - are there any Karma competitors that you know of? Just trying to get a handle on exactly what market Karma has cornered.
cheers

@the1mills Sorry I only saw your question just now -- didn't get a notification about the edit.

I don't know much about Karma competitors off the top of my head, although it's possible some of the multiple-browser cloud-based testing services I alluded to can be used directly, without Karma. Karma's main feature is allowing you to run JS tests in a browser -- any browser for which there is a Karma runner plugin -- but initiating from and receiving the results on the commandline. Thus, essentially, it brings together automation and browser environments for JS tests, whereas with something like Mocha alone you'd typically have to open a page manually to run tests in a browser environment. (But it doesn't replace Mocha, rather allowing just about any JS test library to be run inside the browser -- so they work together in a complementary fashion.) This sort of feature is useful just any time you want automated tests for JS code that the code is supposed to work in a browser.

(In contrast, if you want to test from a level of effects that the code has on a webpage -- basically functional/integration-testing site behavior instead of unit-testing the underlying JS -- you'd want Selenium or some such WebDriver implementation, which runs in server-side code such as Node JS but gives you functions to open a browser and simulate user interactions within it. Like Karma, Selenium is complementary to Mocha; but the two tackle completely different aspects of "browser testing": Karma tests JS code in a browser, Selenium provides code to test the page as a UI.)

Thanks @ScottFreeCode that is useful info for me

I am a bot that watches issues for inactivity.
This issue hasn't had any recent activity, and I'm labeling it stale. In 14 days, if there are no further comments or activity, I will close this issue.
Thanks for contributing to Mocha!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SGD1953 picture SGD1953  路  75Comments

ibc picture ibc  路  59Comments

jbnicolai picture jbnicolai  路  37Comments

stevemao picture stevemao  路  46Comments

wclr picture wclr  路  50Comments