Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marella committed Jun 18, 2021
1 parent a5e835e commit 5169e1c
Show file tree
Hide file tree
Showing 10 changed files with 388 additions and 0 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,29 @@
"files": [
"index.js"
],
"scripts": {
"test": "jest",
"start": "npm test -- --watchAll",
"coverage": "npm test -- --coverage",
"prepublishOnly": "npm test",
"preversion": "npm test"
},
"main": "index.js",
"peerDependencies": {
"webpack": "^5.0.0"
},
"devDependencies": {
"file-loader": "^6.2.0",
"jest": "^27.0.4",
"memfs": "^3.2.2",
"url-loader": "^4.1.1",
"webpack": "^5.0.0"
},
"jest": {
"collectCoverageFrom": [
"index.js"
]
},
"license": "MIT",
"repository": {
"type": "git",
Expand Down
205 changes: 205 additions & 0 deletions test/__snapshots__/index.test.js.snap

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions test/fixtures/file.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions test/fixtures/nested.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import file from './nested/component.js';

__export__ = file;

export default file;
3 changes: 3 additions & 0 deletions test/fixtures/nested/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import file from './files/file.png?foo=bar#hash';

export default file;
Binary file added test/fixtures/nested/files/file.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions test/fixtures/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import file from './file.svg?foo=bar#hash';

__export__ = file;

export default file;
84 changes: 84 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const path = require('path');
const { compile } = require('./utils');

const loader = path.resolve(__dirname, '../index.js');
const files = /\.(png|svg)$/;
const entries = ['simple.js', 'nested.js'];
const baseURIs = [
'http://localhost/',
'http://localhost/foo/',
'http://localhost/foo/bar',
];
const publicPaths = ['', '/', '/foo/', '/foo/bar', 'foo/', 'foo/bar'];
const table = [];
for (const entry of entries) {
for (const baseURI of baseURIs) {
for (const publicPath of publicPaths) {
table.push({ entry, baseURI, publicPath });
}
}
}

describe('loader', () => {
it.each(entries)('should match url-loader %s', async (entry) => {
const actual = await compile(
{
test: files,
oneOf: [
{
dependency: { not: ['url'] },
use: [loader],
},
{
type: 'asset',
},
],
},
{ entry }
);
expect(actual).toMatchSnapshot('result');

const expected = await compile(
{
test: files,
use: ['url-loader'],
},
{ entry }
);
expect(actual).toEqual(expected);
});

it.each(table)(
'should match file-loader %s',
async ({ entry, publicPath, baseURI }) => {
const actual = await compile(
{
test: files,
oneOf: [
{
dependency: { not: ['url'] },
use: [loader],
},
{
type: 'asset/resource',
},
],
},
{ entry, publicPath, baseURI }
);
expect(actual).toMatchSnapshot('result');

const path = await compile(
{
test: files,
use: ['file-loader'],
},
{ entry, publicPath }
);
// new-url-loader returns an URL where as file-loader returns a path
// both should resolve to the same URL when using the same baseURI
const expected = new URL(path, baseURI).toString();
expect(actual).toEqual(expected);
}
);
});
53 changes: 53 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const Module = require('module');
const path = require('path');
const webpack = require('webpack');
const { createFsFromVolume, Volume } = require('memfs');

/**
* @see https://github.com/webpack-contrib/file-loader/blob/master/test/helpers/compile.js
* @see https://github.com/webpack-contrib/file-loader/blob/master/test/helpers/readAsset.js
* @see https://github.com/gregberge/svgr/blob/main/packages/webpack/src/index.test.js
*/
const compile = async (rule, { entry, publicPath = '', baseURI } = {}) => {
const compiler = webpack({
mode: 'production',
context: __dirname,
entry: `./fixtures/${entry}`,
output: {
publicPath,
hashDigestLength: 32, // default is 20 but file-loader uses 32
},
module: { rules: [rule] },
});
const fs = (compiler.outputFileSystem = createFsFromVolume(new Volume()));
const stats = await new Promise((resolve, reject) => {
compiler.run((err, stats) => (err ? reject(err) : resolve(stats)));
});
const dist = stats.compilation.outputOptions.path;
const content = fs.readFileSync(path.join(dist, 'main.js')).toString();
return execute(content, { baseURI }).toString();
};

/**
* @see https://github.com/webpack-contrib/file-loader/blob/master/test/helpers/execute.js
* @see https://github.com/webpack-contrib/url-loader/blob/master/test/helpers/execute.js
*/
const execute = (code, { baseURI } = {}) => {
const resource = 'test.js';
const mod = new Module(resource, module);
mod.paths = Module._nodeModulePaths(path.resolve(__dirname, './fixtures'));
mod.filename = resource;
mod._compile(script(code, { baseURI }), resource);
return mod.exports;
};

const script = (code, { baseURI = 'http://localhost/' } = {}) => `
const document = { baseURI: ${JSON.stringify(baseURI)} };
let __export__;
${code};
module.exports = __export__;
`;

module.exports = {
compile,
};

0 comments on commit 5169e1c

Please sign in to comment.