How to use mocha

Comprehensive mocha code examples:

How to use mocha.g:

26
27
28
29
30
31
32
33
34
35
36
37
  // eslint-disable-next-line no-console
  console.warn("Couldn't determine Mocha version");
}
module.exports = MochaJUnitReporter;


// A subset of invalid characters as defined in http://www.w3.org/TR/xml/#charsets that can occur in e.g. stacktraces
// regex lifted from https://github.com/MylesBorins/xml-sanitizer/ (licensed MIT)
var INVALID_CHARACTERS_REGEX = /[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007f-\u0084\u0086-\u009f\uD800-\uDFFF\uFDD0-\uFDFF\uFFFF\uC008]/g; //eslint-disable-line no-control-regex


function findReporterOptions(options) {

How to use mocha.suite:

73
74
75
76
77
78
79
80
81
82
      assert.strictEqual(bom.version, newVersion)
    })
  )
)

suite('cannot set version', () =>
  [
    0, -1, 3.5, -3.5,
    'foo', '3',
    true, false,

How to use mocha.grep:

105
106
107
108
109
110
111
112
113
114
 * Exclusive suite.
 */

context.describe.only = function(title, fn){
        var suite = context.describe(title, fn);
        mocha.grep(suite.fullTitle());
        return suite;
};

/**

How to use mocha.Context:

18
19
20
21
22
23
24
25
26
27
const ast = parser.parse(text);

if (!ast.feature) {
  throw new Error(`No 'Features' available in Gherkin '${file}' provided!`);
}
const suite = new Suite(ast.feature.name, new Context());
const tags = ast.feature.tags.map(t => t.name);
suite.title = `${suite.title} ${tags.join(' ')}`.trim();
suite.tags = tags || [];
suite.comment = ast.feature.description;

How to use mocha.test:

38
39
40
41
42
43
44
45
46
47
  assert.strictEqual(bom.vulnerabilities.size, 0)
  assert.strictEqual(bom.version, 1)
  assert.strictEqual(bom.serialNumber, undefined)
})

test('construct with preset properties', () => {
  const version = Math.round(Math.random() * 1000)
  const serialNumber = 'urn:uuid:12345678-4321-0987-6547-abcdef123456'
  const metadata = new Metadata()
  const components = new ComponentRepository()

How to use mocha.Suite:

5
6
7
8
9
10
11
12
13
14
const reportDirectory = path.resolve(__dirname, `../execution-report`)

// mocha setup
const Test = Mocha.Test;

const suiteInstance = Mocha.Suite;

const mocha = new Mocha({
    timeout: 200000,
    reporter: 'mochawesome',

How to use mocha.Test:

116
117
118
119
120
121
122
123
124
125
}

if (child.scenario) {
  const tags = child.scenario.tags.map(t => t.name);
  const title = `${child.scenario.name} ${tags.join(' ')}`.trim();
  const test = new Test(title, async () => runSteps(child.scenario.steps));
  test.tags = suite.tags.concat(tags);
  test.file = file;
  suite.addTest(scenario.test(test));
}

How to use mocha.after:

21
22
23
24
25
26
27
28
29
30
      utils = equality_utils;
      done();
    });
});

after(function() {
  testHelper.remove();
});

describe('equality', function() {

How to use mocha.afterEach:

13
14
15
16
17
18
19
20
21
22
beforeEach(() => {
  sandbox.replace(core, 'getInput', (key) => {
    return metadata.inputs[key].default
  })
})
afterEach(() => {
  sandbox.restore()
})
it('handles unconventional commit', async () => {
  const addLabels = sandbox.stub(api, 'addLabels').resolves(undefined)

How to use mocha.Runner:

41
42
43
44
45
46
47
48
49
50
  EVENT_SUITE_BEGIN, // suite starts
  EVENT_SUITE_END, // suite ends
  EVENT_TEST_BEGIN, // test begins
  EVENT_TEST_END, // test ends
  EVENT_TEST_PENDING, // test pending
} = Mocha.Runner.constants;

const {
  Base,
} = Mocha.reporters;

How to use mocha.before:

12
13
14
15
16
17
18
19
20
21
22
23
const testHelper = require('./test-helper');


describe('general utilities', function () {
  let utils;


  before(function (done) {
    testHelper.install();
    requirejs(['utils/common-utilities'],
      function (equality_utils) {
        utils = equality_utils;

How to use mocha.reporters:

22
23
24
25
26
27
28
29
30
var _mocha = require('mocha');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var debug = require('debug')('mocha-ci-slack-reporter');
var Base = _mocha.reporters.Base;

var Reporter = function (_Base) {
  (0, _inherits3.default)(Reporter, _Base);

How to use mocha.beforeEach:

17
18
19
20
21
22
23
24
25
26

const session = new Session()
const cache = session.getCache('cache-client')
this.timeout(30000)

beforeEach(async () => {
  await cache.clear()
  await cache.set(val123, val123)
  await cache.set(val234, val234)
  await cache.set(val345, val345)

How to use mocha.describe:

How to use mocha.it:

127
128
129
130
131
132
133
134
135
136
const { describe, it } = require('mocha');
const { check, gen } = require('mocha-testcheck');
const { expect } = require('chai');

describe('MySpec', () => {
  it('accepts an int', check(gen.int, x => {
    expect(x).to.be.a('number');
  });
});
```

How to use mocha.prototype:

5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
  throw err;
};


/**
 * Override ui to ensure that the ui functions are initialized.
 * Normally this would happen in Mocha.prototype.loadFiles.
 */


mocha.ui = function(ui){
  Mocha.prototype.ui.call(this, ui);

How to use mocha.utils:

5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
 */


process.removeListener = function(e, fn){
  if ('uncaughtException' == e) {
    global.onerror = function() {};
    var i = Mocha.utils.indexOf(uncaughtExceptionHandlers, fn);
    if (i != -1) { uncaughtExceptionHandlers.splice(i, 1); }
  }
};