Hi, I'm following the wiki page but when running it via node, the above error exists.
I was expecting the same test files can be run using mocha CLI or via the node require style.
Is there anything that I missed out?
Thanks in advance.
@beeing you have to run mocha test files with the mocha executable. mocha sets up the test harness, and defines the globals which include describe, before, after, etc. Oh, nvm, you are using Mocha programmatically? In that case, I am not sure, will have defer to someone else.
Oh ok, then meaning the mocha.run will not be usable?
I haven't found any problems with the example from the wiki; can you put together a minimal but complete example that doesn't work and post it here for us to look into?
Thanks!
@ScottFreeCode here you go.
We can run this successfully: mocha try.js
try.js
const chai = require('chai')
, expect = chai.expect
describe('Settings', () => {
it('Contact - set', done => {
done(Error('not implemented'))
})
it('Contact - get', done => {
done(Error('not implemented'))
})
})
Then run in node using: node fromnode.js
fromnode.js
let Mocha = require('mocha'),
path = require('path');
// Instantiate a Mocha instance.
let mocha = new Mocha({
ui: 'tdd',
reporter: 'list'
});
let testDir = './'
mocha.addFile(
path.join(testDir, 'try.js')
)
// Run the tests.
mocha.run(failures => {
process.on('exit', () => {
process.exit(failures) // exit with non-zero status if there were failures
})
})
Ok, that鈥檚 what I鈥檓 missing. Thanks!
You're welcome!
(Please let us know if you have any further trouble with it; we can always reopen this issue if appropriate.)
Most helpful comment
You're using the
'bdd'interface (describe,it...), but configuring Mocha for the'tdd'interface; just change'tdd'to'bdd'in yourfromnode.jsand it should work.