From 93176d3e681d25ee1dfc6463c457d3808b9c46e7 Mon Sep 17 00:00:00 2001 From: Victor van Poppelen Date: Sat, 15 Aug 2020 18:15:21 -0700 Subject: [PATCH] test: add test suite for getWebpackResolver Because `getWebpackResolver` is now part of `utils.js`' public API, we should test it separately so that we have some protection against its API changing drastically from potential future refactors. --- package.json | 1 + test/resolver.test.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 test/resolver.test.js diff --git a/package.json b/package.json index 7f193ef3..eb50b689 100644 --- a/package.json +++ b/package.json @@ -77,6 +77,7 @@ "css-loader": "^4.2.0", "del": "^5.1.0", "del-cli": "^3.0.1", + "enhanced-resolve": "^4.3.0", "eslint": "^7.6.0", "eslint-config-prettier": "^6.11.0", "eslint-plugin-import": "^2.21.2", diff --git a/test/resolver.test.js b/test/resolver.test.js new file mode 100644 index 00000000..6463c956 --- /dev/null +++ b/test/resolver.test.js @@ -0,0 +1,36 @@ +import { fileURLToPath } from 'url'; + +import enhanced from 'enhanced-resolve'; + +import { getWebpackResolver } from '../src/utils'; + +/** + * Because `getWebpackResolver` is a public function that can be imported by + * external packages, we want to test it separately to ensure its API does not + * change unexpectedly. + */ +describe('getWebpackResolver', () => { + const resolve = (request, ...options) => + getWebpackResolver(enhanced.create, ...options)(__filename, request); + + it('should resolve .scss from node_modules', async () => { + expect(await resolve('scss/style')).toMatch(/style\.scss$/); + }); + + it('should resolve from passed `includePaths`', async () => { + expect(await resolve('empty', null, [`${__dirname }/scss`])).toMatch( + /empty\.scss$/ + ); + }); + + it('should reject when file cannot be resolved', async () => { + await expect(resolve('foo/bar/baz')).rejects.toBe(); + }); + + it('should strip an invalid file URL of its scheme', async () => { + const invalidFileURL = 'file://scss/empty'; + + expect(() => fileURLToPath(invalidFileURL)).toThrow(); + expect(await resolve(invalidFileURL)).toMatch(/empty\.scss$/); + }); +});