Skip to content

test: refactor tests #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
254 changes: 254 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import normalizeErrors from './normalizeErrors';

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