2

I'm setting up a testing framework for my Node.js application, and I have my Mocha tests already set up and working.

I wanted a UI for my test suite where I can select check boxes to determine which tests should be in the current execution. I have already created a UI for this with Reactjs.

Is there any way for me to trigger and start my mocha test suite through a Reactjs button onClick on the UI? (I do not want to test the React UI, I want the UI to start the Mocha tests which test a different application)

I am looking for a way to run Mocha either through the web or programmatically. React has made this a bit of a challenge since I used the create-react-app command. Attempting to add Mocha via index.html results in an issue since node_modules is outside of the public directory, and attempts to require mocha programmatically result in an error saying there is no constructor.

Kevin P
  • 31
  • 5
  • Possible duplicate of [How do you run mocha tests in the browser?](https://stackoverflow.com/questions/42857778/how-do-you-run-mocha-tests-in-the-browser) – nicholaswmin Feb 09 '19 at 15:00
  • 1
    This unfortunately did not fit my use case, as the react configurations will not allow the index.html file to reference files outside of the public folder. However, I was able to get Mocha to work programmatically by following this documentation: https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically I had to make a slight change though due to the way that react imports the package. Require the Mocha package like so: import Mocha from 'mocha'; let mocha = new Mocha.Mocha(); – Kevin P Feb 11 '19 at 16:51

1 Answers1

1

I was unable to make using Mocha in web via script tags work, however after some tinkering I was able to get Mocha to work programmatically.

I followed the documentation for this use case, though I had to make a slight change to get the constructor to work: https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically

Rather than this (resulted in an error regarding the constructor):

var Mocha = require('mocha')
var mocha = new Mocha();

I did this:

import Mocha from 'mocha';
let mocha = new Mocha.Mocha();

This allowed me to create an instance of Mocha and make calls to the different Mocha functions. This was from within the generated App.js file made by create-react-app.

Kevin P
  • 31
  • 5