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">
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');
}
});
});
runTests();
</script>
</body>
</html>
Configuring mocha options
You can configure mocha options using the testFramework.config
option:
module.exports = {
testFramework: {
config: {
ui: 'bdd',
timeout: '2000',
},
},
};
The config entry accepts any of the official mocha browser options.
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';