From 7717637d66fdf2870a1aabb3b4a84459f5b48ed1 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Tue, 12 Apr 2022 04:44:34 +0700 Subject: [PATCH] add some tests --- package.json | 6 +- tests/compat-tool.mjs | 122 +++++++++++++++++++++++++++++++++++++++ tests/targets-parser.mjs | 24 +++----- 3 files changed, 134 insertions(+), 18 deletions(-) create mode 100644 tests/compat-tool.mjs diff --git a/package.json b/package.json index b12978f54114..35ae8bfbc7c7 100644 --- a/package.json +++ b/package.json @@ -121,10 +121,12 @@ "test-entries-basic": "zx tests/commonjs.mjs", "test-entries-content": "zx tests/commonjs-entries-content.mjs", "test-entries-standalone": "run-s init test-entries", + "test-compat-tools": "run-p test-compat-tool test-targets-parser", + "test-compat-tool": "zx tests/compat-tool.mjs", "test-targets-parser": "zx tests/targets-parser.mjs", - "test": "run-s init test-lint bundle test-unit test-promises test-observables test-entries test-targets-parser check", + "test": "run-s init test-lint bundle test-unit test-promises test-observables test-entries test-compat-tools check", "ci-karma": "run-s init bundle test-unit-karma", - "ci-tests": "run-s init bundle test-unit-node test-promises test-observables test-entries test-targets-parser", + "ci-tests": "run-s init bundle test-unit-node test-promises test-observables test-entries test-compat-tools", "clean-dependencies": "node scripts/clean-dependencies.mjs", "refresh": "npm run clean-dependencies && npm it", "downloads": "zx scripts/downloads-by-versions.mjs", diff --git a/tests/compat-tool.mjs b/tests/compat-tool.mjs new file mode 100644 index 000000000000..3432c036d83c --- /dev/null +++ b/tests/compat-tool.mjs @@ -0,0 +1,122 @@ +import { deepStrictEqual } from 'assert'; +const compat = require('core-js-compat/compat'); + +deepStrictEqual(compat({ + modules: [ + 'core-js/es/math', + 'es.array.at', + /^es\.reflect/, + ], + exclude: [ + 'es.reflect.prevent-extensions', + ], + targets: 'firefox 27', +}), { + list: [ + 'es.array.at', + 'es.math.clz32', + 'es.math.expm1', + 'es.math.to-string-tag', + 'es.reflect.apply', + 'es.reflect.construct', + 'es.reflect.define-property', + 'es.reflect.delete-property', + 'es.reflect.get', + 'es.reflect.get-own-property-descriptor', + 'es.reflect.get-prototype-of', + 'es.reflect.has', + 'es.reflect.is-extensible', + 'es.reflect.own-keys', + 'es.reflect.set', + 'es.reflect.set-prototype-of', + 'es.reflect.to-string-tag', + ], + targets: { + 'es.array.at': { firefox: '27' }, + 'es.math.clz32': { firefox: '27' }, + 'es.math.expm1': { firefox: '27' }, + 'es.math.to-string-tag': { firefox: '27' }, + 'es.reflect.apply': { firefox: '27' }, + 'es.reflect.construct': { firefox: '27' }, + 'es.reflect.define-property': { firefox: '27' }, + 'es.reflect.delete-property': { firefox: '27' }, + 'es.reflect.get': { firefox: '27' }, + 'es.reflect.get-own-property-descriptor': { firefox: '27' }, + 'es.reflect.get-prototype-of': { firefox: '27' }, + 'es.reflect.has': { firefox: '27' }, + 'es.reflect.is-extensible': { firefox: '27' }, + 'es.reflect.own-keys': { firefox: '27' }, + 'es.reflect.set': { firefox: '27' }, + 'es.reflect.set-prototype-of': { firefox: '27' }, + 'es.reflect.to-string-tag': { firefox: '27' }, + }, +}, 'basic'); + +deepStrictEqual(compat({ + modules: [ + /^es\.math\.a/, + /^es\.math\.c/, + ], + exclude: 'es.math.asinh', +}), { + list: [ + 'es.math.acosh', + 'es.math.atanh', + 'es.math.cbrt', + 'es.math.clz32', + 'es.math.cosh', + ], + targets: { + 'es.math.acosh': {}, + 'es.math.atanh': {}, + 'es.math.cbrt': {}, + 'es.math.clz32': {}, + 'es.math.cosh': {}, + }, +}, 'no target'); + +deepStrictEqual(compat({ + modules: /^es\.math\.a/, +}), { + list: [ + 'es.math.acosh', + 'es.math.asinh', + 'es.math.atanh', + ], + targets: { + 'es.math.acosh': {}, + 'es.math.asinh': {}, + 'es.math.atanh': {}, + }, +}, 'no exclude'); + +deepStrictEqual( + compat({ targets: { chrome: 93 } }), + compat({ modules: 'core-js', targets: { chrome: 93 } }), + 'no modules', +); + +deepStrictEqual(compat({ + modules: 'core-js/es/math', + targets: { + chrome: 40, + firefox: 27, + }, +}), { + list: [ + 'es.math.acosh', + 'es.math.clz32', + 'es.math.expm1', + 'es.math.hypot', + 'es.math.to-string-tag', + ], + targets: { + 'es.math.acosh': { chrome: '40' }, + 'es.math.clz32': { firefox: '27' }, + 'es.math.expm1': { firefox: '27' }, + 'es.math.hypot': { chrome: '40' }, + 'es.math.to-string-tag': { chrome: '40', firefox: '27' }, + }, +}, 'some targets'); + +console.log(chalk.green('compat tool tested')); diff --git a/tests/targets-parser.mjs b/tests/targets-parser.mjs index 4c51482b0584..0381e7729812 100644 --- a/tests/targets-parser.mjs +++ b/tests/targets-parser.mjs @@ -1,14 +1,12 @@ import { deepStrictEqual } from 'assert'; const targetsParser = require('core-js-compat/targets-parser'); -// browserslist deepStrictEqual(targetsParser('ie 11, chrome 56, ios 12.2'), new Map([ ['chrome', '56'], ['ie', '11'], ['ios', '12.2-12.5'], -])); +]), 'browserslist'); -// targets object deepStrictEqual(targetsParser({ ie: 11, chrome: 56, @@ -17,16 +15,14 @@ deepStrictEqual(targetsParser({ ['chrome', '56'], ['ie', '11'], ['ios', '12.2'], -])); +]), 'targets object'); -// targets.browsers deepStrictEqual(targetsParser({ browsers: 'ie 11, chrome 56, ios_saf 12.2' }), new Map([ ['chrome', '56'], ['ie', '11'], ['ios', '12.2-12.5'], -])); +]), 'targets.browsers'); -// targets.esmodules deepStrictEqual(targetsParser({ esmodules: true }), new Map([ ['android', '61'], ['chrome', '61'], @@ -38,19 +34,16 @@ deepStrictEqual(targetsParser({ esmodules: true }), new Map([ ['opera_mobile', '45'], ['safari', '10.1'], ['samsung', '8.0'], -])); +]), 'targets.esmodules'); -// targets.node: current deepStrictEqual(targetsParser({ node: 'current' }), new Map([ ['node', String(process.versions.node)], -])); +]), 'targets.node: current'); -// targets.node: version deepStrictEqual(targetsParser({ node: '13.2' }), new Map([ ['node', '13.2'], -])); +]), 'targets.node: version'); -// normalization deepStrictEqual(targetsParser({ ie_mob: 11, chromeandroid: 56, @@ -65,9 +58,8 @@ deepStrictEqual(targetsParser({ ['ie', '11'], ['ios', '12.2'], ['opera_mobile', '40'], -])); +]), 'normalization'); -// mixed deepStrictEqual(targetsParser({ esmodules: true, node: 'current', @@ -97,6 +89,6 @@ deepStrictEqual(targetsParser({ ['opera_mobile', '40'], ['safari', '5.1'], ['samsung', '4'], -])); +]), 'mixed'); console.log(chalk.green('targets parser tested'));