Giter VIP home page Giter VIP logo

Comments (3)

bendrucker avatar bendrucker commented on June 12, 2024

Just declare your shims in your package. Browserify will only bundle it if you actually require it. You'll also need the depends option:

"browserify": {
  "transform": [
    "browserify-shim"
  ]
},
"browser": {
  "angular": "./bower_components/angular/angular.js",
  "angular-mocks": "./bower_components/angular-mocks/angular-mocks.js"
},
"browserify-shim": {
  "angular": {
    "exports": "angular"
  },
  "angular-mocks": {
    "depends": "angular"
  }
}

For what it's worth though, I've moved entirely to the global config for several reasons. The main one is that I write a lot of reusable angular modules with CommonJS and then use them in Angular projects. If you use the path-based shim config, you have to rely on browserify to dedupe your modules and all modules that depend on Angular must use an identical version. Also you need to have bower installed globally and run bower i as an install script for your reusable modules. It ends up being horribly unmaintainable and buggy.

As of #1 you can just stick angular and angular-mocks in the file array before your test glob and your bundle will be added after angular-mocks.

from karma-browserify.

thaiat avatar thaiat commented on June 12, 2024

Hi, thks for your answer.
I ended up leaving the all configuration for browserify in the package.config file.
Then in karma my files section looks like this

...
files: [
    './scripts/**/*.js'
],
...

I like to have my tests files along with the source code instead of a separate tests folder.

Finally my test start like this:

require('angular-mocks');
require('./app');
describe('myService', function () {
    var service = null;

    beforeEach(angular.mock.module('myApp'));

    beforeEach(inject(function ($injector) {
        service = $injector.get('myApp.myService');

    }));

    it('should exist', function () {
        expect(service).toBeDefined();
    });

    it('add should succeed', function () {
        expect(service.add(1, 3)).toBe(4);
    });

    it('sub should succeed', function () {
        expect(service.sub(1, 3)).toBe(-2);
    })
});

because angular-mocks has a dependency on it will also load angular.
This works like a charm.

The only thing (but it's not really an issue) is that i have to do angular.mock.module(...) to load the module in the beforeEach instead of classical module(...)

from karma-browserify.

bendrucker avatar bendrucker commented on June 12, 2024

That's a limitation born from the fact that Angular choose to export multiple things from angular-mocks. That concept isn't compatible with CommonJS where there's only one module.exports. Browserify could do something clever and attach the exports to a base object but that's not really its place.

from karma-browserify.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.