Skip to content

Commit

Permalink
fix: mock css files imported with "require" (#2679)
Browse files Browse the repository at this point in the history
This is usually happens inside UI libraries
  • Loading branch information
sheremet-va committed Jan 16, 2023
1 parent f22ca93 commit 6c1a26a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/vitest/src/runtime/setup.ts
@@ -1,3 +1,5 @@
/* eslint-disable n/no-deprecated-api */

import { installSourcemapsSupport } from 'vite-node/source-map'
import { environments } from '../integrations/env'
import type { Environment, ResolvedConfig } from '../types'
Expand All @@ -23,6 +25,11 @@ export async function setupGlobalEnv(config: ResolvedConfig) {
if (globalSetup)
return

// always mock "required" `css` files, because we cannot process them
require.extensions['.css'] = () => ({})
require.extensions['.scss'] = () => ({})
require.extensions['.sass'] = () => ({})

globalSetup = true

if (isNode) {
Expand Down
3 changes: 3 additions & 0 deletions test/core/src/file-css.css
@@ -0,0 +1,3 @@
.red {
color: red;
}
2 changes: 2 additions & 0 deletions test/core/src/file-sass.sass
@@ -0,0 +1,2 @@
.red
color: red;
3 changes: 3 additions & 0 deletions test/core/src/file-scss.scss
@@ -0,0 +1,3 @@
.red {
color: red;
}
15 changes: 15 additions & 0 deletions test/core/test/require.test.ts
@@ -0,0 +1,15 @@
import { describe, expect, it } from 'vitest'

const _require = require

describe('using "require" to import a module', () => {
it('importing css files works, but doesn\'t process them', () => {
const css = _require('./../src/file-css.css')
const sass = _require('./../src/file-sass.sass')
const scss = _require('./../src/file-scss.scss')

expect(css).toEqual({})
expect(sass).toEqual({})
expect(scss).toEqual({})
})
})

0 comments on commit 6c1a26a

Please sign in to comment.