deno icon indicating copy to clipboard operation
deno copied to clipboard

Coverage failing to generate -- Missing transpiled source code

Open deer opened this issue 1 year ago • 3 comments

I was in the process of writing some tests for a project, and I wanted to test out the new html coverage feature. Unfortunately, as I got further along, the coverage started to fail to generate.

deno 1.39.0 (release, x86_64-apple-darwin)
v8 12.0.267.8
typescript 5.3.3
git clone [email protected]:deer/fresh_ga4.git
cd fresh_ga4
git checkout tests_coverage
deno test -A --coverage
deno coverage --html ./coverage

This results in the following error:

Task coverage deno coverage --html ./coverage
error: Missing transpiled source code for: "file:///Users/reed/code/temp/fresh_ga4/src/deps.ts".
              Before generating coverage report, run `deno test --coverage` to ensure consistent state.

The error is not at all helpful in this case (frustrating, even), since I've literally just run the tests with --coverage. I'm not sure how I've ended up in this state, but please let me know if any further info is needed.

There's one report from discord here and here, but there didn't seem to be any resolution.

deer avatar Dec 17 '23 23:12 deer

I'm not sure how I've ended up in this state, but please let me know if any further info is needed.

Could you try to remove coverage/ dir and run the deno test --coverage and deno coverage again?

The error message means that Deno can't find transpiled version of file:///Users/reed/code/temp/fresh_ga4/src/deps.ts in DENO_DIR, which is required by internal coverage data. My guess is that some internal coverage data are remained from the past run of deno test --coverage command, and the source has been changed since then, and Deno can't find it anymore. (fresh_ga4/src/deps.ts doesn't seem existing now https://github.com/deer/fresh_ga4/tree/main/src )

kt3k avatar Dec 18 '23 03:12 kt3k

Sorry, I should have mentioned that. Of course I was deleting the coverage dir prior to running the tests. (Exactly for that reason -- to ensure nothing was left behind.)

Additionally, I moved the file in the branch -- it's here: https://github.com/deer/fresh_ga4/blob/tests_coverage/src/deps.ts

deer avatar Dec 18 '23 07:12 deer

Note: this is an indirect blocker to some works for Deno SaaSKit, which will be using Reed's plugin.

iuioiua avatar Dec 21 '23 03:12 iuioiua

I'll spare all the details of how I got to this point, but I ended up changing my test to be the following:

import { assert, assertEquals } from "./test_deps.ts";
import { startFreshServer } from "./test_utils.ts";
import { ga4Plugin } from "../plugin/ga4.ts";

Deno.test("ga4Plugin creates a plugin with the correct name", () => {
  const plugin = ga4Plugin();
  assertEquals(plugin.name, "ga4_plugin");
});

Deno.test("warn when no ID", async () => {
  const { warnLines, serverProcess, address } = await startFreshServer({
    args: ["run", "-A", "./src/tests/fixture/dev.ts"],
    env: { GA4_MEASUREMENT_ID: "" },
  });

  const result = await fetch(`${address}`);

  for await (const line of warnLines) {
    if (
      line.includes(
        "GA4_MEASUREMENT_ID environment variable not set. Google Analytics reporting disabled.",
      )
    ) {
      assert(true);
      break;
    }
  }

  await result.body?.cancel();
  serverProcess.kill("SIGTERM");
  await serverProcess.status;
});

And then I tried commenting out the second test and looking at the emitted code. I created the following summary:

src/deps.ts
one tests   {"source_hash":"330212517625129447","emit_hash":"16047471370773192560"}
two tests   {"source_hash":"14958109283117656366","emit_hash":"16047471370773192560"}

mod.ts
one tests   {"source_hash":"9815799449212710933","emit_hash":"7494619615177933661"}
two tests   {"source_hash":"9815799449212710933","emit_hash":"7494619615177933661"}

So for whatever reason, commenting and uncommenting the second test causes the hash of my src/deps.ts to change! This is well beyond my skill (or desire) to deal with, so I went with a workaround. If some funny business with the tests is messing up the cache, I'll just recache things after the tests.

So now I have:

  "tasks": {
    "test": "rm -rf coverage && deno test -A --coverage",
    "coverage": "deno cache mod.ts && deno coverage --html ./coverage"
  }

Now the coverage generates just fine. And of course I am continuing to test with the full file, now that I'm unblocked.

deer avatar Jan 11 '24 19:01 deer