Comments (9)
Hi,
I'm experimenting with Jest and Vue as well. In React+CSS Modules+Jest testing I used identity-obj-proxy to mock CSS modules classes with success.
In a test project I've setup a working test as follow:
Root/index.vue
<template>
<div :class="$style.root">
Hello {{ message }}
</div>
</template>
<script>
export default {
data() {
return {
message: 'World'
};
},
name: 'Root'
};
</script>
<style module>
.root {
font-size: 12px;
}
</style>
Root/index.test.js
import Vue from 'vue';
import idObj from 'identity-obj-proxy';
import Root from './index';
/* eslint-env jest */
describe('<Root>', () => {
let Constructor;
let vm;
beforeEach(() => {
Constructor = Vue.extend(Root);
Constructor.prototype.$style = idObj; // <-- Mocking $style
vm = new Constructor().$mount();
});
it('should render a <div> tag', () => {
expect(vm.$el.tagName).toBe('DIV');
});
it('should contain "Hello World"', () => {
expect(vm.$el.textContent.trim()).toBe('Hello World');
});
});
Relevant Jest config:
"moduleFileExtensions": ["js", "json", "vue"],
"transform": {
"^.+\\.js$": "babel-jest",
".*\\.(vue)$": "jest-vue-preprocessor"
},
Hope it helps
from jest-vue-preprocessor.
I ended up just creating a mixin that I load into my tests. It just creates an empty object on every component's $style
property. I'd still like Vue to create a synchronous converter that isn't tied to webpack or browserify that can be used instead of these preprocessors.
The Mixin
export const CssModuleTestHelperMixin = {
install (Vue, options) {
Vue.mixin({
created () {
this.$style = {}
}
})
}
}
In My Tests
import Vue from 'vue'
import { CssModuleTestHelperMixin } from '../utils/test-helper.js'
import { mount } from 'avoriaz'
import Header from './header.vue'
// shims $style so that jest-preprocessor doesn't need to compile styles
Vue.use(CssModuleTestHelperMixin)
test('test header', () => {
let vm = mount(Header)
})
from jest-vue-preprocessor.
I believe .vue
files need webpack or some other library to be processed and usable on other tools, but maybe I'm not getting the point.
Using a mixin is a good idea though. You could provide identity-obj-proxy
to this.$style
instead of an empty object if you'd need to check which classes gets applied
from jest-vue-preprocessor.
I also checked out avoriaz, didn't know that project! Cool! 😄
from jest-vue-preprocessor.
@dwightjack Yes, I know that .vue files need webpack or browserify. That's my point. It'd be nice if the Vue devs would create a standalone tool to process the .vue files while still getting all the features such as CSS Modules, etc.
Also, YES, Avoriaz is pretty sweet.
from jest-vue-preprocessor.
Ok I got it. I believe it's a way to not force developers in yet a different toolchain. But i get your point.
from jest-vue-preprocessor.
Right, but what I was thinking is that vue-loader and vueify would just wrap it
from jest-vue-preprocessor.
@dwightjack nice solution, thank you!
from jest-vue-preprocessor.
Need support at the level of the transformer, the same as it is implemented at the level of the vue-loader. Otherwise, everything is bad. It's bad when module names are used, it's bad when class names are used as variables inside a component, it's always bad if you use cssModule and Jest.
from jest-vue-preprocessor.
Related Issues (20)
- find-babel-config is basically abandonware, and doesn't support babel.config.js
- Upgrade for peer deps vue-template-compiler, vue-template-es2015-compiler HOT 4
- lost the param fileName
- Supoort vue 2.7.x
- Problem getting jest to work with vue and webpack HOT 6
- vue-template-compiler should be a peer dependency HOT 1
- No sourcemapping HOT 3
- Failed to mount component: template or render function not defined HOT 1
- mapCoverage:true produces misleading Line in coverage report (wrong sourcemaps)
- TypeError: Jest: a transform's `process` function must return a string, HOT 7
- Vue Components do not appear in code coverage if there is no style tag HOT 1
- Coverage problem for Vue component with src attribute
- not recognizing lang="html" (Error: unknown <template lang="html">) HOT 2
- Unexpected token import HOT 4
- Relative imports in multi-file-components do not work in subfolders
- Can't Handle Functional Components HOT 6
- Changelog should highlight breaking change about node engine HOT 2
- Style tag in SFC should not result in Jest coverage results
- Version 10 of node.js has been released
- Line numbers aren't correct
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jest-vue-preprocessor.