Karma testing adding unnecessary folders to path - javascript

I'm trying to do some unit testing and I'm at the end of my rope. I've moved files, rewritten things countless times, and I still can't get the damn thing to work. I've followed this tutorial exactly and it still doesn't work.
When I run "gulp tests", and I am in my ~/git/mobile directory, I get the following output
Josh#DAEDALUS ~/git/mobile (unit-testing)
$ gulp test
[10:28:40] Using gulpfile c:\Users\Josh\git\mobile\gulpfile.js
[10:28:40] Starting 'test'...
WARN [watcher]: Pattern "c:/Users/Josh/git/mobile/tests/www/lib/angular/angular.
js" does not match any file.
WARN [watcher]: Pattern "c:/Users/Josh/git/mobile/tests/node_modules/angular-moc
ks/angular-mocks.js" does not match any file.
WARN [watcher]: Pattern "c:/Users/Josh/git/mobile/tests/www/js/app.js" does not
match any file.
WARN [watcher]: Pattern "c:/Users/Josh/git/mobile/tests/www/js/controller.js" do
es not match any file.
WARN [watcher]: Pattern "c:/Users/Josh/git/mobile/tests/www/js/services.js" does
not match any file.
WARN [watcher]: Pattern "c:/Users/Josh/git/mobile/tests/tests/Controllers/contro
llers.tests.js" does not match any file.
WARN [watcher]: Pattern "c:/Users/Josh/git/mobile/tests/tests/Services/services.
tests.js" does not match any file.
INFO [karma]: Karma v0.12.37 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Windows 8 0.0.0)]: Connected on socket --QoOiVt0ZraOpiQ3q
zd with id 22369565
PhantomJS 1.9.8 (Windows 8 0.0.0): Executed 0 of 0 ERROR (0.003 secs / 0 secs)
[10:28:42] Finished 'test' after 2.19 s
See, I don't get why it decides that it wants to add the /tests/ folder out of nowhere. It's not in my directory. For example, a proper path would be c:/users/josh/git/mobile/www/js/controller.js That tests folder isn't there. There is a tests folder in my mobile directory, but it doesn't contain anything but my "my.conf.js" and two subdirectories which contain my test files.
I cannot for the life of me figure out what's wrong. I've tried adding ../, ./, /, or removing that all together from the beginning of my files paths in my.conf.js.
Here is the contents of "my.conf.js"
Here is the contents of "gulpfile.js"

I fixed this by adding ../ to my basePath.

Related

How do you test one file (from the command line) with a Vue webpack project?

