Skip to content

Commit

Permalink
test: refactor (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed May 18, 2020
1 parent 7f769aa commit 526c229
Show file tree
Hide file tree
Showing 15 changed files with 765 additions and 339 deletions.
254 changes: 254 additions & 0 deletions test/__snapshots__/loader.test.js.snap
@@ -0,0 +1,254 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`source-map-loader should leave normal files untouched: css 1`] = `"without SourceMap"`;

exports[`source-map-loader should leave normal files untouched: errors 1`] = `Array []`;

exports[`source-map-loader should leave normal files untouched: warnings 1`] = `Array []`;

exports[`source-map-loader should process external SourceMaps (external sources): css 1`] = `
"with SourceMap
// comment"
`;

exports[`source-map-loader should process external SourceMaps (external sources): errors 1`] = `Array []`;

exports[`source-map-loader should process external SourceMaps (external sources): map 1`] = `
Object {
"file": "external-source-map2.js",
"mappings": "AAAA",
"sources": Array [
"/test/fixtures/external-source-map2.txt - (normalized for test)",
],
"sourcesContent": Array [
"with SourceMap",
],
"version": 3,
}
`;

exports[`source-map-loader should process external SourceMaps (external sources): warnings 1`] = `Array []`;

exports[`source-map-loader should process external SourceMaps: css 1`] = `
"with SourceMap
// comment"
`;

exports[`source-map-loader should process external SourceMaps: errors 1`] = `Array []`;

exports[`source-map-loader should process external SourceMaps: map 1`] = `
Object {
"file": "external-source-map.js",
"mappings": "AAAA",
"sources": Array [
"external-source-map.txt",
],
"sourcesContent": Array [
"with SourceMap",
],
"version": 3,
}
`;

exports[`source-map-loader should process external SourceMaps: warnings 1`] = `Array []`;

exports[`source-map-loader should process inlined SourceMaps with charset: css 1`] = `
"with SourceMap
// comment"
`;

exports[`source-map-loader should process inlined SourceMaps with charset: errors 1`] = `Array []`;

exports[`source-map-loader should process inlined SourceMaps with charset: map 1`] = `
Object {
"file": "charset-inline-source-map.js",
"mappings": "AAAA",
"sources": Array [
"charset-inline-source-map.txt",
],
"sourcesContent": Array [
"with SourceMap",
],
"version": 3,
}
`;

exports[`source-map-loader should process inlined SourceMaps with charset: warnings 1`] = `Array []`;

exports[`source-map-loader should process inlined SourceMaps: css 1`] = `
"with SourceMap
// comment"
`;

exports[`source-map-loader should process inlined SourceMaps: errors 1`] = `Array []`;

exports[`source-map-loader should process inlined SourceMaps: map 1`] = `
Object {
"file": "inline-source-map.js",
"mappings": "AAAA",
"sources": Array [
"inline-source-map.txt",
],
"sourcesContent": Array [
"with SourceMap",
],
"version": 3,
}
`;

exports[`source-map-loader should process inlined SourceMaps: warnings 1`] = `Array []`;

exports[`source-map-loader should skip invalid base64 SourceMap: css 1`] = `
"without SourceMap
// @sourceMappingURL=data:application/source-map;base64,\\"something invalid\\"
// comment"
`;

exports[`source-map-loader should skip invalid base64 SourceMap: errors 1`] = `Array []`;

exports[`source-map-loader should skip invalid base64 SourceMap: warnings 1`] = `Array []`;

exports[`source-map-loader should support absolute sourceRoot paths in sourcemaps: css 1`] = `
"with SourceMap
// comment"
`;

exports[`source-map-loader should support absolute sourceRoot paths in sourcemaps: errors 1`] = `Array []`;

exports[`source-map-loader should support absolute sourceRoot paths in sourcemaps: map 1`] = `
Object {
"file": "absolute-sourceRoot-source-map.js",
"mappings": "AAAA",
"sources": Array [
"/test/fixtures/absolute-sourceRoot-source-map.txt - (normalized for test)",
],
"sourcesContent": Array [
"with SourceMap
// comment",
],
"version": 3,
}
`;

