Skip to content

Commit

Permalink
test: add test suite for getWebpackResolver
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vvanpo committed Aug 16, 2020
1 parent 0d59f66 commit 93176d3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
36 changes: 36 additions & 0 deletions test/resolver.test.js
Original file line number Diff line number Diff line change
@@ -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$/);
});
});

0 comments on commit 93176d3

Please sign in to comment.