diff --git a/lib/read-credentials-file.ts b/lib/read-credentials-file.ts index 466bd4c64..ee91c7042 100644 --- a/lib/read-credentials-file.ts +++ b/lib/read-credentials-file.ts @@ -6,6 +6,10 @@ import path = require('path'); const filename: string = 'ibm-credentials.env'; export function readCredentialsFile() { + if (!fs.existsSync) { + return {}; + } + // first look for an env variable called IBM_CREDENTIALS_FILE // it should be the path to the file @@ -48,4 +52,4 @@ export function constructFilepath(filepath): string { } return filepath; -} \ No newline at end of file +} diff --git a/test/unit/readCredentialsFile.test.js b/test/unit/readCredentialsFile.test.js index 9d8347602..bb53c98c7 100644 --- a/test/unit/readCredentialsFile.test.js +++ b/test/unit/readCredentialsFile.test.js @@ -4,6 +4,23 @@ const readCredentialsFunctions = require('../../lib/read-credentials-file'); const constructFilepath = readCredentialsFunctions.constructFilepath; const fileExistsAtPath = readCredentialsFunctions.fileExistsAtPath; const readCredentialsFile = readCredentialsFunctions.readCredentialsFile; +const fs = require('fs'); + +describe('browser scenario', () => { + const existSync = fs.existsSync; + beforeAll(() => { + fs.existsSync = undefined; + }); + + it('should return empty object when webpack override fs with empty object', () => { + const cred = readCredentialsFile(); + expect(cred).toEqual({}); + }); + + afterAll(() => { + fs.existsSync = existSync; + }); +}); describe('read ibm credentials file', () => { const locationOfActualFile = __dirname + '/../resources';