Mocha test passes locally, but fails on Travis CI - javascript

I am trying to add testing to the website I'm building. I'm using Mocha as my testing framework and Chai and expect as my assertion library. I made a simple test just to make sure things work and then I created a Gruntfile to run my tests. The test is a simple test that just verifies that true === true and it worked both locally and on Travis CI. Now, even though I haven't changed anything in the test, it only works locally, but fails on Travis CI. It was passing before and it still passes locally, so I'm not sure what to change.
My simple test code looks like this:
'use strict';
var chai = require('chai');
var expect = chai.expect;
describe('Test that tests run', function(done) {
it('should run a test', function(done) {
expect(true).to.eql(true);
done();
});
});
My Gruntfile looks like this:
'use strict';
module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-simple-mocha');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-jscs');
// initialize Grunt
grunt.initConfig({
// create jshint task
jshint: {
dev: {
// tell jshint what check
src: ['Gruntfile.js', 'server.js', 'js/**/*.js', 'models/**/*.js', 'routes/**/*.js', '!build/**', '!tests/client/bundle.js', '!tests/karma_tests/bundle.js', '!js/imageMapResizer.min.js', '!js/kickstart.js', '!js/form-validator.js'],
options: {
node: true,
globals: {
describe: true,
it: true,
before: true,
after: true,
beforeEach: true,
afterEach: true,
res: true
}
}
},
mocha: {
// tell mocha where test files are
src: ['tests/**/*.js', '!tests/client/bundle.js', '!tests/karma_tests/bundle.js'],
options: {
node: true,
globals: {
describe: true,
it: true,
before: true,
after: true,
beforeEach: true,
afterEach: true,
res: true,
expect: true
}
}
},
// create jscs task
jscs: {
dev: {
// tell jscs to test the same files as jshint
src: ['<%= jshint.dev.src %>', '<%= jshint.mocha.src %>']
}
}
},
// create simplemocha task
simplemocha: {
dev: {
src: ['tests/test_entry.js']
}
}
});
// register linting task
grunt.registerTask('lint', ['jshint:dev', 'jshint:mocha' /*, 'jshint:jasmine'*/ ]);
// register mocha test task
grunt.registerTask('test', ['simplemocha:dev']);
grunt.registerTask('default', ['test']);
};
And .travis.yml looks like this:
language: node_js
node_js:
- "4.1"
- "4.0"
- "0.12"
- "0.11"
- "0.10"
- "0.8"
- "0.6"
- "iojs"
before_install:
- npm install -g grunt-cli
script: grunt test
Let me know if you have questions or want to see more code. Thanks in advance for all your help!

After some more digging with Travis CI I found there were 2 problems. The first was that node.js versions 0.6 and 0.8 were incompatible with many of my node packages. The other problem was 2 node packages I had included were not compatible with linux, which is the OS Travis CI uses to run tests. The packages were node-sqlserver-unofficial and msnodesqlv8.
I don't really need to work on node.js versions 0.6 or 0.8 and the I can work without the 2 node packages, so once I removed the the older node versions and the packages and my tests passed with flying colors.

Related

Grunt no "qunit" targets found

Configuring grunt to make automated JS tests with jenkins and qunit, I am actually blocking on this issue.
When I run grunt:
Running "qunit_junit" task
XML reports will be written to _build/test-reports
No "qunit" targets found.
Warning: Task "qunit" failed. Use --force to continue.
Aborted due to warnings.
My Gruntfile:
'use strict';
module.exports = function(grunt) {
var gruntConfig = {};
grunt.initConfig({
sync: {
target: {}
}
});
grunt.registerTask('default', ['qunit_junit', 'qunit']);
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.loadNpmTasks('grunt-qunit-istanbul');
gruntConfig.qunit = {
src: ['static/test/index.html'],
options: {
coverage: {
src: ['static/js/**/*.js'],
instrumentedFiles: 'temp/',
htmlReport: 'report/coverage',
coberturaReport: 'report/',
linesThresholdPct: 20
}
}
};
grunt.loadNpmTasks('grunt-qunit-junit');
gruntConfig.qunit_junit = {
options: {
dest: 'report/'
}
};
};
I checked and console.log() in the node_modules, the grunt-contrib-qunit is installed and the task is in it so grunt finds the module and the task but seems not to load it.
Just at a glance - you are creating your config, but not doing anything with it.
Change this line
grunt.initConfig({
sync: {
target: {}
}
});
to this:
grunt.initConfig(gruntConfig);
You might also want to move that down below all the other stuff you add to gruntConfig.

Grunt provide html report for mocha test

I use grunt to run my mocha test and I see the test result in the console which is OK, the problem is that this task are generating report but when you run this HTML report you just see the log of running in text...I want to see the test aggregations ,and the mocha unit test are run OK, what I am missing here?
mochaTest: {
test: {
options: {
reporter: 'spec',
colors: true,
summery: true,
captureFile: 'results.html', // Optionally capture the reporter output to a file
quiet: false, // Optionally suppress output to standard out (defaults to false)
clearRequireCache: true // Optionally clear the require cache before running tests (defaults to false)
},
src: ['test/*spec.js'],
excludes: ['plugins']
},
'travis-cov': {
options: {
reporter: 'travis-cov'
}
}
},
I use the package
grunt.loadNpmTasks('grunt-mocha-test');
https://github.com/pghalliday/grunt-mocha-test
I want Report like this or any other nice html report which I can use...
You can use Mochawesome is a custom reporter for use with the Javascript testing framework, mocha. It generates a nice HTML/CSS report that helps visualize your test suites:
First, you need to install the plugin:
npm install --save-dev mochawesome
Then you change your grunt-mocha-test reporter:
mochaTest: {
test: {
options: {
reporter: 'mochawesome', //You need to change this !
colors: true,
summery: true,
captureFile: 'results.html',
quiet: false,
clearRequireCache: true
},
src: ['test/*spec.js'],
excludes: ['plugins']
},
'travis-cov': {
options: {
reporter: 'travis-cov'
}
}
},

Grunt concats JS file multiple times

