From 5dbb8d94b562cc05b1c87983ba066f37c829cacc Mon Sep 17 00:00:00 2001 From: Luca Greco Date: Wed, 3 Nov 2021 15:33:19 +0100 Subject: [PATCH] chore: Fix missing codecov reports (#345) --- .circleci/config.yml | 2 ++ test/setup.js | 30 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 57f5f36..bfc3531 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -95,6 +95,8 @@ commands: - run: name: run linting check and unit tests with coverage command: npm run test-coverage + environment: + COVERAGE_WITH_SOURCEMAP: "1" - store_artifacts: path: coverage - codecov/upload diff --git a/test/setup.js b/test/setup.js index f6da13b..50c97a3 100644 --- a/test/setup.js +++ b/test/setup.js @@ -21,6 +21,30 @@ if (process.env.TEST_MINIFIED_POLYFILL) { BROWSER_POLYFILL_PATH = process.env.TEST_BUNDLED_POLYFILL; } +function getInputSourceMap() { + // Enabled only on CI until we have fixed the mapping to the source file + // (by making sure that babel generates a sourcemap that does also take into + // account the api metadata being interpolated into the src script). + // + // TODO(https://github.com/mozilla/webextension-polyfill/issues/348): + // disabling the sourcemapped code coverage will not be necessary anymore + // once we fix the generated sourcemap to correctly map to src/browser-polyfill.js + if (process.env.COVERAGE_WITH_SOURCEMAP != "1") { + return undefined; + } + if (process.env.TEST_BUNDLED_POLYFILL) { + // Running the unit tests on the bundled files are meant to be used as smoke tests, + // we don't need to collect code coverage for it. + throw new Error("Unexpected code coverage enabled while testing bundled modules"); + } + let sourceMapPath = process.env.TEST_MINIFIED_POLYFILL + ? "./dist/browser-polyfill.js.min.map" + : "./dist/browser-polyfill.js.map"; + let sourceMap = JSON.parse(fs.readFileSync(sourceMapPath, {encoding: "utf-8"})); + sourceMap.sources = ["../src/browser-polyfill.js"]; + return sourceMap; +} + // Create the jsdom window used to run the tests const testDOMWindow = jsdom("", {virtualConsole}).defaultView; @@ -71,7 +95,11 @@ function setupTestDOMWindow(chromeObject, browserObject = undefined) { compact: false, esModules: false, produceSourceMap: false, }); const scriptContent = fs.readFileSync(BROWSER_POLYFILL_PATH, "utf-8"); - scriptEl.textContent = inst.instrumentSync(scriptContent, BROWSER_POLYFILL_PATH); + scriptEl.textContent = inst.instrumentSync( + scriptContent, + BROWSER_POLYFILL_PATH, + getInputSourceMap() + ); } else { scriptEl.src = BROWSER_POLYFILL_PATH; }