Bug Report
Version
jest-styled-components@7.3.1, vitest@4.1.0
Description
The new /vitest entrypoint added in 7.3.x cannot be imported in a Vitest project because it uses require('vitest'), but Vitest is ESM-only and throws an error when loaded via CJS require().
vitest/index.js:
const { expect, beforeEach } = require('vitest'); // ← causes the crash
Because the package has "type": "commonjs", Node resolves this to vitest/index.cjs, which is Vitest's own CJS shim that intentionally throws:
Error: Vitest cannot be imported in a CommonJS module using require(). Please use "import" instead.
Steps to Reproduce
- Install
jest-styled-components@7.3.1 in a Vitest project
- Add
import 'jest-styled-components/vitest' to your setup file
- Run
vitest
Error
Error: Vitest cannot be imported in a CommonJS module using require(). Please use "import" instead.
❯ Object.<anonymous> node_modules/vitest/index.cjs:1:7
❯ node_modules/jest-styled-components/vitest/index.js:1:32
Workaround
Import the main package entry instead of /vitest. The src/index.js main entry uses typeof beforeEach / typeof expect guards on globals rather than require('vitest'), so it works correctly when Vitest's globals: true is set:
// Instead of: import 'jest-styled-components/vitest'
import 'jest-styled-components'
Suggested Fix
The /vitest entrypoint should use ESM import syntax rather than CJS require. Options:
- Add an
exports field with an "import" condition pointing to an .mjs version of the entrypoint
- Convert
vitest/index.js to ESM (.mjs extension or scoped "type": "module")
// vitest/index.mjs
import { expect, beforeEach } from 'vitest';
import toHaveStyleRule from '../src/toHaveStyleRule.js';
import styleSheetSerializer from '../src/styleSheetSerializer.js';
import { resetStyleSheet } from '../src/utils.js';
beforeEach(resetStyleSheet);
expect.addSnapshotSerializer(styleSheetSerializer);
expect.extend({ toHaveStyleRule });
export { styleSheetSerializer, resetStyleSheet };
Bug Report
Version
jest-styled-components@7.3.1,vitest@4.1.0Description
The new
/vitestentrypoint added in 7.3.x cannot be imported in a Vitest project because it usesrequire('vitest'), but Vitest is ESM-only and throws an error when loaded via CJSrequire().vitest/index.js:Because the package has
"type": "commonjs", Node resolves this tovitest/index.cjs, which is Vitest's own CJS shim that intentionally throws:Steps to Reproduce
jest-styled-components@7.3.1in a Vitest projectimport 'jest-styled-components/vitest'to your setup filevitestError
Workaround
Import the main package entry instead of
/vitest. Thesrc/index.jsmain entry usestypeof beforeEach/typeof expectguards on globals rather thanrequire('vitest'), so it works correctly when Vitest'sglobals: trueis set:Suggested Fix
The
/vitestentrypoint should use ESMimportsyntax rather than CJSrequire. Options:exportsfield with an"import"condition pointing to an.mjsversion of the entrypointvitest/index.jsto ESM (.mjsextension or scoped"type": "module")