Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Demo of input functions.
// This file can be directly run:
// - first install `test-each`
// - then `node node_modules/test-each/examples/functions.js`
// An online demo is also available at:
// https://repl.it/@ehmicky/test-each
'use strict'
// Ignore the following line: this is only needed for internal purposes.
require('./utils.js')
const { each } = require('test-each')
// Run callback with a different random number each time
each(['red', 'green', 'blue'], Math.random, (info, color, randomNumber) => {
console.log(color, randomNumber)
})
// Input functions are called with the same arguments as the callback
/* eslint-disable max-params */
each(
['02', '15', '30'],
['January', 'February', 'March'],
['1980', '1981'],
(info, day, month, year) => `${day}/${month}/${year}`,
(info, day, month, year, date) => {
console.log(date)
},
)
/* eslint-enable max-params */
each(
METHODS,
[{ opts: { env: {} } }, { opts: { env: { test: true } } }],
({ title }, methodProps, data) =>
test(`Valid options | ${title}`, async t => {
await snapshotTest({ t, methodProps, data })
}),
)
each(METHODS, [{}, { opts: {} }], ({ title }, methodProps, data) =>
test(`No options | ${title}`, async t => {
await snapshotTest({ t, methodProps, data })
}),
)
each(
METHODS,
[{ command: 'gulp --version', opts: { env: { PATH: '' } } }],
({ title }, methodProps, data) =>
test(`Default options | ${title}`, async t => {
await snapshotTest({ t, methodProps, data })
}),
)
import test from 'ava'
import { each } from 'test-each'
import { equal } from '../src/main.js'
import { callCli } from './helpers/cli.js'
import { BINARY_DATA } from './helpers/data/binary.js'
import { VALID_FULL_DATA } from './helpers/data/full/main.js'
each(BINARY_DATA, ({ title }, args) => {
test(`equal (JavaScript) | ${title}`, t => {
try {
t.snapshot(equal(...args))
} catch (error) {
t.snapshot(error)
}
})
test(`equal (CLI) | ${title}`, async t => {
t.snapshot(await callCli('equal', ...args))
})
})
each(VALID_FULL_DATA, ({ title }, arg) => {
test(`equal (self) | ${title}`, t => {
t.true(equal(arg, arg))
import test from 'ava'
import { each } from 'test-each'
import { EVENTS } from './helpers/events/main.js'
import { normalizeCall } from './helpers/normalize.js'
import { removeProcessListeners } from './helpers/remove.js'
const LOADERS = `${__dirname}/helpers/loaders/`
removeProcessListeners()
each(EVENTS, ({ title }, { eventName }) => {
test(`should work using the -r flag | ${title}`, async t => {
t.snapshot(
await normalizeCall(`node ${LOADERS}/register.js ${eventName}`, {
colors: false,
}),
)
})
test(`should work using both the -r flag and init() | ${title}`, async t => {
t.snapshot(await normalizeCall(`node ${LOADERS}/noop.js ${eventName}`))
})
})
import { inspect } from 'util'
import test from 'ava'
import { each } from 'test-each'
import hasAnsi from 'has-ansi'
import supportsColor from 'supports-color'
import { EVENTS } from './helpers/events/main.js'
import { startLogging } from './helpers/init.js'
import { removeProcessListeners } from './helpers/remove.js'
removeProcessListeners()
each(EVENTS, ({ title }, { eventName, emit }) => {
test.serial(`should colorize the error | ${title}`, async t => {
const { stopLogging, log } = startLogging({ log: 'spy', eventName })
await emit()
t.true(log.calledOnce)
t.false(hasAnsi(String(log.firstCall.args[0])))
t.false(hasAnsi(log.firstCall.args[0].stack))
t.is(hasAnsi(inspect(log.firstCall.args[0])), Boolean(supportsColor.stdout))
stopLogging()
})
test.serial(
`should allow disabling colorizing the error | ${title}`,
async t => {
import { LEVELS } from './helpers/level.js'
import { startLogging } from './helpers/init.js'
import { normalizeMessage } from './helpers/normalize.js'
import { removeProcessListeners } from './helpers/remove.js'
removeProcessListeners()
const snapshotArgs = function([error, level]) {
return [
normalizeMessage(inspect(error), { colors: false }),
String(error),
level,
]
}
each(EVENTS, ({ title }, { eventName, emit }) => {
test.serial(`should fire opts.log() | ${title}`, async t => {
const { stopLogging, log } = startLogging({ log: 'spy' })
t.true(log.notCalled)
await emit()
t.true(log.called)
stopLogging()
})
test.serial(`should fire opts.log() once | ${title}`, async t => {
const { stopLogging, log } = startLogging({ log: 'spy', eventName })
t.true(log.notCalled)
import test from 'ava'
import { each } from 'test-each'
import { set } from '../src/main.js'
import { callCli } from './helpers/cli.js'
import { BINARY_DATA } from './helpers/data/binary.js'
each(BINARY_DATA, ({ title }, args) => {
test(`set (JavaScript) | ${title}`, t => {
try {
t.snapshot(set(...args))
} catch (error) {
t.snapshot(error)
}
})
test(`set (CLI) | ${title}`, async t => {
t.snapshot(await callCli('set', ...args))
})
})
import { callCli } from './helpers/cli.js'
import { UNARY_DATA } from './helpers/data/unary.js'
import { VALID_FULL_DATA } from './helpers/data/full/main.js'
each(UNARY_DATA, ({ title }, arg) => {
test(`not (JavaScript) | ${title}`, t => {
t.snapshot(not(arg))
})
test(`not (CLI) | ${title}`, async t => {
t.snapshot(await callCli('not', arg))
})
})
each(VALID_FULL_DATA, ({ title }, arg) => {
test(`not (idempotence) | ${title}`, t => {
t.deepEqual(normalize(arg), not(not(arg)))
})
})
import test from 'ava'
import { each } from 'test-each'
import { convert } from '../src/main.js'
import { callCli } from './helpers/cli.js'
import { UNARY_DATA } from './helpers/data/unary.js'
import { TYPES } from './helpers/data/types.js'
each(TYPES, UNARY_DATA, ({ title }, type, arg) => {
test(`serialize (JavaScript) | ${title}`, t => {
t.snapshot(convert[type](arg))
})
test(`serialize (CLI) | ${title}`, async t => {
t.snapshot(await callCli(`convert.${type}`, arg))
})
})
import test from 'ava'
import { each } from 'test-each'
import { not, normalize } from '../src/main.js'
import { callCli } from './helpers/cli.js'
import { UNARY_DATA } from './helpers/data/unary.js'
import { VALID_FULL_DATA } from './helpers/data/full/main.js'
each(UNARY_DATA, ({ title }, arg) => {
test(`not (JavaScript) | ${title}`, t => {
t.snapshot(not(arg))
})
test(`not (CLI) | ${title}`, async t => {
t.snapshot(await callCli('not', arg))
})
})
each(VALID_FULL_DATA, ({ title }, arg) => {
test(`not (idempotence) | ${title}`, t => {
t.deepEqual(normalize(arg), not(not(arg)))
})
})