I see that you could run karma start and then karma run -- --grep=testDescriptionFilter to run a test for just one file. However, when I try to do that in a project where I am using Vue and got started with the webpack template, it doesn't work.
I've tried editing the files array in karma.conf.js, in hopes of being able to test one file this way. Normally the array looks like this:
files: [
'../../node_modules/jquery/dist/jquery.min.js',
'../../node_modules/babel-polyfill/dist/polyfill.js',
'./index.js'
],
And index.js looks like this:
import Vue from 'vue'
Vue.config.productionTip = false
// require all test files (files that ends with .spec.js)
const testsContext = require.context('./specs', true, /\.spec$/)
testsContext.keys().forEach(testsContext)
// require all src files except main.js for coverage.
// you can also change this to match only the subset of files that
// you want coverage for.
const srcContext = require.context('../../src', true, /^\.\/(?!main(\.js)?$)/)
srcContext.keys().forEach(srcContext)
First I tried to include the file I want to test instead of ./index.js like so:
files: [
'../../node_modules/jquery/dist/jquery.min.js',
'../../node_modules/babel-polyfill/dist/polyfill.js',
'./specs/players/OutcomeAnalyzerPlayer/setRangesAccordingToFilters/afterPreflop1/afterFlop1.spec.js'
],
When I do that, I get a SyntaxError: Use of reserved word 'import' error:
code/premium-poker-tools [master●] » npm run unit
> premium-poker-tools#1.0.0 unit /Users/adamzerner/code/premium-poker-tools
> BABEL_ENV=test karma start test/unit/karma.conf.js
18 10 2018 12:25:49.014:WARN [karma]: No captured browser, open http://localhost:9876/
18 10 2018 12:25:49.021:INFO [karma]: Karma v1.7.1 server started at http://0.0.0.0:9876/
18 10 2018 12:25:49.022:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
18 10 2018 12:25:49.026:INFO [launcher]: Starting browser PhantomJS
18 10 2018 12:25:49.801:INFO [PhantomJS 2.1.1 (Mac OS X 0.0.0)]: Connected on socket 1jJ_7wJ0bOiMO299AAAA with id 58854798
PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
SyntaxError: Use of reserved word 'import'
at specs/players/OutcomeAnalyzerPlayer/setRangesAccordingToFilters/afterPreflop1/afterFlop1.spec.js:1
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 0 of 0 SUCCESS (0 secs / 0 secs)
TOTAL: 0 SUCCESS
I'm not sure why I'm getting that error, but after looking through that ./index.js file, it looks like it is necessary, and that if I want to specify that I want to only run the specs for a certain file, I'll have to do it there. So I tried creating a set-ranges-according-to-filters.js file that only includes the file i want to test, and is otherwise the same as index.js:
import Vue from 'vue'
Vue.config.productionTip = false
// require all test files (files that ends with .spec.js)
const testsContext = require.context('./specs/players/OutcomeAnalyzerPlayer/setRangesAccordingToFilters/afterPreflop1/afterFlop1.spec.js', true, /\.spec$/)
testsContext.keys().forEach(testsContext)
// require all src files except main.js for coverage.
// you can also change this to match only the subset of files that
// you want coverage for.
const srcContext = require.context('../../src', true, /^\.\/(?!main(\.js)?$)/)
srcContext.keys().forEach(srcContext)
Then I updated the files array in karma.conf.js:
files: [
'../../node_modules/jquery/dist/jquery.min.js',
'../../node_modules/babel-polyfill/dist/polyfill.js',
'./set-ranges-according-to-filters.js'
],
and ran karma. I again get the same error:
code/premium-poker-tools [master●] » npm run unit
> premium-poker-tools#1.0.0 unit /Users/adamzerner/code/premium-poker-tools
> BABEL_ENV=test karma start test/unit/karma.conf.js
18 10 2018 12:30:35.072:WARN [karma]: No captured browser, open http://localhost:9876/
18 10 2018 12:30:35.087:INFO [karma]: Karma v1.7.1 server started at http://0.0.0.0:9876/
18 10 2018 12:30:35.087:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
18 10 2018 12:30:35.119:INFO [launcher]: Starting browser PhantomJS
18 10 2018 12:30:35.937:INFO [PhantomJS 2.1.1 (Mac OS X 0.0.0)]: Connected on socket 5-U0Mca0_Vgr07-rAAAA with id 87593561
PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
SyntaxError: Use of reserved word 'import'
at set-ranges-according-to-filters.js:1
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 0 of 0 SUCCESS (0 secs / 0 secs)
TOTAL: 0 SUCCESS
Preferably, I'd like to be able to test one file from the command line, something like karma start --file-path-to-my-file. But if that isn't possible, being able to edit karma.conf.js so that it only runs the tests for my file would be an ok consolation.
By editing test/unit/index.js
One way is by editing test/unit/index.js. The following code for test/unit/index.js tests the file I wanted to test:
import Vue from 'vue'
Vue.config.productionTip = false
// require all test files (files that ends with .spec.js)
const testsContext = require.context('./specs/players/OutcomeAnalyzerPlayer/setRangesAccordingToFilters/afterPreflop1', true, /afterFlop1\.spec\.js/)
testsContext.keys().forEach(testsContext)
// require all src files except main.js for coverage.
// you can also change this to match only the subset of files that
// you want coverage for.
const srcContext = require.context('../../src', true, /^\.\/(?!main(\.js)?$)/)
srcContext.keys().forEach(srcContext)
Note that my initial attempt at using require.context - require.context('./specs/players/OutcomeAnalyzerPlayer/setRangesAccordingToFilters/afterPreflop1/afterFlop1.spec.js', true, /\.spec$/) - wouldn't have worked. The first argument is a folder, not a file. The second argument is a flag indicating whether subdirectories should be searched too, and the third is a regex to match files against.
I initially tried to create a different file in place of test/unit/index.js and include it in files instead of ./index.js. The reason why I was getting the SyntaxError: Use of reserved word 'import' error is because import is an ES6 feature that isn't part of Node. It needs webpack to make it available. To get it working, I had to add it to the preprocessors array in karma.conf.js like so:
files: [
'../../node_modules/jquery/dist/jquery.min.js',
'../../node_modules/babel-polyfill/dist/polyfill.js',
// './index.js'
'./set-ranges-according-to-filters.js'
],
preprocessors: {
'./index.js': ['webpack', 'sourcemap'],
'./set-ranges-according-to-filters.js': ['webpack', 'sourcemap']
},
By directly including the file in the files array
Directly including the file in the files array instead of ./index.js works too:
files: [
'../../node_modules/jquery/dist/jquery.min.js',
'../../node_modules/babel-polyfill/dist/polyfill.js',
// './index.js'
'../../src/services/monkey-patches.js',
'./specs/players/OutcomeAnalyzerPlayer/setRangesAccordingToFilters/afterPreflop1/afterFlop1.spec.js'
],
There are two caveats (that I could think of) though:
1) Using ./index.js includes other specs and src files. When you just add your file directly instead of using ./index.js, those other files don't get loaded, and that could cause stuff to break. It did for me, and I had to add something to the files array in front of my spec to get it to work.
2) I needed to add the following to the preprocessors array:
preprocessors: {
// './index.js': ['webpack', 'sourcemap'],
'../../src/services/monkey-patches.js': ['webpack', 'sourcemap'],
'./specs/players/OutcomeAnalyzerPlayer/setRangesAccordingToFilters/afterPreflop1/afterFlop1.spec.js': ['webpack', 'sourcemap']
},
Using the command line
I wasn't able to get the karma run -- --grep=testDescriptionFilter approach working. And when I run karma run --help and karma start --help, I don't see any options for running the tests of a specific file.
If you really want to, you could create different karma configuration files that run tests for different files, and then create scripts in package.json to run your tests with different configuration files. Eg. karma-1.conf.js would run the tests for file-1.spec.js, and then you could have a npm run unit-1 command that runs karma start test/unit/karma-1.conf.js:
{
"scripts": {
"unit-1": "karma start test/unit/karma-1.conf.js"
}
}

Webstorm Karma Test Runner Not Working with Yeoman angular-fullstack

I created a web application using the Yeoman Angular-Fullstack generator. Everything runs correctly from the command line. I can use grunt to start the service and run tests.
I want to run the Karma unit tests inside the Webstorm user interface. I right clicked on karma.conf.js and had Webstorm create a Karma run configuration for me. When I try to run that configuration, it spins for a few seconds then fails.
The Karma Server window in Webstorm contains this:
/usr/local/bin/node /Applications/WebStorm.app/Contents/plugins/js-karma/js_reporter/karma-intellij/lib/intellijServer.js --karmaPackageDir=/Users/williammcneill/Documents/Work/LearnNode/gecko/node_modules/karma --configFile=/Users/williammcneill/Documents/Work/LearnNode/gecko/karma.conf.js
WARN [watcher]: Pattern "/Users/williammcneill/Documents/Work/LearnNode/gecko/client/bower_components/angular-route/angular-route.js" does not match any file.
WARN [watcher]: Pattern "/Users/williammcneill/Documents/Work/LearnNode/gecko/client/app/app.coffee" does not match any file.
WARN [watcher]: Pattern "/Users/williammcneill/Documents/Work/LearnNode/gecko/client/components/**/*.coffee" does not match any file.
WARN [watcher]: Pattern "/Users/williammcneill/Documents/Work/LearnNode/gecko/client/components/**/*.jade" does not match any file.
WARN [watcher]: Pattern "/Users/williammcneill/Documents/Work/LearnNode/gecko/client/app/**/*.coffee" does not match any file.
WARN [watcher]: Pattern "/Users/williammcneill/Documents/Work/LearnNode/gecko/client/app/**/*.jade" does not match any file.
INFO [karma]: Karma v0.12.37 server started at http://localhost:8080/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Mac OS X 0.0.0)]: Connected on socket 29_J57vgWL6mWwyV13tv with id 98142631
Which all looks correct to me. The Test Run window contains this:
/usr/local/bin/node /Applications/WebStorm.app/Contents/plugins/js-karma/js_reporter/karma-intellij/lib/intellijRunner.js --karmaPackageDir=/Users/williammcneill/Documents/Work/LearnNode/gecko/node_modules/karma --serverPort=8080 --urlRoot=/
/Applications/WebStorm.app/Contents/plugins/js-karma/js_reporter/karma-intellij/lib/intellijRunner.js:54
throw e;
^
Error: connect ETIMEDOUT 127.0.0.1:8080
at Object.exports._errnoException (util.js:837:11)
at exports._exceptionWithHostPort (util.js:860:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1060:14)
Process finished with exit code 1
I can't figure out what is timing out. If I open http://localhost:8080 I see a webpage that looks like this.
Which also looks correct to me.
I haven't actually written any code. I'm taking all the defaults that Yeoman and Webstorm give me. I think I have all the relevant Webstorm plugins (AngularJs, Karma).
Does anyone have insight into what might be happening or how I could debug it?
Webstorm 10.0.4, Yeoman 1.4.8, Karma 0.13.9, Node 4.1.0, OS X 10.10.5
The port value in karma.conf.js is set to 8080, even though the server runs on 9000. Change this value to 9000 and everything works.
Not sure exactly how the grunt configuration makes it all work, or why it's set up this way, but that's the workaround.

