Skip to content

Commit

Permalink
remove PhantomJS for running tests, only build the runner html files
Browse files Browse the repository at this point in the history
  • Loading branch information
ccprog committed Sep 27, 2018
1 parent 1211b76 commit 05fc971
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 36 deletions.
39 changes: 10 additions & 29 deletions Gruntfile.js
Expand Up @@ -30,24 +30,22 @@ module.exports = function(grunt) {
srcdir + 'thickbox-size-hack.js'
],
vendor = [
testdir + 'vendor/jquery-1.12.4.min/index.js',
testdir + 'vendor/tv4/tv4.js',
testdir + 'vendor/uri.js/src/URI.min.js',
destdir + 'angular.js',
destdir + 'angular-route.js',
destdir + 'qantic.angularjs.stylemodel.js'
],
vendor_3 = [testdir + 'vendor/jquery-1.10.2.min/index.js'].concat(vendor)
.concat([testdir + 'vendor/angular-mocks-1.7.4/index.js']),
vendor_4 = [testdir + 'vendor/jquery-1.12.4.min/index.js'].concat(vendor)
.concat([testdir + 'vendor/angular-mocks-1.7.4/index.js']);
testdir + 'vendor/angular-mocks-1.7.4/index.js'
];

var processJasmineTemplate = function (grunt, task, context) {
var processJasmineTemplate = function (grunt, context) {
var letterData = grunt.file.readJSON('src/json/letter.json', {encoding:'utf8'}),
localeData = grunt.file.readJSON('src/json/locale.json', {encoding:'utf8'});
context.crwBasics = JSON.stringify({
locale: localeData.locale,
letterDist: letterData.en.letterDist,
numerals: letterData.en.numerals,
dimensions: {},
letterRegEx: letterData.en.letterRegEx,
textDirection: 'ltr',
imagesPath: 'mock/',
Expand Down Expand Up @@ -131,35 +129,19 @@ module.exports = function(grunt) {
template: { process: processJasmineTemplate },
keepRunner: true
},
app_wp_3: {
src: jslist,
options: {
specs: testdir + 'unit/*Spec.js',
vendor: vendor_3,
outfile: testdir + '_SpecRunner-app.html'
}
},
wizzard_wp_3: {
src: wzlist.slice(0, 4),
options: {
specs: testdir + 'unit/*Spec2.js',
vendor: vendor_3,
outfile: testdir + '_SpecRunner-wizzard.html'
}
},
app_wp_4: {
app_wp: {
src: jslist,
options: {
specs: testdir + 'unit/*Spec.js',
vendor: vendor_4,
vendor: vendor,
outfile: testdir + '_SpecRunner-app.html'
}
},
wizzard_wp_4: {
wizzard_wp: {
src: wzlist.slice(0, 4),
options: {
specs: testdir + 'unit/*Spec2.js',
vendor: vendor_4,
vendor: vendor,
outfile: testdir + '_SpecRunner-wizzard.html'
}
},
Expand Down Expand Up @@ -222,8 +204,7 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-sass');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-pot');
grunt.loadNpmTasks('grunt-po2mo');
grunt.loadNpmTasks('grunt-contrib-jasmine');
grunt.loadNpmTasks('grunt-po2mo-multiFiles');
grunt.task.loadTasks('tasks/');

grunt.registerTask('msgupdate', ['block', 'pot', 'msgmerge']);
Expand Down
7 changes: 3 additions & 4 deletions README.md
Expand Up @@ -60,10 +60,9 @@ some Jasmine test dependencies installed in `tests/vendor` with bower.

The following tasks are available for the development process:

+ `grunt jasmine` will execute the unit tests in `tests/unit/` (with subtasks for testing different
jQuery versions delivered with WordPress, and for the main app and the shortcode wizzard)
Tip: if you uncomment the line `keepRunner: true` in the jasmine task configuration, you get a
`tests/_SpecRunner-*.html` file you can run directly in the browser.
+ `grunt jasmine` will build files `tests/_SpecRunner-*.html` that can run the unit tests in
`tests/unit/` in any browser you like (one for the main app and one the shortcode wizzard).
It will not run the tests as part of the task.
+ `grunt jshint:main` will lint the files in `src/js/`, `grunt jshint:spec` those in `tests/unit/`
+ `grunt uglify` will link the files in `src/js/` together as `plugin/js/crosswordsearch.js`
and minify them as `plugin/js/crosswordsearch.min.js`. Please note that if you add a new file,
Expand Down
10 changes: 7 additions & 3 deletions package.json
Expand Up @@ -9,14 +9,18 @@
"dateformat": "^1.0.8-1.2.3",
"grunt": "~0.4.4",
"grunt-babel": "^8.0.0-beta.0",
"grunt-contrib-jasmine": "^0.7.0",
"grunt-contrib-jshint": "~0.10.0",
"grunt-contrib-uglify": "~0.4.0",
"grunt-po2mo": "~0.1.0",
"grunt-pot": "~0.1.2",
"grunt-sass": "^2.1.0"
"grunt-sass": "^2.1.0",
"jasmine-core": "^2.99.1",
"lodash": "^4.17.11",
"rimraf": "^2.6.2"
},
"scripts": {
"postinstall": "bower install"
},
"dependencies": {
"grunt-po2mo-multiFiles": "^0.1.6"
}
}
36 changes: 36 additions & 0 deletions tasks/jasmine.js
@@ -0,0 +1,36 @@
/*
* grunt-contrib-jasmine
* http://gruntjs.com/
*
* Licensed under the MIT license.
*/

'use strict';

module.exports = function(grunt) {

// local lib
var jasmine = require('./lib/jasmine').init(grunt);

grunt.registerMultiTask('jasmine', 'Run jasmine specs headlessly through PhantomJS.', function() {

// Merge task-specific options with these defaults.
var options = this.options({
timeout : 10000,
styles : [],
specs : [],
helpers : [],
vendor : [],
outfile : '_SpecRunner.html',
template : __dirname + '/jasmine/templates/DefaultRunner.tmpl',
templateOptions : {},
ignoreEmpty: grunt.option('force') === true,
});

if (grunt.option('debug')) {
grunt.log.debug(options);
}

jasmine.buildSpecrunner(this.filesSrc, options);
});
};
149 changes: 149 additions & 0 deletions tasks/lib/jasmine.js
@@ -0,0 +1,149 @@

'use strict';

exports.init = function(grunt) {
// node api
var fs = require('fs'),
path = require('path');

// npm
var rimraf = require('rimraf'),
_ = require('lodash');

var baseDir = '.',
tempDir = '.grunt/jasmine';

var exports = {};

exports.copyTempFile = function(src, dest) {
var file = path.join(tempDir,dest);
grunt.file.copy(src, file);
};

exports.cleanTemp = function(cb) {
rimraf(tempDir, function(){
// if this fails, then ./.grunt isn't empty and that's ok.
fs.rmdir('.grunt', cb);
});
};

exports.buildSpecrunner = function (src, options){
var source = '',
outfile = options.outfile,
specrunner = path.join(baseDir,outfile),
outdir = path.dirname(outfile),
gruntfilter = grunt.option("filter"),
filteredSpecs = exports.getRelativeFileList(outdir, options.specs);

// Let's filter through the spec files here,
// there's no need to go on if no specs matches
if (gruntfilter) {
filteredSpecs = specFilter(gruntfilter, filteredSpecs);

if(filteredSpecs.length === 0) {
grunt.log.warn("the --filter flag did not match any spec within " + grunt.task.current.target);
return null;
}
}

['jasmine.css', 'jasmine.js', 'jasmine-html.js', 'boot.js'].forEach(function(name){
var path = __dirname + '/../../node_modules/jasmine-core/lib/jasmine-core/' + name;
if (fs.existsSync(path)) exports.copyTempFile(path, name);
});
var favicon = __dirname + '/../../node_modules/jasmine-core/images/jasmine_favicon.png'
if (fs.existsSync(favicon)) exports.copyTempFile(favicon, 'jasmine_favicon.png');

var jasmineCss = [
tempDir + '/jasmine.css'
];

jasmineCss = jasmineCss.concat(options.styles);

var jasmineCore = [
tempDir + '/jasmine.js',
tempDir + '/jasmine-html.js'
];

var context = {
temp : tempDir,
outfile: outfile,
css : exports.getRelativeFileList(outdir, jasmineCss, { nonull : true }),
scripts : {
jasmine : exports.getRelativeFileList(outdir, jasmineCore),
helpers : exports.getRelativeFileList(outdir, options.helpers, { nonull : true }),
specs : filteredSpecs,
src : exports.getRelativeFileList(outdir, src, { nonull : true }),
vendor : exports.getRelativeFileList(outdir, options.vendor, { nonull : true }),
boot : exports.getRelativeFileList(outdir, tempDir + '/boot.js')
},
options : options.templateOptions || {}
};

if (options.template.process) {
source = options.template.process(grunt, context);
grunt.file.write(specrunner, source);
} else {
grunt.file.copy(options.template, specrunner, {
process : function(src) {
source = _.template(src, context);
return source;
}
});
}

return source;
};

exports.getRelativeFileList = function(outdir, patterns, options) {
patterns = patterns instanceof Array ? patterns : [ patterns ];
options = options || {};

var files = grunt.file.expand(options, grunt.util._(patterns).compact());
files = grunt.util._(files).map(function(file){
return (/^https?:/).test(file) ? file : path.relative(outdir, file).replace(/\\/g, '/');
});
return files;
};

// Allows for a spec file to be specified via the command line
function specFilter(pattern, files) {
var specPattern,
patternArray,
filteredArray = [],
scriptSpecs = [],
matchPath = function(path) {
return !!path.match(specPattern);
};

if(pattern) {
// For '*' to work as a wildcard.
pattern = pattern.split("*").join("[\\S]*").replace(/\./g, "\\.");
// This allows for comma separated strings to which we can match the spec files.
patternArray = pattern.split(",");

while(patternArray.length > 0) {
pattern = (patternArray.splice(0, 1)[0]);

if(pattern.length > 0) {
if(pattern.indexOf('/') === -1) {
specPattern = new RegExp("("+pattern+"[^/]*)(?!/)$", "ig");
} else if(pattern.indexOf('/') === 0) {
specPattern = new RegExp("("+pattern+"[^/]*)(?=/)", "ig");
} else {
throw new TypeError("--filter flag seems to be in the wrong format.");
}

// push is usually faster than concat.
[].push.apply(scriptSpecs, files.filter(matchPath));
}
}

filteredArray = _.uniq(scriptSpecs);
}

return filteredArray;
}

return exports;
};

0 comments on commit 05fc971

Please sign in to comment.