Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A full example of using mocha and jsdom in coderpad.
const jsdom = require('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 topLevel = this;
mocha.suite.emit('pre-require', topLevel, 'solution', mocha);
describe('#getContentByClassName', () => {
it('should return an empty string if given a non existent class', () => {
topLevel.document = jsdom.jsdom('<div class="foo">my content</div>');
assert(getContentByClassName('bar') === '');
});
it('should return the content of a class', () => {
topLevel.document = jsdom.jsdom('<div class="foo">my content</div>');
assert(getContentByClassName('foo') === 'my content');
});
});
mocha.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment