2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
2020-05-19 12:08:49 -07:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-06-23 09:47:54 -07:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
2024-09-20 09:23:15 -06:00
|
|
|
* found in the LICENSE file at https://angular.dev/license
|
2016-06-23 09:47:54 -07:00
|
|
|
*/
|
|
|
|
|
|
2026-08-14 09:10:52 +02:00
|
|
|
import {existsSync, readFileSync} from 'fs';
|
2025-06-26 18:59:19 +00:00
|
|
|
import {resolve} from 'path';
|
2026-08-14 09:10:52 +02:00
|
|
|
import * as webdriver from 'selenium-webdriver';
|
|
|
|
|
import {RawSourceMap, SourceMapConsumer} from 'source-map';
|
|
|
|
|
import {createWebDriver, waitForAngular} from '../../../../packages/examples/test-utils/index.js';
|
2015-02-11 14:54:59 -08:00
|
|
|
|
2015-05-29 14:33:54 -07:00
|
|
|
describe('sourcemaps', function () {
|
2026-08-14 09:10:52 +02:00
|
|
|
let driver: webdriver.WebDriver;
|
|
|
|
|
let baseUrl: string;
|
|
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
|
({driver, baseUrl} = await createWebDriver());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
|
await driver.quit();
|
|
|
|
|
});
|
2015-02-11 14:54:59 -08:00
|
|
|
|
2022-07-19 09:52:03 +00:00
|
|
|
it('should map sources', async function () {
|
2026-08-14 09:10:52 +02:00
|
|
|
await driver.get(`${baseUrl}/`);
|
|
|
|
|
await waitForAngular(driver);
|
2022-07-19 09:52:03 +00:00
|
|
|
|
2026-08-14 09:10:52 +02:00
|
|
|
const errorBtn = await driver.findElement(webdriver.By.css('error-app .errorButton'));
|
|
|
|
|
await errorBtn.click();
|
2022-07-19 09:52:03 +00:00
|
|
|
|
2026-08-14 09:10:52 +02:00
|
|
|
const logs = await driver.manage().logs().get(webdriver.logging.Type.BROWSER);
|
2022-07-19 09:52:03 +00:00
|
|
|
|
|
|
|
|
let errorLine: number | null = null;
|
|
|
|
|
let errorColumn: number | null = null;
|
|
|
|
|
logs.forEach(function (log: any) {
|
|
|
|
|
const match = log.message.match(/\.createError\s+\(.+:(\d+):(\d+)/m);
|
|
|
|
|
if (match) {
|
|
|
|
|
errorLine = parseInt(match[1]);
|
|
|
|
|
errorColumn = parseInt(match[2]);
|
|
|
|
|
}
|
2015-02-11 14:54:59 -08:00
|
|
|
});
|
2022-07-19 09:52:03 +00:00
|
|
|
|
|
|
|
|
expect(errorLine).not.toBeNull();
|
|
|
|
|
expect(errorColumn).not.toBeNull();
|
|
|
|
|
|
2026-08-14 09:10:52 +02:00
|
|
|
const mapPath = getFilePath('modules/playground/src/sourcemap/bundles/main.js.map');
|
|
|
|
|
const mapContent = readFileSync(mapPath, 'utf-8');
|
2022-12-14 19:30:53 +00:00
|
|
|
const decoder = await new SourceMapConsumer(JSON.parse(mapContent) as RawSourceMap);
|
2022-07-19 09:52:03 +00:00
|
|
|
const originalPosition = decoder.originalPositionFor({line: errorLine!, column: errorColumn!});
|
2026-08-14 09:10:52 +02:00
|
|
|
const sourcePath = getFilePath('modules/playground/src/sourcemap/main.ts');
|
|
|
|
|
const sourceCodeLines = readFileSync(sourcePath, 'utf-8').split('\n');
|
2022-07-19 09:52:03 +00:00
|
|
|
expect(sourceCodeLines[originalPosition.line! - 1]).toMatch(
|
|
|
|
|
/throw new Error\(\'Sourcemap test\'\)/,
|
|
|
|
|
);
|
2015-02-11 14:54:59 -08:00
|
|
|
});
|
|
|
|
|
});
|
2026-08-14 09:10:52 +02:00
|
|
|
|
|
|
|
|
function getFilePath(relPath: string): string {
|
|
|
|
|
const candidates = [
|
|
|
|
|
resolve(relPath),
|
|
|
|
|
resolve(process.cwd(), relPath),
|
|
|
|
|
process.env['TEST_SRCDIR'] ? resolve(process.env['TEST_SRCDIR'], '_main', relPath) : null,
|
|
|
|
|
process.env['TEST_SRCDIR'] ? resolve(process.env['TEST_SRCDIR'], relPath) : null,
|
|
|
|
|
].filter((p): p is string => Boolean(p));
|
|
|
|
|
|
|
|
|
|
for (const c of candidates) {
|
|
|
|
|
if (existsSync(c)) return c;
|
|
|
|
|
}
|
|
|
|
|
return resolve(relPath);
|
|
|
|
|
}
|