Exemplo n.º 1
0
test('str()', () => {
    assert.equal(str().type, 'str')
    const withEmpty = cleanEnv({ FOO: '' }, { FOO: str() })
    assert.deepEqual(withEmpty, { FOO: '' })

    assert.throws(() => cleanEnv({ FOO: 42 }, { FOO: str() }, makeSilent), EnvError)
})
Exemplo n.º 2
0
test('json()', () => {
    assert.equal(json().type, 'json')
    const env = cleanEnv({ FOO: '{"x": 123}' }, { FOO: json() })
    assert.deepEqual(env, { FOO: { x: 123 } })

    assert.throws(() => cleanEnv({ FOO: 'abc' }, { FOO: json() }, makeSilent), EnvError)
})
Exemplo n.º 3
0
test('email()', () => {
    const spec = { FOO: email() }
    assertPassthrough({ FOO: '*****@*****.**' }, spec)
    assertPassthrough({ FOO: '*****@*****.**' }, spec)

    assert.throws(() => cleanEnv({ FOO: 'asdf@asdf' }, spec, makeSilent), EnvError)
    assert.throws(() => cleanEnv({ FOO: '1' }, spec, makeSilent), EnvError)
})
Exemplo n.º 4
0
test('url()', () => {
    assert.equal(url().type, 'url')
    assertPassthrough({ FOO: 'http://foo.com' }, { FOO: url() })
    assertPassthrough({ FOO: 'http://foo.com/bar/baz' }, { FOO: url() })
    assertPassthrough({ FOO: 'custom://foo.com/bar/baz?hi=1' }, { FOO: url() })

    assert.throws(() => cleanEnv({ FOO: 'abc' }, { FOO: url() }, makeSilent), EnvError)
})
Exemplo n.º 5
0
test('devDefault and default together', () => {
    const spec = {
        FOO: num({ devDefault: 3000, default: 80 })
    }

    const env = cleanEnv({ NODE_ENV: 'test' }, spec)
    assert.deepEqual(env, { NODE_ENV: 'test', FOO: 3000 })

    const prodEnv = cleanEnv({ NODE_ENV: 'production' }, spec)
    assert.deepEqual(prodEnv, { NODE_ENV: 'production', FOO: 80 })
})
Exemplo n.º 6
0
test('falsy devDefault', () => {
    // Falsy values for devDefault work the same as falsy regular defaults
    const spec = {
        FOO: str({ devDefault: '' })
    }

    const env = cleanEnv({ NODE_ENV: 'test' }, spec)
    assert.deepEqual(env, { NODE_ENV: 'test', FOO: '' })

    assert.throws(() => cleanEnv({ NODE_ENV: 'production' }, spec, makeSilent), EnvMissingError)
})
Exemplo n.º 7
0
test('devDefault', () => {
    const spec = {
        FOO: str({ devDefault: 'hi' })
    }

    // For testing/development environments, devDefault values can make fields optional:
    const env = cleanEnv({ NODE_ENV: 'test' }, spec)
    assert.deepEqual(env, { NODE_ENV: 'test', FOO: 'hi' })

    // For a production environment, the field is required:
    assert.throws(() => cleanEnv({ NODE_ENV: 'production' }, spec, makeSilent), EnvMissingError)
})
Exemplo n.º 8
0
test('num()', () => {
    const withInt = cleanEnv({ FOO: '1' }, { FOO: num() })
    assert.deepEqual(withInt, { FOO: 1 })

    const withFloat = cleanEnv({ FOO: '0.34' }, { FOO: num() })
    assert.deepEqual(withFloat, { FOO: 0.34 })

    const withExponent = cleanEnv({ FOO: '1e3' }, { FOO: num() })
    assert.deepEqual(withExponent, { FOO: 1000 })

    assert.throws(() => cleanEnv({ FOO: 'asdf' }, { FOO: num() }, makeSilent), EnvError)
})
test('sell', () => {
  assert.deepEqual(sell({})
    ({price: 1, latestUpdate: YESTERDAY}),
    {price: 1, latestUpdate: YESTERDAY, crafts: 0, stocks: 0, sold: 1})

  assert.deepEqual(sell({timestamp: NOW, price: undefined})
    ({price: 1, latestUpdate: YESTERDAY, crafts: 2, stocks: 2, sold: 0}),
    {price: 1, latestUpdate: YESTERDAY, crafts: 2, stocks: 1, sold: 1})

  assert.deepEqual(sell({timestamp: NOW, price: 2})
    ({price: 1, latestUpdate: YESTERDAY, crafts: 2, stocks: 0, sold: 0}),
    {price: 2, latestUpdate: NOW, crafts: 1, stocks: 0, sold: 1})
})
test('buy', () => {
  assert.deepEqual(buy({})
    ({price: 1, latestUpdate: YESTERDAY}),
    {price: 1, latestUpdate: YESTERDAY, stocks: 1},
  )
  assert.deepEqual(buy({timestamp: NOW, price: undefined})
    ({price: 1, latestUpdate: YESTERDAY, stocks: 2}),
    {price: 1, latestUpdate: YESTERDAY, stocks: 3},
  )
  assert.deepEqual(buy({timestamp: NOW, price: 2})
    ({price: 1, latestUpdate: YESTERDAY, stocks: 2}),
    {price: 2, latestUpdate: NOW, stocks: 3},
  )
})
Exemplo n.º 11
0
test('host()', () => {
    assert.equal(host().type, 'host')
    const spec = { FOO: host() }
    assertPassthrough({ FOO: 'example.com' }, spec)
    assertPassthrough({ FOO: 'localhost' }, spec)
    assertPassthrough({ FOO: '192.168.0.1' }, spec)
    assertPassthrough({ FOO: '2001:0db8:85a3:0000:0000:8a2e:0370:7334' }, spec)

    assert.throws(() => cleanEnv({ FOO: '' }, spec, makeSilent), EnvError)
    assert.throws(() => cleanEnv({ FOO: 'example.com.' }, spec, makeSilent), EnvError)
    // https://github.com/chriso/validator.js/issues/704
    // assert.throws(() => cleanEnv({ FOO: '127.0.0' }, spec, makeSilent), EnvError)
    // assert.throws(() => cleanEnv({ FOO: '127.0.0.256' }, spec, makeSilent), EnvError)
    assert.throws(() => cleanEnv({ FOO: '2001:0db8:85a3:0000:0000' }, spec, makeSilent), EnvError)
})
Exemplo n.º 12
0
test('custom types', () => {
    const alwaysFoo = makeValidator(x => 'foo')
    const fooEnv = cleanEnv({ FOO: 'asdf' }, { FOO: alwaysFoo() })
    assert.deepEqual(fooEnv, { FOO: 'foo' })

    const hex10 = makeValidator(x => {
        if (/^[a-f0-9]{10}$/.test(x)) return x
        throw new Error('need 10 hex chars')
    })
    assertPassthrough({ FOO: 'a0d9aacbde' }, { FOO: hex10() })
    assert.throws(() => cleanEnv({ FOO: 'abc' }, { FOO: hex10() }, makeSilent), Error, '10 hex')

    // Default values work with custom validators as well
    const withDefault = cleanEnv({}, { FOO: hex10({ default: 'abcabcabc0' }) })
    assert.deepEqual(withDefault, { FOO: 'abcabcabc0' })
})
Exemplo n.º 13
0
test('misconfigured spec', () => {
    // Validation throws with different error if spec is invalid
    assert.throws(
        () => cleanEnv({ FOO: 'asdf' }, { FOO: {} }, makeSilent),
        EnvError,
        'Invalid spec'
    )
})
test('calcCosts should set the cost to the item\'s price when recipe is empty', () => {
  const tested = {price: 42, recipe: []}
  const input = new Map([
    ['tested', tested],
  ])
  const output = calcCosts(input).get('tested')
  return assert.equal(output.cost, tested.price)
})
test('calcCosts should not alter the Map\'s structure', () => {
  const input = new Map([
    ['1', {}],
    ['2', {}],
  ])
  const output = calcCosts(input)
  return assert.deepEqual([...input.keys()], [...output.keys()])
})
test('isIngredientValid', () => {
  const db = new Map([
    ['validIngredient1', {recipe: [{id: 'validIngredient2'}]}],
    ['validIngredient2', {price: 1}],
    ['invalidIngredient1', {recipe: [{id: 'missingIngredient'}]}],
    ['invalidIngredient2', {}],
  ])
  assert.isNotOk(isIngredientValid(db, undefined,
    'should return false on undefined'))
  assert.isOk(isIngredientValid(db, db.get('validIngredient1'),
    'should return true if has valid recipe'))
  assert.isOk(isIngredientValid(db, db.get('validIngredient2'),
    'should return true if has price'))
  assert.isNotOk(isIngredientValid(db, db.get('invalidIngredient1'),
    'should return false otherway'))
  assert.isNotOk(isIngredientValid(db, db.get('invalidIngredient2'),
    'should return false otherway'))
})
Exemplo n.º 17
0
test('default value can be blank', () => {
    const env = cleanEnv(
        {},
        {
            FOO: str({ default: '' })
        }
    )
    assert.deepEqual(env, { FOO: '' })
})
Exemplo n.º 18
0
test('default set to undefined', () => {
    const env = cleanEnv(
        {},
        {
            FOO: str({ default: undefined })
        }
    )
    assert.deepEqual(env, { FOO: undefined })
})
Exemplo n.º 19
0
test('devDefault set to undefined', () => {
    const env = cleanEnv(
        { NODE_ENV: 'test' },
        {
            FOO: str({ devDefault: undefined })
        }
    )
    assert.deepEqual(env, { NODE_ENV: 'test', FOO: undefined })
})
Exemplo n.º 20
0
test('using provided default value', () => {
    const env = cleanEnv(
        {},
        {
            FOO: str({ default: 'asdf' })
        }
    )
    assert.deepEqual(env, { FOO: 'asdf' })
})
test(`queryPredicate: empty query`, () => {
  const filterer = filter(queryPredicate())
  const input = [
    {name: 'Acrobate mineur', type: 'Trophée'},
    {name: 'Acrobate', type: 'Trophée'},
    {name: 'Acrobate majeur', type: 'Trophée'},
    {name: 'Bihilète mineure', type: 'Idole'},
  ]
  return assert.deepEqual(filterer(input), input)
})
Exemplo n.º 22
0
test('choices field', () => {
    // Throws when the env var isn't in the given choices:
    const spec = {
        FOO: str({ choices: ['foo', 'bar', 'baz'] })
    }
    assert.throws(() => cleanEnv({}, spec, makeSilent), EnvMissingError)
    assert.throws(() => cleanEnv({ FOO: 'bad' }, spec, makeSilent), EnvError, 'not in choices')

    // Works fine when a valid choice is given
    assertPassthrough({ FOO: 'bar' }, spec)
    assertPassthrough({ FOO: 'foo' }, spec)
    assertPassthrough({ FOO: 'baz' }, spec)

    // Throws an error when `choices` is not an array
    assert.throws(
        () => cleanEnv({ FOO: 'hi' }, { FOO: str({ choices: 123 }) }, makeSilent),
        Error,
        'must be an array'
    )
})
test('isRecipeValid', () => {
  const db = new Map([
    ['validIngredient1', {recipe: [{id: 'validIngredient2'}]}],
    ['validIngredient2', {price: 1}],
    ['invalidIngredient', {}],
  ])
  assert.isNotOk(isRecipeValid(db, {}),
    'should be true on non array')
  assert.isNotOk(isRecipeValid(db, []),
    'should be false on empty array')
  assert.isNotOk(isRecipeValid(db, [{id: 'missingIngredient'}]),
    'should be false on missing ingredient')
  assert.isNotOk(isRecipeValid(db, [{id: 'invalidIngredient'}]),
    'should be false on invalid ingredient')
  assert.isOk(isRecipeValid(db, [
    {id: 'validIngredient1'},
    {id: 'validIngredient2'},
  ]),
    'should be true otherways')
})
test(`queryPredicate: multiple negative and positive filters`, () => {
  const filterer = filter(queryPredicate('mine !mine'))
  const input = [
    {name: 'Acrobate mineur', type: 'Trophée'},
    {name: 'Acrobate', type: 'Trophée'},
    {name: 'Acrobate majeur', type: 'Trophée'},
    {name: 'Bihilète mineure', type: 'Idole'},
  ]
  const output = []
  return assert.deepEqual(filterer(input), output)
})
Exemplo n.º 25
0
test('smoke test to ensure the browser export works', () => {
    // Make sure we get the browser file.
    const { cleanEnv, str } = require(path.join(__dirname, '..', pkg.browser))
    const env = cleanEnv(
        {},
        {
            FOO: str({ default: 'asdf' })
        }
    )
    assert.deepEqual(env, { FOO: 'asdf' })
})
test('calcCosts should set the cost to the item\'s price when ingredient is missing', () => {
  const tested = {price: 42, recipe: [
    {quantity: 5, id: 'validIngredient'},
    {quantity: 3, id: 'missingIngredient'},
  ]}
  const input = new Map([
    ['tested', tested],
    ['validIngredient', {price: 1}],
  ])
  const output = calcCosts(input).get('tested')
  return assert.equal(output.cost, tested.price)
})
test(`queryPredicate: property scoped search`, () => {
  const filterer = filter(queryPredicate('type:trophee description:amazing'))
  const input = [
    {name: 'Acrobate mineur', type: 'Trophée', description: 'Amazing too'},
    {name: 'Acrobate', type: 'Trophée', description: 'Bof bof'},
    {name: 'Acrobate majeur', type: 'Trophée'},
    {name: 'Bihilète mineure', type: 'Idole', description: 'Amazing too'},
  ]
  const output = [
    {name: 'Acrobate mineur', type: 'Trophée', description: 'Amazing too'},
  ]
  return assert.deepEqual(filterer(input), output)
})
test(`makeSorter('cost') should order by cost`, () => {
  const sort = makeSorter({property: 'cost'})
  const input = [
    {cost: 4},
    {cost: 1},
    {cost: 40},
  ]
  const expected = [
    {cost: 1},
    {cost: 4},
    {cost: 40},
  ]
  const output = sort(input)
  return assert.deepEqual(output, expected)
})
test(`makeSorter('price') should order by price`, () => {
  const sort = makeSorter({property: 'price'})
  const input = [
    {price: 4},
    {price: 1},
    {price: 40},
  ]
  const expected = [
    {price: 1},
    {price: 4},
    {price: 40},
  ]
  const output = sort(input)
  return assert.deepEqual(output, expected)
})
test(`makeSorter('benefits') should order by benefits asc`, () => {
  const sort = makeSorter({property: 'benefits'})
  const input = [
    {price: 4, cost: 2}, //benefits = 2
    {price: 1, cost: 2}, //benefits = -1
    {price: 4, cost: 3}, //benefits = 1
  ]
  const expected = [
    {price: 1, cost: 2}, //benefits = -1
    {price: 4, cost: 3}, //benefits = 1
    {price: 4, cost: 2}, //benefits = 2
  ]
  const output = sort(input)
  return assert.deepEqual(output, expected)
})