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

fix: don't mock css-module if inline is specified #3952

Merged
merged 4 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/vitest/src/node/plugins/cssEnabler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const cssLangs = '\\.(css|less|sass|scss|styl|stylus|pcss|postcss)($|\\?)'
const cssLangRE = new RegExp(cssLangs)
const cssModuleRE = new RegExp(`\\.module${cssLangs}`)
const cssInlineRE = /[?&]inline(&|$)/

function isCSS(id: string) {
return cssLangRE.test(id)
Expand All @@ -16,6 +17,12 @@
return cssModuleRE.test(id)
}

// inline css requests are expected to just return the
// string content directly and not the proxy module
function isInline(id: string) {
return cssInlineRE.test(id);

Check failure on line 23 in packages/vitest/src/node/plugins/cssEnabler.ts

View workflow job for this annotation

GitHub Actions / lint

Extra semicolon
}

function getCSSModuleProxyReturn(strategy: CSSModuleScopeStrategy, filename: string) {
if (strategy === 'non-scoped')
return 'style'
Expand Down Expand Up @@ -55,7 +62,7 @@
if (!isCSS(id) || shouldProcessCSS(id))
return

if (isCSSModule(id)) {
if (isCSSModule(id) && !isInline(id)) {
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
// return proxy for css modules, so that imported module has names:
// styles.foo returns a "foo" instead of "undefined"
// we don't use code content to generate hash for "scoped", because it's empty
Expand Down
21 changes: 21 additions & 0 deletions test/css/test/process-inline.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, expect, test } from 'vitest'
import { useRemoveStyles } from './utils'

describe('processing inline css', () => {
useRemoveStyles()

test('doesn\'t apply css', async () => {
await import('../src/App.module.css?inline')

const element = document.createElement('div')
element.className = 'main'
const computed = window.getComputedStyle(element)
expect(computed.display, 'css is not processed').toBe('block')
})

test('returns a string', async () => {
const { default: style } = await import('../src/App.module.css?inline')
expect(typeof style).toBe('string');

Check failure on line 18 in test/css/test/process-inline.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Extra semicolon
console.log(style);

Check failure on line 19 in test/css/test/process-inline.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

Check failure on line 19 in test/css/test/process-inline.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Extra semicolon
thebanjomatic marked this conversation as resolved.
Show resolved Hide resolved
})
})
5 changes: 3 additions & 2 deletions test/css/testing.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { startVitest } from 'vitest/node'
const configs = [
['test/default-css', {}],
['test/process-css', { include: [/App\.css/] }],
['test/process-module', { include: [/App\.module\.css/] }],
[['test/process-module', 'test/process-inline'], { include: [/App\.module\.css/] }],
['test/scope-module', { include: [/App\.module\.css/], modules: { classNameStrategy: 'scoped' } }],
['test/non-scope-module', { include: [/App\.module\.css/], modules: { classNameStrategy: 'non-scoped' } }],
]

async function runTests() {
for (const [name, config] of configs) {
await startVitest('test', [name], {
const names = Array.isArray(name) ? name : [name];
await startVitest('test', names, {
run: true,
css: config,
update: false,
Expand Down
Loading