From 8bb2ca7c9defafcfce008322ef165df6df4c3bc6 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Mon, 27 May 2019 10:00:00 +0200 Subject: [PATCH] Improve tests --- package.json | 3 +- test/helpers/error.js | 17 +++ test/main.js | 37 +++++- test/snapshots/main.js.md | 233 ++++++++++++++++++++++++++++++++++++ test/snapshots/main.js.snap | Bin 0 -> 1143 bytes 5 files changed, 287 insertions(+), 3 deletions(-) create mode 100644 test/helpers/error.js create mode 100644 test/snapshots/main.js.md create mode 100644 test/snapshots/main.js.snap diff --git a/package.json b/package.json index 417f825..f5a4d10 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,8 @@ "ava": "^1.4.1", "gulp": "^4.0.2", "gulp-shared-tasks": "^0.27.81", - "husky": "^2.3.0" + "husky": "^2.3.0", + "pretty-format": "^24.8.0" }, "engines": { "node": ">=8.12.0" diff --git a/test/helpers/error.js b/test/helpers/error.js new file mode 100644 index 0000000..9953188 --- /dev/null +++ b/test/helpers/error.js @@ -0,0 +1,17 @@ +// Make sure exceptions are snapshot as well +export const stringifyErrors = function(func) { + return addErrorHandler(func, String) +} + +// Wrap a function with a error handler +const addErrorHandler = function(func, errorHandler) { + return errorHandledFunc.bind(null, func, errorHandler) +} + +const errorHandledFunc = function(func, errorHandler, ...args) { + try { + return func(...args) + } catch (error) { + return errorHandler(error, ...args) + } +} diff --git a/test/main.js b/test/main.js index efca555..2b6d60d 100644 --- a/test/main.js +++ b/test/main.js @@ -1,7 +1,40 @@ import test from 'ava' +import prettyFormat from 'pretty-format' import fastCartesian from '../src/main.js' -test('Dummy test', t => { - t.is(typeof fastCartesian, 'function') +import { stringifyErrors } from './helpers/error.js' + +const eFastCartesian = stringifyErrors(fastCartesian) + +const generator = function*() { + yield 0 + yield 1 +} + +;[ + [], + [[]], + [[], []], + [undefined], + [null], + [[], true], + [[0]], + [[0], [1]], + [[0, 1]], + [[0, 1], [2]], + [[0, 1], [2, 3]], + // eslint-disable-next-line no-magic-numbers + [[0, 1, 2], [3, 4]], + [[[0]]], + [[0, undefined, 1]], + ['abc'], + [new Map([[{}, 0], [{}, 1]])], + [new Set([0, 1])], + [generator()], +].forEach(args => { + const title = prettyFormat(args, { min: true }) + test(title, t => { + t.snapshot(eFastCartesian(...args)) + }) }) diff --git a/test/snapshots/main.js.md b/test/snapshots/main.js.md new file mode 100644 index 0000000..42da365 --- /dev/null +++ b/test/snapshots/main.js.md @@ -0,0 +1,233 @@ +# Snapshot report for `test/main.js` + +The actual snapshot is saved in `main.js.snap`. + +Generated by [AVA](https://ava.li). + +## ["abc"] + +> Snapshot 1 + + [ + [ + 'a', + ], + [ + 'b', + ], + [ + 'c', + ], + ] + +## [Map {{} => 0, {} => 1}] + +> Snapshot 1 + + [ + [ + [ + {}, + 0, + ], + ], + [ + [ + {}, + 1, + ], + ], + ] + +## [Set {0, 1}] + +> Snapshot 1 + + [ + [ + 0, + ], + [ + 1, + ], + ] + +## [[0, 1, 2], [3, 4]] + +> Snapshot 1 + + [ + [ + 0, + 3, + ], + [ + 0, + 4, + ], + [ + 1, + 3, + ], + [ + 1, + 4, + ], + [ + 2, + 3, + ], + [ + 2, + 4, + ], + ] + +## [[0, 1], [2, 3]] + +> Snapshot 1 + + [ + [ + 0, + 2, + ], + [ + 0, + 3, + ], + [ + 1, + 2, + ], + [ + 1, + 3, + ], + ] + +## [[0, 1], [2]] + +> Snapshot 1 + + [ + [ + 0, + 2, + ], + [ + 1, + 2, + ], + ] + +## [[0, 1]] + +> Snapshot 1 + + [ + [ + 0, + ], + [ + 1, + ], + ] + +## [[0, undefined, 1]] + +> Snapshot 1 + + [ + [ + 0, + ], + [ + undefined, + ], + [ + 1, + ], + ] + +## [[0], [1]] + +> Snapshot 1 + + [ + [ + 0, + 1, + ], + ] + +## [[0]] + +> Snapshot 1 + + [ + [ + 0, + ], + ] + +## [[[0]]] + +> Snapshot 1 + + [ + [ + [ + 0, + ], + ], + ] + +## [[], []] + +> Snapshot 1 + + [] + +## [[], true] + +> Snapshot 1 + + 'TypeError: Argument must be iterable: true' + +## [[]] + +> Snapshot 1 + + [] + +## [] + +> Snapshot 1 + + [] + +## [null] + +> Snapshot 1 + + 'TypeError: Argument must be iterable: null' + +## [undefined] + +> Snapshot 1 + + 'TypeError: Argument must be iterable: undefined' + +## [{}] + +> Snapshot 1 + + [ + [ + 0, + ], + [ + 1, + ], + ] diff --git a/test/snapshots/main.js.snap b/test/snapshots/main.js.snap new file mode 100644 index 0000000000000000000000000000000000000000..6f3a299cc7064ea18d68d9190fb31fe945949090 GIT binary patch literal 1143 zcmV--1c>`VRzV+CY!?%ir*|G3GQd*++( z+&PcA_s;erzy#=i#GCedeMQaOdacZ`)q>bLk#iU+uzWXDL&K zJ#GeQt-9LM)_m#)+xMU3o6n?ufs8F115j1e^iV;dpk~dH*4>BZ#&siOUmgpvr84vC zfwbhWUOCxReezO602x~y2XLTG{Pyg|j!iQTH}9YR__-=%?4)r3vn}Puw$0N$z0L?(9c5Y`p~;TNe)?f|+#@$G}81jYngaEZfTw|F#km z5=K~Jgz-2hue?NBAj_-dbW4`J(qAlje3oMWb3ThhvbcPbYh@lCp(QRbxuMAr*WhZAPGf41N_^k ziV}^_5}xNhSc!{W9BZh?{-a+F%giKyI0t;u6>PS|JG9pW-mdveDqaxA!B-pDH ziM*mkB1b5GpBBHkjm$>EK28qeXC&w(^zicFwLplZ(h?2H!ZJ0c(m0=BB`hU8$;;Tl z@^on#R|hjDvt2ROiUbr>Wa-7DG9$n=ywcJ+kXC7{|aGH+K* zwaIzNwn3$mFOc~vp_pm@7Gi_|j0XdnsT_%%Y z7U5ouwz+9(S=k@0YZp6&-FHT~&yrUsH|08tq(Yz4te}~gm^kRJIsESbpMRqp|6i$? JOAc@k003NiHg5m` literal 0 HcmV?d00001