node package manager
Love JavaScript? Your insights can make it even better. Take the 2017 JavaScript Ecosystem Survey »



mock html dom document

Version Coverage Status Downloads Version License


$ npm init
$ npm install mockbot-document --save-dev


With tools like browserify, it's easy to create client side code in node.js. But, when testing with tools like mocha, code that references browser elements or the document object will throw an error.

This can be worked around by creating a mock object that simulates the document object. Assign it to global before each test starts, then delete it from global when each test finishes. Here is an example using mocha:

"use strict";

var documentFactory = require("mockbot-document");

describe('module smoke test', () => {

  beforeEach( done => {
    // Call before all tests
    // mock browser document
    global.document = documentFactory.create();
  afterEach( done => {
    // Call after all tests
    delete global.document;
  it('createElement should return object', done => {
    var result = document.createElement('div');


The main objective of this module is to provide placeholders to avoid lint and compiler errors. Duplicating functionality of a real browser is not as important. Though attempts will be made to simulate a response from a browser, actual functionality is not guaranteed.

Available Methods

Only a small subset of mock document methods are currently available. Over time others will be added. See the module reference below to see what is currently available.

Requesting Methods

If a specific method is desired ASAP, open up an issue on to request it.


For information on available element methods, see mockbot-element.





Factory module



Mock Element



mockbot-document.mockElement(spec) ⇒ mockbot-element

Creates a mock element to simulate html elements.

Kind: instance method of mockbot-document

Param Type Description
spec Object Named parameters object
spec.tagName string Required element type name (a, div, x-thing, etc.) string Optional element id

Example (usage)

document.mockElement( { tagName: tagName, id: id } );
var result = document.getElementById(id);

mockbot-document.querySelector() ⇒ null

Mock document.querySelector(). CURRENTLY NON-FUNCTIONAL - just a place holder for now.

Kind: instance method of mockbot-document
Example (usage)


mockbot-document.getElementById(id) ⇒ mockbot-element

Mock document.getElementById()

Kind: instance method of mockbot-document

Param Type Description
id string Element id

Example (usage)

var el = document.getElementById("id");

mockbot-document.getElementsByTagName(tagName) ⇒ Array.<mockbot-element>

Mock document.getElementsByTagName()

Kind: instance method of mockbot-document

Param Type Description
tagName string Element tagName (div,p,a,etc.)

Example (usage)

var elArray = document.getElementsByTagName("div");

mockbot-document.createElement(tagName) ⇒ mockbot-element

Mock document.createElement()

Kind: instance method of mockbot-document

Param Type Description
tagName string name of HTML element (a, div, x-thing, etc.)

Example (usage)

var el = document.createElement("div");


Factory module

mockbot-document-factory.create(spec) ⇒ mockbot-document

Factory method It takes one spec parameter that must be an object with named parameters

Kind: static method of mockbot-document-factory

Param Type Description
spec Object Named parameters object

Example (Usage example)

var factory = require("mockbot-document");
var obj = factory.create({});


Mock Element

Kind: global external
See: mockbot-element


To test, go to the root folder and type (sans $):

$ npm test



In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

Version History

Version 0.1.14

  • fixex getElementsByTagName documentation

Version 0.1.13

  • added getElementsByTagName
  • fixed mockElement documentation

Version 0.1.12

  • fixed issue where getElementById was returning array instead of first element

Version 0.1.11

  • updated mockbot-element to version 0.1.8

Version 0.1.10

  • fixed type-o in doc

Version 0.1.9

  • refactored getElementById
  • cleaned up documentation

Version 0.1.8

  • fixed version history

Version 0.1.7

  • updated mockbot-element to version 0.1.7
  • removed client example

Version 0.1.6

  • updated mockbot-element to version 0.1.6

Version 0.1.5

  • updated mockbot-element to version 0.1.5

Version 0.1.4

  • updated mockbot-element to version 0.1.4 (contains tagName property)
  • updated createElement to create element using tagName

Version 0.1.3

  • fixed doc errors

Version 0.1.2

  • fixed version history

Version 0.1.1

  • added test cases to bring coverage up to 100%
  • added mockElement method
  • updated to latest version of mockbot-element

Version 0.1.0

  • initial release