Karma - Test Runner for Javascript

Maksim Ryzhikov

Karma - Test Runner for Javascript

Качество & Продуктивность

TDD - Test Driven Development

Ингредиенты успешного TDD

Тестовое окружение

Karma main goal

The main goal for Karma is to bring a productive testing environment to developers. The environment being one where they don`t have to set up loads of configurations, but rather a place where developers can just write the code and get instant feedback from their tests. Because getting quick feedback is what makes you productive and creative.

Установка и настройка

npm install -g karma-cli
npm install karma # Install Karma
­
karma init # Create configuration file
­
npm install karma-mocha # Testing framework
npm install karma-expect # Assertion lib
npm install karma-phantomjs-launcher # Browser

Запуск (Client-Server)

karma start
­
INFO [karma]: Karma v0.12.0 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.7 (Linux)]: Connected on socket M64_IXz with id 98199717
PhantomJS 1.9.7 (Linux): Executed 1 of 1 SUCCESS (0.011 secs / 0.003 secs)

Базовая настройка karma.conf.js

module.exports = function(config) {
    config.set({
        frameworks: ['mocha', 'expect'],
        files: ['test/**/*.js'],
        autoWatch: false,
        singleRun: true,
        browsers: ['PhantomJS']
    });
}

Watch & Fast tests

    karma start --single-run=false --auto-watch=true
    ...
    PhantomJS 1.9.7 (Linux): Executed 1 of 1 SUCCESS (0.038 secs / 0.005 secs)
    INFO [watcher]: Changed file "/tmp/karma/test/foo_spec.js".
    PhantomJS 1.9.7 (Linux): Executed 2 of 2 SUCCESS (0.037 secs / 0.006 secs)
    ...

➜ Изменил файл ➜ Тесты ➜ Изменил файл ➜ Тесты ... ➜

Выходя за рамки стандартного

module.exports = function(config) {
    config.set({
        files: ['src/**/*.js', 'test/*.coffee', 'lib/*.dart'],
        preprocessors: {
            '**/*.js': ['borschik'],
            '**/*.coffee': ['coffee'],
            '**/*.dart': ['dart']
        }
    });
}

Karma уже умеет тестировать:

... и это только часть plugin`ов

Any Browsers, Devices, OS

Просто зайдите на host где запущен karma-server

Karma v0.12.0 server started at http://localhost:9876/
...
PhantomJS 1.9.7 (Linux): Executed 2 of 2 SUCCESS (0.03 secs / 0.009 secs)
Chromium 31.0.1650 (Ubuntu): Executed 2 of 2 SUCCESS (0.27 secs / 0.009 secs)
TOTAL: 4 SUCCESS

Debugging!

WebStorm 7

WebStorm 7 with Debug

Debug in Browser

http://localhost:9876/debug.html

Сравнения

Спасибо за внимание!!!

Karma - Test Runner for Javascript

Максим Рыжиков, Yandex