It would be nice to be able to run Mocha tests from within normal Node.js code. Here's what I'm talking about:
const Mocha = require('Mocha');
const mocha = new Mocha();
const assert = require('chai').assert;
//do normal Node.js things
mocha.run(() => {
describe('an amazing suite of tests', () => {
it('should be true', () => {
assert.equal(true, true);
});
});
});
// do some more Node.js things
Or maybe a template string could work if we had to:
const Mocha = require('Mocha');
const mocha = new Mocha();
const assert = require('chai').assert;
//do normal Node.js things
mocha.run(`
describe('an amazing suite of tests', () => {
it('should be true', () => {
assert.equal(true, true);
});
});
`);
// do some more Node.js things
I'm running into problems with the limitations of only being able to run Mocha tests by loading stand-alone files. I'm trying to get some interesting test environments working inside of Electron and with web components.
"node-able" test files are a goal for the future; see #1969 and work done in mocha-core. as such, closing this as a dupe
That #1969 is quite useless if one wants to figure out how to run mocha tests as a regular nodejs app. Any clue?
There was something suggested there: const {describe, it, timeout} = require('mocha'); but this doesn't work and I'm still getting TypeError: describe is not a function
I'm experiencing this as well in vs code with node, selenium & mocha. Looking for a solution. Thanks.
+1 TypeError: describe is not a function even when using:
let mocha = require('mocha')
let describe = mocha.describe
I'd love the feature explained by @lastmjs too. I am using AWS SDK to query infrastructure resources that I'd like to assert afterwords
The SDK allows to run node js code as a node app. At the moment I am using a workaround by writing out to a file response from AWS API and then read that file with mocha and do the mocha things, but it would be extremely helpful to be able to run mocha from within a node app. Any updates on this?
this is how i got it working within electron AND i just found out the way to re-run tests! turns out there is a bug in the options parser
main.js:
var Mocha = require('mocha')
var mochaOptions = {
timeout: 10000,
ui: 'tdd',
bail: true,
cleanReferencesAfterRun: true, // https://github.com/mochajs/mocha/pull/4234
}
var mocha = new Mocha(mochaOptions)
mocha._cleanReferencesAfterRun = false // BUG: mochaOptions seems not to be correctly parsed
function testMochaRenderer() {
// https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically
mocha.addFile(path.join(testDir, 'mochaTest.js'))
runner = mocha.grep('testGroup1').run()
}
mochaTest.js:
mocha = require('mocha')
describe = mocha.describe
it = mocha.it
before = mochabefore
after = mocha.after
const assert = require('assert')
const chai = require('chai')
const chaiAsPromised = require('chai-as-promised')
chai.should()
chai.use(chaiAsPromised)
const timeout = 10000
describe('testMocha', () => {
before(() => {
return true
})
describe('testGroup1', () => {
it('should return -1 when not present', async function() {
await sleep(1000)
assert.strictEqual([1, 2, 3].indexOf(4), -1)
})
})
})
Most helpful comment
That #1969 is quite useless if one wants to figure out how to run mocha tests as a regular nodejs app. Any clue?
There was something suggested there:
const {describe, it, timeout} = require('mocha');but this doesn't work and I'm still gettingTypeError: describe is not a function