Last active
August 29, 2015 14:25
-
-
Save iurii-kondratiuk-zz/c6d3184b6d7e7a62af08 to your computer and use it in GitHub Desktop.
Testing Angular application with CoffeeScript, Jasmine and Karma
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function (config) { | |
var preprocessors = { | |
'coffee/**/*.coffee': ['coffee'], | |
'spec/**/*.coffee': ['coffee'], | |
'www/views/**/*.html': ['ng-html2js'] | |
}; | |
// when we run coverage report we can't use `coffee` preprocessor, | |
// so we change preprocessor depending on reporters | |
if(process.argv[6] === 'progress,coverage') { | |
preprocessors['coffee/**/*.coffee'] = ['coverage'] | |
} | |
return config.set({ | |
basePath: './', | |
files: [ | |
'lib/angular/angular.min.js', | |
'lib/angular-cache/dist/angular-cache.min.js', | |
'lib/angular-i18n/angular-locale_en-gb.js', | |
'lib/angular-animate/angular-animate.min.js', | |
'lib/angular-translate/angular-translate.min.js', | |
'lib/angular-ui-router/release/angular-ui-router.min.js', | |
'lib/angular-bootstrap/ui-bootstrap-tpls.min.js', | |
'lib/angular-mocks/angular-mocks.js', | |
'coffee/**/*.coffee', | |
'spec/**/*.coffee', | |
'www/views/**/*.html', | |
'www/js/translations.min.js' | |
], | |
frameworks: ['jasmine'], | |
plugins: [ | |
'karma-coffee-preprocessor', | |
'karma-jasmine', | |
'karma-coverage', | |
'karma-junit-reporter', | |
'karma-chrome-launcher', | |
'karma-ng-html2js-preprocessor' | |
], | |
preprocessors: preprocessors, | |
ngHtml2JsPreprocessor: { | |
stripPrefix: 'client/', | |
moduleName: 'templates' | |
}, | |
coverageReporter: { | |
type: 'html', | |
dir: 'coverage/', | |
subdir: function (browser) { | |
return browser.toLowerCase().split(/[ /-]/)[0]; | |
} | |
}, | |
browsers: ['Chrome'], | |
autoWatch: false, | |
singleRun: true | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"company": "Best company", | |
"name": "siervice-unit-test", | |
"version": "1.0.0", | |
"scripts": { | |
"test": "node ./node_modules/karma/bin/karma start ./karma.conf.js --single-run", | |
"test:dev": "node ./node_modules/karma/bin/karma start ./karma.conf.js --no-single-run --auto-watch", | |
"test:cov": "rm -rf ./coverage mkdir -p ./coverage node ./node_modules/karma/bin/karma start ./karma.conf.js --single-run --reporters progress,coverage" | |
}, | |
"engines": { | |
"node": ">= 0.10.0" | |
}, | |
"devDependencies": { | |
"coffee-script": "^1.9.3", | |
"jasmine-core": "^2.1.3", | |
"karma": "^0.12.37", | |
"karma-chrome-launcher": "^0.2.0", | |
"karma-coffee-preprocessor": "^0.2.1", | |
"karma-coverage": "^0.2.7", | |
"karma-jasmine": "^0.3.2", | |
"karma-junit-reporter": "^0.2.2", | |
"karma-ng-html2js-preprocessor": "^0.1.2" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(-> | |
$inject = ["$http", "apiConfig"] | |
accountService = ($http, apiConfig) -> | |
apiURL = apiConfig.getApiURL() | |
return { | |
get: (id = null) -> | |
$http.get apiURL + "users" + (if id then "/#{id}" else '') | |
create: (user) -> | |
$http.post apiURL + "users", user | |
modify: (user) -> | |
$http.put apiURL + "users/" + user.id, user | |
doAction: (action, {id}) -> | |
$http.post apiURL + "users/#{id}/actions/#{action}", {} | |
delete: (action, id) -> | |
$http.delete apiURL + "users/#{id}" | |
} | |
accountService.$inject = $inject | |
angular | |
.module('dbce') | |
.factory('userService', userService) | |
)() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe 'accountService', -> | |
service = null | |
apiConfig = null | |
httpBackend = null | |
user = id: 6, name: 'John Show' | |
URL = "users" | |
beforeEach module "tests" | |
beforeEach module ($provide) -> | |
apiConfig = jasmine.createSpyObj 'apiConfig', ['getApiURL'] | |
apiConfig.getApiURL.and.returnValue '' | |
$provide.value 'apiConfig', apiConfig | |
return | |
beforeEach inject ($httpBackend, userService) -> | |
httpBackend = $httpBackend | |
service = userService | |
it 'should call apiConfig::getApiURL', -> | |
expect(apiConfig.getApiURL).toHaveBeenCalled() | |
describe '.get', -> | |
it 'should GET all users', -> | |
httpBackend.expectGET(URL).respond {} | |
service.get() | |
httpBackend.flush() | |
it 'should GET user by id', -> | |
httpBackend.expectGET(URL + "/#{user.id}").respond {} | |
service.get user.id | |
httpBackend.flush() | |
describe '.create', -> | |
it 'should POST user', -> | |
httpBackend.expectPOST(URL, user).respond {} | |
service.create user | |
httpBackend.flush() | |
describe '.modify', -> | |
it 'should PUT user', -> | |
httpBackend.expectPUT(URL + "/#{user.id}", user).respond {} | |
service.modify user | |
httpBackend.flush() | |
describe '.doAction', -> | |
it 'should POST on the user', -> | |
action = 'block' | |
httpBackend.expectPOST(URL + "/#{user.id}/actions/#{action}", {}).respond {} | |
service.doAction action, user | |
httpBackend.flush() | |
describe '.delete', -> | |
it 'should DELETE user', -> | |
httpBackend.expectDELETE(URL + "/#{user.id}").respond {} | |
service.delete user | |
httpBackend.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment