Git Product home page Git Product logo

karma-commonjs's People

Contributors

bendrucker avatar jamesshore avatar pkozlowski-opensource avatar vojtajina avatar zzo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

karma-commonjs's Issues

karma-commonjs doesn't seem to be able to follow symlinks

i have symlink from node_modules/@grid to a src folder that has util/index.js in it. works fine for node and browserify, but with karma-commonjs i get:

getting Error: Could not find module '@grid/util' from '/Users/scamden/projects/riq-grid/src/modules/cell-scroll-model/index.js'
at /Users/scamden/projects/riq-grid/node_modules/karma-commonjs/client/commonjs_bridge.js:91

Multiple Load Paths

Not sure how feasible this would be, but it would be really useful to be able to have multiple paths for modules. One use-case is if there is library code in one folder and application code in another. It's impossible to test application code that depends on library code.

Support more of the node.js module loading algorithm

This is a top-level bug to track progress to better support of node's loading algorithm:
http://nodejs.org/api/modules.html#modules_all_together

We won't be able to support core modules but other than this we can capture pretty much the whole algorithm.

The exact task to tackle are:

karma-commonjs does not work along with angularjs mock module

Hi,

I tried to use this module with application that built around AngularJS framework and faced a problem. I write unit tests using Mocha and my specifications look like this:

