Skip to content

Commit

Permalink
Merge pull request #71 from magento-vanilla/MAGETWO-31592
Browse files Browse the repository at this point in the history
[Vanilla] MAGETWO-31592 - Javascript Unit Test Framework Update
  • Loading branch information
vpelipenko committed Feb 2, 2015
2 parents 3e8e126 + 51dca39 commit 5b1aef1
Show file tree
Hide file tree
Showing 12 changed files with 396 additions and 67 deletions.
61 changes: 55 additions & 6 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@

// For performance use one level down: 'name/{,*/}*.js'
// If you want to recursively match all subfolders, use: 'name/**/*.js'

'use strict';

module.exports = function (grunt) {
'use strict';

// Required plugins
// _____________________________________________
var specRunner = require('./dev/tests/js/framework/spec_runner')(grunt);

require('./dev/tools/grunt/tasks/mage-minify')(grunt);

// Time how long tasks take. Can help when optimizing build times
require('time-grunt')(grunt);

// Load grunt tasks automatically
require('load-grunt-tasks')(grunt);
require('load-grunt-tasks')(grunt, {
pattern: ['grunt-*', '!grunt-template-jasmine-requirejs']
});

var svgo = require('imagemin-svgo');

Expand All @@ -42,7 +43,8 @@ module.exports = function (grunt) {
uglify: {
legacy: 'lib/web/legacy-build.min.js'
},
doc: 'lib/web/css/docs'
doc: 'lib/web/css/docs',
spec: 'dev/tests/js/spec'
};

// Define Themes
Expand Down Expand Up @@ -459,8 +461,38 @@ module.exports = function (grunt) {
'<%= path.doc %>': '<%= path.doc %>/source'
}
}
}
},

specRunner: {
options: {
shareDir: 'base'
},
backend: {
options: {
port: 8000,
areaDir: 'adminhtml',
theme: 'backend'
}
},
frontend: {
options: {
port: 3000,
areaDir: 'frontend',
theme: 'blank'
}
}
},

jasmine: {
'options': {
template: require('grunt-template-jasmine-requirejs'),
ignoreEmpty: true
},
'backend-unit': specRunner.configure('unit', 'adminhtml', 8000),
'backend-integration': specRunner.configure('integration', 'adminhtml', 8000),
'frontend-unit': specRunner.configure('unit', 'frontend', 3000),
'frontend-integration': specRunner.configure('integration', 'frontend', 3000)
}
});

// Assembling tasks
Expand Down Expand Up @@ -514,4 +546,21 @@ module.exports = function (grunt) {
}
});

// Tests
// ---------------------------------------------

grunt.registerTask('spec', [
'specRunner:backend',
'specRunner:frontend'
]);

grunt.registerTask('unit', [
'jasmine:backend-unit',
'jasmine:frontend-unit'
]);

grunt.registerTask('integration', [
'jasmine:backend-integration',
'jasmine:frontend-integration'
]);
};
191 changes: 191 additions & 0 deletions dev/tests/js/framework/spec_runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Creates jasmine configuration object
*
* @param {String} type - type of tests
* @param {String} dir - area dir
* @param {Number} port - port to run on
* @return {Object}
*/
function buildConfig(type, dir, port) {
'use strict';

return {
src: '<%= path.spec %>/shim.js',
options: {
host: 'http://localhost:' + port,
specs: '<%= path.spec %>/' + type + '/**/' + dir + '/**/*.js',
templateOptions: {
requireConfigFile: [
'<%= path.spec %>/require.config.js',
'<%= path.spec %>/' + type + '/config/' + dir + '.js'
]
}
}
};
}

module.exports = function (grunt) {
'use strict';

var connect = require('connect'),
logger = require('morgan'),
serveStatic = require('serve-static'),
fs = require('fs'),
root;

root = __dirname
.replace('/dev/tests/js/framework', '')
.replace('\\dev\\tests\\js\\framework', '');

grunt.registerMultiTask('specRunner', function () {
var app = connect(),
options,
area,
theme,
share,
middlewares;

options = this.options({
port: 3000,
theme: 'blank',
areaDir: 'adminhtml',
shareDir: 'base',
enableLogs: false,
middleware: null
});

area = options.areaDir;
share = options.shareDir;
theme = options.theme;

if (options.enableLogs) {
app.use(logger('dev'));
}

app.use(function (req, res, next) {
var url = req.url,
match = url.match(/^\/([A-Z][^\/]+)_(\w+)\/(.+)$/),
vendor,
module,
path,
getModuleUrl,
getThemeUrl;

/**
* Returns path to theme root folder
*
* @return {String}
*/
function themeRoot() {
return [
'/app/design',
area,
vendor,
theme
].join('/');
}

/**
* Based on 'thematic' parameter, returnes either path to theme's lib,
* or 'lib/web'.
*
* @param {Boolean} thematic
* @return {String}
*/
function lib(thematic) {
return thematic ? themeRoot() + '/web' : '/lib/web';
}

if (match !== null) {
vendor = match[1];
module = match[2];
path = match[3];

/**
* Assembles modular path. If 'shared' flag provided and is truthy,
* will use share dir instead of area one.
*
* @param {Boolean} shared
* @return {String}
*/
getModuleUrl = function (shared) {
return [
'/app/code',
vendor,
module,
'view',
!!shared ? share : area,
'web',
path
].join('/');
};

/**
* Assembles theme modular path.
*
* @return {String}
*/
getThemeUrl = function () {
return [
themeRoot(),
vendor + '_' + module,
'web',
path
].join('/');
};

url = exists(url = getThemeUrl()) ?
url :
exists(url = getModuleUrl()) ?
url : getModuleUrl(true);

} else if (canModify(url)) {
url = (exists(url = lib(true)) ? url : lib()) + req.url;
}

req.url = url;

next();
});

if (options.middleware && typeof options.middleware === 'function') {
middlewares = options.middleware(connect, options);

if (Array.isArray(middlewares)) {
middlewares.forEach(function (middleware) {
app.use(middleware);
});
}
}

app.use(serveStatic(root));

app.listen(options.port);
});

/**
* Defines if passed file path exists
*
* @param {String} path
* @return {Boolean}
*/
function exists(path) {
return fs.existsSync(root + path);
}

/**
* Restricts url's which lead to '/_SpecRunner.html', '/dev/tests' or '.grunt' folders from being modified
*
* @param {String} url
* @return {Boolean}
*/
function canModify(url) {
return url.match(/^\/(\.grunt)|(dev\/tests)|(dev\\tests)|(_SpecRunner\.html)/) === null;
}

return { configure: buildConfig };
};
Loading

0 comments on commit 5b1aef1

Please sign in to comment.