5. ● Rodar os testes em diferentes navegadores (inclusive headless,
como PhantomJS) com apenas um comando.
● Rodar os testes rapidamente (em segundos) e frequentemente.
● Rodar os testes em modo watch, onde alterações no código
disparam os testes automaticamente.
● Poder gerar relatórios diversos (resumo dos testes, cobertura do
código testado, etc.)
● Isolar o frontend do backend, evitando interferências da rede, do
banco de dados, etc.
Objetivos
6. ● Roda os testes em diferentes
navegadores com um comando.
● Modo watch.
● Permite gerar relatórios através de
plugins.
● Fácil integração com Gulp, Grunt e
pipelines de CI (como Jenkins).
Karma: o executador
http://karma-runner.github.io
8. Karma e Gulp
var KarmaServer = require('karma').Server,
KARMA_CONF_PATH = __dirname + '/karma.conf.js';
gulp.task('test', function (done) {
var server = new KarmaServer({
configFile: KARMA_CONF_PATH,
browsers: ['Chrome'],
singleRun: false
}, done);
server.start();
});
Sem Gulp
> karma start
> karma start --single-run
Com Gulp
> gulp test
> gulp tdd
> gulp test-ci
9. describe(‘What is being tested’, function () {
describe(‘context’, function () {
beforeEach(function () {
...
});
it(‘should do something’, function () {
...
});
it(‘should do something else’, function () {
...
});
});
});
Jasmine: o framework
http://jasmine.github.io
10. it(‘should sum the numbers’, function () {
var result = sum(1,2,3);
expect(result).toEqual(6);
});
it(‘should return something’, function () {
var result = foo();
expect(result).not.toBeNull();
});
Jasmine: Matchers
.toEqual(value)
.toBeNull()
.toBeDefined()
.toMatch(regex ou string)
.toBeTruthy(value) // cast para boolean
.toBeFalsy(value) // cast para boolean
.toContain(value) // array contém
.toBeGreaterThan(value)
.toBeLessThan(value)
.toThrow()
11. // esta suíte não vai ser executada
xdescribe(‘Suite’, function () {
it(‘should do something’, function () {
...
});
it(‘should do something else’, function () {
...
});
});
Jasmine: skip e focus
describe(‘Suite’, function () {
// apenas este teste será executado
fit(‘should do something’, function () {
...
});
it(‘should do something else’, function () {
...
});
});