eddyerburgh / vue-test-utils-jest-example Goto Github PK
View Code? Open in Web Editor NEWAn example vue-test-utils project with jest
An example vue-test-utils project with jest
After much Googling I'm coming up short on how to test a Vue plugin using Jest without getting the rather annoying warning log:
You are running Vue in development mode.
I've searched an searched on how to disable/mute/appease this and the only solution I have found that works is to set NODE_ENV
to "production" before running jest
:
"test": "cross-env NODE_ENV=production jest"
Since jest
sets NODE_ENV
to "test" by default, I'm not sure if this has any side effects or not?
Either way, it feels a little wrong to me.
As a really simple test case, let's say I have a plugin that looks like this:
// plugin.js
export default {
install(Vue, options) {
Vue.prototype.$upper = (text) => text.toUpperCase()
}
}
...then if I wanted to test this plugin, my test file might look something like this:
// plugin.spec.js
import Vue from 'vue'
import VueUpper from './plugin'
it('adds an $upper method to the Vue prototype', () => {
expect(Vue.prototype.$upper).toBeUndefined()
Vue.use(VueUpper)
expect(typeof Vue.prototype.$upper).toBe('function')
})
it('transforms a string to upper case', () => {
expect(Vue.prototype.$upper('foo')).toBe('FOO')
})
Any help would be very much appreciated!
If I use an alias to resolve my components path e.g. import Message from '@/components/Message'
instead of import Message from ./Message
, my tests fail.
Cannot find module './src/components/Message' from 'MessageToggle.vue'
I can temporarly use relative paths instead of @/
, but using aliases is extremely convenient for me and my team ...
I also tried the current official unit test usage with karma/mocha/phantomjs in vuejs-tempaltes/webpack, it didn't work neither: vuejs/vue-loader#195 (comment)).
Thanks for your awesome work btw!
test pass
yarn unit
yarn unit v0.23.4
$ jest
PASS test/specs/Message.spec.js
FAIL test/specs/List.spec.js
● Test suite failed to run
Couldn't find preset "es2015" relative to directory "/Volumes/DevelopOnly/Github/vue-test-utils-jest-example"
at node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
at Array.map (<anonymous>)
FAIL test/specs/MessageToggle.spec.js
● Test suite failed to run
Couldn't find preset "es2015" relative to directory "/Volumes/DevelopOnly/Github/vue-test-utils-jest-example"
at node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
at Array.map (<anonymous>)
Test Suites: 2 failed, 1 passed, 3 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 4.532s
Ran all test suites.
error Command failed with exit code 1.
same with npm install && npm run unit
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.