exports[`source-map-loader should support absolute sourceRoot paths in sourcemaps: warnings 1`] = `Array []`;

exports[`source-map-loader should support relative sourceRoot paths in sourcemaps: css 1`] = `
"with SourceMap
// comment"
`;

exports[`source-map-loader should support relative sourceRoot paths in sourcemaps: errors 1`] = `Array []`;

exports[`source-map-loader should support relative sourceRoot paths in sourcemaps: map 1`] = `
Object {
"file": "relative-sourceRoot-source-map.js",
"mappings": "AAAA",
"sources": Array [
"/test/fixtures/data/relative-sourceRoot-source-map.txt - (normalized for test)",
],
"sourcesContent": Array [
"with SourceMap
// comment",
],
"version": 3,
}
`;

exports[`source-map-loader should support relative sourceRoot paths in sourcemaps: warnings 1`] = `Array []`;

exports[`source-map-loader should use last SourceMap directive: css 1`] = `
"with SourceMap
anInvalidDirective = \\"\\\\n/*# sourceMappingURL=data:application/json;base64,\\"+btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))))+\\" */\\";
// comment"
`;

exports[`source-map-loader should use last SourceMap directive: errors 1`] = `Array []`;

exports[`source-map-loader should use last SourceMap directive: map 1`] = `
Object {
"file": "inline-source-map.js",
"mappings": "AAAA",
"sources": Array [
"inline-source-map.txt",
],
"sourcesContent": Array [
"with SourceMap",
],
"version": 3,
}
`;

exports[`source-map-loader should use last SourceMap directive: warnings 1`] = `Array []`;

exports[`source-map-loader should warn on invalid SourceMap: css 1`] = `
"with SourceMap
//#sourceMappingURL=invalid-source-map.map
// comment"
`;

exports[`source-map-loader should warn on invalid SourceMap: errors 1`] = `Array []`;

exports[`source-map-loader should warn on invalid SourceMap: warnings 1`] = `
Array [
"ModuleWarning: Module Warning (from \`replaced original path\`):
(Emitted value instead of an instance of Error) Cannot parse SourceMap 'invalid-source-map.map': SyntaxError: Unexpected string in JSON at position 102",
]
`;

exports[`source-map-loader should warn on invalid base64 SourceMap: css 1`] = `
"without SourceMap
// @sourceMappingURL=data:application/source-map;base64,invalid/base64=
// comment"
`;

exports[`source-map-loader should warn on invalid base64 SourceMap: errors 1`] = `Array []`;

exports[`source-map-loader should warn on invalid base64 SourceMap: warnings 1`] = `
Array [
"ModuleWarning: Module Warning (from \`replaced original path\`):
(Emitted value instead of an instance of Error) Cannot parse inline SourceMap: data:application/source-map;base64,invalid/base64=",
]
`;

exports[`source-map-loader should warn on missing SourceMap: css 1`] = `
"with SourceMap
//#sourceMappingURL=missing-source-map.map
// comment"
`;

exports[`source-map-loader should warn on missing SourceMap: errors 1`] = `Array []`;

exports[`source-map-loader should warn on missing SourceMap: warnings 1`] = `
Array [
"ModuleWarning: Module Warning (from \`replaced original path\`):
(Emitted value instead of an instance of Error) Cannot find SourceMap 'missing-source-map.map': Error: Can't resolve './missing-source-map.map' in '/test/fixtures'",
]
`;

exports[`source-map-loader should warn on missing source file: css 1`] = `
"with SourceMap
// comment"
`;

exports[`source-map-loader should warn on missing source file: errors 1`] = `Array []`;

