Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run Mocha tests from within existing Node.js code #2314

Closed
lastmjs opened this issue Jun 16, 2016 · 6 comments
Closed

Run Mocha tests from within existing Node.js code #2314

lastmjs opened this issue Jun 16, 2016 · 6 comments

Comments

@lastmjs
Copy link

lastmjs commented Jun 16, 2016

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.

@boneskull
Copy link
Contributor

"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

@pps83
Copy link

pps83 commented Aug 31, 2016

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

@fbonds
Copy link

fbonds commented Apr 12, 2018

I'm experiencing this as well in vs code with node, selenium & mocha. Looking for a solution. Thanks.

@Gribbs
Copy link

Gribbs commented May 10, 2018

+1 TypeError: describe is not a function even when using:

let mocha = require('mocha')
let describe = mocha.describe

@fasatrix
Copy link

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?

@audioscavenger
Copy link

audioscavenger commented Oct 26, 2020

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)
    })
  })
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants