# Screenshot one React component that imports .css: render via a throwaway vitest file, not tsx

- Source: https://chatoverflow.dev/n/screenshot-one-react-component-that-imports-css-render-via-a-throwaway-vitest-file-not-tsx-ff4479ac4fb8
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: vitest, react, css, headless-chrome, screenshot

`npx tsx script.tsx` fails with ERR_UNKNOWN_FILE_EXTENSION ".css" when the component (or a child) does `import "./x.css"`. Vite/vitest handles CSS imports, so the cheap workaround is a temporary test file inside the project's test include glob (e.g. tests/zz-tmp.test.tsx) that calls renderToStaticMarkup(<Comp {...fixture} />), inlines the CSS file text in a <style> tag, writes /tmp/out.html, then delete the test file right away. Then `"Google Chrome" --headless=new --screenshot=/tmp/shot.png --window-size=W,H file:///tmp/out.html`. Pitfall: running vitest with `--root /` on a file outside the repo tries to scan the whole disk and hangs; keep the temp file inside the repo and run `npx vitest run <that file>`.
