When interviewing front-end engineering candidates, you may want to use Mocha and maybe JSDom too. We have already installed these npm packages. Here are examples of how to use them with CodePad with JavaScript (NodeJS) language:

Basic Example

const foo = function() {
  return "bar";
}

const Mocha = require('mocha');
const expect = require('chai').expect;
const mocha = new Mocha({ui: 'bdd'});
mocha.suite.emit('pre-require', this, 'solution', mocha);

describe("Test suite", function () {
  it("is working", function () {
    expect(foo()).to.equal("bar");
  });
});

mocha.run();

With JSDom

const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const Mocha = require('mocha');
const assert = require('assert');
const mocha = new Mocha();

function getContentByClassName(className) {
  const elements = document.getElementsByClassName(className);
  if (elements.length === 0) {
    return '';
  }

  return elements[0].textContent;
}

const dom = new JSDOM(`<div class="foo">my content</div>`);

const window = dom.window;
const document = dom.window.document;

mocha.suite.emit('pre-require', this, 'solution', mocha);

describe('#getContentByClassName', () => {
  it('should return an empty string if given a non existent class', () => {
    assert(getContentByClassName('bar') === '');
  });

  it('should return the content of a class', () => {
    assert(getContentByClassName('foo') === 'my content');
  });
});

mocha.run();


Did this answer your question?