Skip to content

Commit

Permalink
test: Add missing tests for 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
franky47 committed Apr 23, 2020
1 parent 4ab5db7 commit 6987b59
Showing 1 changed file with 86 additions and 1 deletion.
87 changes: 86 additions & 1 deletion src/check-env.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import checkEnv, {
CheckEnvInput,
MissingEnvironmentVariableError
MissingEnvironmentVariableError,
UnsafeEnvironmentVariableError
} from './index'

test('Empty options', () => {
Expand Down Expand Up @@ -49,6 +50,20 @@ test('Some required env are missing', () => {
expect(console.warn).not.toHaveBeenCalled()
})

test('A single required envs is available', () => {
const input: CheckEnvInput = {
required: 'foo'
}
const env = {
foo: 'foo'
}
console.error = jest.fn()
console.warn = jest.fn()
checkEnv(input, env)
expect(console.error).not.toHaveBeenCalled()
expect(console.warn).not.toHaveBeenCalled()
})

test('All required envs are available', () => {
const input: CheckEnvInput = {
required: ['foo', 'bar']
Expand Down Expand Up @@ -161,6 +176,21 @@ test('Custom logging methods', () => {
expect(input.logUnsafe).toHaveBeenCalledTimes(2)
})

test('Check if sensitive variable is set in production', () => {
const input: CheckEnvInput = {
unsafe: 'foo',
logUnsafe: jest.fn(),
noThrow: true
}
const env = {
NODE_ENV: 'production',
foo: 'foo'
}
const received = checkEnv(input, env)
expect(input.logUnsafe).toHaveBeenCalledTimes(1)
expect(received.unsafe).toEqual(['foo'])
})

test('Check if sensitive variables are set in production', () => {
const input: CheckEnvInput = {
unsafe: ['foo'],
Expand All @@ -175,3 +205,58 @@ test('Check if sensitive variables are set in production', () => {
expect(input.logUnsafe).toHaveBeenCalledTimes(1)
expect(received.unsafe).toEqual(['foo'])
})

test('No sensitive variable is defined', () => {
const input: CheckEnvInput = {
unsafe: 'foo',
logUnsafe: jest.fn(),
noThrow: true
}
const env = {
NODE_ENV: 'production'
}
const received = checkEnv(input, env)
expect(input.logUnsafe).not.toHaveBeenCalled()
expect(received.unsafe).toEqual([])
})

test('No sensitive variables are defined', () => {
const input: CheckEnvInput = {
unsafe: ['foo'],
logUnsafe: jest.fn(),
noThrow: true
}
const env = {
NODE_ENV: 'production'
}
const received = checkEnv(input, env)
expect(input.logUnsafe).not.toHaveBeenCalled()
expect(received.unsafe).toEqual([])
})

test('Unsafe variables are logged to the console', () => {
const input: CheckEnvInput = {
unsafe: ['foo'],
noThrow: true
}
const env = {
NODE_ENV: 'production',
foo: 'foo'
}
console.error = jest.fn()
checkEnv(input, env)
expect(console.error).toHaveBeenCalledTimes(1)
})

test('Unsafe variables throw an error', () => {
const input: CheckEnvInput = {
unsafe: ['foo']
}
const env = {
NODE_ENV: 'production',
foo: 'foo'
}
expect(() => checkEnv(input, env)).toThrowError(
UnsafeEnvironmentVariableError
)
})

0 comments on commit 6987b59

Please sign in to comment.