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

Use useDefineForClassFields with coverage-v8 and vitest workspace cause error #5329

Open
6 tasks done
colinaaa opened this issue Mar 4, 2024 · 3 comments · Fixed by bcoe/v8-coverage#2
Open
6 tasks done
Labels
feat: coverage Issues and PRs related to the coverage feature upstream

Comments

@colinaaa
Copy link

colinaaa commented Mar 4, 2024

Describe the bug

I'm using vitest and a coverage error showed up after I added a static class field:

TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))
 ❯ RangeTree.fromSortedRanges node_modules/.pnpm/@bcoe+v8-coverage@0.2.3/node_modules/@bcoe/v8-coverage/dist/lib/range-tree.js:27:39
 ❯ normalizeFunctionCov node_modules/.pnpm/@bcoe+v8-coverage@0.2.3/node_modules/@bcoe/v8-coverage/dist/lib/normalize.js:74:41
 ❯ Object.deepNormalizeScriptCov node_modules/.pnpm/@bcoe+v8-coverage@0.2.3/node_modules/@bcoe/v8-coverage/dist/lib/normalize.js:59:9
 ❯ mergeScriptCovs node_modules/.pnpm/@bcoe+v8-coverage@0.2.3/node_modules/@bcoe/v8-coverage/dist/lib/merge.js:59:21
 ❯ mergeProcessCovs node_modules/.pnpm/@bcoe+v8-coverage@0.2.3/node_modules/@bcoe/v8-coverage/dist/lib/merge.js:34:21
 ❯ node_modules/.pnpm/@vitest+coverage-v8@1.3.1_vitest@1.3.1/node_modules/@vitest/coverage-v8/dist/provider.js:265:22
 ❯ V8CoverageProvider.reportCoverage node_modules/.pnpm/@vitest+coverage-v8@1.3.1_vitest@1.3.1/node_modules/@vitest/coverage-v8/dist/provider.js:262:11
 ❯ Vitest.reportCoverage node_modules/.pnpm/vitest@1.3.1_@types+node@20.11.24/node_modules/vitest/dist/vendor/cac.wWT9ELdg.js:11918:7
 ❯ async node_modules/.pnpm/vitest@1.3.1_@types+node@20.11.24/node_modules/vitest/dist/vendor/cac.wWT9ELdg.js:11709:7

I tried to minimize the reproduction demo and it seems like it is related to useDefineForClassFields. See the Reproduction section for details.

Reproduction

A minimal reproduction repo is created at: https://github.com/colinaaa/vitest-v8-repro

Just run

  • pnpm install
  • pnpm vitest --coverage

to reproduce the issue.

Note that any of these will eliminate the error:

  • removing useDefineForClassFields in tsconfig.json
  • replacing coverage-v8 with coverage-istanbul
  • removing static Bar
  • replacing static Baz with require.resole('./loader.js')
  • not using vitest workspace

System Info

System:
    OS: macOS 14.0
    CPU: (12) arm64 Apple M2 Max
    Memory: 14.89 GB / 96.00 GB
    Shell: 3.7.0 - /opt/homebrew/bin/fish
  Binaries:
    Node: 20.11.1 - ~/Library/Caches/fnm_multishells/77527_1709530742451/bin/node
    Yarn: 1.22.19 - ~/Library/Caches/fnm_multishells/77527_1709530742451/bin/yarn
    npm: 10.2.4 - ~/Library/Caches/fnm_multishells/77527_1709530742451/bin/npm
    pnpm: 7.33.6 - ~/Library/Caches/fnm_multishells/77527_1709530742451/bin/pnpm
  Browsers:
    Chrome: 122.0.6261.94
    Firefox Nightly: 115.0
    Safari: 17.0
  npmPackages:
    @vitest/coverage-v8: ^1.3.1 => 1.3.1 
    vitest: ^1.3.1 => 1.3.1 

Used Package Manager

pnpm

Validations

@AriPerkkio AriPerkkio added upstream feat: coverage Issues and PRs related to the coverage feature and removed pending triage labels Mar 4, 2024
@AriPerkkio
Copy link
Member