describe('Myservice', function () {

    beforeEach(module('myModule', ['$provide', function ($provide) {
        ...
    });
    ...
});

After loading karma-commonjs plugin, karma fails to run tests with the following error:

Uncaught TypeError: object is not a function

I understand why it happens: karma-commonjs modifies file contents as follows:

window.__cjs_modules_root__ = "basePath";
window.__cjs_module__ = window.__cjs_module__ || {};
window.__cjs_module__["myServiceSpec.js"] = function(require, module, exports) {
    describe('Myservice', function () {

        beforeEach(module('myModule', ['$provide', function ($provide) {
            ...
        });
        ...
    });
}

This modification breaks reference to angular.module - module variable now points to CommonJS module, not to AngularJS mock module. So it seems to me that there is a name conflicting issue. Can we address this issue or there is only one possible solution not to use module, but to use angular.module instead?

Trouble loading from node_modules

I am currently using browserify to build tests & source before a karma test run and would really like to get away from that model so that we don't have to have the extra time of the browserify task as well as having more useful output from karma when doing things like code coverage reports.

This preprocessor is working great(!) for the source and test files, however I can not get it to load anything from node_modules.

var chai = require('chai');
Error: Could not find module 'chai' from '~/project_path/someSpec.js'

I've done some digging and logging in commonjs_bridge.js and the path and everything is resolving/normalizing correctly... it seems the issue is that none of the node_modules paths are in window.__cjs_module__. That seems to only be populated with files that I give to karma.

Should node_modules be there? Am I doing something wrong or is there potentially a bug in this? Thanks!

Do not preprocess non project files (eg. adapters)

Not sure how to solve this, but it's kinda tempting to use '**/*.js': ['commonjs'] configuration for preprocessors... and that will result in preprocessing jasmine and its adapter (or any other testing framework), assuming the karma-jasmine plugin is installed locally (in the basePath).

If we introduce "labels" for patterns, than we could have something like "dont-preprocess" label...

package.json is not used to resolve module path

If one has a dependency that looks like this:

node_modules/a-library/
  package.json
  main-library-file.js

And in package.json:

...
main: "./main-library-file"
...

Then there is a

Error: Could not find module 'node_modules/a-library.js' from 'x.js'

From a brief look at source code I suppose that karma-commonjs doesn't parse package.jsons. Maybe it's fine, doing it in the browser would be strange, but this means (correct me if I'm wrong) that I have to use built JS to run tests. And that's what I'd like to avoid because build procedure can grow complex and slow and running it on every "Shift+F10" makes people sad.
What if a special "map" is introduced in config file, so we can do

'a-library': 'node-modules/a-library/main-library-file.js'

or

'node-modules/a-library/main-library-file.js:a-library'

somewhere, in the spirit of browserify-shim and similar libraries? This would also make karma-commonjs feasible in cases where the dependencies are not in node_modules folder (e.g. if bower+debowerify is used).

Having hard time testing client code

@vojtajina I'm preparing bigger changes for this plugin as discussed in #3. But being a test-obsessed I feel uncomfortable doing so without tests. Well, I even feel very bad about submitting trivial patches without tests...

So, my question is about general strategy used to test client-side plugins code. Looking at https://github.com/karma-runner/karma-commonjs/blob/master/client/commonjs_bridge.js I can't see a place where I could invoke logic under test... I guess the immediately-executing function - while preventing namespace pollution - makes the testing hard (or at least hard for me...).

Just seeking general guidance here, would like to stay in-line with practices you guys envision for plugins. Looking at other plugins code I can see that those are not always wrapped into an immediately-executing function. Would be grateful for any input here.

Shouldn't run all modules by default

In commonjs_bridge.wrapper, karma-commonjs "requires" all files. This has the effect of running all files, which can cause odd behavior. The correct behavior is only to run files when a require statement is encountered.

I'm not sure what we should do here, but I think we need some sort of "entry point" configuration that says which files should be run.

Requiring node_modules works and I don't know why

I know that we're supposed to reference any and all node_modules in the karma.conf.js if we want to require them, but I accidentally stumbled upon a scenario in which that isn't required.

In my case, I'm using browserify as a build process, but I don't have this luxury in karma, because I need to generate a coverage report and browserify messes all that up (unless I'm doing something wrong).

Anyway, long story short, I figured out that adding the following to my package.json allows me to get accurate coverage reports w/o karma-commonjs throwing errors that it can't find a node module:

{
  "devDependencies": {
    "karma-commonjs": "git://github.com/karma-runner/karma-commonjs.git#v0.0.12"
  }
}

If I do a normal npm install --save-dev karma-commonjs I get errors that I can't find the react node_module, but if I manually specify the github repo and tag name, all is well.

The thing that scares me is that I don't know WHY this works! Can you think of anything?

Here's my karma.conf.js:

module.exports = function(config) {
    config.set({

        basePath: '',

        frameworks: ['jasmine', 'commonjs'],

        files: [
            'lib/src/**/*.js',
        ],

        exclude: [
        ],

        preprocessors: {
            'lib/src/**/*.js': ['commonjs'],
            'lib/src/**/*[^(\.spec)].js': ['coverage']
        },

        reporters: ['progress', 'coverage'],

        coverageReporter: {
            type: 'lcov'
        },

        port: 9876,

        colors: true,

        logLevel: config.LOG_INFO,

        autoWatch: true,

        browsers: ['PhantomJS'],

        singleRun: false
    });
};

Add plugins to readme?

Hey guys, just noting an issue I encountered when trying to get karma-commonjs set up.

I wasn't able to get karma-commonjs to work until I added 'karma-commonjs' to the plugins array in my karma configuration file:

plugins: [
      'karma-jasmine',
      'karma-chrome-launcher',
      'karma-firefox-launcher',
      'karma-commonjs'
    ]

Didn't see it anywhere in the readme though, only was able to figure this out from looking at https://github.com/karma-runner/karma/blob/master/test/client/karma.conf.js .

Could you guys add that to the README.md? For those of us new to karma and karma-commonjs.

Otherwise, works great, thanks!

NPM modules not having a main attribute

Hi there!

I had a very hard time trying to make my little project work with karma-commonjs not because of this plugin directly but because some of the dependencies I'm using.
Some of them do not declare a main attribute, which is the one karma-commonjs uses to load the right files from the package.json for every dependency.

These are the dependencies and how they declare its main file in their package.json file:

The way I solved this issue was to create a post-install script in my project which adds the main atribute to the package.json file for each one of these dependencies, but I'm not very proud fo this workaround.

I would like to know what would be the right solution to be able to use these kind of dependencies.

Thanks a lot.

Could not find any modules. Did you remember to set 'preprocessors' in your Karma config?

Followed the configuration in the read me but I am getting the following error:

Error: Could not find any modules. Did you remember to set 'preprocessors' in your Karma config?
  at /Development/codecentric/angular2-hamis-seed/node_modules/karma-commonjs/client/commonjs_bridge.js:5
basePath: '',

        frameworks: ['jasmine', 'commonjs'],
        browsers: ['PhantomJS'],
        reporters: ['progress', 'coverage'],

        preprocessors: {
            'app/**/!(*.spec)+(.js)': ['coverage'],
            'app/**/*.js': ['sourcemap'],
            'app/**/*.js': ['commonjs']
        },

        commonjsPreprocessor: {
            modulesRoot: 'node_modules'
        },

I also tried setting the commonjs Preprocessor option but did not help.

Part of the config:

Any idea's?

Unable to load external module that has been transpiled from Typescript and that is required from my app

I have an external npm common module, shared from all my apps, that is wirtten in Typescript. Supposing that this module is called common_module, when I build it, the transpiler for each typescript import generates a require('file_name'). When karma-commonjs tries to resolve these require, it searches for something like .../node_modules/file_name, but the module is located at .../node_modules/common_module.

Karma-commonjs does not work in LernaJS structure

I work on a project that uses lernajs to structure its components inside a packages folder. One "package" requires one or more sibling "package".

  • rootFolder
    • packages
      • package-1
      • package-2

If package-1 requires package-2, Lerna creates a symbolic link to package-2 inside the package-1 node_modules folder.

By running tests for package-1 I got this error:

Uncaught Error: Could not find module '../../package-2/src/Package2' from '/**pathToPackage1**/src/Package1.js' at node_modules/karma-commonjs/client/commonjs_bridge.js:85

I'm not sure, but It might be related to this issue #34

Simple example

I know that you say "For an example project, check out Karma's client tests."

I was trying to create an extremely simple example with a minimum configuration. But I am getting an error

ReferenceError: Can't find variable: global

in the Jasmine2 library. I have put more detail at StackOverflow, but I would be remiss if I didn't ask here... Any help much appreciated, and would be glad to add to examples.

Modules not found during testing

Hello,

I just switched a project over to using karma-commonjs, but i'm seeing an issue where a required module isn't found. Perhaps you may know how to fix it?

https://github.com/airbnb/rendr/compare/karma-commonjs

The error I'm getting is:

Uncaught Error: Could not find module '/Users/spike/code/rendr/examples/00_simple/app/collections/repos.js' from '/Users/spike/code/rendr/examples/00_simple/test/app/collections/repos.test.js'
        at /Users/spike/code/rendr/examples/00_simple/node_modules/karma-commonjs/client/commonjs_bridge.js:18

Which is strange because is the file is certainly there:

$ ls /Users/spike/code/rendr/examples/00_simple/app/collections/repos.js
-rw-r--r--  1 spike  staff   291B Oct 25 13:23 /Users/spike/code/rendr/examples/00_simple/app/collections/repos.js

You can see the test file, repos.test.js, here:

https://github.com/airbnb/rendr/blob/42e160e212ceee333ae93b2742ef1cdfb61ac485/examples/00_simple/test/app/collections/repos.test.js

Any thoughts?

Add support for coffeescript using transformPath or similar

When using the commonjs preprocessor with coffeescript preprocessor files modules get wrong name since plugin.js uses file.originalPath to set module name in the cjs_module object.

This makes it hard to support coffeescript files.

A solution is to add something similar to the transformPath in the coffee-preprocessor to set the module filepath.

I've done this and it works.