Node.js with Webstorm -

I've followed this example to try and add a record to MongoDB database.
When trying to run ./server/server.js I get the following message (at 2:22 in video):
"C:\Program Files (x86)\JetBrains\WebStorm 9.0.1\bin\runnerw.exe" "C:\Program Files\nodejs\npm" server.js
CreateProcess failed with error 193 (no message available)
Process finished with exit code 0
The node.exe path is right but don't know why I'm getting this error and can't find information on debugging it.
I got the same error while using WebStorm with Babel as the Node executable. After re-installing the dependencies, the correct path to the babel-node executable was node_modules\.bin\babel-node.cmd.

warnings and errors with Karma/Chrome

Just learning angularjs right now with the book 'AngularJS Up and Running' from O'Reilly..
Got to the chapter on unit testing with Karma & Jasmine, but having trouble making it work
EDIT: Changing logLevel to config.LOG_DEBUG now matches file patterns correctly. But I still have the ultimate 'adapter' error at the end.
running the 'karma start' command gives me the following:
DEBUG [plugin]: Loading karma-* from /Work/[lab]/angularjs-up-and-running/chapter3/node_modules
DEBUG [plugin]: Loading plugin /Work/[lab]/angularjs-up-and-running/chapter3/node_modules/karma-chrome-launcher.
DEBUG [plugin]: Loading plugin /Work/[lab]/angularjs-up-and-running/chapter3/node_modules/karma-jasmine.
INFO [karma]: Karma v0.12.16 server started at http://localhost:8080/
INFO [launcher]: Starting browser Chrome
DEBUG [temp-dir]: Creating temp dir at /var/folders/dw/qt56vk_s4cz5h6hg8qddvmrm0000gn/T/karma-24748311
DEBUG [launcher]: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome --user-data-dir=/var/folders/dw/qt56vk_s4cz5h6hg8qddvmrm0000gn/T/karma-24748311 --no-default-browser-check --no-first-run --disable-default-apps --disable-popup-blocking --disable-translate http://localhost:8080/?id=24748311
WARN [watcher]: Pattern "/Work/[lab]/angularjs-up-and-running/chapter3/node_modules/karma-jasmine/lib/jasmine.js" does not match any file.
WARN [watcher]: Pattern "/Work/[lab]/angularjs-up-and-running/chapter3/angular-mocks.js" does not match any file.
WARN [watcher]: Pattern "/Work/[lab]/angularjs-up-and-running/chapter3/node_modules/karma-jasmine/lib/adapter.js" does not match any file.
WARN [watcher]: Pattern "/Work/[lab]/angularjs-up-and-running/chapter3/angular.min.js" does not match any file.
WARN [watcher]: Pattern "/Work/[lab]/angularjs-up-and-running/chapter3/simpleSpec.js" does not match any file.
WARN [watcher]: Pattern "/Work/[lab]/angularjs-up-and-running/chapter3/controller.js" does not match any file.
WARN [watcher]: Pattern "/Work/[lab]/angularjs-up-and-running/chapter3/controllerSpec.js" does not match any file.
DEBUG [watcher]: Resolved files:
DEBUG [watcher]: Watching "/Work/[lab]/angularjs-up-and-running/chapter3/angular.min.js"
DEBUG [watcher]: Watching "/Work/[lab]/angularjs-up-and-running/chapter3/angular-mocks.js"
DEBUG [watcher]: Watching "/Work/[lab]/angularjs-up-and-running/chapter3/controller.js"
DEBUG [watcher]: Watching "/Work/[lab]/angularjs-up-and-running/chapter3/simpleSpec.js"
DEBUG [watcher]: Watching "/Work/[lab]/angularjs-up-and-running/chapter3/controllerSpec.js"
DEBUG [web-server]: serving: /Work/[lab]/angularjs-up-and-running/chapter3/node_modules/karma/static/client.html
DEBUG [web-server]: serving: /Work/[lab]/angularjs-up-and-running/chapter3/node_modules/karma/static/karma.js
DEBUG [web-server]: upgrade /socket.io/1/websocket/YVK_k4vczJIuLIdg5a_f
DEBUG [karma]: A browser has connected on socket YVK_k4vczJIuLIdg5a_f
INFO [Chrome 38.0.2125 (Mac OS X 10.10.0)]: Connected on socket YVK_k4vczJIuLIdg5a_f with id 24748311
DEBUG [launcher]: Chrome (id 24748311) captured in 5.575 secs
DEBUG [karma]: All browsers are ready, executing
DEBUG [web-server]: serving: /Work/[lab]/angularjs-up-and-running/chapter3/node_modules/karma/static/context.html
Chrome 38.0.2125 (Mac OS X 10.10.0) ERROR
You need to include some adapter that implements __karma__.start method!
So first of all it has warnings finding files, however all of those files do exist at that location.
My karma.conf.js file looks like this:
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'angular.min.js',
'angular-mocks.js',
'controller.js',
'simpleSpec.js',
'controllerSpec.js'
],
// list of files / patterns to exclude
exclude: [],
// web server port
port: 8080,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR ||
// LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_DEBUG,
// enable / disable watching file and executing tests
// whenever any file changes
autoWatch: true,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['Chrome'],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
});
};
karma is installed in my project folder (where karma.conf.js is located)
I really have no idea what's wrong and I can't see a way to contact the author of this book. Thanks!
I was facing that same issue and my mind was about to explode. I couldn't find anything anywhere, all the configurations were ok and nothing seemed wrong. Your question was the closest thing I could find.
I realized that both, your project filepath and mine contained square braces (.../[lab]/... in yours) and that it might be the cause. It is known that special caracters sometimes are troublesome.
I deleted the square braces from my filepath and all the tests worked perfectly.
If you pay attention to the Debug lines, jasmine.js and adapter.js were not resolved.
I hope this could be of some help.

