Skip to content

Commit af269f4

Browse files
committed
Merge pull request #1 from SomMeri/jasmine
Refactoring browser unit tests into grunt-contrib-jasmine
2 parents 38a8664 + 3722ff0 commit af269f4

39 files changed

+362
-424
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ test/browser/less.js
1616
test/browser/test-runner-*.htm
1717
test/sourcemaps/*.map
1818
test/sourcemaps/*.css
19+
20+
# grunt
21+
.grunt

Gruntfile.js

Lines changed: 107 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ module.exports = function(grunt) {
2828
test: {
2929
command: 'node test/less-test.js'
3030
},
31-
browser: {
32-
command: 'node test/browser-test-prepare.js'
33-
},
34-
phantom: {
35-
command: 'phantomjs test/browser/phantom-runner.js'
36-
},
3731
benchmark: {
3832
command: 'node benchmark/less-benchmark.js'
3933
}
@@ -115,6 +109,105 @@ module.exports = function(grunt) {
115109
}
116110
},
117111

112+
connect: {
113+
server: {
114+
options: {
115+
port: 8081
116+
}
117+
}
118+
},
119+
120+
jasmine: {
121+
options: {
122+
keepRunner: true,
123+
host: 'http://localhost:8081/',
124+
vendor: 'test/browser/common.js',
125+
template: 'test/browser/test-runner-template.tmpl'
126+
},
127+
main: {
128+
//src is used to build list of less files to compile
129+
src: ['test/less/*.less', '!test/less/javascript.less', '!test/less/urls.less'],
130+
options: {
131+
helpers: 'test/browser/runner-main-options.js',
132+
specs: 'test/browser/runner-main-spec.js',
133+
outfile: 'test/browser/test-runner-main.html'
134+
}
135+
},
136+
legacy: {
137+
src: ['test/less/legacy/*.less'],
138+
options: {
139+
helpers: 'test/browser/runner-legacy-options.js',
140+
specs: 'test/browser/runner-legacy-spec.js',
141+
outfile: 'test/browser/test-runner-legacy.html'
142+
}
143+
},
144+
errors: {
145+
src: ['test/less/errors/*.less', '!test/less/errors/javascript-error.less'],
146+
options: {
147+
timeout: 20000,
148+
helpers: 'test/browser/runner-errors-options.js',
149+
specs: 'test/browser/runner-errors-spec.js',
150+
outfile: 'test/browser/test-runner-errors.html'
151+
}
152+
},
153+
noJsErrors: {
154+
src: ['test/less/no-js-errors/*.less'],
155+
options: {
156+
helpers: 'test/browser/runner-no-js-errors-options.js',
157+
specs: 'test/browser/runner-no-js-errors-spec.js',
158+
outfile: 'test/browser/test-runner-no-js-errors.html'
159+
}
160+
},
161+
browser: {
162+
src: ['test/browser/less/*.less'],
163+
options: {
164+
helpers: 'test/browser/runner-browser-options.js',
165+
specs: 'test/browser/runner-browser-spec.js',
166+
outfile: 'test/browser/test-runner-browser.html'
167+
}
168+
},
169+
relativeUrls: {
170+
src: ['test/browser/less/relative-urls/*.less'],
171+
options: {
172+
helpers: 'test/browser/runner-relative-urls-options.js',
173+
specs: 'test/browser/runner-relative-urls-spec.js',
174+
outfile: 'test/browser/test-runner-relative-urls.html'
175+
}
176+
},
177+
rootpath: {
178+
src: ['test/browser/less/rootpath/*.less'],
179+
options: {
180+
helpers: 'test/browser/runner-rootpath-options.js',
181+
specs: 'test/browser/runner-rootpath-spec.js',
182+
outfile: 'test/browser/test-runner-rootpath.html'
183+
}
184+
},
185+
rootpathRelative: {
186+
src: ['test/browser/less/rootpath-relative/*.less'],
187+
options: {
188+
helpers: 'test/browser/runner-rootpath-relative-options.js',
189+
specs: 'test/browser/runner-rootpath-relative-spec.js',
190+
outfile: 'test/browser/test-runner-rootpath-relative.html'
191+
}
192+
},
193+
production: {
194+
src: ['test/browser/less/production/*.less'],
195+
options: {
196+
helpers: 'test/browser/runner-production-options.js',
197+
specs: 'test/browser/runner-production-spec.js',
198+
outfile: 'test/browser/test-runner-production.html'
199+
}
200+
},
201+
modifyVars: {
202+
src: ['test/browser/less/modify-vars/*.less'],
203+
options: {
204+
helpers: 'test/browser/runner-modify-vars-options.js',
205+
specs: 'test/browser/runner-modify-vars-spec.js',
206+
outfile: 'test/browser/test-runner-modify-vars.html'
207+
}
208+
}
209+
},
210+
118211
// Before running tests, clean out the results
119212
// of any previous tests. (this will need to be
120213
// setup based on configuration of browser tests.
@@ -162,13 +255,18 @@ module.exports = function(grunt) {
162255
'uglify:beta'
163256
]);
164257

258+
// Run all tests
259+
grunt.registerTask('browserTest', [
260+
'connect:server',
261+
'jasmine'
262+
]);
263+
165264
// Run all tests
166265
grunt.registerTask('test', [
167266
'jshint:lib',
168267
'clean',
169-
'shell:test'
170-
// 'shell:browser',
171-
// 'shell:phantom'
268+
'shell:test',
269+
'browserTest'
172270
]);
173271

174272
// Run benchmark

build/build.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ lib: lib/less
1919
# General
2020
# =================================
2121
prepend:
22-
browser: build/require.js
22+
browser: [build/require.js, build/browser-header.js]
2323
rhino: build/require-rhino.js
2424

2525
append:
@@ -45,6 +45,8 @@ less:
4545
to_css : <%= build.lib %>/to-css-visitor.js
4646
extend : <%= build.lib %>/extend-visitor.js
4747
browser : <%= build.lib %>/browser.js
48+
source_map_output : <%= build.lib %>/source-map-output.js
49+
to_css_visitor : <%= build.lib %>/to-css-visitor.js
4850

4951
# glob all files in ./lib/less/tree directory
5052
treedir : <%= build.lib %>/tree/*.js
@@ -72,6 +74,8 @@ browser:
7274
- <%= build.less.join %>
7375
- <%= build.less.to_css %>
7476
- <%= build.less.extend %>
77+
- <%= build.less.source_map_output %>
78+
- <%= build.less.to_css_visitor %>
7579

7680
# append browser-specific code
7781
- <%= build.append.browser %>
@@ -99,6 +103,8 @@ rhino:
99103
- <%= build.less.colors %>
100104
- <%= build.less.treedir %> # glob all files
101105
- <%= build.less.tree %>
106+
- <%= build.less.source_map_output %>
107+
- <%= build.less.to_css_visitor %>
102108

103109
# append rhino-specific code
104110
- <%= build.append.rhino %>
@@ -143,4 +149,4 @@ tree:
143149
- <%= build.lib %>/tree/unicode-descriptor.js
144150
- <%= build.lib %>/tree/url.js
145151
- <%= build.lib %>/tree/value.js
146-
- <%= build.lib %>/tree/variable.js
152+
- <%= build.lib %>/tree/variable.js

dist/less-1.4.2.min.js

Lines changed: 13 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,16 @@
4141
"devDependencies": {
4242
"diff": "~1.0",
4343
"jshint": "~2.1.4",
44-
"http-server": "~0.5.5"
44+
"http-server": "~0.5.5",
45+
"grunt": "~0.4.1",
46+
"matchdep": "~0.1.2",
47+
"grunt-contrib-clean": "~0.5.0",
48+
"grunt-contrib-concat": "~0.3.0",
49+
"grunt-contrib-jshint": "~0.6.0",
50+
"grunt-contrib-uglify": "~0.2.2",
51+
"grunt-shell": "~0.3.1",
52+
"grunt-contrib-connect": "~0.3.0",
53+
"grunt-contrib-jasmine": "~0.5.1"
4554
},
4655
"keywords": [
4756
"compile less",

test/browser-test-prepare.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

test/browser/common.js

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
/*if not async then phantomjs fails to run the webserver and the test concurrently*/
2-
var less = { async: true, strictMath: true };
3-
41
/* record log messages for testing */
2+
var logAllIds = function() {
3+
var allTags = document.head.getElementsByTagName('style');
4+
var ids = [];
5+
for (var tg = 0; tg< allTags.length; tg++) {
6+
var tag = allTags[tg];
7+
if (tag.id) {
8+
console.log(tag.id);
9+
}
10+
}
11+
};
12+
513
var logMessages = [],
614
realConsoleLog = console.log;
715
console.log = function(msg) {
@@ -33,11 +41,23 @@ var testSheet = function(sheet) {
3341
it(sheet.id + " should match the expected output", function() {
3442
var lessOutputId = sheet.id.replace("original-", ""),
3543
expectedOutputId = "expected-" + lessOutputId,
36-
lessOutput = document.getElementById(lessOutputId).innerText,
44+
lessOutputObj,
45+
lessOutput,
3746
expectedOutputHref = document.getElementById(expectedOutputId).href,
3847
expectedOutput = loadFile(expectedOutputHref);
3948

49+
// Browser spec generates less on the fly, so we need to loose control
4050
waitsFor(function() {
51+
lessOutputObj = document.getElementById(lessOutputId);
52+
// the type condition is necessary because of inline browser tests
53+
return lessOutputObj!==null && lessOutputObj.type==="text/css";
54+
}, "generation of " + lessOutputId + "", 700);
55+
56+
runs(function() {
57+
lessOutput = lessOutputObj.innerText;
58+
});
59+
60+
waitsFor(function() {
4161
return expectedOutput.loaded;
4262
}, "failed to load expected outout", 10000);
4363

@@ -48,23 +68,37 @@ var testSheet = function(sheet) {
4868
});
4969
};
5070

71+
//TODO: do it cleaner - the same way as in css
72+
function extractId(href) {
73+
return href.replace(/^[a-z-]+:\/+?[^\/]+/, '' ) // Remove protocol & domain
74+
.replace(/^\//, '' ) // Remove root /
75+
.replace(/\.[a-zA-Z]+$/, '' ) // Remove simple extension
76+
.replace(/[^\.\w-]+/g, '-') // Replace illegal characters
77+
.replace(/\./g, ':'); // Replace dots with colons(for valid id)
78+
}
79+
5180
var testErrorSheet = function(sheet) {
5281
it(sheet.id + " should match an error", function() {
5382
var lessHref = sheet.href,
54-
id = sheet.id.replace(/^original-less:/, "less-error-message:"),
83+
id = "less-error-message:"+extractId(lessHref),
84+
// id = sheet.id.replace(/^original-less:/, "less-error-message:"),
5585
errorHref = lessHref.replace(/.less$/, ".txt"),
5686
errorFile = loadFile(errorHref),
57-
actualErrorElement = document.getElementById(id),
87+
actualErrorElement,
5888
actualErrorMsg;
5989

60-
describe("the error", function() {
61-
expect(actualErrorElement).not.toBe(null);
62-
});
63-
64-
actualErrorMsg = actualErrorElement.innerText
90+
// Less.js sets 10ms timer in order to add error message on top of page.
91+
waitsFor(function() {
92+
actualErrorElement = document.getElementById(id);
93+
return actualErrorElement!==null;
94+
}, "error message was not generated", 70);
95+
96+
runs(function() {
97+
actualErrorMsg = actualErrorElement.innerText
6598
.replace(/\n\d+/g, function(lineNo) { return lineNo + " "; })
6699
.replace(/\n\s*in /g, " in ")
67100
.replace("\n\n", "\n");
101+
});
68102

69103
waitsFor(function() {
70104
return errorFile.loaded;
@@ -123,4 +157,4 @@ var loadFile = function(href) {
123157
}, 3000);
124158
}
125159

126-
})();
160+
})();
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
@import "http://localhost:8081/browser/less/imports/modify-this.css";
1+
@import "http://localhost:8081/test/browser/less/imports/modify-this.css";
22

3-
@import "http://localhost:8081/browser/less/imports/modify-again.css";
3+
@import "http://localhost:8081/test/browser/less/imports/modify-again.css";
44
.modify {
5-
my-url: url("http://localhost:8081/browser/less/imports/a.png");
5+
my-url: url("http://localhost:8081/test/browser/less/imports/a.png");
66
}
77
.modify {
8-
my-url: url("http://localhost:8081/browser/less/imports/b.png");
8+
my-url: url("http://localhost:8081/test/browser/less/imports/b.png");
99
}
1010
@font-face {
1111
src: url("/fonts/garamond-pro.ttf");
12-
src: local(Futura-Medium), url(http://localhost:8081/browser/less/relative-urls/fonts.svg#MyGeometricModern) format("svg");
12+
src: local(Futura-Medium), url(http://localhost:8081/test/browser/less/relative-urls/fonts.svg#MyGeometricModern) format("svg");
1313
}
1414
#shorthands {
1515
background: url("http://www.lesscss.org/spec.html") no-repeat 0 4px;
1616
}
1717
#misc {
18-
background-image: url(http://localhost:8081/browser/less/relative-urls/images/image.jpg);
18+
background-image: url(http://localhost:8081/test/browser/less/relative-urls/images/image.jpg);
1919
}
2020
#data-uri {
2121
background: url(data:image/png;charset=utf-8;base64,
@@ -29,8 +29,8 @@
2929
background: transparent url('data:image/svg+xml, <svg version="1.1"><g></g></svg>');
3030
}
3131
.comma-delimited {
32-
background: url(http://localhost:8081/browser/less/relative-urls/bg.jpg) no-repeat, url(http://localhost:8081/browser/less/relative-urls/bg.png) repeat-x top left, url(http://localhost:8081/browser/less/relative-urls/bg);
32+
background: url(http://localhost:8081/test/browser/less/relative-urls/bg.jpg) no-repeat, url(http://localhost:8081/test/browser/less/relative-urls/bg.png) repeat-x top left, url(http://localhost:8081/test/browser/less/relative-urls/bg);
3333
}
3434
.values {
35-
url: url('http://localhost:8081/browser/less/relative-urls/Trebuchet');
35+
url: url('http://localhost:8081/test/browser/less/relative-urls/Trebuchet');
3636
}

test/browser/css/rootpath-relative/urls.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@import "https://www.github.com/cloudhead/imports/modify-this.css";
2+
23
@import "https://www.github.com/cloudhead/imports/modify-again.css";
34
.modify {
45
my-url: url("https://www.github.com/cloudhead/imports/a.png");

0 commit comments

Comments
 (0)