Skip to content

Instantly share code, notes, and snippets.

@guaracyalima
Created July 9, 2019 19:38
Show Gist options
  • Save guaracyalima/ad64aa2d73cc3a775a0fd47459588889 to your computer and use it in GitHub Desktop.
Save guaracyalima/ad64aa2d73cc3a775a0fd47459588889 to your computer and use it in GitHub Desktop.
Setup de testes no front end - Cast Group - INFRABB
describe("Bloco COntroller", function(){
//init das injeções do teste ( aqui vc injeta as dependencias, define o controller, etc)
beforeEach(function () {
angular.mock.module('blocoListagemControllers');
module(function ($provide) {
//esses trecos do gaw eu so fiz um mock porco
$provide.factory('GAWHubClient', function () {
return {
conectar: function () {
return true;
},
secaoSelecionada: function () {
return true;
},
getAmbiente: function () {
return true;
},
getDadosApp: function(){
return true;
}
};
});
});
});
//injeta as definições do controller e da um alias pra ele
beforeEach(inject(function ($rootScope, $controller, GAWHubClient, _$location_) {
scope = $rootScope.$new();
$location = _$location_;
GAWHubClient = GAWHubClient;
vm = $controller('BlocoListagemController', {
$scope: scope,
});
}));
//esse é seu teste definitivo
//neste caso so verifica se o controller existe
it('.test', function(){
expect(vm).toBeDefined();
});
});
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// Aqui você deve adicionar os arquivos de teste e os que você injeta no seu controller, factory, service, classe, etc
files: [
'node_modules/angular/angular.js',
'node_modules/angular-mocks/angular-mocks.js',
'src/app/spas/servicos-globais/bloco-servicos-globais.js',
'src/app/spas/bloco/listagem/controllers.js',
'src/app/spas/bloco/servicos-especificos.js',
'src/app/spas/bloco/listagem/app-spec.js'
],
// list of files / patterns to exclude
exclude: [
],
// Lê a cobertura de teste
preprocessors: {
'app/**/*.js' : ['coverage']
},
// TIpos de relatorios de teste
reporters: ['progress', 'spec', 'coverage'],
// web server port
port: 9876,
hostname: 'localhost',
listenAddress: 'localhost',
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,
//Onde vai ser gerado os relatorios de cobertura de teste
coverageReporter: {
type : 'html',
dir : 'coverage/'
}
})
}
{
"name": "afd-estatico",
"version": "1.0.106-beta11",
"description": "Projeto AFD com conteúdo estático",
"main": "karma.conf.js",
"dependencies": {
"angular": "^1.6.0",
"angular-mocks": "^1.7.8",
"jasmine-core": "^2.99.1",
"karma": "^1.7.1",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage": "^1.1.2",
"karma-jasmine": "^1.1.2",
"karma-spec-reporter": "0.0.32",
"sweetalert2": "^8.3.0"
},
"devDependencies": {
"connect-livereload": "^0.6.1",
"grunt": "^1.0.4",
"grunt-contrib-jshint": "^1.1.0"
},
"scripts": {
"test": "karma start karma.conf.js"
},
"keywords": [],
"author": "",
"license": "MIT",
"repository": {
"type": "git",
}
}
@guaracyalima
Copy link
Author

Você ainda deve ter instalado globalmente

  • karma cli
  • jasmine

Pra rodar:
karma start

Pra mostrar as coberturas de teste:
karma start --reporters coverage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment