From f20ddf9413bfc2f44ad099b5cb1e5f478de5d35f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iiro=20J=C3=A4ppinen?= Date: Sun, 21 Nov 2021 19:34:26 +0200 Subject: [PATCH] fix: fix Windows JS config loading by using file:// URLs See issue https://github.com/nodejs/node/issues/34765 --- lib/loadConfig.js | 4 +++- test/index.spec.js | 8 ++++++++ test/loadConfig.spec.js | 4 +--- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/loadConfig.js b/lib/loadConfig.js index 2ee43e0f5..8c9e02f71 100644 --- a/lib/loadConfig.js +++ b/lib/loadConfig.js @@ -1,3 +1,5 @@ +import { pathToFileURL } from 'url' + import { lilconfig } from 'lilconfig' import YAML from 'yaml' @@ -21,7 +23,7 @@ const searchPlaces = [ /** exported for tests */ export const dynamicImport = (path) => - import(path) + import(pathToFileURL(path)) .then((module) => module.default) .catch((error) => { console.error(error) diff --git a/test/index.spec.js b/test/index.spec.js index 4f2b77922..a5fbb3ed3 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -18,6 +18,14 @@ const mockLilConfig = (result) => { })) } +/** + * This converts paths into `file://` urls, but this doesn't + * work with `import()` when using babel + jest. + */ +jest.mock('url', () => ({ + pathToFileURL: (path) => path, +})) + jest.mock('../lib/getStagedFiles') jest.mock('../lib/gitWorkflow') jest.mock('../lib/validateOptions', () => ({ diff --git a/test/loadConfig.spec.js b/test/loadConfig.spec.js index 533177a75..ea3a51ba1 100644 --- a/test/loadConfig.spec.js +++ b/test/loadConfig.spec.js @@ -14,8 +14,6 @@ describe('dynamicImport', () => { }) it('should log errors into console', () => { - expect(() => dynamicImport('not-found.js')).rejects.toMatchInlineSnapshot( - `[Error: Cannot find module 'not-found.js' from 'lib/loadConfig.js']` - ) + expect(() => dynamicImport('not-found.js')).rejects.toThrowError(`Cannot find module`) }) })