web icon indicating copy to clipboard operation
web copied to clipboard

Tree shaken content shows up as tested in code coverage

Open dgp1130 opened this issue 1 year ago • 0 comments
trafficstars

Description

To integrate Angular CLI with Web Test Runner, we're pre-bundling application tests into a bunch of JavaScript files and then running WTR on the bundled output. By nature of this bundling, some code may be dropped (tree shaken) from the output if it is not used anywhere. Take for example:

// tree_shaking_test.ts

import { doSomething } from './tree_shaking.js';

describe('tree_shaking', () => {
  it('does something', () => {
    expect(doSomething()).toBeUndefined();
  });
});
// tree_shaking.ts

// Called in test, should be green.
export function doSomething(): string | undefined {
  if ('does not exist' in window) return untested();

  return undefined;
}

// Bundled, but not executed. Should be red in code coverage.
export function untested(): string {
  return 'I am untested!';
}

// Not bundled. Should be red in code coverage.
export function unreferenced(): string {
  return 'I am unreferenced and will be tree shaken!';
}

This outputs the following coverage report:

Screenshot from 2024-02-07 17-47-31

doSomething and untested are both accurate (though the fact that export on line 9 is tested is a little weird but :shrug:).

However unreferenced is considered tested and marked green, which is misleading as nothing imports or invokes that function anywhere. Looking at the bundled output which is actually being tested we can see:

// dist/tests/chunk-606SASW2.js

// src/tree_shaking.ts
function doSomething() {
  if ("does not exist" in window)
    return untested();
  return void 0;
}
function untested() {
  return "I am untested!";
}

export {
  doSomething
};

Note that unreferenced is missing because ESBuild dropped it from the output.

Reproduction

This repo includes a reproduction in src/tree_shaking_test.ts. npm run coverage to see it.

Proposed Solution

Code eliminated by tree shaking and not referenced by any source map should be considered untested and marked red, as I think this would better align with user expectations.

I'm happy to help contribute a fix here, though I'd likely need some direction to understand what I would need to change to handle this.

dgp1130 avatar Feb 08 '24 01:02 dgp1130