AriPerkkio commented Mar 4, 2024

This looks like a bug in @bcoe/v8-coverage (👋 @bcoe). Here's minimal reproduction without Vitest in pure Javascript https://github.com/AriPerkkio/reproductions/tree/v8-merge.

import { mergeProcessCovs } from "@bcoe/v8-coverage";
import { writeFileSync } from "node:fs";
import inspector from "node:inspector";

writeFileSync(
  "./source-file.js",
  `
export class Foo {
  static Baz = process.env["NODE_ENV"] === "test" ? "some" : "thing";

  // Remove this will not cause error
  static Bar = {};
}
`.trim(),
  "utf8"
);

const session = new inspector.Session();
session.connect();
session.post("Profiler.enable");
session.post("Profiler.startPreciseCoverage", {
  callCount: true,
  detailed: true,
});

await import("./source-file.js");

const coverage = await collectCoverage("source-file.js");
console.log(JSON.stringify(coverage, null, 2));

mergeProcessCovs([coverage]);

async function collectCoverage(filename) {
  return new Promise((resolve, reject) => {
    session.post("Profiler.takePreciseCoverage", async (error, coverage) => {
      if (error) return reject(error);

      const result = coverage.result.filter((entry) =>
        entry.url.includes(filename)
      );

      resolve({ result });
    });
  });
}
$ node index.mjs 
{
  "result": [
    {
      "scriptId": "499",
      "url": "file:///<root>/reproductions/source-file.js",
      "functions": [
        {
          "functionName": "",
          "ranges": [
            {
              "startOffset": 0,
              "endOffset": 148,
              "count": 1
            }
          ],
          "isBlockCoverage": true
        },
        {
          "functionName": "<static_initializer>",
          "ranges": [
            {
              "startOffset": 130,
              "endOffset": 145,
              "count": 1
            },
            {
              "startOffset": 69,
              "endOffset": 77,
              "count": 0
            }
          ],
          "isBlockCoverage": true
        }
      ]
    }
  ]
}
/<root>/reproductions/node_modules/.pnpm/@bcoe+v8-coverage@0.2.3/node_modules/@bcoe/v8-coverage/dist/lib/range-tree.js:27
                [parent, parentCount] = stack[stack.length - 1];
                                      ^

TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))
    at RangeTree.fromSortedRanges (/<root>/reproductions/node_modules/.pnpm/@bcoe+v8-coverage@0.2.3/node_modules/@bcoe/v8-coverage/dist/lib/range-tree.js:27:39)
    at normalizeFunctionCov (/<root>/reproductions/node_modules/.pnpm/@bcoe+v8-coverage@0.2.3/node_modules/@bcoe/v8-coverage/dist/lib/normalize.js:74:41)
    at Object.deepNormalizeScriptCov (/<root>/reproductions/node_modules/.pnpm/@bcoe+v8-coverage@0.2.3/node_modules/@bcoe/v8-coverage/dist/lib/normalize.js:59:9)
    at mergeScriptCovs (/<root>/reproductions/node_modules/.pnpm/@bcoe+v8-coverage@0.2.3/node_modules/@bcoe/v8-coverage/dist/lib/merge.js:59:21)
    at mergeProcessCovs (/<root>/reproductions/node_modules/.pnpm/@bcoe+v8-coverage@0.2.3/node_modules/@bcoe/v8-coverage/dist/lib/merge.js:34:21)
    at file:///<root>/reproductions/index.mjs:32:1

Node.js v18.17.1

@bcoe
Copy link

bcoe commented Dec 10, 2024

I'm going to need to strip out the ancient TypeScript from v8-coverage before I can build and release, the toolchain from 6 years ago is very broken.

@bcoe
Copy link

bcoe commented Dec 10, 2024

@colinaaa @AriPerkkio, I believe this should now be addressed in @bcoe/v8-coverage@1.0.1. I did a major bump because I tore the library apart a bit to make it build again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: coverage Issues and PRs related to the coverage feature upstream
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants