From 4012b2cb62dc73b5043fe75e4818ee34ec555dd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Dziewo=C5=84ski?= Date: Fri, 8 Oct 2021 15:53:28 +0200 Subject: [PATCH] feat: pass styleOption into compiler (#398) --- README.md | 21 ++++++++++++++++ e2e/2.x/basic/test.js | 32 +++++++++++++++++++++++++ packages/vue2-jest/lib/process-style.js | 5 +++- 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 244463c8..51aab1a6 100644 --- a/README.md +++ b/README.md @@ -232,3 +232,24 @@ You can provide [TemplateCompileOptions](https://github.com/vuejs/component-comp } } ``` + +## Style options + +Possbility to change style loader options (sass, scss, less etc). + +`styleOptions`: `Object` Default `{}`. + +```json +{ + "jest": { + "globals": { + "vue-jest": { + "styleOptions": { + "quietDeps" // e.q. sass options https://sass-lang.com/documentation/js-api#quietdeps + // unfortunately rest options like `data`, `file` doesnt work because @vue/compiler-component-utils internally overwrite options with their values + }, + } + } + } +} +``` diff --git a/e2e/2.x/basic/test.js b/e2e/2.x/basic/test.js index ae1c004b..03c124d0 100644 --- a/e2e/2.x/basic/test.js +++ b/e2e/2.x/basic/test.js @@ -19,7 +19,13 @@ import Pug from './components/Pug.vue' import PugRelative from './components/PugRelativeExtends.vue' import Jsx from './components/Jsx.vue' import Constructor from './components/Constructor.vue' +import { compileStyle } from '@vue/component-compiler-utils' +jest.mock('@vue/component-compiler-utils', () => ({ + ...jest.requireActual('@vue/component-compiler-utils'), + compileStyle: jest.fn(() => ({ errors: [], code: '' })) +})) +beforeEach(() => jest.clearAllMocks()) test('processes .vue files', () => { const wrapper = mount(Basic) expect(wrapper.vm.msg).toEqual('Welcome to Your Vue.js App') @@ -149,3 +155,29 @@ test('processes SFC with no template', () => { const wrapper = mount(RenderFunction) expect(wrapper.element.tagName).toBe('SECTION') }) + +test('should pass properly "styleOptions" into "preprocessOptions"', () => { + const filePath = resolve(__dirname, './components/Basic.vue') + const fileString = readFileSync(filePath, { encoding: 'utf8' }) + const config = { + moduleFileExtensions: ['js', 'vue'], + globals: { + 'vue-jest': { + styleOptions: { + quietDeps: true + } + } + } + } + + jestVue.process(fileString, filePath, { + config + }) + + expect(compileStyle.mock.calls[0][0].preprocessOptions).toStrictEqual({ + quietDeps: true + }) + expect(compileStyle.mock.calls[1][0].preprocessOptions).toStrictEqual({ + quietDeps: true + }) +}) diff --git a/packages/vue2-jest/lib/process-style.js b/packages/vue2-jest/lib/process-style.js index a5850c30..589a03fe 100644 --- a/packages/vue2-jest/lib/process-style.js +++ b/packages/vue2-jest/lib/process-style.js @@ -92,7 +92,10 @@ module.exports = function processStyle(stylePart, filePath, config = {}) { source: content, filePath, preprocessLang: stylePart.lang, - preprocessOptions, + preprocessOptions: { + ...preprocessOptions, + ...vueJestConfig.styleOptions + }, scoped: false }) logResultErrors(result)