Should I make a pull request or is this not the "correct" way to solve the problem?

Update docs to reflect use cases / shortcomings

The wild world of karma + browserify is finally being tamed: nikku/karma-browserify#37. The short version of the upcoming changes is that karma-bro is becoming karma-browserify (replacing the existing package there) and all other karma/browserify modules will be deprecated. karma-bro is quite stable and supports the browserify API extremely well.

I wanted to open a discussion about deprecating this module, especially after noticing last week that many of the angular-hint* modules are using karma-bro. This is a bit of a different situation than the other karma-bro* modules because the use cases are overlapping but not identical. I think it's safe to say that enough people are making use of Browserify features like module resolution, transforms, and plugins to severely diminish the usefulness of a simple CommonJS implementation. Offering the sort of behavior people expect would require a massive duplication of effort (e.g. #29 (comment)).

cc @nikku

npm modules are loaded from wrong path

As @abbakym mentioned in #16:

karma-commonjs tries to load dependencies by their immediate names, e.g. root/lodash.js and not root/lodash/lodash.js.

This makes npm modules pretty much impossible to use with karma-commonjs.

Jasmine Uncaught ReferenceError: jasmineRequire is not defined

Hello,

I am trying to make karma-commonjs work with jasmine and I get the following error on a very basic example:

Uncaught ReferenceError: jasmineRequire is not defined
  at node_modules/karma-jasmine/lib/boot.js:14:17
  
  ReferenceError: jasmineRequire is not defined
      at node_modules/karma-jasmine/lib/boot.js:14:17
      at window.__cjs_module__./home/alexander/projects/karma-test/node_modules/karma-jasmine/lib/boot.js (node_modules/karma-jasmine/lib/boot.js:43:2)
      at runModule (node_modules/karma-commonjs/client/commonjs_bridge.js:40:5)
      at require (node_modules/karma-commonjs/client/commonjs_bridge.js:79:14)
      at node_modules/karma-commonjs/client/commonjs_bridge.js:141:13
      at node_modules/karma-commonjs/client/commonjs_bridge.js:145:3

Here is my karma.config.js

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://www.npmjs.com/search?q=keywords:karma-adapter
    frameworks: ['commonjs', 'jasmine'],


    // list of files / patterns to load in the browser
    files: [
      {pattern: 'lib/**/*.js', included: false},
      {pattern: 'src/**/*.js', included: false},
      {pattern: 'test/**/*spec.js', included: false}
    ],

    // preprocess matching files before serving them to the browser
    // available preprocessors: https://www.npmjs.com/search?q=keywords:karma-preprocessor
    preprocessors: {
      '**/*.js': ['commonjs']
    },

    browsers: ['ChromeHeadless'],
  })
}

Any help is greatly appreciated.
Thank you
Regards Alex

karma-commonjs cannot load module lodash

Hi there,

I've been trying to make your plugin work with my project, but it seems it doesn't want to recognize lodash as a module.

(Here in utils.js) -- I use typescript but everything is compiled beforehand.

exports._ = require('lodash');
exports._ = require('/lodash/lodash');
exports._ = require('/lodash/lodash.js');
exports._ = require('lodash.js');

None of these work!

I always get

Uncaught Error: Could not find module '/Users/ludydoo/WebstormProjects/rajs/node_modules/lodash.js' from '/Users/ludydoo/WebstormProjects/rajs/lib/utils.js'
at http://localhost:9876/base/node_modules/karma-commonjs/client/commonjs_bridge.js?1389254165000:6

Cannot load JSON files

cannot require a module which is a JSON file.
"SyntaxError: missing ; before statement"

Fails on modules with a shebang

If you require a module that starts with a shebang, such as #! /usr/bin/node, the code will fail with a syntax error. This is because karma-commonjs inserts its wrapper in front of the shebang, which leads to invalid code.

We should look for the presence of a shebang line and add our wrapper below it when it's present.

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.