Ask coding questions

← Back to all posts
Testing with Mocha and Chai
hiitsme (1)

Does anyone know how to use mocha and chai on this site? I cannot figure out how.

Commentshotnewtop
akatechis (3)

You can't run mocha directly as a CLI program, since this service gives you a REPL rather than a system shell. I managed to get this working using mocha's JS API which you can find at https://mochajs.org/api/

In your index.js file, you wanna put something like the following:

const Mocha = require('mocha')

const runner = new Mocha({})

runner.addFile('./mytest.js')

runner.run(failures => {
  if (failures) {
    console.error(failures)
  } else {
    console.log('All passed.')
  }
})

This will start mocha, add your test file, and then run the suites it found. You can add as many files as you want, or even use the fs module to dynamically get a list of all test files in your repl package.

From there, your mytest.js file is a typical mocha test module which requires mocha, chai, and any other package you want.