Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fails when trying to use custom Vitest fixtures #1852

@shairez

Description

@shairez

Please provide the environment you discovered this bug in.

clone
https://github.com/shairez/angular-vitest-fixtures-bug

  1. pnpm install
  2. pnpm test

see the error

Which area/package is the issue in?

vite-plugin-angular

Description

There's a collision between the angular vite plugin compilation that happens via the angular-vite-plugin and the way vitest analyzes the fixtures destructured object.

Vitest uses .toString on the "provider" function of the fixture, and expects it to have a destructured object that contains that fixtures' dependencies.

AKA myFixture: async ({ }, use) => { ... }

It then throws if it doesn't find a { } as the first param.

The problem:

even if you write the code correctly (as can be seen in the repro repo), by the time the code gets to vitest, the Angular plugin has already compiled it via performCompilation and replaced the {} with a _a and that causes vitest to throw.

I thought a workaround could be to add my fixtures file to the transformFilter option, but that revealed a different bug.

After manually applying the workaround, I then noticed this bug happens for the test files themselves (when they try to inject a fixture).

So I think finding the root cause and fixing that is better than filtering it.

My current thinking:

we need to make sure any file that imports the `test from vitest and extends it, needs not to be compiled before vitest has a chance to read it.

(maybe switching the order somehow?)

I'm willing to write a PR for this bug (or better yet, pair with @brandonroberts on this 😊 ), as I think that fixtures are crucial for component testing DX.

Please provide the exception or error you saw

FAIL  src/app/app.component.spec.ts [ src/app/app.component.spec.ts ]                                                                                                                        
Error: The first argument inside a fixture must use object destructuring pattern, e.g. ({ test } => {}). Instead, received "_a".

Other information

No response

I would be willing to submit a PR to fix this issue

  • Yes
  • No

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions