-
Notifications
You must be signed in to change notification settings - Fork 1
How to test ECMAScript modules using Node.js
Lloyd Brookes edited this page Dec 12, 2019
·
2 revisions
Install esm-runner.
$ npm install --save-dev esm-runner
Typical ESM test file example.
import TestRunner from 'test-runner'
import assert from 'assert'
import fetch from 'node-fetch'
const tom = new TestRunner.Tom()
tom.test('Math.random() should return a number between 0 and 1', function () {
const result = Math.random()
assert.equal(typeof result, 'number')
assert.ok(result >= 0 && result <= 1)
})
tom.test('REST API should return the current todo item', async function () {
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1')
const todo = await response.json()
assert.equal(todo.userId, 1)
assert.equal(todo.title, 'delectus aut autem')
})
export default tom
Run the tests using esm-runner. This runner is identical to test-runner with the one difference that it supports ECMAScript Modules (ESM).
$ npx esm-runner test.mjs
Start: 2 tests loaded
✓ synopsis Math.random() should return a number between 0 and 1
✓ synopsis REST API should return the current todo item
Completed in 199ms. Pass: 2, fail: 0, skip: 0.