import { mapStackTrace } from 'sourcemapped-stacktrace';


/**
 * Enable sourcemaps for Jasmine exception output.
 *
 * This will locate any stack traces outputted by Jasmine as the result of
 * unit test failures and run the stack traces through the third-party
 * sourcemapped-stacktrace.js. This allows for debugging code generated by
 * Babel, CoffeeScript, and others.
 */
window.addEventListener('DOMContentLoaded', () => {
    jasmine.getEnv().addReporter({
        jasmineDone() {
            const traces = document.querySelectorAll('.jasmine-stack-trace');

            for (const traceEl of traces) {
                const textContent = traceEl.textContent;

                mapStackTrace(
                    textContent,
                    stack => {
                        traceEl.textContent = [
                            traceEl.previousSibling.textContent,
                            ...stack,
                        ].join('\n');
                    });
            }
        }
    });
});
