From 7bd0d9fb3a761d03ec18f62528d2e1b1a1c6f63e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20=C5=81aziuk?= Date: Fri, 2 Feb 2018 22:57:03 +0100 Subject: [PATCH] refactor tests to make it more readable --- service-tests/bundlephobia.js | 116 ++++++++++++++++------------------ 1 file changed, 56 insertions(+), 60 deletions(-) diff --git a/service-tests/bundlephobia.js b/service-tests/bundlephobia.js index fa2d6eaa105c8..d80a6cf30646e 100644 --- a/service-tests/bundlephobia.js +++ b/service-tests/bundlephobia.js @@ -15,68 +15,64 @@ const formats = { D: '/bundlephobia/:type/@:scope/:package/:version.:format', } -const withoutErrorsMin = { - A: '/min/preact.json', - B: '/min/preact/8.2.7.json', - C: '/min/@cycle/core.json', - D: '/min/@cycle/core/7.0.0.json', -} - -const withoutErrorsGzip = { - A: '/minzip/preact.json', - B: '/minzip/preact/8.2.7.json', - C: '/minzip/@cycle/core.json', - D: '/minzip/@cycle/core/7.0.0.json', -} - -const withoutErrorSizesMin = { - A: isFileSize, - B: '8.94 kB', - C: isFileSize, - D: '3.51 kB', -} - -const withoutErrorSizesGzip = { - A: isFileSize, - B: '3.58 kB', - C: isFileSize, - D: '1.23 kB', -} - -const noExistPackages = { - A: '/min/some-no-exist.json', - C: '/min/@some-no-exist/some-no-exist.json', -} - -Object.keys(formats).forEach(format => { - const withoutErrorMin = withoutErrorsMin[format] - const withoutErrorSizeMin = withoutErrorSizesMin[format] - - const withoutErrorGzip = withoutErrorsGzip[format] - const withoutErrorSizeGzip = withoutErrorSizesGzip[format] - - const noExistPackage = noExistPackages[format] - - if (typeof withoutErrorMin === 'string') { - t.create(`Format '${formats[format]}' to get the minified bundle size`) - .get(withoutErrorMin) - .expectJSONTypes(Joi.object().keys({ name: 'minified size', value: withoutErrorSizeMin })) - } - - if (typeof withoutErrorGzip === 'string') { - t.create(`Format '${formats[format]}' to get the gzipped bundle size`) - .get(withoutErrorGzip) - .expectJSONTypes(Joi.object().keys({ name: 'minzipped size', value: withoutErrorSizeGzip })) +const data = [ + { + format: formats.A, + get: '/min/preact.json', + expect: { name: 'minified size', value: isFileSize }, + }, + { + format: formats.B, + get: '/min/preact/8.0.0.json', + expect: { name: 'minified size', value: '7.94 kB' }, + }, + { + format: formats.C, + get: '/min/@cycle/core.json', + expect: { name: 'minified size', value: isFileSize }, + }, + { + format: formats.D, + get: '/min/@cycle/core/7.0.0.json', + expect: { name: 'minified size', value: '3.51 kB' }, + }, + { + format: formats.A, + get: '/minzip/preact.json', + expect: { name: 'minzipped size', value: isFileSize }, + }, + { + format: formats.B, + get: '/minzip/preact/8.0.0.json', + expect: { name: 'minzipped size', value: '3.35 kB' }, + }, + { + format: formats.C, + get: '/minzip/@cycle/core.json', + expect: { name: 'minzipped size', value: isFileSize }, + }, + { + format: formats.D, + get: '/minzip/@cycle/core/7.0.0.json', + expect: { name: 'minzipped size', value: '1.23 kB' }, + }, + { + format: formats.A, + get: '/min/some-no-exist.json', + expect: { name: 'minified size', value: 'package not found error' }, + }, + { + format: formats.C, + get: '/min/@some-no-exist/some-no-exist.json', + expect: { name: 'minified size', value: 'package not found error' }, } +] - if (typeof noExistPackage === 'string') { - t.create(`Format '${formats[format]}' where it doesn't exist`) - .get(noExistPackage) - .expectJSON({ - name: 'minified size', - value: 'package not found error', - }) +data.forEach( ({format, get, expect }) => { + t.create(`Testing format '${format}' against '${get}'`) + .get(get) + .expectJSONTypes(Joi.object().keys(expect)) } -}) +) module.exports = t