Skip to content

Instantly share code, notes, and snippets.

Embed
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
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.