Testing ES6 modules with ease

Testing ES6 modules may seem like a difficult task, but is quite straightforward once you know the approach.

First, create the module you'd like to test. The important part is to export an object with functions you'd like to spy/stub.

function whom = () => { return 'world' }
function sayHello = () => { return `Hello, ${whom()}!` }

export { whom, sayHello }

In our test file, it's important to import the whole object and name it using the import * as someName syntax.

import * as helloWorld from './hello-world'
import sinon from 'sinon'

test('it should say hello to compadres', function (assert) {
  sinon.stub(helloWorld, 'whom', () => { return 'compadres' })
  assert.equal(helloWorld.sayHello, 'Hello, compadres!')
})