things turning me on this week
☕enjoy with a hot cupa java/script
JavaScript has gotten even better and you can now run 100% ES Modules code “natively” through the entire node, JS, testing, and coverage (and more!) ecosystem. Picking the right tools that understand ES Modules
is key - here we use mocha
and c8
(also show how to use jasmine
(what is under jest
) and nyc
/. istanbul
if desired). Bonus points? sinon
for mocking.
Sample code setup
demonstration minimal working git repository
Uses simply: mocha and c8
The goal is to avoid babel, transpiling, and any dynamic transform of the JS source/test code as possible.
- mocha since it can understand ES Modules
(and make
import
/export
work nicely). - c8 for code coverage (since it understands ES Modules)
- expectations for
jest
/jasmine
like testing, eg:expect('hai'.length).toBe(3)
- sinon for mocking, stubs, and spies (more below on that)
There’s a few subtle things to get setup and “just right” to make it all work seamlessly. But once you know them, you’re good to go.
If you prefer jasmine
Thus
you can now write all your source and test code in the modern/future default 100% ES Modules
(AKA import
/ export
), eg:
import { foo, foobar } from '../src/foo.js'
describe('foo()', function () {
it('should return number plus one', function () {
expect(foo(1)).toEqual(2)
})
})
run
git clone https://github.com/traceypooh/js-es-modules-coverage-testing
cd js-es-modules-coverage-testing
npm install
npm test
(or alternatively: npx c8 mocha
)
yields
> @ test /Users/tracey/dev/js-es-modules-coverage-testing
> c8 mocha
bar()
✓ should return number minus one
foo()
✓ should return number plus one
foobar()
✓ should return same number
goo()
✓ should switch from 2 to 3 via a swap out
✓ should switch from 2 to 3 via a mock
5 passing (11ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
bar.js | 100 | 100 | 100 | 100 |
foo.js | 100 | 100 | 100 | 100 |
goo.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
the magic
- in
package.json
, this tellsnode
and scripts all.js
files are ES Modules
{
"type": "module",
}
- in
package.json
, this tellsmocha
to preloadexpectations
(forjest
-like testing)
{
"mocha": {
"require": "expectations"
}
}
mocking, stubs, and spies
By Far, this is the most complicated thing to get right. I spent hours and hours over a few days on/off trying to get mocking to work (first with jasmine
, then sinon
).
Fortunately, I found this lil’ gem:
Normally, ES Modules are very locked down and hard to mock. By grouping all the export-ed methods via export default Goo
, and importing via import Goo from '../src/goo.js'
, we can replace methods if needed for testing. (Imagine needing to test something that live-fetches resources, etc.)
- see src/goo.js
- see test/goo.test.js
2020 as ever, is a super-exciting time 🥳 for javascript!
Comments
Nothing yet.