Git Product home page Git Product logo

Comments (9)

dwightjack avatar dwightjack commented on June 14, 2024 4

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.

joezimjs avatar joezimjs commented on June 14, 2024

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.

dwightjack avatar dwightjack commented on June 14, 2024

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.

dwightjack avatar dwightjack commented on June 14, 2024

I also checked out avoriaz, didn't know that project! Cool! 😄

from jest-vue-preprocessor.

joezimjs avatar joezimjs commented on June 14, 2024

@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.

dwightjack avatar dwightjack commented on June 14, 2024

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.

joezimjs avatar joezimjs commented on June 14, 2024

Right, but what I was thinking is that vue-loader and vueify would just wrap it

from jest-vue-preprocessor.

epszaw avatar epszaw commented on June 14, 2024

@dwightjack nice solution, thank you!

from jest-vue-preprocessor.

PowerfulPony avatar PowerfulPony commented on June 14, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.