Not found error on browser after sails lift,

I am creating a new app with "sails new" command. Upon running sails lift, I am getting a "Not Found"(404) error on the browser. I have sails installed locally & here are my node & sails version info
vsnag#like:~/sails/node_modules$ sails --version
0.10.5
vsnag#like:~/sails/node_modules$ node --version
v0.10.32
The sails lift with verbose options gives me this
vsnag#like:~/sails/app$ sails lift --verbose
info: Starting app...
warn: Cannot read package.json in the current directory (/home/vsnag/sails/app)
warn: Are you sure this is a Sails app?
warn:
warn: The package.json in the current directory does not list Sails as a dependency...
warn: Are you sure `/home/vsnag/sails/app` is a Sails app?
warn:
verbose: Setting Node environment...
verbose: Please run `npm install coffee-script` to use coffescript (skipping for now)
verbose: moduleloader hook loaded successfully.
......
....
error:
------------------------------------------------------------------------
grunt-cli: The grunt command line interface. (v0.1.13)
Fatal error: Unable to find local grunt.
If you're seeing this message, either a Gruntfile wasn't found or grunt
hasn't been installed locally to your project. For more information about
installing and configuring grunt, please see the Getting Started guide:
http://gruntjs.com/getting-started
error: Looks like a Grunt error occurred--
error: Please fix it, then **restart Sails** to continue running tasks (e.g. watching for changes in assets)
error: Or if you're stuck, check out the troubleshooting tips below.
......
...
verbose: Sending 404 ("Not Found") response
verbose: res.notFound() :: Could not locate view for error page (sending JSON instead). Details: Could not render view "404". Tried locating view file # "/home/vsnag/sails/app/views/404". Layout configured as "layout.ejs", so tried using layout # "/home/vsnag/sails/app/views/layout.ejs")
I tried the solution suggested in this so post, but of no use.
When I compare the anatomy of a sails App mentioned in sails docs & my app they don't seem to match
vsnag#like:~/sails/app$ ls -la
total 20
drwxrwxr-x 5 vsnag vsnag 4096 Oct 9 21:36 .
drwxrwxr-x 4 vsnag vsnag 4096 Oct 10 18:54 ..
drwxrwxr-x 7 vsnag vsnag 4096 Oct 9 21:36 api
drwxrwxr-x 4 vsnag vsnag 4096 Oct 9 21:36 config
drwxrwxr-x 2 vsnag vsnag 4096 Oct 9 21:36 views
I am missing the Gruntfile.js, README, app.js, .gitignore, package.json, .sailsrc files.
Any suggestions what might be the problem? or any setting that I am missing...
make a empty new sails app (in your home-folder) with:
$> sails new myapp
than copy your api, views and config in this "myapp"-folder and run sails lift again
Check your importer.less
I accidently included something that is not in there (a missing folder) and after fixing it, it worked. (:
If it doesn't check your pipeline.js to see if you have injected the correct files.
I was running the sails new command with npm because I installed it locally instead of globally. I got the not found and a 404. When I installed it globally, it worked.
For me I was not inside project folder,
After creating news sails project
cd into project folder
eg:
> sail new todo-app
> cd todo-app
Then run sails lift

Categories