Jasmine-node tests executed twice - node.js

My jasmine-node tests are executed twice.
I run those test from Grunt task and also from Jasmine command. Result is the same my tests are run twice.
My package.json :
{
"name": "test",
"version": "0.0.0",
"dependencies": {
"express": "4.x",
"mongodb": "~2.0"
},
"devDependencies": {
"grunt": "~0.4.5",
"grunt-jasmine-node":"~0.3.1 "
}
}
Here is my Gruntfile.js extract :
grunt.initConfig({
jasmine_node: {
options: {
forceExit: true,
match: '.',
matchall: true,
extensions: 'js',
specNameMatcher: 'spec'
},
all: ['test/']
}
});
grunt.loadNpmTasks('grunt-jasmine-node');
grunt.registerTask('jasmine', 'jasmine_node');
One of my test file :
describe("Configuration setup", function() {
it("should load local configurations", function(next) {
var config = require('../config')();
expect(config.mode).toBe('local');
next();
});
it("should load staging configurations", function(next) {
var config = require('../config')('staging');
expect(config.mode).toBe('staging');
next();
});
it("should load production configurations", function(next) {
var config = require('../config')('production');
expect(config.mode).toBe('production');
next();
});
});
I have 2 test files for 4 assertions
Here is my prompt :
grunt jasmine
Running "jasmine_node:all" (jasmine_node) task
........
Finished in 1.781 seconds
8 tests, 8 assertions, 0 failures, 0 skipped
Have you got any idea ?

All credit to 1.618. He answered the question here: grunt jasmine-node tests are running twice
This looks likes some buggy behaviour. The quick fix is to configure jasmine_node in your Gruntfile like this:
jasmine_node: {
options: {
forceExit: true,
host: 'http://localhost:' + port + '/',
match: '.',
matchall: false,
extensions: 'js',
specNameMatcher: '[sS]pec'
},
all: []
}
The key is the all parameter. The grunt plugin is looking for files with spec in the name. For some reason, it looks in the spec/ directory and everywhere else. If you specify the spec directory, its files get picked up twice. If you don't specify, it only gets set once, but then you can't put spec in any of your non-test filenames.

Related

newman, postman's cli runner, can not find a custom reporter

When I run newman with a custom reporter it can not find it, and the error states the reporter should be installed in the newman directory. I am on windows 10. It is named newman-reporter-csvconsole. Where is the newman default directory, to look for reporters?
the reporter package index.js
function csvconsole (emitter, reporterOptions, collectionRunOptions) {
emitter.on('start',function (err, args)
{ // on start of run, log to console
console.log('running a collection...');
});
}
module.exports = csvconsole;
I then install a local package
C:\Users<user>\AppData\Roaming\npm\node_modules\newman\newman-reporter-csvconsole>npm init -w newman-reporter-csvconsole -S
C:\Users<user>\AppData\Roaming\npm\node_modules\newman\newman-reporter-csvconsole>npm pack
C:\Users<user>\AppData\Roaming\npm\node_modules\newman>npm install -S ./csvconsoleReporter/newman-reporter-csvconsole-1.0.0.tgz
The package and pack-lock files
C:\Users<user>\AppData\Roaming\npm\node_modules\newman\package.json
"dependencies": {
...
"newman-reporter-csvconsole": "file:newman-reporter-csvconsole",
...
C:\Users<user>\AppData\Roaming\npm\node_modules\newman\package-lock.json
"dependencies": {
...
"newman-reporter-csvconsole": "file:newman-reporter-csvconsole",
...
"newman-reporter-csvconsole": {
"version": "1.0.0",
"license": "ISC"
},
...
"node_modules/newman-reporter-csvconsole": {
"resolved": "newman-reporter-csvconsole",
"link": true
},
...
"newman-reporter-csvconsole": {
"version": "file:newman-reporter-csvconsole"
},
module.exports = function csvconsole (emitter, reporterOptions, collectionRunOptions)
{
// emitter is is an event emitter that triggers the following events: https://github.com/postmanlabs/newman#newmanrunevents
// reporterOptions is an object of the reporter specific options. See usage examples below for more details.
// collectionRunOptions is an object of all the collection run options:
// https://github.com/postmanlabs/newman#newmanrunoptions-object--callback-function--run-eventemitter
emitter.on('start',function (err, args)
{ // on start of run, log to console
console.log('running a collection...');
});
}

grunt-contrib-connect - undefined is not a function for connect.static

I am trying to configure the grunt connect such a way that
For static pages it serves pages from src directory
web service calls are intercepted by middle-ware and static json is served.
While the web service calls are being mocked correctly the connect.static call ends up giving an error
TypeError: undefined is not a function
While I realize that the connect.static was provided in later versions of this module, I have already upgraded to a version later than that
Here is my package.json file
{
"name": "my-angular-seed-project",
"version": "1.0.0",
"description": "angular seed with only bower and grunt",
"main": "index.js",
"dependencies": {
},
"devDependencies": {
"bower": "^1.4.1",
"grunt": "^0.4.5",
"grunt-cli": "^0.1.13",
"grunt-contrib-connect": ">=0.10.0",
"grunt-contrib-jshint": "latest",
"grunt-contrib-uglify": "latest",
"grunt-contrib-watch": "latest",
"jshint-stylish": "^2.0.1"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Here is the gruntfile.js
// Gruntfile.js
// our wrapper function (required by grunt and its plugins)
// all configuration goes inside this function
module.exports = function(grunt) {
var restEndPoints = {
"/restapi/users": {"GET":"json-files/users.get.json"},
"/restapi/users/login": {"GET":"json-files/users.get.json"},
"/restapi/users/john#gmail.com": {"GET":"json-files/users.get.john.json"},
"/restapi/nodes": {"GET":"json-files/nodes.get.json","PUT":"json-files/nodes.put.json","POST":"json-files/nodes.put.json"},
"/restapi/nodes/Node1": {"GET":"json-files/nodes.get.node1.json","DELETE":"json-files/nodes.delete.node1.json"},
"/restapi/services": {"GET":"json-files/services.get.json","PUT":"json-files/services.put.json","POST":"json-files/services.put.json"},
"/restapi/services/nginx": {"GET":"json-files/services.get.nginx.json","DELETE":"json-files/services.delete.nginx.json"},
"/restapi/commands": {"GET":"json-files/commands.get.json","PUT":"json-files/commands.put.json","POST":"json-files/commands.put.json"},
"/restapi/commands/pwd": {"GET":"json-files/commands.get.pwd.json","DELETE":"json-files/commands.delete.pwd.json"}
};
String.prototype.endsWith = function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;};
// ===========================================================================
// CONFIGURE GRUNT ===========================================================
// ===========================================================================
grunt.initConfig({
// get the configuration info from package.json ----------------------------
// this way we can use things like name and version (pkg.name)
pkg: grunt.file.readJSON('package.json'),
// all of our configuration will go here
watch: {
},
// configure jshint to validate js files -----------------------------------
jshint: {
options: {
reporter: require('jshint-stylish') // use jshint-stylish to make our errors look and read good
},
// when this task is run, lint the Gruntfile and all js files in src
build: ['Gruntfile.js', 'src/**/*.js']
},
// configure connect to run server (on test/serve or example)
connect: {
server: {
options: {
port : 8000,
hostname : 'localhost',
base : 'src',
middleware: function (connect,options){return [
//Middleware #1 - for rest api calls
function restapiMiddleware(req, res, next) {
if (req.url.indexOf('restapi') > 0){
console.log(req.method+' request received for webservice api ['+req.url+']');
var match = false;
var json_file_to_serve = "";
var keys = Object.keys(restEndPoints);
keys.forEach(function(urlAsKey) {
if (req.url.endsWith(urlAsKey)) {
Object.keys(restEndPoints[urlAsKey]).forEach(function(httpMethodsAsKey) {
if (req.method == httpMethodsAsKey){
match = true;
json_file_to_serve = restEndPoints[urlAsKey][httpMethodsAsKey];
}
}); //forEach ends
}
}); //forEach ends
//no match with the url, move along
if (match == false) {
return next();
}
if (req.url.endsWith('/login')){
res.writeHead(200, { 'user-auth-token':'56f7997504b352cbf6ba6210409e423f5fdac49a','user-enc-email':'lJUXityStsKko/lPr9eJUc5fLFCV5kFm' });
}
//Finalize this response with json file
res.end(grunt.file.read(json_file_to_serve));
// if not restapi call then goto next middleware
// can we serve static right here ?
}else{
return next();
}
} // element/middleware one ends so comma just json objects this is awesome
,
//Middleware #2 for static page calls
function staticMiddleware(connect,options) {
connect.static(options.base);
//connect.static('src');
}
] // array ends
}
}
}
}
});
// ===========================================================================
// LOAD GRUNT PLUGINS ========================================================
// ===========================================================================
// we can only load these if they are in our package.json
// make sure you have run npm install so our app can find these
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-watch');
//register the task
grunt.registerTask('serve', ['connect', 'watch']);
};
Am I missing something trivial here ?
Thanks #R4c00n & #Christian Fritz, I was going through the gruntfile of the grunt-contrib-connect and it uses the serveStatic call instead of connect.static and yes the module serve-static is part of grunt contrib connect's node_modules. So now a serveStatic(base.options) does wire the static files as well.
Here is the updated grunt file section (the serve static call has to be first though)
middleware: function (connect,options){return [
//statically serve pages from src directory
serveStatic('src'),
//Middleware #1 - for rest api calls
function restapiMiddleware(req, res, next) {
// middleware code
}];}

Grunt hangs on uglify

I am trying to minify a little angular script, but for some reason uglify just hangs indefinitely. JSHint runs fine and completes if I add it into the task list, and then it hangs on uglify.
Here is my Gruntfile:
module.exports = function (grunt) {
// Project configuration
grunt.initConfig({
// make node configuration available for use
pkg: grunt.file.readJSON('package.json'),
// configure uglify
uglify: {
options: {
mangle: false
},
my_target: {
dist: {'dist/test.min.js': ['src/test.js']}
}
},
// configure JSHint
jshint: {
app: ['src/*.js']
}
});
// load pluginsng
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
// default
grunt.registerTask('default', ['jshint', 'uglify']);
grunt.registerTask('uglify', ['uglify']);
};
Here are the versions I am using:
"devDependencies": {
"grunt": "~0.4.5",
"grunt-contrib-jshint": "~0.11.2",
"grunt-contrib-uglify": "~0.9.1"
}
I ran grunt -v and after jshint finishes it outputs this forever:
Running "uglify" task
Running "uglify" task
Running "uglify" task
It doesn't seem to care what file I give it either, so it seems to be hitting some issue before it gets to reading my file.
Any ideas?
You're redefining the uglify task to run itself in your last line, replacing grunt-contrib-uglify:
grunt.registerTask('uglify', ['uglify']);
That's why your grunt is looping endlessly.
Just give it a different name:
grunt.registerTask('compress', ['uglify']);

What am I doing wrong with Gruntjs targets?

I followed the instructions on the grunt.option page to create different configurations for different environments/targets such as development, staging, and production in my Gruntfile. However, upon doing so I found that my tasks silently fail.
I've reduced the problem to a very simple example. The following Gruntfile fails to build the file:
module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
less: {
dev: {
options: {
compress: true
},
build: {
src: ['src/css/test.less'],
dest: 'build/css/test.css'
}
}
}
});
grunt.loadNpmTasks('grunt-contrib-less');
grunt.registerTask('default', ['less:dev']);
};
The output in my terminal is the following:
$ grunt
Running "less:dev" (less) task
Done, without errors.
If, however, I use the following Gruntfile, the build output is as expected:
module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
less: {
options: {
compress: true
},
build: {
src: ['src/css/test.less'],
dest: 'build/css/test.css'
}
}
});
grunt.loadNpmTasks('grunt-contrib-less');
grunt.registerTask('default', ['less']);
};
The terminal output for this Gruntfile reflects the built file:
$ grunt
Running "less:build" (less) task
File build/css/test.css created.
Done, without errors.
What am I doing wrong in the first Gruntfile? What is it that I am missing about this task:target convention?
Your first Gruntfile - If you want per-target options, you need to specify the files object. So your code would be something like this:
less: {
dev: {
files: {
"build/css/test.css": "src/css/test.less"
}
},
production: {
options: {
compress: true
},
files: {
"build/css/test.css": "src/css/test.less"
}
},
}
Basically in your first Gruntfile build is an unknown object. Your target is named dev and grunt-contrib-less doesn't have an option called build so Grunt doesn't know where to write the files. Your second Gruntfile works because you set the options as a global. Use the above code if you want per-target options.

