workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

🐛 BUG: vitest isolated runtimes AssertionError when run from vscode extension

Open gourgouris opened this issue 1 year ago • 1 comments

Which Cloudflare product(s) does this pertain to?

Workers Vitest Integration

What version(s) of the tool(s) are you using?

wrangler: 3.57.2, vitest: 1.5.0, @cloudflare/vitest-pool-workers: 0.3.0, VSCode Vitest: 0.10.7

What version of Node are you using?

20.12.2

What operating system and version are you using?

Win 11

Describe the Bug

Observed behavior

When:

  • Vitest is configured with singleWorker: false and isolatedStorage: true
  • More than 1 test files are present in the project
  • Only one of the test files is "expanded" in the VSCode Test Explorer

After pressing "run tests" in Test Explorer, vitest crashes with

AssertionError: The expression evaluated to a falsy value:
assert4(fileMf !== void 0)

which is this line: https://github.com/cloudflare/workers-sdk/blob/8e5e5897f0de5f8a4990f88165d7a963018a06ef/packages/vitest-pool-workers/src/pool/index.ts#L987

Expected behavior

No crash

Steps to reproduce

Clone and npm install any fixture from the repo with more than one test files. Eg https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/basics-unit-integration-self

Change vitest.config.ts to singleWorker: false and isolatedStorage: true Install Vitest VSCode extension Go to Test Explorer and "refresh". Expand only one node fully, and make sure the others are collapsed. This is easily confirmed from the VSCode "output" tab, by selecting "Vitest". In the logs, there should be a line [INFO 5:19:51 PM] [API] Collecting tests: test/scheduled-unit.test.ts this must show only 1 file

image

Click "run tests" in VSCode Test Explorer. You get the assertion.

image

If on the other hand all nodes are expanded image Then everything works as expected.

I have fixed this by going to the source at line https://github.com/cloudflare/workers-sdk/blob/ee63d3175ca7e7e09ec4fcb7f9d5b4cd2eddcb60/packages/vitest-pool-workers/src/pool/index.ts#L633

and add in the "else"

if (!singleInstance) {
   for (const testFile of project.testFiles) {
        if (!project.mf.get(testFile)) { 
          log.info(`Starting isolated runtime for ${testFile}...`);
          project.mf.set(testFile, new Miniflare(mfOptions));
        }
    }
}

image

It looks like the caching logic assumes that runTests is run with all test files always, which does not seem to be the case from the VSCode extension

Please provide a link to a minimal reproduction

No response

Please provide any relevant error logs

No response

gourgouris avatar May 30 '24 14:05 gourgouris

I'm seeing this too, though thankfully enabling singleWorker: true fixes the issue without much downside in my configuration.

wilhuff avatar Aug 29 '24 22:08 wilhuff

I have this same issue, and am unable to use singleWorker: true unfortunately. I can confirm however that the above solution works - when pasting the above if (!singleinstance) block into the else statement (currently at line 1451 of dist/pool/index.mjs), the issue is fixed. Would love that merged into the project, happy to create the branch if it would be useful.

connorpb avatar Feb 25 '25 01:02 connorpb