Skip to content

Commit 2ceba27

Browse files
test: code (#108)
1 parent 1e785a1 commit 2ceba27

14 files changed

+229
-6
lines changed

test/__snapshots__/sourceMapperRegexp.test.js.snap

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ exports[`source-map-loader should work: result 6`] = `"absolute-sourceRoot-sourc
2222

2323
exports[`source-map-loader should work: result 7`] = `"http://sampledomain.com/external-source-map2.map"`;
2424

25-
exports[`source-map-loader should work: result 8`] = `"data:application/source-map;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAuanMiLCJzb3VyY2VzIjpbImlubGluZS1zb3VyY2UtbWFwLnR4dCJdLCJzb3VyY2VzQ29udGVudCI6WyJ3aXRoIFNvdXJjZU1hcCJdLCJtYXBwaW5ncyI6IkFBQUEifQ=="`;
25+
exports[`source-map-loader should work: result 8`] = `"//sampledomain.com/external-source-map2.map"`;
2626

27-
exports[`source-map-loader should work: result 9`] = `"/sample-source-map.map"`;
27+
exports[`source-map-loader should work: result 9`] = `"data:application/source-map;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAuanMiLCJzb3VyY2VzIjpbImlubGluZS1zb3VyY2UtbWFwLnR4dCJdLCJzb3VyY2VzQ29udGVudCI6WyJ3aXRoIFNvdXJjZU1hcCJdLCJtYXBwaW5ncyI6IkFBQUEifQ=="`;
28+
29+
exports[`source-map-loader should work: result 10`] = `"/sample-source-map.map"`;

test/cjs.test.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import src from '../src';
2+
import cjs from '../src/cjs';
3+
4+
describe('cjs', () => {
5+
it('should exported', () => {
6+
expect(cjs).toEqual(src);
7+
});
8+
});

test/fixtures/app.css

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/app.css.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/inline-sources.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Inline Sources in SourceMap
2+
//#sourceMappingURL=inline-sources.js.map
3+
// comment

test/fixtures/inline-sources.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Some content
2+
// # sourceMappingURL=//sampledomain.com/external-source-map2.map
3+
// comment

test/fixtures/skip-sourcesContent.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Skip SourcesContent in SourceMap
2+
// # sourceMappingURL=skip-sourcesContent.js.map
3+
// comment

test/fixtures/skip-sourcesContent.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// with SourceMap
2+
//#sourceMappingURL=ftp://exampleurl.com
3+
// comment

test/helpers/getCompiler.js

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ export default (
4040
test: /\.js/i,
4141
use: loaders,
4242
},
43+
44+
{
45+
test: /\.css/i,
46+
use: loaders,
47+
},
4348
],
4449
},
4550
plugins: [],

test/helpers/normalizeMap.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ export default (map) => {
22
const result = map;
33

44
if (result.sources) {
5-
result.sources = normilizeArr(result.sources);
5+
result.sources = normalizeArr(result.sources);
66
}
77

88
if (result.file) {
9-
[result.file] = normilizeArr([result.file]);
9+
[result.file] = normalizeArr([result.file]);
1010
}
1111

1212
if (result.sourceRoot) {
13-
[result.sourceRoot] = normilizeArr([result.sourceRoot]);
13+
[result.sourceRoot] = normalizeArr([result.sourceRoot]);
1414
}
1515

1616
return result;
1717
};
1818

19-
function normilizeArr(arr) {
19+
function normalizeArr(arr) {
2020
return arr.map((str) => {
2121
const normilized = removeCWD(str);
2222

test/loader.test.js

+164
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,168 @@ describe('source-map-loader', () => {
341341
expect(bundle.indexOf(fixture) !== -1).toBe(true);
342342
});
343343
});
344+
345+
it.skip('should process protocol-relative-url-path', async () => {
346+
const testId = 'protocol-relative-url-path.js';
347+
const compiler = getCompiler(testId);
348+
const stats = await compile(compiler);
349+
const codeFromBundle = getCodeFromBundle(stats, compiler);
350+
351+
expect(codeFromBundle.map).toBeUndefined();
352+
expect(codeFromBundle.css).toMatchSnapshot('css');
353+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
354+
expect(getErrors(stats)).toMatchSnapshot('errors');
355+
});
356+
357+
it.skip('should support mixed paths in sources without sourceRoot', async () => {
358+
const sourceRoot = path.resolve(__dirname, 'fixtures');
359+
const javaScriptFilename = 'absolute-path.js';
360+
const entryFileAbsolutePath = path.join(sourceRoot, javaScriptFilename);
361+
const sourceMapPath = path.join(sourceRoot, 'map-with-sourceroot.js.map');
362+
363+
// Create the sourcemap file
364+
const rawSourceMap = {
365+
version: 3,
366+
sources: [
367+
'normal-file.js',
368+
path.resolve(__dirname, 'fixtures', 'normal-file2.js'),
369+
'http://path-to-map.com',
370+
'ftp://path-to-map.com',
371+
],
372+
mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA',
373+
};
374+
fs.writeFileSync(sourceMapPath, JSON.stringify(rawSourceMap));
375+
376+
// Create the entryPointFile file
377+
const entryFileContent = `// Some content \r\n // # sourceMappingURL=${sourceMapPath}`;
378+
fs.writeFileSync(entryFileAbsolutePath, entryFileContent);
379+
380+
const compiler = getCompiler(javaScriptFilename);
381+
const stats = await compile(compiler);
382+
const codeFromBundle = getCodeFromBundle(stats, compiler);
383+
384+
expect(codeFromBundle.map).toBeDefined();
385+
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
386+
expect(codeFromBundle.css).toMatchSnapshot('css');
387+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
388+
expect(getErrors(stats)).toMatchSnapshot('errors');
389+
});
390+
391+
it.skip('should support mixed paths in sources with sourceRoot', async () => {
392+
const sourceRoot = path.resolve(__dirname, 'fixtures');
393+
const javaScriptFilename = 'absolute-path.js';
394+
const entryFileAbsolutePath = path.join(sourceRoot, javaScriptFilename);
395+
const sourceMapPath = path.join(sourceRoot, 'map-with-sourceroot.js.map');
396+
397+
// Create the sourcemap file
398+
const rawSourceMap = {
399+
version: 3,
400+
sourceRoot,
401+
sources: [
402+
'normal-file.js',
403+
path.resolve(__dirname, 'fixtures', 'normal-file2.js'),
404+
'http://path-to-map.com',
405+
'ftp://path-to-map.com',
406+
],
407+
mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA',
408+
};
409+
fs.writeFileSync(sourceMapPath, JSON.stringify(rawSourceMap));
410+
411+
// Create the entryPointFile file
412+
const entryFileContent = `// Some content \r\n // # sourceMappingURL=${sourceMapPath}`;
413+
fs.writeFileSync(entryFileAbsolutePath, entryFileContent);
414+
415+
const compiler = getCompiler(javaScriptFilename);
416+
const stats = await compile(compiler);
417+
const codeFromBundle = getCodeFromBundle(stats, compiler);
418+
419+
expect(codeFromBundle.map).toBeDefined();
420+
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
421+
expect(codeFromBundle.css).toMatchSnapshot('css');
422+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
423+
expect(getErrors(stats)).toMatchSnapshot('errors');
424+
});
425+
426+
it.skip('should support absolute paths to sourcemaps', async () => {
427+
const sourceRoot = path.resolve(__dirname, 'fixtures');
428+
const javaScriptFilename = 'absolute-path.js';
429+
const entryFileAbsolutePath = path.join(sourceRoot, javaScriptFilename);
430+
const sourceMapPath = path.join(sourceRoot, 'normal-map.js.map');
431+
432+
// Create the sourcemap file
433+
const rawSourceMap = {
434+
version: 3,
435+
sourceRoot,
436+
sources: [
437+
'normal-file.js',
438+
path.resolve(__dirname, 'fixtures', 'normal-file2.js'),
439+
],
440+
mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA',
441+
};
442+
fs.writeFileSync(sourceMapPath, JSON.stringify(rawSourceMap));
443+
444+
// Create the entryPointFile file
445+
const entryFileContent = `// Some content \r\n // # sourceMappingURL=${sourceMapPath}`;
446+
fs.writeFileSync(entryFileAbsolutePath, entryFileContent);
447+
448+
const compiler = getCompiler(javaScriptFilename);
449+
const stats = await compile(compiler);
450+
const codeFromBundle = getCodeFromBundle(stats, compiler);
451+
452+
expect(codeFromBundle.map).toBeDefined();
453+
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
454+
expect(codeFromBundle.css).toMatchSnapshot('css');
455+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
456+
expect(getErrors(stats)).toMatchSnapshot('errors');
457+
});
458+
459+
it.skip('should reject not support url', async () => {
460+
const testId = 'unSupport-file-source-map.js';
461+
const compiler = getCompiler(testId);
462+
const stats = await compile(compiler);
463+
const codeFromBundle = getCodeFromBundle(stats, compiler);
464+
465+
expect(codeFromBundle.css).toMatchSnapshot('css');
466+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
467+
expect(getErrors(stats)).toMatchSnapshot('errors');
468+
});
469+
470+
it.skip('should process inlined sources', async () => {
471+
const testId = 'inline-sources.js';
472+
const compiler = getCompiler(testId);
473+
const stats = await compile(compiler);
474+
const codeFromBundle = getCodeFromBundle(stats, compiler);
475+
476+
expect(codeFromBundle.map).toBeDefined();
477+
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
478+
expect(codeFromBundle.css).toMatchSnapshot('css');
479+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
480+
expect(getErrors(stats)).toMatchSnapshot('errors');
481+
});
482+
483+
it.skip('should process css sourceMap', async () => {
484+
const testId = 'app.css';
485+
const compiler = getCompiler(testId);
486+
const stats = await compile(compiler);
487+
const codeFromBundle = getCodeFromBundle(stats, compiler);
488+
489+
expect(codeFromBundle.map).toBeDefined();
490+
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
491+
expect(codeFromBundle.css).toMatchSnapshot('css');
492+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
493+
expect(getErrors(stats)).toMatchSnapshot('errors');
494+
});
495+
496+
it.skip('should process css sourceMap', async () => {
497+
const testId = 'skip-sourcesContent.js';
498+
const compiler = getCompiler(testId);
499+
const stats = await compile(compiler);
500+
const codeFromBundle = getCodeFromBundle(stats, compiler);
501+
502+
expect(codeFromBundle.map).toBeDefined();
503+
expect(normalizeMap(codeFromBundle.map)).toMatchSnapshot('map');
504+
expect(codeFromBundle.css).toMatchSnapshot('css');
505+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
506+
expect(getErrors(stats)).toMatchSnapshot('errors');
507+
});
344508
});

test/sourceMapperRegexp.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ describe('source-map-loader', () => {
5050
expect(url).toMatchSnapshot('result');
5151
});
5252

53+
it('should work', async () => {
54+
const code = `// #sourceMappingURL = //sampledomain.com/external-source-map2.map`;
55+
const { url } = getSourceMappingUrl(code);
56+
57+
expect(url).toMatchSnapshot('result');
58+
});
59+
5360
it('should work', async () => {
5461
const code = `// @sourceMappingURL=data:application/source-map;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAuanMiLCJzb3VyY2VzIjpbImlubGluZS1zb3VyY2UtbWFwLnR4dCJdLCJzb3VyY2VzQ29udGVudCI6WyJ3aXRoIFNvdXJjZU1hcCJdLCJtYXBwaW5ncyI6IkFBQUEifQ==`;
5562
const { url } = getSourceMappingUrl(code);

0 commit comments

Comments
 (0)