misc.spec.ts 2.77 KB
Newer Older
1
/* Imports: Internal */
2
import { expect } from './setup'
3
import { sleep, clone, reqenv, getenv } from '../src'
4 5 6 7 8 9 10 11 12

describe('sleep', async () => {
  it('should return wait input amount of ms', async () => {
    const startTime = Date.now()
    await sleep(1000)
    const endTime = Date.now()
    expect(startTime + 1000 <= endTime).to.deep.equal(true)
  })
})
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

describe('clone', async () => {
  it('should return a cloned object', async () => {
    const exampleObject = { example: 'Example' }
    const clonedObject = clone(exampleObject)
    expect(clonedObject).to.not.equal(exampleObject)
    expect(JSON.stringify(clonedObject)).to.equal(JSON.stringify(exampleObject))
  })
})

describe('reqenv', async () => {
  let cachedEnvironment
  const temporaryEnvironmentKey = 'testVariable'
  const temporaryEnvironment = {
    [temporaryEnvironmentKey]: 'This is an environment variable',
  }

  before(() => {
    cachedEnvironment = process.env
    process.env = temporaryEnvironment
  })

  it('should return an existent environment variable', async () => {
    const requiredEnvironmentValue = reqenv(temporaryEnvironmentKey)
    expect(requiredEnvironmentValue).to.equal(
      temporaryEnvironment[temporaryEnvironmentKey]
    )
  })

  it('should throw an error trying to return a variable that does not exist', async () => {
    const undeclaredVariableName = 'undeclaredVariable'
    const failedReqenv = () => reqenv(undeclaredVariableName)
    expect(failedReqenv).to.throw()
  })

  after(() => {
    process.env = cachedEnvironment
  })
})

describe('getenv', async () => {
  let cachedEnvironment
  const temporaryEnvironmentKey = 'testVariable'
  const temporaryEnvironment = {
    [temporaryEnvironmentKey]: 'This is an environment variable',
  }
  const fallback = 'fallback'

  before(() => {
    cachedEnvironment = process.env
    process.env = temporaryEnvironment
  })

  it('should return an existent environment variable', async () => {
    const environmentVariable = getenv(temporaryEnvironmentKey)
    expect(environmentVariable).to.equal(
      temporaryEnvironment[temporaryEnvironmentKey]
    )
  })

  it('should return an existent environment variable even if fallback is passed', async () => {
    const environmentVariable = getenv(temporaryEnvironmentKey, fallback)
    expect(environmentVariable).to.equal(
      temporaryEnvironment[temporaryEnvironmentKey]
    )
  })

  it('should return fallback if variable is not defined', async () => {
    const undeclaredVariableName = 'undeclaredVariable'
    expect(getenv(undeclaredVariableName, fallback)).to.equal(fallback)
  })

  it('should return undefined if no fallback is passed and variable is not defined', async () => {
    expect(getenv('undeclaredVariable')).to.be.undefined
  })

  after(() => {
    process.env = cachedEnvironment
  })
})