Skip to content

test: coverage #111

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 1 commit into from
May 26, 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
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
/node_modules
test/output/*
test/fixtures/absolute-path.js
test/fixtures/absolute-sourceRoot-source-map.map
test/fixtures/file-protocol-path.js
test/fixtures/file-protocol-path.js.map
test/fixtures/map-with-sourceroot.js.map
test/fixtures/map-without-sourceroot.js.map
test/fixtures/normal-map.js.map
logs
*.log
npm-debug.log*
Expand All @@ -14,4 +21,4 @@ Thumbs.db
*.iml
.vscode
*.sublime-project
*.sublime-workspace
*.sublime-workspace
3 changes: 2 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ async function fetchFromURL(

if (protocol === 'file:') {
const pathFromURL = urlUtils.fileURLToPath(url);
const sourceURL = getAbsolutePath(context, pathFromURL, sourceRoot);

const sourceURL = path.normalize(pathFromURL);

let sourceContent;

Expand Down
56 changes: 56 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,62 @@ Object {

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

exports[`source-map-loader should support file protocol path: css 1`] = `
"// Some content
"
`;

exports[`source-map-loader should support file protocol path: errors 1`] = `Array []`;

exports[`source-map-loader should support file protocol path: map 1`] = `
Object {
"mappings": "CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA",
"sources": Array [
"/test/fixtures/normal-file.js - (normalized for test)",
"/test/fixtures/normal-file2.js - (normalized for test)",
],
"sourcesContent": Array [
"without SourceMap",
"// without SourceMap
anInvalidDirective = \\"\\\\n/*# sourceMappingURL=data:application/json;base64,\\"+btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))))+\\" */\\";
// comment
",
],
"version": 3,
}
`;

exports[`source-map-loader should support file protocol path: warnings 1`] = `Array []`;

exports[`source-map-loader should support indexed sourcemaps 2: css 1`] = `
"console.log('with SourceMap')
"
`;

exports[`source-map-loader should support indexed sourcemaps 2: errors 1`] = `Array []`;

exports[`source-map-loader should support indexed sourcemaps 2: map 1`] = `
Object {
"mappings": "CAAC,IAAI,IAAM,SAAU,GAClB,OAAO,IAAI;CCDb,IAAI,IAAM,SAAU,GAClB,OAAO",
"names": Array [],
"sources": Array [
"/test/fixtures/indexed-sourcemap/nested1.js - (normalized for test)",
"/different/root/nested2.js",
],
"sourcesContent": Array [
" ONE.foo = function (bar) {
return baz(bar);
};",
" TWO.inc = function (n) {
return n + 1;
};",
],
"version": 3,
}
`;

exports[`source-map-loader should support indexed sourcemaps 2: warnings 1`] = `Array []`;

exports[`source-map-loader should support indexed sourcemaps: css 1`] = `
"console.log('with SourceMap')
// Map taken from here
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/indexed-sourcemap/file2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.log('with SourceMap')
//#sourceMappingURL=file2.js.map
34 changes: 34 additions & 0 deletions test/fixtures/indexed-sourcemap/file2.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 71 additions & 1 deletion test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,43 @@ describe('source-map-loader', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should support file protocol path', async () => {
const sourceRoot = path.resolve(__dirname, 'fixtures');
const javaScriptFilename = 'file-protocol-path.js';
const entryFileAbsolutePath = path.join(sourceRoot, javaScriptFilename);
const sourceMapPath = path.join(sourceRoot, 'file-protocol-path.js.map');

// Create the sourcemap file
const rawSourceMap = {
version: 3,
sources: [
'normal-file.js',
`file://${path
.resolve(__dirname, 'fixtures', 'normal-file2.js')
.replace(/\\/g, '/')}`,
],
mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA',
};
fs.writeFileSync(sourceMapPath, JSON.stringify(rawSourceMap));

// Create the entryPointFile file
const entryFileContent = `// Some content \r\n // # sourceMappingURL=file://${sourceMapPath.replace(
/\\/g,
'/'
)}`;
fs.writeFileSync(entryFileAbsolutePath, entryFileContent);

const compiler = getCompiler(javaScriptFilename);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);

expect(codeFromBundle.map).toBeDefined();
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should use last SourceMap directive', async () => {
const testId = 'multi-source-map.js';
const compiler = getCompiler(testId);
Expand Down Expand Up @@ -319,6 +356,36 @@ describe('source-map-loader', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should support indexed sourcemaps 2', async () => {
const currentDirPath = path.join(
__dirname,
'fixtures',
'indexed-sourcemap'
);

const testId = path.join(currentDirPath, 'file2.js');
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const deps = stats.compilation.fileDependencies;

const dependencies = [
path.join(currentDirPath, 'file2.js'),
path.join(currentDirPath, 'file2.js.map'),
path.join(currentDirPath, 'nested1.js'),
path.normalize(`/different/root/nested2.js`),
];

dependencies.forEach((fixture) => {
expect(deps.has(fixture)).toBe(true);
});
expect(codeFromBundle.map).toBeDefined();
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should transform to webpack', async () => {
const currentDirPath = path.join(
__dirname,
Expand Down Expand Up @@ -359,7 +426,10 @@ describe('source-map-loader', () => {
const sourceRoot = path.resolve(__dirname, 'fixtures');
const javaScriptFilename = 'absolute-path.js';
const entryFileAbsolutePath = path.join(sourceRoot, javaScriptFilename);
const sourceMapPath = path.join(sourceRoot, 'map-with-sourceroot.js.map');
const sourceMapPath = path.join(
sourceRoot,
'map-without-sourceroot.js.map'
);

// Create the sourcemap file
const rawSourceMap = {
Expand Down