JavaScript Tester

Check a snippet for syntax errors, run it, and prove it behaves with built in assert and expect helpers.

JavaScript
Test report
Runs locally in your browser

About the JavaScript Tester

Testing a small function normally means creating a project, installing a runner and writing a config file. This page removes all of that. Paste the function and a few assertions, and you get a pass or fail line for each one plus a count at the bottom. It is meant for the fifteen minute questions: does this regular expression handle an empty string, does this rounding helper deal with negative numbers, does this parser cope with a missing field.

Two helper styles are available and can be mixed freely. The node style is assert(value, label) with assert.equal, assert.notEqual, assert.deepEqual and assert.throws. The expectation style is expect(actual).toBe(expected, label), along with toEqual, toBeTruthy, toBeFalsy, toContain, toHaveLength, toBeCloseTo and toThrow. A failing line prints the expected value next to the value it actually received, so you rarely need to add logging to work out what went wrong.

Before anything runs, the code is parsed and a syntax error is reported with its exact line and column, which saves a confusing run. Execution then happens in a disposable sandboxed frame with a time limit, so an infinite loop stops rather than freezing the tab. Objects and arrays are compared by their serialised form, which makes deep equality work for ordinary data without any extra setup.

When you only want to watch output rather than assert on it, the JavaScript Playground is the friendlier page, and JavaScript Validator checks syntax without running anything at all.

How to use

  1. Paste the function you want to check, then add assertions below it.
  2. Use assert.equal(actual, expected, label) or expect(actual).toBe(expected, label). The label is what appears in the report.
  3. Read the report: every assertion becomes a PASS or FAIL line, and a failure shows both values.
  4. Check the summary at the bottom for the pass and fail counts and the run time.
  5. Download the report if you want to paste it into a pull request.

Common questions

Which assertion helpers are available?
assert, assert.equal, assert.notEqual, assert.deepEqual and assert.throws, plus expect with toBe, toEqual, toBeTruthy, toBeFalsy, toContain, toHaveLength, toBeCloseTo and toThrow.
How are objects and arrays compared?
By their serialised form, so two structures with the same keys and values in the same order are treated as equal without any extra configuration.
Can I import a library or a module?
No. The sandbox has no network and no module loader, so paste the code you need to test directly into the editor.
What happens if my code throws before the assertions run?
The error is reported at the end of the output and the counts reflect only the assertions that were reached before it.
Does a failing assertion stop the run?
No. Every assertion is recorded and execution continues, so one run shows you all of the failures at once.