Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree shaken content shows up as tested in code coverage #2638

Open
dgp1130 opened this issue Feb 8, 2024 · 0 comments
Open

Tree shaken content shows up as tested in code coverage #2638

dgp1130 opened this issue Feb 8, 2024 · 0 comments

Comments

@dgp1130
Copy link
Contributor

dgp1130 commented Feb 8, 2024

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 🤷).

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant