Skip to content

Commit b44696e

Browse files
committed
test: use cssesc to test with escaped class names
1 parent 4b2e9ca commit b44696e

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"@babel/core": "^7.7.7",
5656
"@babel/preset-env": "^7.11.5",
5757
"@intlify/vue-i18n-loader": "^3.0.0",
58+
"@types/cssesc": "^3.0.2",
5859
"@types/estree": "^0.0.45",
5960
"@types/hash-sum": "^1.0.0",
6061
"@types/jest": "^26.0.13",
@@ -65,6 +66,7 @@
6566
"cache-loader": "^4.1.0",
6667
"conventional-changelog-cli": "^2.1.1",
6768
"css-loader": "^4.3.0",
69+
"cssesc": "^3.0.0",
6870
"file-loader": "^6.1.0",
6971
"html-webpack-plugin": "^4.5.0",
7072
"html-webpack-plugin-v5": "npm:html-webpack-plugin@^5.3.2",

pnpm-lock.yaml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/advanced.spec.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { SourceMapConsumer } from 'source-map'
22
import { fs as mfs } from 'memfs'
3+
import cssesc from 'cssesc'
34
import {
45
bundle,
56
mockBundleAndRun,
@@ -14,7 +15,7 @@ test('support chaining with other loaders', async () => {
1415
const { componentModule } = await mockBundleAndRun({
1516
entry: 'basic.vue',
1617
modify: (config) => {
17-
config!.module!.rules[0] = {
18+
config!.module!.rules![0] = {
1819
test: /\.vue$/,
1920
use: [DEFAULT_VUE_USE, require.resolve('./mock-loaders/js')],
2021
}
@@ -28,7 +29,7 @@ test.skip('inherit queries on files', async () => {
2829
const { componentModule } = await mockBundleAndRun({
2930
entry: 'basic.vue?change',
3031
modify: (config) => {
31-
config!.module!.rules[0] = {
32+
config!.module!.rules![0] = {
3233
test: /\.vue$/,
3334
use: [DEFAULT_VUE_USE, require.resolve('./mock-loaders/query')],
3435
}
@@ -199,9 +200,10 @@ test('support rules with oneOf', async () => {
199200
const { window, instance } = await run('css-modules-simple.vue')
200201

201202
const className = instance.$style.red
202-
expect(className).toMatch(/^red_\w{5}/)
203+
const escapedClassName = cssesc(instance.$style.red, { isIdentifier: true })
204+
expect(className).toMatch(/^red_.{5}/)
203205
style = normalizeNewline(window.document.querySelector('style')!.textContent!)
204-
expect(style).toContain('.' + className + ' {\n color: red;\n}')
206+
expect(style).toContain('.' + escapedClassName + ' {\n color: red;\n}')
205207
})
206208

207209
test.todo('should work with eslint loader')

test/style.spec.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import cssesc from 'cssesc'
12
import {
23
mockBundleAndRun,
34
genId,
@@ -130,6 +131,7 @@ test('CSS Modules', async () => {
130131

131132
// get local class name
132133
const className = instance.$style.red
134+
const escapedClassName = cssesc(instance.$style.red, { isIdentifier: true })
133135
expect(className).toMatch(regexToMatch)
134136

135137
// class name in style
@@ -140,7 +142,7 @@ test('CSS Modules', async () => {
140142
})
141143
.join('\n')
142144
style = normalizeNewline(style)
143-
expect(style).toContain('.' + className + ' {\n color: red;\n}')
145+
expect(style).toContain('.' + escapedClassName + ' {\n color: red;\n}')
144146

145147
// animation name
146148
const match = style.match(/@keyframes\s+(\S+)\s+{/)
@@ -151,9 +153,12 @@ test('CSS Modules', async () => {
151153

152154
// default module + pre-processor + scoped
153155
const anotherClassName = instance.$style.red
156+
const escapedAnotherClassName = cssesc(instance.$style.red, {
157+
isIdentifier: true,
158+
})
154159
expect(anotherClassName).toMatch(regexToMatch)
155160
const id = 'data-v-' + genId('css-modules.vue')
156-
expect(style).toContain('.' + anotherClassName + '[' + id + ']')
161+
expect(style).toContain('.' + escapedAnotherClassName + '[' + id + ']')
157162
}
158163

159164
// default ident
@@ -194,8 +199,9 @@ test('CSS Modules Extend', async () => {
194199
})
195200

196201
expect(instance.$el.className).toBe(instance.$style.red)
202+
const escapedClassName = cssesc(instance.$style.red, { isIdentifier: true })
197203
const style = window.document.querySelectorAll('style')![1]!.textContent
198-
expect(style).toContain(`.${instance.$style.red} {\n color: #FF0000;\n}`)
204+
expect(style).toContain(`.${escapedClassName} {\n color: #FF0000;\n}`)
199205
})
200206

201207
test('v-bind() in CSS', async () => {

0 commit comments

Comments
 (0)