angular-docs-cn/modules/playground/e2e_test/sourcemap/sourcemap_spec.ts

57 lines
1.8 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {$, browser} from 'protractor';
import {logging} from 'selenium-webdriver';
const fs = require('fs');
const sourceMap = require('source-map');
2015-02-11 17:54:59 -05:00
describe('sourcemaps', function() {
const URL = '/';
2015-02-11 17:54:59 -05:00
it('should map sources', function() {
browser.get(URL);
$('error-app .errorButton').click();
browser.manage().logs().get(logging.Type.BROWSER).then(function(logs: any) {
let errorLine: number = null;
let errorColumn: number = null;
logs.forEach(function(log: any) {
const match = log.message.match(/\.createError\s+\(.+:(\d+):(\d+)/m);
2015-02-11 17:54:59 -05:00
if (match) {
errorLine = parseInt(match[1]);
errorColumn = parseInt(match[2]);
}
});
expect(errorLine).not.toBeNull();
expect(errorColumn).not.toBeNull();
const content =
fs.readFileSync(require.resolve('../../src/sourcemap/index.js')).toString('utf8');
2016-08-05 12:56:53 -04:00
const marker = '//# sourceMappingURL=data:application/json;base64,';
const index = content.indexOf(marker);
const sourceMapData =
Buffer.from(content.substring(index + marker.length), 'base64').toString('utf8');
const decoder = new sourceMap.SourceMapConsumer(JSON.parse(sourceMapData));
2015-02-11 17:54:59 -05:00
const originalPosition = decoder.originalPositionFor({line: errorLine, column: errorColumn});
2015-02-11 17:54:59 -05:00
const sourceCodeLines = fs.readFileSync(require.resolve('../../src/sourcemap/index.ts'), {
encoding: 'UTF-8'
}).split('\n');
expect(sourceCodeLines[originalPosition.line - 1])
.toMatch(/throw new Error\(\'Sourcemap test\'\)/);
2015-02-11 17:54:59 -05:00
});
});
});