forked from vitejs/vite
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsass-tests.ts
More file actions
135 lines (119 loc) · 4.61 KB
/
Copy pathsass-tests.ts
File metadata and controls
135 lines (119 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { expect, test } from 'vitest'
import {
editFile,
getBg,
getColor,
isBuild,
page,
untilUpdated,
viteTestUrl,
} from '~utils'
export const sassTest = () => {
test('sass', async () => {
const imported = await page.$('.sass')
const atImport = await page.$('.sass-at-import')
const atImportAlias = await page.$('.sass-at-import-alias')
const atImportRelative = await page.$('.sass-at-import-relative')
const urlStartsWithVariable = await page.$('.sass-url-starts-with-variable')
const urlStartsWithVariableInterpolation1 = await page.$(
'.sass-url-starts-with-interpolation1',
)
const urlStartsWithVariableInterpolation2 = await page.$(
'.sass-url-starts-with-interpolation2',
)
const urlStartsWithVariableConcat = await page.$(
'.sass-url-starts-with-variable-concat',
)
const urlStartsWithFunctionCall = await page.$(
'.sass-url-starts-with-function-call',
)
const partialImport = await page.$('.sass-partial')
expect(await getColor(imported)).toBe('orange')
expect(await getColor(atImport)).toBe('olive')
expect(await getBg(atImport)).toMatch(
isBuild ? /base64/ : '/nested/icon.png',
)
expect(await getColor(atImportAlias)).toBe('olive')
expect(await getBg(atImportAlias)).toMatch(
isBuild ? /base64/ : '/nested/icon.png',
)
expect(await getColor(atImportRelative)).toBe('olive')
expect(await getBg(atImportRelative)).toMatch(
isBuild ? /base64/ : '/nested/icon.png',
)
expect(await getBg(urlStartsWithVariable)).toMatch(
isBuild ? /ok-[-\w]+\.png/ : `${viteTestUrl}/ok.png`,
)
expect(await getBg(urlStartsWithVariableInterpolation1)).toMatch(
isBuild ? /ok-[-\w]+\.png/ : `${viteTestUrl}/ok.png`,
)
expect(await getBg(urlStartsWithVariableInterpolation2)).toMatch(
isBuild ? /ok-[-\w]+\.png/ : `${viteTestUrl}/ok.png`,
)
expect(await getBg(urlStartsWithVariableConcat)).toMatch(
isBuild ? /ok-[-\w]+\.png/ : `${viteTestUrl}/ok.png`,
)
expect(await getBg(urlStartsWithFunctionCall)).toMatch(
isBuild ? /ok-[-\w]+\.png/ : `${viteTestUrl}/ok.png`,
)
expect(await getColor(partialImport)).toBe('orchid')
expect(await getColor(await page.$('.sass-file-absolute'))).toBe('orange')
expect(await getColor(await page.$('.sass-dir-index'))).toBe('orange')
expect(await getColor(await page.$('.sass-root-relative'))).toBe('orange')
if (isBuild) return
editFile('sass.scss', (code) =>
code.replace('color: $injectedColor', 'color: red'),
)
await untilUpdated(() => getColor(imported), 'red')
editFile('nested/_index.scss', (code) =>
code.replace('color: olive', 'color: blue'),
)
await untilUpdated(() => getColor(atImport), 'blue')
editFile('nested/_partial.scss', (code) =>
code.replace('color: orchid', 'color: green'),
)
await untilUpdated(() => getColor(partialImport), 'green')
})
}
export const sassModuleTests = (enableHmrTests = false) => {
test('sass modules composes/from path resolving', async () => {
const imported = await page.$('.path-resolved-modules-sass')
expect(await getColor(imported)).toBe('orangered')
// check if the generated CSS module class name is indeed using the
// format specified in vite.config.js
expect(await imported.getAttribute('class')).toMatch(
/.composed-module__apply-color___[\w-]{5}/,
)
expect(await imported.getAttribute('class')).toMatch(
/.composes-path-resolving-module__path-resolving-sass___[\w-]{5}/,
)
// @todo HMR is not working on this situation.
// editFile('composed.module.scss', (code) =>
// code.replace('color: orangered', 'color: red')
// )
// await untilUpdated(() => getColor(imported), 'red')
})
test('css modules w/ sass', async () => {
const imported = await page.$('.modules-sass')
expect(await getColor(imported)).toBe('orangered')
expect(await imported.getAttribute('class')).toMatch(
/.mod-module__apply-color___[\w-]{5}/,
)
if (isBuild) return
editFile('mod.module.scss', (code) =>
code.replace('color: orangered', 'color: blue'),
)
await untilUpdated(() => getColor(imported), 'blue')
})
}
export const sassOtherTests = () => {
test('@import dependency w/ sass entry', async () => {
expect(await getColor('.css-dep-sass')).toBe('orange')
})
test('@import dependency w/ sass export mapping', async () => {
expect(await getColor('.css-dep-exports-sass')).toBe('orange')
})
test('@import dependency w/out package scss', async () => {
expect(await getColor('.sass-dep')).toBe('lavender')
})
}