Skip to content

Commit

Permalink
test: code (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed May 25, 2020
1 parent 1e785a1 commit 2ceba27
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 6 deletions.
6 changes: 4 additions & 2 deletions test/__snapshots__/sourceMapperRegexp.test.js.snap
Expand Up @@ -22,6 +22,8 @@ exports[`source-map-loader should work: result 6`] = `"absolute-sourceRoot-sourc

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

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

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

exports[`source-map-loader should work: result 10`] = `"/sample-source-map.map"`;
8 changes: 8 additions & 0 deletions test/cjs.test.js
@@ -0,0 +1,8 @@
import src from '../src';
import cjs from '../src/cjs';

describe('cjs', () => {
it('should exported', () => {
expect(cjs).toEqual(src);
});
});
22 changes: 22 additions & 0 deletions test/fixtures/app.css

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

1 change: 1 addition & 0 deletions test/fixtures/app.css.map

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

3 changes: 3 additions & 0 deletions test/fixtures/inline-sources.js
@@ -0,0 +1,3 @@
// Inline Sources in SourceMap
//#sourceMappingURL=inline-sources.js.map
// comment
1 change: 1 addition & 0 deletions test/fixtures/inline-sources.js.map

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

3 changes: 3 additions & 0 deletions test/fixtures/protocol-relative-url-path.js
@@ -0,0 +1,3 @@
// Some content
// # sourceMappingURL=//sampledomain.com/external-source-map2.map
// comment
3 changes: 3 additions & 0 deletions test/fixtures/skip-sourcesContent.js
@@ -0,0 +1,3 @@
// Skip SourcesContent in SourceMap
// # sourceMappingURL=skip-sourcesContent.js.map
// comment
1 change: 1 addition & 0 deletions test/fixtures/skip-sourcesContent.js.map

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

3 changes: 3 additions & 0 deletions test/fixtures/unSupport-file-source-map.js
@@ -0,0 +1,3 @@
// with SourceMap
//#sourceMappingURL=ftp://exampleurl.com
// comment
5 changes: 5 additions & 0 deletions test/helpers/getCompiler.js
Expand Up @@ -40,6 +40,11 @@ export default (
test: /\.js/i,
use: loaders,
},

{
test: /\.css/i,
use: loaders,
},
],
},
plugins: [],
Expand Down
8 changes: 4 additions & 4 deletions test/helpers/normalizeMap.js
Expand Up @@ -2,21 +2,21 @@ export default (map) => {
const result = map;

if (result.sources) {
result.sources = normilizeArr(result.sources);
result.sources = normalizeArr(result.sources);
}

if (result.file) {
[result.file] = normilizeArr([result.file]);
[result.file] = normalizeArr([result.file]);
}

if (result.sourceRoot) {
[result.sourceRoot] = normilizeArr([result.sourceRoot]);
[result.sourceRoot] = normalizeArr([result.sourceRoot]);
}

return result;
};

function normilizeArr(arr) {
function normalizeArr(arr) {
return arr.map((str) => {
const normilized = removeCWD(str);

Expand Down
164 changes: 164 additions & 0 deletions test/loader.test.js
Expand Up @@ -341,4 +341,168 @@ describe('source-map-loader', () => {
expect(bundle.indexOf(fixture) !== -1).toBe(true);
});
});

it.skip('should process protocol-relative-url-path', async () => {
const testId = 'protocol-relative-url-path.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);

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

it.skip('should support mixed paths in sources without sourceRoot', async () => {
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');

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

// Create the entryPointFile file
const entryFileContent = `// Some content \r\n // # sourceMappingURL=${sourceMapPath}`;
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.skip('should support mixed paths in sources with sourceRoot', async () => {
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');

// Create the sourcemap file
const rawSourceMap = {
version: 3,
sourceRoot,
sources: [
'normal-file.js',
path.resolve(__dirname, 'fixtures', 'normal-file2.js'),
'http://path-to-map.com',
'ftp://path-to-map.com',
],
mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOA',
};
fs.writeFileSync(sourceMapPath, JSON.stringify(rawSourceMap));

// Create the entryPointFile file
const entryFileContent = `// Some content \r\n // # sourceMappingURL=${sourceMapPath}`;
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.skip('should support absolute paths to sourcemaps', async () => {
const sourceRoot = path.resolve(__dirname, 'fixtures');
const javaScriptFilename = 'absolute-path.js';
const entryFileAbsolutePath = path.join(sourceRoot, javaScriptFilename);
const sourceMapPath = path.join(sourceRoot, 'normal-map.js.map');

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

// Create the entryPointFile file
const entryFileContent = `// Some content \r\n // # sourceMappingURL=${sourceMapPath}`;
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.skip('should reject not support url', async () => {
const testId = 'unSupport-file-source-map.js';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);

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

it.skip('should process inlined sources', async () => {
const testId = 'inline-sources.js';
const compiler = getCompiler(testId);
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.skip('should process css sourceMap', async () => {
const testId = 'app.css';
const compiler = getCompiler(testId);
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.skip('should process css sourceMap', async () => {
const testId = 'skip-sourcesContent.js';
const compiler = getCompiler(testId);
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');
});
});
7 changes: 7 additions & 0 deletions test/sourceMapperRegexp.test.js
Expand Up @@ -50,6 +50,13 @@ describe('source-map-loader', () => {
expect(url).toMatchSnapshot('result');
});

it('should work', async () => {
const code = `// #sourceMappingURL = //sampledomain.com/external-source-map2.map`;
const { url } = getSourceMappingUrl(code);

expect(url).toMatchSnapshot('result');
});

it('should work', async () => {
const code = `// @sourceMappingURL=data:application/source-map;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lLXNvdXJjZS1tYXAuanMiLCJzb3VyY2VzIjpbImlubGluZS1zb3VyY2UtbWFwLnR4dCJdLCJzb3VyY2VzQ29udGVudCI6WyJ3aXRoIFNvdXJjZU1hcCJdLCJtYXBwaW5ncyI6IkFBQUEifQ==`;
const { url } = getSourceMappingUrl(code);
Expand Down

0 comments on commit 2ceba27

Please sign in to comment.