diff --git a/packages/io/__tests__/io.test.ts b/packages/io/__tests__/io.test.ts index ddaedded65..2bdb5aaac2 100644 --- a/packages/io/__tests__/io.test.ts +++ b/packages/io/__tests__/io.test.ts @@ -807,14 +807,12 @@ describe('mkdirP', () => { '9', '10' ) - process.env['TEST_MKDIRP_FAILSAFE'] = '10' + + expect.assertions(1) + try { - await io.mkdirP(testPath) - throw new Error('directory should not have been created') + await io.mkdirP(testPath, {loopMax: 10}) } catch (err) { - delete process.env['TEST_MKDIRP_FAILSAFE'] - - // ENOENT is expected, all other errors are not expect(err.code).toBe('ENOENT') } }) diff --git a/packages/io/src/io.ts b/packages/io/src/io.ts index a1f44bea04..84b7e19df9 100644 --- a/packages/io/src/io.ts +++ b/packages/io/src/io.ts @@ -99,9 +99,13 @@ export async function rmRF(inputPath: string): Promise { * Will throw if it fails * * @param fsPath path to create + * @param options options for configuring the mkdirP call. For testing. * @returns Promise */ -export async function mkdirP(fsPath: string): Promise { +export async function mkdirP( + fsPath: string, + {loopMax}: {loopMax: number} = {loopMax: 1000} +): Promise { if (!fsPath) { throw new Error('Parameter p is required') } @@ -112,8 +116,7 @@ export async function mkdirP(fsPath: string): Promise { // eslint-disable-next-line no-constant-condition while (true) { - // validate the loop is not out of control - if (stack.length >= (process.env['TEST_MKDIRP_FAILSAFE'] || 1000)) { + if (stack.length >= loopMax) { // let the framework throw await ioUtil.mkdir(fsPath) return