Skip to content

Commit f20ddf9

Browse files
committed
fix: fix Windows JS config loading by using file:// URLs
See issue nodejs/node#34765
1 parent e96b6d9 commit f20ddf9

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

lib/loadConfig.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { pathToFileURL } from 'url'
2+
13
import { lilconfig } from 'lilconfig'
24
import YAML from 'yaml'
35

@@ -21,7 +23,7 @@ const searchPlaces = [
2123

2224
/** exported for tests */
2325
export const dynamicImport = (path) =>
24-
import(path)
26+
import(pathToFileURL(path))
2527
.then((module) => module.default)
2628
.catch((error) => {
2729
console.error(error)

test/index.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ const mockLilConfig = (result) => {
1818
}))
1919
}
2020

21+
/**
22+
* This converts paths into `file://` urls, but this doesn't
23+
* work with `import()` when using babel + jest.
24+
*/
25+
jest.mock('url', () => ({
26+
pathToFileURL: (path) => path,
27+
}))
28+
2129
jest.mock('../lib/getStagedFiles')
2230
jest.mock('../lib/gitWorkflow')
2331
jest.mock('../lib/validateOptions', () => ({

test/loadConfig.spec.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ describe('dynamicImport', () => {
1414
})
1515

1616
it('should log errors into console', () => {
17-
expect(() => dynamicImport('not-found.js')).rejects.toMatchInlineSnapshot(
18-
`[Error: Cannot find module 'not-found.js' from 'lib/loadConfig.js']`
19-
)
17+
expect(() => dynamicImport('not-found.js')).rejects.toThrowError(`Cannot find module`)
2018
})
2119
})

0 commit comments

Comments
 (0)