Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit 04edfe0

Browse files
committed
fix: upgraded qunit
1 parent 859945b commit 04edfe0

File tree

3 files changed

+55
-73
lines changed

3 files changed

+55
-73
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"@babel/preset-env": "^7.3.1",
1717
"eslint": "^3.9.1",
1818
"eslint-config-google": "^0.7.0",
19-
"qunit": "^0.7.7",
19+
"qunit": "^2.9.2",
2020
"request": "^2.65.0",
2121
"rimraf": "^2.6.3",
2222
"rollup": "^1.2.2",
@@ -27,7 +27,7 @@
2727
"rollup-plugin-uglify": "^6.0.2"
2828
},
2929
"scripts": {
30-
"test": "node tests/testrunner.js",
30+
"test": "qunit 'tests/**/*.js' -r tap",
3131
"pretest": "npm run build-package",
3232
"build": "npm run build-lib",
3333
"build-lib": "babel src --out-dir lib",

tests/testrunner.js

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

tests/tests.js

Lines changed: 53 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
var jsonLogic = require("../dist/jsonLogic.js");
21
var http = require("http");
32
var fs = require("fs");
3+
var jsonLogic = require("../dist/jsonLogic.js");
44

55
var download = function(url, dest, cb) {
66
var file = fs.createWriteStream(dest);
@@ -15,52 +15,57 @@ var download = function(url, dest, cb) {
1515
});
1616
};
1717

18-
var remote_or_cache = function (remote_url, local_file, description, runner){
19-
var parse_and_iterate = function(local_file, description, runner){
20-
fs.readFile(local_file, "utf8", function(error, body) {
21-
var tests;
22-
try{
23-
tests = JSON.parse(body);
24-
}catch(e) {
25-
throw new Error("Trouble parsing " + description + ": " + e.message);
26-
}
27-
28-
// Remove comments
29-
tests = tests.filter(function(test){ return typeof test !== 'string'; });
30-
31-
console.log("Including "+tests.length+" "+description);
32-
33-
QUnit.test(description, function(assert) {
34-
tests.map(runner);
18+
var remote_or_cache = function (remote_url, local_file, description, runner) {
19+
QUnit.test('Load and run remote tests form: ' + remote_url, function(assert) {
20+
assert.expect(0);
21+
// Only waiting on the request() is async
22+
var done = assert.async();
23+
24+
var parse_and_iterate = function (local_file, description, runner) {
25+
fs.readFile(local_file, "utf8", function (error, body) {
26+
var tests;
27+
try {
28+
tests = JSON.parse(body);
29+
} catch (e) {
30+
throw new Error("Trouble parsing " + description + ": " + e.message);
31+
}
32+
33+
// Remove comments
34+
tests = tests.filter(function (test) {
35+
return typeof test !== 'string';
36+
});
37+
38+
console.log("Including " + tests.length + " " + description);
39+
40+
QUnit.test(description, function (assert) {
41+
tests.map(function(test) {
42+
runner(test, assert);
43+
});
44+
});
45+
46+
done();
3547
});
36-
37-
start();
38-
});
39-
40-
};
41-
42-
// Only waiting on the request() is async
43-
stop();
44-
45-
fs.stat(local_file, function(err, stats) {
46-
if(err) {
47-
console.log("Downloading " + description + " from JsonLogic.com");
48-
download(remote_url, local_file, function() {
48+
};
49+
50+
fs.stat(local_file, function (err, stats) {
51+
if (err) {
52+
console.log("Downloading " + description + " from JsonLogic.com");
53+
download(remote_url, local_file, function () {
54+
parse_and_iterate(local_file, description, runner);
55+
});
56+
} else {
57+
console.log("Using cached " + description);
4958
parse_and_iterate(local_file, description, runner);
50-
});
51-
}else{
52-
console.log("Using cached " + description);
53-
parse_and_iterate(local_file, description, runner);
54-
}
59+
}
60+
});
5561
});
56-
5762
};
5863

5964
remote_or_cache(
6065
"http://jsonlogic.com/tests.json",
6166
"tests/tests.json",
6267
"applies() tests",
63-
function(test){
68+
function(test, assert){
6469
var rule = test[0];
6570
var data = test[1];
6671
var expected = test[2];
@@ -69,8 +74,8 @@ remote_or_cache(
6974
jsonLogic.apply(rule, data),
7075
expected,
7176
"jsonLogic.apply("+ JSON.stringify(rule) +"," +
72-
JSON.stringify(data) +") === " +
73-
JSON.stringify(expected)
77+
JSON.stringify(data) +") === " +
78+
JSON.stringify(expected)
7479
);
7580
}
7681
);
@@ -79,7 +84,7 @@ remote_or_cache(
7984
"http://jsonlogic.com/rule_like.json",
8085
"tests/rule_like.json",
8186
"rule_like() tests",
82-
function(test){
87+
function(test, assert){
8388
var rule = test[0];
8489
var pattern = test[1];
8590
var expected = test[2];
@@ -88,17 +93,12 @@ remote_or_cache(
8893
jsonLogic.rule_like(rule, pattern),
8994
expected,
9095
"jsonLogic.rule_like("+ JSON.stringify(rule) +"," +
91-
JSON.stringify(pattern) +") === " +
92-
JSON.stringify(expected)
96+
JSON.stringify(pattern) +") === " +
97+
JSON.stringify(expected)
9398
);
9499
}
95100
);
96101

97-
98-
99-
100-
101-
102102
QUnit.test( "Bad operator", function( assert ) {
103103
assert.throws(
104104
function() {
@@ -108,11 +108,13 @@ QUnit.test( "Bad operator", function( assert ) {
108108
);
109109
});
110110

111-
112111
QUnit.test( "logging", function( assert ) {
113112
var last_console;
113+
var log = console.log;
114114
console.log = function(logged) {
115115
last_console = logged;
116+
117+
log(logged);
116118
};
117119
assert.equal( jsonLogic.apply({"log": [1]}), 1 );
118120
assert.equal( last_console, 1 );
@@ -172,7 +174,7 @@ QUnit.test( "Expanding functionality with add_operator", function( assert) {
172174
42
173175
);
174176

175-
//Remove operation:
177+
// Remove operation:
176178
jsonLogic.rm_operation("times");
177179

178180
assert.throws(
@@ -193,9 +195,6 @@ QUnit.test( "Expanding functionality with add_operator", function( assert) {
193195
),
194196
42
195197
);
196-
197-
198-
199198
});
200199

201200
QUnit.test( "Expanding functionality with method", function( assert) {
@@ -232,7 +231,7 @@ QUnit.test( "Expanding functionality with method", function( assert) {
232231
});
233232

234233

235-
QUnit.test("Control structures don't eval depth-first", function(assert) {
234+
QUnit.test( "Control structures don't eval depth-first", function(assert) {
236235
// Depth-first recursion was wasteful but not harmful until we added custom operations that could have side-effects.
237236

238237
// If operations run the condition, if truthy, it runs and returns that consequent.

0 commit comments

Comments
 (0)