diff --git a/package.json b/package.json index 04e2296f..6dc68c95 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,7 @@ "build:dist": "npm run build:dist:dev && npm run build:dist:prod", "build": "npm run build:data && npm run build:lib && npm run build:dist", "lint": "eslint .", - "test": "cd tests && node sanity.js && node noderunner.js && node saucelabs.js", + "test": "cd tests && node polyfilling.js && node sanity.js && node noderunner.js && node saucelabs.js", "pretest": "npm run lint", "preversion": "npm run clean && npm run build && npm run test", "prepublish": "npm run clean && npm run build" diff --git a/tests/polyfilling.js b/tests/polyfilling.js new file mode 100644 index 00000000..b7813ff6 --- /dev/null +++ b/tests/polyfilling.js @@ -0,0 +1,34 @@ +const vm = require('vm'); + +function assert(value, expected, message) { + console.log(message); + if (value !== expected) { + console.error(' > ERROR: expected value ' + expected + ' but the actual value is ' + value); + process.exit(1); + } else { + console.log(' > PASSED'); + } +} + +const context = new vm.createContext(); +var script = new vm.Script('this'); +const window = script.runInContext(context); + +const fs = require('fs'); +const code = fs.readFileSync(__dirname + '/../dist/Intl.js', 'utf8'); + +// first evaluation +window.window = window; // circular, in case the polyfill uses window +var originalIntl = window.Intl; +var script = new vm.Script(code); +script.runInContext(context); +assert(typeof global.Intl, 'object', 'for this test to function, global.Intl is required'); +assert(typeof window.IntlPolyfill, 'object', 'polyfill should always add the custom global IntlPolyfill'); +assert(window.Intl, originalIntl, 'validating that the polyfilling process does not touch the original Intl value'); + +// second evaluation +window.window = window; // circular, in case the polyfill uses window +window.Intl = undefined; // disabling Intl +var script = new vm.Script(code); +script.runInContext(context); +assert(window.Intl, window.IntlPolyfill, 'validating that the polyfilling process does patch Intl if it does not exist'); diff --git a/tests/sanity.js b/tests/sanity.js index 07737c8a..bbd0d42d 100644 --- a/tests/sanity.js +++ b/tests/sanity.js @@ -1,24 +1,27 @@ var IntlPolyfill = require('../'); -function assert(value, expected) { +function assert(value, expected, message) { + console.log(message); if (value !== expected) { - console.log('expected value ' + expected + ' but the actual value is ' + value); + console.error(' > ERROR: expected value ' + expected + ' but the actual value is ' + value); process.exit(1); + } else { + console.log(' > PASSED'); } } assert(new IntlPolyfill.NumberFormat('de-DE', { minimumFractionDigits: 2, maximumFractionDigits: 2 -}).format(0.015), "0,02"); +}).format(0.015), "0,02", 'fractional digits'); assert(new IntlPolyfill.NumberFormat('en-US', { style: 'currency', currency: 'GBP', minimumFractionDigits: 2, -}).format(59.88), '£59.88'); +}).format(59.88), '£59.88', 'currency with fragtional digits'); assert(new IntlPolyfill.DateTimeFormat('en', { month:'numeric', day: 'numeric' -}).format(new Date('2016/05/16')), '5/16'); +}).format(new Date('2016/05/16')), '5/16', 'month and day');