How to test a browser project using karma / jasmine

I am completely unfamiliar with the concept of testing, and I need one convincing example of how to do this in my project:

I have a gulp file like this (not all, only important portions)

gulp.task('bundle', function() {
    gulp.src('public/angular-app/main.js')
        .pipe(browserify({
            debug: true
        }))
        .pipe(gulp.dest('public/min-js'));           
});

This is a small part of my main.js:

'use strict';

    angular.module('myApp', [
        'ui.router',
        'ui.bootstrap',
        'ngSanitize',
        'ngFx',
        ...
    ], ['$interpolateProvider',
        function($interpolateProvider) {
            $interpolateProvider.startSymbol('{{');
            $interpolateProvider.endSymbol('}}');
        }
    ])

    .config(require('./config/routes'))

        .config(require('./config/authInterceptor'))
        .run(require('./config/runPhase'))
        .run(require('./config/xeditable'))



        .controller('homeController', require('./controllers/homeController'))
        .controller('modalInstanceCtrl', require('./controllers/modalInstanceCtrl'))
        .controller('modalparticipantCtrl',require('./controllers/modalParticipantCtrl'))
        .controller('generatorController',require('./controllers/generatorController'))
        .controller('navController', require('./controllers/navController'))
        .controller('signInController', require('./controllers/signInController'))
        .controller('pricingController', require('./controllers/pricingController'))
        .controller('howItWorksController',require('./controllers/howItWorks'))
        ...

Now this is my karma configuration file:

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'],


    // list of files / patterns to load in the browser
    files: [
      'public/vendor/jquery/dist/jquery.js',
      'public/vendor/angular/angular.js',
      'public/vendor/angular-mocks/angular-mocks.js',
      'public/angular-app/**/*.js',
      'test/**/*Spec.js'
    ],


    // list of files to exclude
    exclude: [
    ],

When I start karma with the beginning of karma, I get:

Unprepared reference error: require is not defined in root / public / angular -app / main.js

So my question is simple, how can I do tests, for example, on my homeController ...

// update

So, I updated my test file to this:

describe("An Angularjs test suite",function(){
    var target, rootScope;
    beforeEach(inject(function($rootScope) {
      rootScope = $rootScope;

      // Mock everything here
      spyOn(rootScope, "$on")
    }));

    beforeEach(inject(function(homeController) {
      target = homeController;
    }));

    it('should have called rootScope.$on', function(){
      expect(rootScope.$on).toHaveBeenCalled();
    });

});

and my configuration file:

// list of files / patterns to load in the browser
files: [
  'public/vendor/jquery/dist/jquery.js',
  'public/vendor/angular/angular.js',
  'public/vendor/angular-mocks/angular-mocks.js',
  'public/min-js/main.js',
  'test/**/*Spec.js'
],


// list of files to exclude
exclude: [
],


browserify: {
        watch: true,
        debug: true
},

preprocessors: {
    'test/*': ['browserify']
},

Nothing still works, at first it says "unknown provider homeControllerProvider", Now, if I delete their lines:

beforeEach(inject(function(homeController) {
          target = homeController;
        }));

- , $, , ?

+1
2

, Browserify .

Karma:

    {
        browserify: {
            watch: true,
            debug: true
        },
        preprocessors: {
            'test/*': ['browserify']
        }
    }

Karma: http://karma-runner.imtqy.com/0.12/config/configuration-file.html

, Karma : smild.

+3

, , ? ,

0

Source: https://habr.com/ru/post/1649057/


All Articles