Skip to content

Commit 4012b2c

Browse files
feat: pass styleOption into compiler (#398)
1 parent c6292f3 commit 4012b2c

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,24 @@ You can provide [TemplateCompileOptions](https://github.com/vuejs/component-comp
232232
}
233233
}
234234
```
235+
236+
## Style options
237+
238+
Possbility to change style loader options (sass, scss, less etc).
239+
240+
`styleOptions`: `Object` Default `{}`.
241+
242+
```json
243+
{
244+
"jest": {
245+
"globals": {
246+
"vue-jest": {
247+
"styleOptions": {
248+
"quietDeps" // e.q. sass options https://sass-lang.com/documentation/js-api#quietdeps
249+
// unfortunately rest options like `data`, `file` doesnt work because @vue/compiler-component-utils internally overwrite options with their values
250+
},
251+
}
252+
}
253+
}
254+
}
255+
```

e2e/2.x/basic/test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ import Pug from './components/Pug.vue'
1919
import PugRelative from './components/PugRelativeExtends.vue'
2020
import Jsx from './components/Jsx.vue'
2121
import Constructor from './components/Constructor.vue'
22+
import { compileStyle } from '@vue/component-compiler-utils'
23+
jest.mock('@vue/component-compiler-utils', () => ({
24+
...jest.requireActual('@vue/component-compiler-utils'),
25+
compileStyle: jest.fn(() => ({ errors: [], code: '' }))
26+
}))
2227

28+
beforeEach(() => jest.clearAllMocks())
2329
test('processes .vue files', () => {
2430
const wrapper = mount(Basic)
2531
expect(wrapper.vm.msg).toEqual('Welcome to Your Vue.js App')
@@ -149,3 +155,29 @@ test('processes SFC with no template', () => {
149155
const wrapper = mount(RenderFunction)
150156
expect(wrapper.element.tagName).toBe('SECTION')
151157
})
158+
159+
test('should pass properly "styleOptions" into "preprocessOptions"', () => {
160+
const filePath = resolve(__dirname, './components/Basic.vue')
161+
const fileString = readFileSync(filePath, { encoding: 'utf8' })
162+
const config = {
163+
moduleFileExtensions: ['js', 'vue'],
164+
globals: {
165+
'vue-jest': {
166+
styleOptions: {
167+
quietDeps: true
168+
}
169+
}
170+
}
171+
}
172+
173+
jestVue.process(fileString, filePath, {
174+
config
175+
})
176+
177+
expect(compileStyle.mock.calls[0][0].preprocessOptions).toStrictEqual({
178+
quietDeps: true
179+
})
180+
expect(compileStyle.mock.calls[1][0].preprocessOptions).toStrictEqual({
181+
quietDeps: true
182+
})
183+
})

packages/vue2-jest/lib/process-style.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ module.exports = function processStyle(stylePart, filePath, config = {}) {
9292
source: content,
9393
filePath,
9494
preprocessLang: stylePart.lang,
95-
preprocessOptions,
95+
preprocessOptions: {
96+
...preprocessOptions,
97+
...vueJestConfig.styleOptions
98+
},
9699
scoped: false
97100
})
98101
logResultErrors(result)

0 commit comments

Comments
 (0)