Testing utility functions
Let's test our code now! Let's start with utils. Create a file called utils.spec.js
and import the leftPad
function:
import { leftPad } from '~/utils/utils'
Have a look at this function again:
// utils/utils.js export const leftPad = value => { if (('' + value).length > 1) { return value } return '0' + value }
This function should return the input string if this string's length is greater than 1
. If the string's length is 1
, it should return the string with a preceding 0
.
Seems quite easy to test it, right? We would write two test cases:
// test/utils.spec.js
describe('utils', () => {
describe('leftPad', () => {
it('should return the string itself if its length is more than 1', () => {
expect(leftPad('01')).toEqual('01') }) ...
Get Vue.js 2 and Bootstrap 4 Web Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.