π§ͺ AngularJS Testing & Project Builds β Unit Testing, E2E & Build Automation (2025 Guide)
π§² Introduction β Why Testing & Builds Matter in AngularJS?
Even though AngularJS (1.x) is a legacy framework, it offers robust testing capabilities and build automation through tools like:
- Jasmine and Karma for unit testing
- Protractor for end-to-end (E2E) testing
- Grunt, Gulp, and Webpack for builds and bundling
Well-tested AngularJS apps are easier to maintain, debug, and migrate to newer frameworks like Angular 2+.
π― In this guide, youβll learn:
- How to write unit and E2E tests for AngularJS
- Which tools to use for testing
- How to configure test runners and automate builds
- How to set up a reliable CI-ready AngularJS project structure
β Unit Testing AngularJS with Jasmine & Karma
π§ What is Jasmine?
Jasmine is a BDD testing framework for JavaScript. It allows you to write readable unit tests using describe
, it
, expect
, etc.
π§ What is Karma?
Karma is a test runner that loads AngularJS modules, injects dependencies, and executes Jasmine tests in real browsers.
β Step-by-Step: Unit Testing a Controller
// Controller
app.controller('CalcCtrl', function($scope) {
$scope.sum = function(a, b) {
return a + b;
};
});
// Test: calcCtrl.spec.js
describe('CalcCtrl', function() {
beforeEach(module('myApp'));
var $controller;
beforeEach(inject(function(_$controller_) {
$controller = _$controller_;
}));
it('should add two numbers correctly', function() {
var $scope = {};
var controller = $controller('CalcCtrl', { $scope: $scope });
expect($scope.sum(2, 3)).toBe(5);
});
});
β This test ensures your controller logic works independently of the UI.
π οΈ Configure Karma
karma.conf.js:
module.exports = function(config) {
config.set({
frameworks: ['jasmine'],
files: [
'app/**/*.js',
'test/**/*.spec.js'
],
browsers: ['ChromeHeadless'],
singleRun: true
});
};
β Run tests with:
npx karma start karma.conf.js
π€ E2E Testing with Protractor
Protractor is built on WebDriverJS to simulate real browser interactions.
β Protractor Test Example
describe('Login Form', function() {
it('should show error for invalid credentials', function() {
browser.get('http://localhost:8000/login');
element(by.model('username')).sendKeys('wrong');
element(by.model('password')).sendKeys('incorrect');
element(by.buttonText('Login')).click();
var error = element(by.binding('errorMessage')).getText();
expect(error).toContain('Invalid');
});
});
β Simulates real user behavior for complete app testing.
βοΈ Protractor Configuration
protractor.conf.js:
exports.config = {
framework: 'jasmine',
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['test/e2e/*.spec.js'],
capabilities: {
browserName: 'chrome'
}
};
βοΈ Automating AngularJS Builds
β Use Gulp for Build Tasks
gulpfile.js:
const gulp = require('gulp');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');
gulp.task('scripts', function() {
return gulp.src('app/**/*.js')
.pipe(concat('app.min.js'))
.pipe(uglify())
.pipe(gulp.dest('dist'));
});
β Run with:
gulp scripts
β Use Webpack for Modern Build Pipelines
webpack.config.js:
module.exports = {
entry: './app/main.js',
output: {
filename: 'bundle.js',
path: __dirname + '/dist'
},
mode: 'production'
};
β Webpack helps bundle, minify, and optimize legacy AngularJS codebases.
π§ CI/CD Integration for AngularJS Projects
Tool | Usage |
---|---|
GitHub Actions | Run Karma & Protractor on push |
Jenkins | Build and test AngularJS apps automatically |
Travis CI | Integrate with GitHub for pull request checks |
CircleCI | Run unit and E2E tests in Docker containers |
β
Use npm test
and npm run build
scripts as CI hooks.
π Summary β Recap & Next Steps
Testing and automation are still very much possible (and important) in AngularJS. With Jasmine + Karma for unit tests and Protractor for E2E, you can confidently build stable and scalable legacy appsβwhile preparing for eventual migration.
π Key Takeaways:
- Use Jasmine + Karma for unit testing AngularJS components
- Use Protractor for E2E testing with real browser scenarios
- Automate builds with Gulp or Webpack
- Integrate with modern CI/CD pipelines like GitHub Actions, Jenkins, etc.
βοΈ Real-world Relevance:
Critical for ensuring long-term stability and scalability in enterprise AngularJS apps used in banking, healthcare, e-commerce, and government platforms.
β FAQ β AngularJS Testing & Builds
β Does AngularJS support unit testing?
β
Yes. Use Jasmine + Karma to write unit tests for controllers, services, filters, and directives.
β Can I automate builds in AngularJS?
β
Absolutely. Use Gulp, Grunt, or Webpack to concatenate, minify, and bundle AngularJS code.
β Is Protractor still valid for AngularJS E2E testing?
β
Yes, although deprecated for Angular 2+, it’s still functional and widely used in AngularJS apps.
β Can I use GitHub Actions with AngularJS?
β
Yes. Create workflows to run karma start
or gulp build
on every code push or pull request.
Share Now :