Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 27, 2019
1 parent c8be509 commit 8bb2ca7
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 17 additions & 0 deletions test/helpers/error.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
37 changes: 35 additions & 2 deletions test/main.js
Original file line number Diff line number Diff line change
@@ -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))
})
})
233 changes: 233 additions & 0 deletions test/snapshots/main.js.md
Original file line number Diff line number Diff line change
@@ -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,
],
]
Binary file added test/snapshots/main.js.snap
Binary file not shown.

0 comments on commit 8bb2ca7

Please sign in to comment.