Running node app from grunt with watch

So, I have the grunt file below. I'm wanting to add a task that will start my node app and watch for changes in a directory and restart. I have been using supervisor, node-dev (which are great) but I want to run one command and start my whole app. There has got to be a simple way to do this, but I'm just missing it. It is written in coffeescript as well (not sure if that changes things)...
module.exports = function(grunt) {
grunt.initConfig({
/*exec: {
startApi: {
command: "npm run-script start-api"
}
},*/
//static server
server: {
port: 3333,
base: './public',
keepalive: true
},
// Coffee to JS compilation
coffee: {
compile: {
files: {
'./public/js/*.js': './src/client/app/**/*.coffee'
},
options: {
//basePath: 'app/scripts'
}
}
},
mochaTest: {
all: ['test/**/*.*']
},
watch: {
coffee: {
files: './src/client/app/**/*.coffee',
tasks: 'coffee'
},
mochaTest: {
files: 'test/**/*.*',
tasks: 'mochaTest'
}
}
});
grunt.loadNpmTasks('grunt-contrib-coffee');
grunt.loadNpmTasks('grunt-mocha-test');
//grunt.loadNpmTasks('grunt-exec');
grunt.registerTask( 'default', 'server coffee mochaTest watch' );
};
As you can see in the comments, I tries grunt-exec, but the node command stops the execution of the other tasks.
You can set grunt to run default task and the watch task when you start your node app:
in app.js
var cp = require('child_process');
var grunt = cp.spawn('grunt', ['--force', 'default', 'watch'])
grunt.stdout.on('data', function(data) {
// relay output to console
console.log("%s", data)
});
Then just run node app as normal!
Credit

Resources