Simple command-line interface plus opinionated configuration for testing browser-based Typescript projects with Mocha.
💡 Builds upon many of the ideas from @tomazzaman's excellent article “How to get fast unit tests with(out) Webpack”
Initializes and configures a testing environment with the following:
- ts-node for writing tests and transpiling
.ts
imports - mocha for running tests
- sinon for mocking/stubbing functions
- chai for assertions w/
- jsdom for a browser-esque environment
- 😏 No webpack
- 🚀 Tracks dependencies in watch mode and only re-runs tests that are affected
- 📦 Limited support for loading
.vue
files (only<template>
and<script lang="ts">
are currently supported
yarn add @snaptopixel/testify
{
"test": "testify -t tests/**/*.spec.ts"
}
{
"test:watch": "testify -t packages/**/*.spec.ts -w"
}
🗒 Install nyc and prepend it to your script before
testify
. Check nyc's README for configuration options
{
"test": "nyc testify -t tests/**/*.spec.ts"
}
You can require any number of .ts or .js files using the -r
argument like so:
{
"test": "testify -t tests/**/*.spec.ts -r some-file.ts some-other-file.js"
}
These scripts will be required and executed in the node environment once jsdom has been initialized.
This is useful if your scripts depend on global variables, for example:
// In required js file
window.SomeGlobal = {
someMethod: sinon.spy()
}
// In test file
describe('globals', () => {
it('can access global', () => {
window.SomeGlobal.someMethod('hey')
expect(window.SomeGlobal.someMethod).calledWith('hey')
})
})
If your require file exports a function it will be called with an api object that allows for a few customization options:
TypeScript example
export default function initTests(api: IRequireApi) {
// Add chai plugins via api.chai.use(MyPlugin)
// Add require aliases via api.addAlias('~'. './path/to/files')
}
JavaScript example
module.exports = function initTests(api) {
// Add chai plugins via api.chai.use(MyPlugin)
// Add require aliases via api.addAlias('~'. './path/to/files')
}