Javascript - Test


Testing with Javascript.

Testing in Javascript is really dependent on which type of engine you test will be running. ie:

  • with the DOM (in a browser against a web page)
  • or without the DOM (ie in node)

Side effects in javascript testing = DOM manipulations


  • An HTML page which executes tests one after the other and show a green or red bar for results reporting. The DOM of that page have two uses:
    • provide fixtures for the tests
    • and production code to work on,
    • and reporting to the end user.

report through the console: you execute tests via a command line or with an IDE plugin. The browser is captured and used to execute the code, but does not show results: it sends them back (via Ajax ??). The execution can be started out of the browser

Process: writing inline event handlers is much easier than using the DOM APIs to bind those events but move those inline events to distinct scripts, either on the same page or even in a separate JavaScript file.

Headless testing

See: Web - Headless browser (Test automation)

test spies, stubs and mocks (for test setup).


  • Spies get you information on function calls, like how many times they were called, or what arguments were passed to them.
  • Stubs are like spies, but they replace the target function. You can use stubs to control a method's behaviour to force a code path (like throwing errors) or to prevent calls to external resources (like HTTP APIs).
  • A mock is a fake method with a pre-programmed behavior and expectations.

Code coverage

Task Runner