exports[`source-map-loader should warn on missing source file: map 1`] = `
Object {
"file": "missing-source-map2.js",
"mappings": "AAAA",
"sources": Array [
"missing-source-map2.txt",
],
"sourcesContent": Array [
null,
],
"version": 3,
}
`;

exports[`source-map-loader should warn on missing source file: warnings 1`] = `
Array [
"ModuleWarning: Module Warning (from \`replaced original path\`):
(Emitted value instead of an instance of Error) Cannot find source file 'missing-source-map2.txt': Error: Can't resolve './missing-source-map2.txt' in '/test/fixtures'",
]
`;
11 changes: 11 additions & 0 deletions test/helpers/compile.js
@@ -0,0 +1,11 @@
export default (compiler) => {
return new Promise((resolve, reject) => {
compiler.run((error, stats) => {
if (error) {
return reject(error);
}

return resolve(stats);
});
});
};
19 changes: 19 additions & 0 deletions test/helpers/execute.js
@@ -0,0 +1,19 @@
import Module from 'module';
import path from 'path';

const parentModule = module;

export default (code) => {
const resource = 'test.js';
const module = new Module(resource, parentModule);
// eslint-disable-next-line no-underscore-dangle
module.paths = Module._nodeModulePaths(
path.resolve(__dirname, '../fixtures')
);
module.filename = resource;

// eslint-disable-next-line no-underscore-dangle
module._compile(code, resource);

return module.exports;
};
32 changes: 32 additions & 0 deletions test/helpers/getCodeFromBundle.js
@@ -0,0 +1,32 @@
import vm from 'vm';

import readAsset from './readAsset';

function getCodeFromBundle(stats, compiler, asset) {
let code = null;

if (
stats &&
stats.compilation &&
stats.compilation.assets &&
stats.compilation.assets[asset || 'main.bundle.js']
) {
code = readAsset(asset || 'main.bundle.js', compiler, stats);
}

if (!code) {
throw new Error("Can't find compiled code");
}

const result = vm.runInNewContext(
`${code};\nmodule.exports = sourceMapLoaderExport;`,
{
module: {},
}
);

// eslint-disable-next-line no-underscore-dangle
return result.__esModule ? result.default : result;
}

export default getCodeFromBundle;
49 changes: 49 additions & 0 deletions test/helpers/getCompiler.js
@@ -0,0 +1,49 @@
import path from 'path';

import webpack from 'webpack';
import { createFsFromVolume, Volume } from 'memfs';

export default (fixture, loaderOptions = {}, config = {}) => {
const fullConfig = {
mode: 'development',
devtool: config.devtool || 'source-map',
context: path.resolve(__dirname, '../fixtures'),
entry: path.resolve(__dirname, '../fixtures', fixture),
output: {
path: path.resolve(__dirname, '../outputs'),
filename: '[name].bundle.js',
chunkFilename: '[name].chunk.js',
library: 'sourceMapLoaderExport',
},
module: {
rules: [
{
test: /\.js/i,
rules: [
{
loader: require.resolve('./testLoader'),
},
{
loader: path.resolve(__dirname, '../../src'),
options: loaderOptions || {},
},
],
},
],
},
plugins: [],
...config,
};

const compiler = webpack(fullConfig);

if (!config.outputFileSystem) {
const outputFileSystem = createFsFromVolume(new Volume());
// Todo remove when we drop webpack@4 support
outputFileSystem.join = path.join.bind(path);

compiler.outputFileSystem = outputFileSystem;
}

return compiler;
};
5 changes: 5 additions & 0 deletions test/helpers/getErrors.js
@@ -0,0 +1,5 @@
import normalizeErrors from './normalizeErrors';

export default (stats) => {
return normalizeErrors(stats.compilation.errors.sort());
};
5 changes: 5 additions & 0 deletions test/helpers/getWarnings.js
@@ -0,0 +1,5 @@
import normalizeErrors from './normalizeErrors';

export default (stats) => {
return normalizeErrors(stats.compilation.warnings.sort());
};

0 comments on commit 526c229

Please sign in to comment.