gotwarlost / istanbul

Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests and browser tests. Built for scale.
Other
8.7k stars 786 forks source link

Function to string without istanbul in runtime #935

Open andrew-aladev opened 4 years ago

andrew-aladev commented 4 years ago

Hello, we are working with web workers:

function inlineWorker () { ... }

const data = `(${inlineWorker}) ...`
const worker = new Worker(URL.createObjectURL(new Blob(data, ....)))

Istanbul is poisoning inlineWorker with cov_${id}++; and worker fails. So we have to add istanbul ignore But it means that inlineWorker will be uncovered. So we are using workaround: function copy.

// can be covered outside worker
export function inlineWorker () { ... }

// istanbul ignore next
function inlineWorkerCopy () { ... }

const data = `(${inlineWorkerCopy}) ...`
const worker = new Worker(URL.createObjectURL(new Blob(data, ....)))

This workaround is ugly but works fine.

We can see here that istanbul ignore comments are not enough for everyday usage. Please add switch to disable istanbul in runtime. It may be inlineWorker.toString({ istanbul: false }) or inlineWorker_without_istanbul.toString(), etc. Thank you.

310 may be related.

fatemeh-fo commented 1 year ago

let useIstanbul = true; // Set this flag to enable or disable Istanbul

export function inlineWorker() { // Your inline worker code here if (useIstanbul) { // Instrumentation for Istanbul coverage cov_${id}++; } }

function inlineWorkerCopy() { // Your copy of the inline worker code without Istanbul instrumentation }

const data = (${useIstanbul ? inlineWorker : inlineWorkerCopy}) ...; const worker = new Worker(URL.createObjectURL(new Blob([data])));