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

Testing with Vitest #666

Closed
domosedov opened this issue Apr 24, 2022 · 10 comments
Closed

Testing with Vitest #666

domosedov opened this issue Apr 24, 2022 · 10 comments

Comments

@domosedov
Copy link
Contributor

Hello, any help how test vanilla-extract with Vitest?

Demo: Stackblitz

i have an error with npm run test

 FAIL  src/components/button/button.test.tsx [ src/components/button/button.test.tsx ]
Error: Styles were unable to be assigned to a file. This is generally caused by one of the following:

- You may have created styles outside of a '.css.ts' context
- You may have incorrect configuration. See https://vanilla-extract.style/documentation/setup
@jrnxf
Copy link

jrnxf commented Apr 25, 2022

Yep I'm seeing the exact same thing. My configuration is as follows:

/// <reference types="vitest" />
/// <reference types="vite/client" />

import react from "@vitejs/plugin-react";
import { vanillaExtractPlugin } from "@vanilla-extract/vite-plugin";

export default {
  plugins: [react(), vanillaExtractPlugin()],
  test: {
    globals: true,
    environment: "happy-dom",
    setupFiles: ["./setup-test-env.ts"],
    include: ["./src/**/*.{test,spec}.tsx"],
    watchIgnore: [".*\\/node_modules\\/.*", ".*\\/build\\/.*"],
  },
};

@tyrkinn
Copy link

tyrkinn commented Apr 26, 2022

Same problem. Also tried to add babel plugin

import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
import { vanillaExtractPlugin } from "@vanilla-extract/vite-plugin";

export default defineConfig({
  plugins: [
    vanillaExtractPlugin(),
    react({
      babel: {
        plugins: ["@vanilla-extract/babel-plugin"],
      },
    }),
  ],
  test: {
    globals: true,
    environment: "jsdom",
    setupFiles: "setupTests.ts",
  },
});

@AndrewLeedham
Copy link
Contributor

Hey All. I ran into this a few weeks ago, it was a pain to debug. The issue is with some code being run in ESM and some in CJS so the file scopes stuff is split across 2 separate modules and therefore cannot see each-others local variables. Long story short you need to include the following in your Vite config:

{
  // ...
  test {
    deps: {
      fallbackCJS: true,
    }
  }
}

https://vitest.dev/config/#deps

See fixed fork here: https://stackblitz.com/edit/vitejs-vite-1fmagr?file=vite.config.ts

@domosedov
Copy link
Contributor Author

Hey All. I ran into this a few weeks ago, it was a pain to debug. The issue is with some code being run in ESM and some in CJS so the file scopes stuff is split across 2 separate modules and therefore cannot see each-others local variables. Long story short you need to include the following in your Vite config:

{
  // ...
  test {
    deps: {
      fallbackCJS: true,
    }
  }
}

https://vitest.dev/config/#deps

See fixed fork here: https://stackblitz.com/edit/vitejs-vite-1fmagr?file=vite.config.ts

Thank you 👍

@frankwallis
Copy link

frankwallis commented May 28, 2022

It looks to be related to this issue: vitest-dev/vitest#747

Similar to the comment in that thread one solution is to delete
node_modules/@vanilla-extract/css/fileScope/package.json

which interferes with the module resolution defined at the top-level
(fallbackCJS: true did not work for me)

@gkiely
Copy link

gkiely commented Jul 8, 2022

@markdalgleish Might be worth adding @domosedov solution to the docs? It does print this error message by default with vitest.

image

MH4GF added a commit to MH4GF/log.mh4gf.dev that referenced this issue Jul 28, 2022
@AndrewLeedham
Copy link
Contributor

This has been properly fixed with #833

@swernerx
Copy link

This has been properly fixed with #833

That's great. It's not yet in any release, right? Because when removing the code, I still see the old behavior.

@AndrewLeedham
Copy link
Contributor

@swernerx it was released in @vanilla-extract/[email protected], working for me.

@swernerx
Copy link

Interesting, I am on 1.9.2, and it is definitely not working without the fallbackCJS hack.

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

7 participants