From 11b85027bb67b5df85e704e15adff1725bffcde0 Mon Sep 17 00:00:00 2001 From: Rik Smale <13023439+WikiRik@users.noreply.github.com> Date: Tue, 25 Oct 2022 18:08:33 +0000 Subject: [PATCH] refactor: change test files to prepare for #1874 --- package.json | 2 +- test/{client-side.js => clientSide.test.js} | 0 test/{exports.js => exports.test.js} | 0 test/{sanitizers.js => sanitizers.test.js} | 0 test/testFunctions.js | 56 +++++++++++++++++ test/{util.js => util.test.js} | 0 test/{validators.js => validators.test.js} | 70 +++------------------ 7 files changed, 67 insertions(+), 61 deletions(-) rename test/{client-side.js => clientSide.test.js} (100%) rename test/{exports.js => exports.test.js} (100%) rename test/{sanitizers.js => sanitizers.test.js} (100%) create mode 100644 test/testFunctions.js rename test/{util.js => util.test.js} (100%) rename test/{validators.js => validators.test.js} (99%) diff --git a/package.json b/package.json index 7d505205e..13768746f 100644 --- a/package.json +++ b/package.json @@ -66,7 +66,7 @@ "build:node": "babel src -d .", "build": "run-p build:*", "pretest": "npm run build && npm run lint", - "test": "nyc --reporter=cobertura --reporter=text-summary mocha --require @babel/register --reporter dot" + "test": "nyc --reporter=cobertura --reporter=text-summary mocha --require @babel/register --reporter dot --recursive" }, "engines": { "node": ">= 0.10" diff --git a/test/client-side.js b/test/clientSide.test.js similarity index 100% rename from test/client-side.js rename to test/clientSide.test.js diff --git a/test/exports.js b/test/exports.test.js similarity index 100% rename from test/exports.js rename to test/exports.test.js diff --git a/test/sanitizers.js b/test/sanitizers.test.js similarity index 100% rename from test/sanitizers.js rename to test/sanitizers.test.js diff --git a/test/testFunctions.js b/test/testFunctions.js new file mode 100644 index 000000000..bcd7c15b0 --- /dev/null +++ b/test/testFunctions.js @@ -0,0 +1,56 @@ +import assert from 'assert'; +import { format } from 'util'; +import validator from '../src/index'; + +export default function test(options) { + const args = options.args || []; + + args.unshift(null); + + if (options.error) { + options.error.forEach((error) => { + args[0] = error; + + try { + assert.throws(() => validator[options.validator](...args)); + } catch (err) { + const warning = format( + 'validator.%s(%s) passed but should error', + options.validator, args.join(', ') + ); + + throw new Error(warning); + } + }); + } + + if (options.valid) { + options.valid.forEach((valid) => { + args[0] = valid; + + if (validator[options.validator](...args) !== true) { + const warning = format( + 'validator.%s(%s) failed but should have passed', + options.validator, args.join(', ') + ); + + throw new Error(warning); + } + }); + } + + if (options.invalid) { + options.invalid.forEach((invalid) => { + args[0] = invalid; + + if (validator[options.validator](...args) !== false) { + const warning = format( + 'validator.%s(%s) passed but should have failed', + options.validator, args.join(', ') + ); + + throw new Error(warning); + } + }); + } +} diff --git a/test/util.js b/test/util.test.js similarity index 100% rename from test/util.js rename to test/util.test.js diff --git a/test/validators.js b/test/validators.test.js similarity index 99% rename from test/validators.js rename to test/validators.test.js index c0e36bec7..4178c4610 100644 --- a/test/validators.js +++ b/test/validators.test.js @@ -3,60 +3,10 @@ import fs from 'fs'; import { format } from 'util'; import vm from 'vm'; import validator from '../src/index'; +import test from './testFunctions'; let validator_js = fs.readFileSync(require.resolve('../validator.js')).toString(); -function test(options) { - let args = options.args || []; - args.unshift(null); - if (options.error) { - options.error.forEach((error) => { - args[0] = error; - try { - assert.throws(() => validator[options.validator](...args)); - } catch (err) { - let warning = format( - 'validator.%s(%s) passed but should error', - options.validator, args.join(', ') - ); - throw new Error(warning); - } - }); - } - if (options.valid) { - options.valid.forEach((valid) => { - args[0] = valid; - if (validator[options.validator](...args) !== true) { - let warning = format( - 'validator.%s(%s) failed but should have passed', - options.validator, args.join(', ') - ); - throw new Error(warning); - } - }); - } - if (options.invalid) { - options.invalid.forEach((invalid) => { - args[0] = invalid; - if (validator[options.validator](...args) !== false) { - let warning = format( - 'validator.%s(%s) passed but should have failed', - options.validator, args.join(', ') - ); - throw new Error(warning); - } - }); - } -} - -function repeat(str, count) { - let result = ''; - for (; count; count--) { - result += str; - } - return result; -} - describe('Validators', () => { it('should validate email addresses', () => { test({ @@ -74,9 +24,9 @@ describe('Validators', () => { '"foobar"@example.com', '" foo m端ller "@example.com', '"foo\\@bar"@example.com', - `${repeat('a', 64)}@${repeat('a', 63)}.com`, - `${repeat('a', 64)}@${repeat('a', 63)}.com`, - `${repeat('a', 31)}@gmail.com`, + `${'a'.repeat(64)}@${'a'.repeat(63)}.com`, + `${'a'.repeat(64)}@${'a'.repeat(63)}.com`, + `${'a'.repeat(31)}@gmail.com`, 'test@gmail.com', 'test.1@gmail.com', 'test@1337.com', @@ -90,10 +40,10 @@ describe('Validators', () => { 'foo@bar.co.uk.', 'z@co.c', 'gmailgmailgmailgmailgmail@gmail.com', - `${repeat('a', 64)}@${repeat('a', 251)}.com`, - `${repeat('a', 65)}@${repeat('a', 250)}.com`, - `${repeat('a', 64)}@${repeat('a', 64)}.com`, - `${repeat('a', 64)}@${repeat('a', 63)}.${repeat('a', 63)}.${repeat('a', 63)}.${repeat('a', 58)}.com`, + `${'a'.repeat(64)}@${'a'.repeat(251)}.com`, + `${'a'.repeat(65)}@${'a'.repeat(250)}.com`, + `${'a'.repeat(64)}@${'a'.repeat(64)}.com`, + `${'a'.repeat(64)}@${'a'.repeat(63)}.${'a'.repeat(63)}.${'a'.repeat(63)}.${'a'.repeat(58)}.com`, 'test1@invalid.co m', 'test2@invalid.co m', 'test3@invalid.co m', @@ -128,10 +78,10 @@ describe('Validators', () => { 'foobar@gmail.com', 'foo.bar@gmail.com', 'foo.bar@googlemail.com', - `${repeat('a', 30)}@gmail.com`, + `${'a'.repeat(30)}@gmail.com`, ], invalid: [ - `${repeat('a', 31)}@gmail.com`, + `${'a'.repeat(31)}@gmail.com`, 'test@gmail.com', 'test.1@gmail.com', '.foobar@gmail.com',