github.com/ethereum-optimism/optimism@v1.7.2/packages/core-utils/test/common/misc.spec.ts (about) 1 /* Imports: Internal */ 2 import { expect } from '../setup' 3 import { sleep, clone, reqenv, getenv } from '../../src' 4 5 describe('sleep', async () => { 6 it('should return wait input amount of ms', async () => { 7 const startTime = Date.now() 8 await sleep(1000) 9 const endTime = Date.now() 10 expect(startTime + 1000 <= endTime).to.deep.equal(true) 11 }) 12 }) 13 14 describe('clone', async () => { 15 it('should return a cloned object', async () => { 16 const exampleObject = { example: 'Example' } 17 const clonedObject = clone(exampleObject) 18 expect(clonedObject).to.not.equal(exampleObject) 19 expect(JSON.stringify(clonedObject)).to.equal(JSON.stringify(exampleObject)) 20 }) 21 }) 22 23 describe('reqenv', async () => { 24 let cachedEnvironment: NodeJS.ProcessEnv 25 const temporaryEnvironmentKey = 'testVariable' 26 const temporaryEnvironment = { 27 [temporaryEnvironmentKey]: 'This is an environment variable', 28 } 29 30 before(() => { 31 cachedEnvironment = process.env 32 process.env = temporaryEnvironment 33 }) 34 35 it('should return an existent environment variable', async () => { 36 const requiredEnvironmentValue = reqenv(temporaryEnvironmentKey) 37 expect(requiredEnvironmentValue).to.equal( 38 temporaryEnvironment[temporaryEnvironmentKey] 39 ) 40 }) 41 42 it('should throw an error trying to return a variable that does not exist', async () => { 43 const undeclaredVariableName = 'undeclaredVariable' 44 const failedReqenv = () => reqenv(undeclaredVariableName) 45 expect(failedReqenv).to.throw() 46 }) 47 48 after(() => { 49 process.env = cachedEnvironment 50 }) 51 }) 52 53 describe('getenv', async () => { 54 let cachedEnvironment: NodeJS.ProcessEnv 55 const temporaryEnvironmentKey = 'testVariable' 56 const temporaryEnvironment = { 57 [temporaryEnvironmentKey]: 'This is an environment variable', 58 } 59 const fallback = 'fallback' 60 61 before(() => { 62 cachedEnvironment = process.env 63 process.env = temporaryEnvironment 64 }) 65 66 it('should return an existent environment variable', async () => { 67 const environmentVariable = getenv(temporaryEnvironmentKey) 68 expect(environmentVariable).to.equal( 69 temporaryEnvironment[temporaryEnvironmentKey] 70 ) 71 }) 72 73 it('should return an existent environment variable even if fallback is passed', async () => { 74 const environmentVariable = getenv(temporaryEnvironmentKey, fallback) 75 expect(environmentVariable).to.equal( 76 temporaryEnvironment[temporaryEnvironmentKey] 77 ) 78 }) 79 80 it('should return fallback if variable is not defined', async () => { 81 const undeclaredVariableName = 'undeclaredVariable' 82 expect(getenv(undeclaredVariableName, fallback)).to.equal(fallback) 83 }) 84 85 it('should return undefined if no fallback is passed and variable is not defined', async () => { 86 expect(getenv('undeclaredVariable')).to.be.undefined 87 }) 88 89 after(() => { 90 process.env = cachedEnvironment 91 }) 92 })