Skip to content

Instantly share code, notes, and snippets.

@pbalduino
Created August 17, 2017 04:35
Show Gist options
  • Save pbalduino/add938d956bec8ad0d184353be7190da to your computer and use it in GitHub Desktop.
Save pbalduino/add938d956bec8ad0d184353be7190da to your computer and use it in GitHub Desktop.
How to test a filter in VUE.js 2
/* component */
<script>
export default {
name: 'sample',
filters: {
round: function (value) {
if (!value) return NaN;
return Math.round(value);
}
}
}
</script>
/* test */
import Vue from 'vue'
import Sample from '@/components/Sample'
describe('Sample component', () => {
describe('#round filter', () => {
const Constructor = Vue.extend(Sample);
const Component = new Constructor().$mount();
/*
here you access any of your component filter.
filters are stateless and immutable, so don't
worry about a single instantiation
*/
const round = Component.$options.filters.round;
// then test just like a plain simple function
it("should round a number", () => {
expect(round(3.14)).to.equal(3);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment