Skip to content

Instantly share code, notes, and snippets.

@TheSavior
Created September 26, 2016 21:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save TheSavior/dbe21187dc7c8a6cb2bd5000d100e555 to your computer and use it in GitHub Desktop.
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