-
Notifications
You must be signed in to change notification settings - Fork 1
How to test ECMAScript modules in the browser
Lloyd Brookes edited this page Dec 8, 2019
·
2 revisions
To run our test file in the browser we need to make a couple of adjustments. Instead of Node's assert we use chai. Also, there is no need to load node-fetch since fetch is built into the browser.
import Tom from 'test-object-model'
import 'https://www.chaijs.com/chai.js'
const assert = chai.assert
const tom = new 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 tomRun the tests in Chromium using web-runner.
$ web-runner browser.mjs
Start: 2 tests loaded
✓ browser Math.random() should return a number between 0 and 1 3.7ms
✓ browser REST API should return the current todo item 122.0ms
Completed in 134ms. Pass: 2, fail: 0, skip: 0.