Package Exports
- @web/test-runner-mocha
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@web/test-runner-mocha) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Test Runner Mocha
Test framework implementation of Mocha for Web Test Runner
See @web/test-runner for a default implementation and CLI for the test runner.
Writing tests
Writing JS tests
Mocha relies on global variables, in any JS test file describe
and it
are available globally and can be used directly:
describe('my test', () => {
it('foo is bar', () => {
if ('foo' !== 'bar') {
throw new Error('foo does not equal bar');
}
});
});
Writing HTML tests
If you're writing tests as HTML, you can import this library to run tests with mocha:
<html>
<body>
<script type="module">
// after importing "@web/test-runner-mocha", describe and it will be available globally
import { runTests } from '@web/test-runner-mocha';
describe('my test', () => {
it('foo is bar', () => {
if ('foo' !== 'bar') {
throw new Error('foo does not equal bar');
}
});
});
// you need to call runTests() when you've loaded all your tests
runTests();
</script>
</body>
</html>
Configuring mocha to use TDD is not yet supported.
Browser compatible code
Web Test Runner is based on bundle-free development, and runs your tests in the browser without any modification. This means you need to make sure your tests, code and test libraries can work in the browser as is. For example they should be standard es modules.
The test runner server can be configured to do some code transformations on the fly, although this may impact performance. Check out the docs to learn more about that.
Libraries
@open-wc/testing is a general purpose library, including assertions via chai, HTML test fixtures, a11y tests and test helpers.
It is an opinionated implementation which brings together multiple libraries. You could also use the individual libraries together:
For stubbing and mocking, we recommend sinon which ships an es module variant out of the box:
import { stub, useFakeTimers } from 'sinon';