Skip to content

Commit 5b1aef1

Browse files
committed
Merge pull request #71 from magento-vanilla/MAGETWO-31592
[Vanilla] MAGETWO-31592 - Javascript Unit Test Framework Update
2 parents 3e8e126 + 51dca39 commit 5b1aef1

File tree

12 files changed

+396
-67
lines changed

12 files changed

+396
-67
lines changed

Gruntfile.js

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@
44

55
// For performance use one level down: 'name/{,*/}*.js'
66
// If you want to recursively match all subfolders, use: 'name/**/*.js'
7-
8-
'use strict';
9-
107
module.exports = function (grunt) {
8+
'use strict';
119

1210
// Required plugins
1311
// _____________________________________________
12+
var specRunner = require('./dev/tests/js/framework/spec_runner')(grunt);
1413

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

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

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

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

@@ -42,7 +43,8 @@ module.exports = function (grunt) {
4243
uglify: {
4344
legacy: 'lib/web/legacy-build.min.js'
4445
},
45-
doc: 'lib/web/css/docs'
46+
doc: 'lib/web/css/docs',
47+
spec: 'dev/tests/js/spec'
4648
};
4749

4850
// Define Themes
@@ -459,8 +461,38 @@ module.exports = function (grunt) {
459461
'<%= path.doc %>': '<%= path.doc %>/source'
460462
}
461463
}
462-
}
464+
},
465+
466+
specRunner: {
467+
options: {
468+
shareDir: 'base'
469+
},
470+
backend: {
471+
options: {
472+
port: 8000,
473+
areaDir: 'adminhtml',
474+
theme: 'backend'
475+
}
476+
},
477+
frontend: {
478+
options: {
479+
port: 3000,
480+
areaDir: 'frontend',
481+
theme: 'blank'
482+
}
483+
}
484+
},
463485

486+
jasmine: {
487+
'options': {
488+
template: require('grunt-template-jasmine-requirejs'),
489+
ignoreEmpty: true
490+
},
491+
'backend-unit': specRunner.configure('unit', 'adminhtml', 8000),
492+
'backend-integration': specRunner.configure('integration', 'adminhtml', 8000),
493+
'frontend-unit': specRunner.configure('unit', 'frontend', 3000),
494+
'frontend-integration': specRunner.configure('integration', 'frontend', 3000)
495+
}
464496
});
465497

466498
// Assembling tasks
@@ -514,4 +546,21 @@ module.exports = function (grunt) {
514546
}
515547
});
516548

549+
// Tests
550+
// ---------------------------------------------
551+
552+
grunt.registerTask('spec', [
553+
'specRunner:backend',
554+
'specRunner:frontend'
555+
]);
556+
557+
grunt.registerTask('unit', [
558+
'jasmine:backend-unit',
559+
'jasmine:frontend-unit'
560+
]);
561+
562+
grunt.registerTask('integration', [
563+
'jasmine:backend-integration',
564+
'jasmine:frontend-integration'
565+
]);
517566
};