I'm working on a Node.js website and I'm using Grunt to concat and minify my CSS and JS files. However, after running the grunt command I'm getting the error message:
fullPage: Fullpage.js can only be initialized once and you are doing it multiple times!
Here's my grunt file:
/*global module */
module.exports = function (grunt) {
"use strict";
grunt.initConfig({
// read in the project settings from the package.json file into the pkg property
pkg: grunt.file.readJSON("package.json"),
// Install only the bower packages that we need
bower: {
install: {
options: {
"targetDir": "./public/lib",
"copy": true,
"cleanup": true,
"install": true
}
}
},
concat: {
css: {
src: ["public/lib/css/**/*.css", "public/css/cts.css"],
dest: "public/lib/dist/main.css"
},
js: {
src: ["public/lib/**/jquery.js", "public/lib/**/*.js", "public/js/cts.js"],
dest: "public/lib/dist/main.js"
}
},
cssmin: {
target: {
files: {
"public/lib/dist/main.min.css": "public/lib/dist/main.css"
}
}
},
uglify : {
js: {
files: {
"public/lib/dist/main.min.js": "public/lib/dist/main.js"
}
}
},
copy: {
files: {
expand: true,
flatten: true,
src: ["public/lib/fonts/**/*"],
dest: "public/lib/fonts/",
filter: "isFile"
}
}
});
// Add all plugins that your project needs here
grunt.loadNpmTasks("grunt-bower-task");
grunt.loadNpmTasks("grunt-contrib-concat");
grunt.loadNpmTasks("grunt-contrib-copy");
grunt.loadNpmTasks("grunt-contrib-cssmin");
grunt.loadNpmTasks("grunt-contrib-uglify");
grunt.loadNpmTasks("grunt-contrib-watch");
// this would be run by typing "grunt test" on the command line
// the array should contains the names of the tasks to run
grunt.registerTask("test", []);
// define the default task that can be run just by typing "grunt" on the command line
// the array should contains the names of the tasks to run
grunt.registerTask("default", [ "bower", "concat", "cssmin", "uglify", "copy"]);
grunt.registerInitTask("install", ["bower"]);
};
If anything I would have thought jQuery would be the one that's getting concatenated multiple times but it's not. Any suggestions what I might be doing wrong?
EDIT: Here's my upgraded grunt file with all 3rd party libraries listed in the concat.src.
/// <binding BeforeBuild='default' />
/*global module */
module.exports = function (grunt) {
"use strict";
grunt.initConfig({
// read in the project settings from the package.json file into the pkg property
pkg: grunt.file.readJSON("package.json"),
// Install only the bower packages that we need
bower: {
install: {
options: {
"targetDir": "./public/lib",
"copy": true,
"cleanup": true,
"install": true
}
}
},
concat: {
css: {
src: ["public/lib/css/**/*.css", "public/css/cts.css"],
dest: "public/lib/dist/main.css"
},
js: {
src: [
"public/lib/js/jquery/jquery.js",
"public/lib/js/bootstrap/bootstrap.js",
"public/lib/js/fullpage.js/jquery.fullpage.js",
"public/lib/js/jquery-easing-original/jquery.easing.js",
"public/lib/js/slimscroll/jquery.slimscroll.js",
"public/lib/js/wow/wow.js",
"public/js/cts.js"
],
dest: "public/lib/dist/main.js"
}
},
cssmin: {
target: {
files: {
"public/lib/dist/main.min.css": "public/lib/dist/main.css"
}
}
},
uglify : {
js: {
files: {
"public/lib/dist/main.min.js": "public/lib/dist/main.js"
}
}
},
copy: {
files: {
expand: true,
flatten: true,
src: ["public/lib/fonts/**/*"],
dest: "public/lib/fonts/",
filter: "isFile"
}
}
});
// Add all plugins that your project needs here
grunt.loadNpmTasks("grunt-bower-task");
grunt.loadNpmTasks("grunt-contrib-concat");
grunt.loadNpmTasks("grunt-contrib-copy");
grunt.loadNpmTasks("grunt-contrib-cssmin");
grunt.loadNpmTasks("grunt-contrib-uglify");
grunt.loadNpmTasks("grunt-contrib-watch");
// this would be run by typing "grunt test" on the command line
// the array should contains the names of the tasks to run
grunt.registerTask("test", []);
// define the default task that can be run just by typing "grunt" on the command line
// the array should contains the names of the tasks to run
grunt.registerTask("default", [ "bower", "concat", "cssmin", "uglify", "copy"]);
grunt.registerTask("combine", [ "concat", "cssmin", "uglify", "copy"]);
grunt.registerInitTask("install", ["bower"]);
};
Your issue seems to be in concate.js.src
src: ["public/lib/**/jquery.js", "public/lib/**/*.js", "public/js/cts.js"]
This will have your files added multiple times as there might some files common among the paths mentioned in src.
You should probably move all your vendor files like jquery out of the public directory and put in a different one, say vendor.
Your src should then look something like
src: ["vendor/**/*.js", "public/**/*.js"]
As you see now there are no common files among these two paths.
Also its a good practice to always have 3rd party code outside your app directory as a sibling folder and not inside it.
EDIT:
Ah! I see whats your problem. You want to have jquery first among the other vendor files.
public/lib/**/jquery.js and public/lib/**/*.js together might be causing files added twice.
Try this
src: ["public/lib/jquery/jquery.js", "public/lib/**/*.js", "!public/lib/jquery/jquery.js", public/js/cts.js"]
Put the full path of jquery first public/lib/jquery/jquery.js and then the !public/lib/jquery/jquery.js should prevent jquery being added again as part of public/lib/**/*.js
Got the above pattern from here http://gruntjs.com/configuring-tasks#globbing-patterns
If this still doesn't work, then another option is to add all paths in the src array individually. If you have a requirejs config just copy the paths from there, as jquery might not be the only dependency issue you face in future.

gulp wiredep does not load bower dependency

i have a project on Angularjs which covered by unit tests with help of bower and jasmine. Every thing worked fine until I added a new module (angular-treeview). Module works fine in browser but not in Phantomjs while testing. The js is missing by some reason. Here is my gulp task for test :
'use strict';
var gulp = require('gulp');
var wiredep = require('wiredep');
var karmaServer = require('karma').Server;
/**
* Run the unit tests
*/
gulp.task('test', ['templates'], function() {
var bowerDeps = wiredep({
directory: 'src/bower_components',
exclude: [],
dependencies: true,
devDependencies: true
});
bowerDeps.js.unshift('src/bower_components/jquery/dist/jquery.js');
var testFiles = bowerDeps.js.concat([
'src/app/app.js', // loading the module first to make Karma avoid nomod-errors
'src/{app,components}/**/*.js',
'.tmp/templates/*.js', // HTML-templates converted to JS for directives/etc
'test/unit/**/*.js'
]);
new karmaServer({
files: testFiles,
singleRun: false,
autoWatch: true,
configFile: __dirname + '/../test/karma.conf.js'
}).start();
});
angular-treeview is in src/bower_components but is still missing.
Any ideas?

issue in testing with gruntjs and phantomjs

I want to create a Gruntfile.js to run bunch of phantomjs tests, when I execute > grunt run-test from commandline, it runs a bunch of tests. I created a Gruntfile.js and package.json which works ok and it reads bunch of tests from a directory. Now my problem is that when I write a phantomjs test and run the "grunt", it gives me this error:
Error: Cannot find module 'system'
Error: Cannot find module 'phantom'
However phantomjs is installed before by using npm install phantomjs
Example of phantomtest which gives me the above error:
var system = require('system');
if (system.args.length === 1) {
console.log('Try to pass some args when invoking this script!');
} else {
system.args.forEach(function (arg, i) {
console.log(i + ': ' + arg);
});
}
phantom.exit();
When I run phantomjs test1 (name of the test file) it runs the test so I think maybe I should append "phantomjs" somewhere in the Gruntfile. Any idea?
Gruntfile.js
'use strict';
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
jshint: {
all: [
'Gruntfile.js',
'tests/*.js',
'<%= nodeunit.tests %>',
],
options: {
jshintrc: '.jshintrc',
},
},
// Before generating any new files, remove any previously-created files.
clean: {
tests: ['tmp'],
},
// Configuration to be run (and then tested).
testArgs: {
configFile:"test/testConf.js",
options: {
args: {
params: {
number: 1,
bool: true,
str: "string",
nil: null, // Null is not supported.
obj: {
array: [1, 2, 3],
undef: undefined
}
},
capabilities: {
'browserName': 'chrome'
},
rootElement:"body",
specs:["test/argsTest.js"],
verbose:true
}
}
},
testDebug: {
configFile:"test/testConf.js",
options: {
debug:true,
args: {
specs:["test/debugTest.js"],
}
}
},
// Unit tests.
nodeunit: {
tests: ['tests/*_test.js'],
},
});
// Actually load this plugin's task(s).
grunt.loadTasks('tests');
// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-nodeunit');
// Whenever the "test" task is run, first clean the "tmp" dir, then run this
// plugin's task(s), then test the result.
grunt.registerTask('test', ['clean']);
// By default, lint and run all tests.
grunt.registerTask('default', ['jshint', 'test']);
};

Categories