karma-runner / karma-commonjs Goto Github PK
View Code? Open in Web Editor NEWA Karma plugin. Test CommonJS modules.
License: MIT License
A Karma plugin. Test CommonJS modules.
License: MIT License
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
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.
Similar to https://github.com/karma-runner/karma-closure parse source files (probably using https://github.com/shtylman/node-required to build a dependency tree and optimize file loading (load only required files):
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:
package.json
- fixed via 55c752aindex.js
- fixed via 4f5c043Hi,
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?
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!
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...
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.json
s. 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).
@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.
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.
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
});
};
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!
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.
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?
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
.
I work on a project that uses lernajs
to structure its components inside a packages
folder. One "package" requires one or more sibling "package".
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
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.
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:
Any thoughts?
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?
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
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.
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
When using an external dependency it sometimes does not load correctly.
It would be useful to provide the ability to map a dependency to a pre-bundled file.
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 require a module which is a JSON file.
"SyntaxError: missing ; before statement"
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.
This should be pretty trivial change and would make the plugin behave even closer to the way node.js operates.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.