Skip to content
This repository was archived by the owner on Apr 14, 2021. It is now read-only.

Commit 4d75e49

Browse files
committed
adding more sanity check to testing the polyfilling process in browsers
1 parent 2e31880 commit 4d75e49

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
"build:dist": "npm run build:dist:dev && npm run build:dist:prod",
7373
"build": "npm run build:data && npm run build:lib && npm run build:dist",
7474
"lint": "eslint .",
75-
"test": "cd tests && node sanity.js && node noderunner.js && node saucelabs.js",
75+
"test": "cd tests && node polyfilling.js && node sanity.js && node noderunner.js && node saucelabs.js",
7676
"pretest": "npm run lint",
7777
"preversion": "npm run clean && npm run build && npm run test",
7878
"prepublish": "npm run clean && npm run build"

tests/polyfilling.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const vm = require('vm');
2+
3+
function assert(value, expected, message) {
4+
console.log(message);
5+
if (value !== expected) {
6+
console.error(' > ERROR: expected value ' + expected + ' but the actual value is ' + value);
7+
process.exit(1);
8+
} else {
9+
console.log(' > PASSED');
10+
}
11+
}
12+
13+
const context = new vm.createContext();
14+
var script = new vm.Script('this');
15+
const window = script.runInContext(context);
16+
17+
const fs = require('fs');
18+
const code = fs.readFileSync(__dirname + '/../dist/Intl.js', 'utf8');
19+
20+
// first evaluation
21+
window.window = window; // circular, in case the polyfill uses window
22+
var originalIntl = window.Intl;
23+
var script = new vm.Script(code);
24+
script.runInContext(context);
25+
assert(typeof global.Intl, 'object', 'for this test to function, global.Intl is required');
26+
assert(typeof window.IntlPolyfill, 'object', 'polyfill should always add the custom global IntlPolyfill');
27+
assert(window.Intl, originalIntl, 'validating that the polyfilling process does not touch the original Intl value');
28+
29+
// second evaluation
30+
window.window = window; // circular, in case the polyfill uses window
31+
window.Intl = undefined; // disabling Intl
32+
var script = new vm.Script(code);
33+
script.runInContext(context);
34+
assert(window.Intl, window.IntlPolyfill, 'validating that the polyfilling process does patch Intl if it does not exist');

tests/sanity.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
var IntlPolyfill = require('../');
22

3-
function assert(value, expected) {
3+
function assert(value, expected, message) {
4+
console.log(message);
45
if (value !== expected) {
5-
console.log('expected value ' + expected + ' but the actual value is ' + value);
6+
console.error(' > ERROR: expected value ' + expected + ' but the actual value is ' + value);
67
process.exit(1);
8+
} else {
9+
console.log(' > PASSED');
710
}
811
}
912

1013
assert(new IntlPolyfill.NumberFormat('de-DE', {
1114
minimumFractionDigits: 2,
1215
maximumFractionDigits: 2
13-
}).format(0.015), "0,02");
16+
}).format(0.015), "0,02", 'fractional digits');
1417

1518
assert(new IntlPolyfill.NumberFormat('en-US', {
1619
style: 'currency',
1720
currency: 'GBP',
1821
minimumFractionDigits: 2,
19-
}).format(59.88), '£59.88');
22+
}).format(59.88), '£59.88', 'currency with fragtional digits');
2023

2124
assert(new IntlPolyfill.DateTimeFormat('en', {
2225
month:'numeric',
2326
day: 'numeric'
24-
}).format(new Date('2016/05/16')), '5/16');
27+
}).format(new Date('2016/05/16')), '5/16', 'month and day');

0 commit comments

Comments
 (0)