Modern, simpler Mocha clone, no globals, lint friendly
Supports node.js >= maintained LTS.
npm install kocha --save-dev
Require describe
, it
etc from kocha, and write tests as in mocha
:
test.js
:
const { describe, it } = require('kocha')
const assert = require('assert')
const add = (a, b) => a + b
describe('add', () => {
it('adds the given numbers', () => {
const sum = add(12, 13)
assert(sum === 25)
})
})
Then run it by kocha
command.
./node_modules/.bin/kocha test.js
This outputs the report like the below:
$ ./node_modules/.bin/kocha test.js
add
โ adds the given numbers
1 passing (4ms)
Use in karma
npm install kocha karma-kocha --save-dev
Init karma.conf.js by invoking karma init
command and following the instructions.
Then add kocha
configs to karma.conf.js
like the below:
module.exports = config => config.set({
frameworks: ['kocha', 'browserify'],
files: [
'test/**/*.js'
],
preprocessors: {
'test/**/*.js': 'browserify'
}
browserify: {
debug: true,
...
},
...
})
And write the tests like the below:
test/add.js
:
const { describe, it } = require('kocha')
const assert = require('assert')
const add = (a, b) => a + b
describe('add', () => {
it('adds the given numbers', () => {
const sum = add(12, 13)
assert(sum === 25)
})
})
Then hit the command karma start
:
karma start
It outputs the test result like the below:
$ karma start
07 06 2017 13:41:07.792:INFO [framework.browserify]: bundle built
07 06 2017 13:41:07.809:INFO [karma]: Karma v1.7.0 server started at http:https://0.0.0.0:9876/
07 06 2017 13:41:07.809:INFO [launcher]: Launching browser Chrome with unlimited concurrency
07 06 2017 13:41:07.819:INFO [launcher]: Starting browser Chrome
07 06 2017 13:41:09.349:INFO [Chrome 58.0.3029 (Mac OS X 10.12.5)]: Connected on socket Qs7TxwAJjfsy7LElAAAA with id 79520559
Chrome 58.0.3029 (Mac OS X 10.12.5): Executed 1 of 1 SUCCESS (0.007 secs / 0.001 secs)
That's it.
Note: You need a CommonJS bundler (typically browserify
or webpack
) to use kocha in karma.
Note: If you use babel together with the bundler, then import describe
, it
etc like the below:
import { describe, it } from 'kocha'
describe('foo', () => { ... })
const {
describe,
it,
before,
beforeEach,
after,
afterEach,
timeout,
retries
} = require('kocha')
Adds the test suite by the title
and callback
. In callback
function you can add child test suites and/or test cases.
Note: The alias context(title, callback)
is also available.
Adds the skipped test suite by the title
and callback
. The test suites and cases under this suite are all skipped as well.
Note: The alias xdescribe(title, callback)
and xcontext(title, callback)
are also available.
Adds the skipped test case by the title
and callback
. callback
implements your test case.
Note: The alias specify(title, callback)
is also available.
Adds the skipped test case by the title
and callback
.
Note: The alias xit(title, callback)
and xspecify(title, callback)
is also available.
Adds the before hook to the current suite.
Adds the beforeEach hook to the current suite.
Adds the after hook to the current suite.
Adds the afterEach hook to the current suite.
Sets the timeout duration to the test cases or the test suites.
Sets the retry count of the test cases or the test suites.
Usage: kocha [options] <file[, ...files]>
Options:
-h, --help Shows the help message
-v, --version Shows the version number
-r, --require <name> Requires the given module e.g. --require @babel/register
-c, --config <path> Specify the config file path e.g. --config kocha.e2e.config.js
-t, --timeout <ms> Sets the test-case timeout in milliseconds. Default is 2000.
Examples:
kocha test/ Runs all the tests under test/.
kocha "src{/,**/}__tests__/**/*.js"
Runs tests under the directory pattern src/**/__tests__/.
kocha --require @babel/register --require @babel/polyfill test/
Runs tests under test/ using @babel/register and @babel/polyfill.
kocha --require coffee-script/register "test/**/*.coffee"
Runs coffeescript tests under test/.
kocha command automatically looks up kocha.config.js
from the current directory and execute it. You can configure the test runner there.
kocha.config.js:
const { timeout, retries } = require('kocha')
timeout(5000) // Sets the default timeout to 5000ms
retries(2) // Sets the default retry count to 2
// Other preparations
// For example, babel settings
require('@babel/register')
require('@babel/polyfill')
// For example, power-assert settings
require('espower-loader')({
pattern: 'test/**/*.js'
})
// For example, coffee-script settings
require('coffee-script/register')
- Support BDD mode, Spec reporter of mocha in CommonJS environment.
- This includes Karma environment with CommonJS bundler (browserify, webpack).
- Keeping the basic usability of Mocha, modernize the rusty old aspects of Mocha.
- Kocha isn't a drop-in replacement of mocha.
- Kocha doesn't support interfaces other than
BDD
, likeTDD
,QUnit
,Exports
etc - Kocha doesn't support standalone mode in browser. Use bundlers and karma for browser unit testing.
- Kocha's BDD interface is not identical to Mocha's BDD interface. See the below for details.
- Kocha doesn't have
this.timeout(ms)
API. Usekocha.timeout(ms)
API instead. - Kocha doesn't have
this.retries(n)
API. Usekocha.retries(n)
API instead.
- Kocha doesn't support
--opts
option andmocha.opts
(orkocha.opts
). Write options directly instead. - Kocha doesn't support
-w, --watch
option. Usechokidar-cli
and run-scripts instead. - Kocha doesn't support
--compilers
option. Use--require
instead.
- Install
kocha
- Use
kocha
command instead ofmocha
or_mocha
command. - Add
const { describe, it, ... } = require('kocha')
statement on the top of each mocha test script. - Rewrite
this.timeout(N)
tokocha.timeout(N)
if you have any. - Rewrite
this.retries(N)
tokocha.retries(N)
if you have any. - Then your tests should work with kocha.
If the above doesn't work, please file an issue.
For karma
- Install
karma-kocha
andkocha
- Set
framework: ['kocha', ...]
instead offramework: ['mocha', ...]
. - Introduce a bundler (
browserify
orwebpack
) if don't have any. - Add
const { describe, it, ... } = require('kocha')
statement on the top of each mocha test script. - Rewrite
this.timeout(N)
tokocha.timeout(N)
if you have any. - Rewrite
this.retries(N)
tokocha.retries(N)
if you have any. - Then your tests should work with kocha.
If the above doesn't work, please file an issue.
Examples of migration: 1
Kocha (็ด ่ถ, pronounced like ko-cha, not like ko-ka) means black tea in Japanese.
- 2017-12-14 v1.9.0 Change handling of --require option.
- 2017-06-20 v1.8.0 Add node v4 support.
- 2017-06-18 v1.7.0 Add kocha.config.js feature. Add --config option.
- 2017-06-17 v1.6.0 Add --timeout option. Input path handling is now similar to mocha.
- 2017-06-12 v1.5.6 Fail when done is called multiple times.
- 2017-06-12 v1.5.5 Add uncaught error handling.
- 2017-06-11 v1.5.4 Improve hook error handling.
- 2017-06-11 v1.5.3 Improve error formatting of karma-kocha.
- 2017-06-10 v1.5.1 Support IE11.
- 2017-06-09 v1.4.0 Support hook events.
- 2017-06-07 v1.3.0 Add babel's import support.
MIT