dev/tests/js/framework/spec_runner.js

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/**
2+
* Copyright © 2015 Magento. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
/**
6+
* Creates jasmine configuration object
7+
*
8+
* @param {String} type - type of tests
9+
* @param {String} dir - area dir
10+
* @param {Number} port - port to run on
11+
* @return {Object}
12+
*/
13+
function buildConfig(type, dir, port) {
14+
'use strict';
15+
16+
return {
17+
src: '<%= path.spec %>/shim.js',
18+
options: {
19+
host: 'http://localhost:' + port,
20+
specs: '<%= path.spec %>/' + type + '/**/' + dir + '/**/*.js',
21+
templateOptions: {
22+
requireConfigFile: [
23+
'<%= path.spec %>/require.config.js',
24+
'<%= path.spec %>/' + type + '/config/' + dir + '.js'
25+
]
26+
}
27+
}
28+
};
29+
}
30+
31+
module.exports = function (grunt) {
32+
'use strict';
33+
34+
var connect = require('connect'),
35+
logger = require('morgan'),
36+
serveStatic = require('serve-static'),
37+
fs = require('fs'),
38+
root;
39+
40+
root = __dirname
41+
.replace('/dev/tests/js/framework', '')
42+
.replace('\\dev\\tests\\js\\framework', '');
43+
44+
grunt.registerMultiTask('specRunner', function () {
45+
var app = connect(),
46+
options,
47+
area,
48+
theme,
49+
share,
50+
middlewares;
51+
52+
options = this.options({
53+
port: 3000,
54+
theme: 'blank',
55+
areaDir: 'adminhtml',
56+
shareDir: 'base',
57+
enableLogs: false,
58+
middleware: null
59+
});
60+
61+
area = options.areaDir;
62+
share = options.shareDir;
63+
theme = options.theme;
64+
65+
if (options.enableLogs) {
66+
app.use(logger('dev'));
67+
}
68+
69+
app.use(function (req, res, next) {
70+
var url = req.url,
71+
match = url.match(/^\/([A-Z][^\/]+)_(\w+)\/(.+)$/),
72+
vendor,
73+
module,
74+
path,
75+
getModuleUrl,
76+
getThemeUrl;
77+
78+
/**
79+
* Returns path to theme root folder
80+
*
81+
* @return {String}
82+
*/
83+
function themeRoot() {
84+
return [
85+
'/app/design',
86+
area,
87+
vendor,
88+
theme
89+
].join('/');
90+
}
91+
92+
/**
93+
* Based on 'thematic' parameter, returnes either path to theme's lib,
94+
* or 'lib/web'.
95+
*
96+
* @param {Boolean} thematic
97+
* @return {String}
98+
*/
99+
function lib(thematic) {
100+
return thematic ? themeRoot() + '/web' : '/lib/web';
101+
}
102+
103+
if (match !== null) {
104+
vendor = match[1];
105+
module = match[2];
106+
path = match[3];
107+
108+
/**
109+
* Assembles modular path. If 'shared' flag provided and is truthy,
110+
* will use share dir instead of area one.
111+
*
112+
* @param {Boolean} shared
113+
* @return {String}
114+
*/
115+
getModuleUrl = function (shared) {
116+
return [
117+
'/app/code',
118+
vendor,
119+
module,
120+
'view',
121+
!!shared ? share : area,
122+
'web',
123+
path
124+
].join('/');
125+
};
126+
127+
/**
128+
* Assembles theme modular path.
129+
*
130+
* @return {String}
131+
*/
132+
getThemeUrl = function () {
133+
return [
134+
themeRoot(),
135+
vendor + '_' + module,
136+
'web',
137+
path
138+
].join('/');
139+
};
140+
141+
url = exists(url = getThemeUrl()) ?
142+
url :
143+
exists(url = getModuleUrl()) ?
144+
url : getModuleUrl(true);
145+
146+
} else if (canModify(url)) {
147+
url = (exists(url = lib(true)) ? url : lib()) + req.url;
148+
}
149+
150+
req.url = url;
151+
152+
next();
153+
});
154+
155+
if (options.middleware && typeof options.middleware === 'function') {
156+
middlewares = options.middleware(connect, options);
157+
158+
if (Array.isArray(middlewares)) {
159+
middlewares.forEach(function (middleware) {
160+
app.use(middleware);
161+
});
162+
}
163+
}
164+
165+
app.use(serveStatic(root));
166+
167+
app.listen(options.port);
168+
});
169+
170+
/**
171+
* Defines if passed file path exists
172+
*
173+
* @param {String} path
174+
* @return {Boolean}
175+
*/
176+
function exists(path) {
177+
return fs.existsSync(root + path);
178+
}
179+
180+
/**
181+
* Restricts url's which lead to '/_SpecRunner.html', '/dev/tests' or '.grunt' folders from being modified
182+
*
183+
* @param {String} url
184+
* @return {Boolean}
185+
*/
186+
function canModify(url) {
187+
return url.match(/^\/(\.grunt)|(dev\/tests)|(dev\\tests)|(_SpecRunner\.html)/) === null;
188+
}
189+
190+
return { configure: buildConfig };
191+
};

0 commit comments

Comments
 (0)