I am using Cypress with Cucumber to develope a front-end with vue.js using behaviour driven development (BDD). I can already use Cypress for end-to-end testing, but I was also required to create and run unit tests. I could not find anything on the official Cypress website. How can I create and run unit tests for this front-end?
You can find all recipes related to the unit tests in the official docs.
Let's say you want to unit test math functions from the application's code, you can do so with Cypress:
/// <reference types="cypress" />
import math from '../../math'
describe('Unit Test Application Code', function () {
const { add, divide, multiply, subtract } = math
before(() => {
// check if the import worked correctly
expect(add, 'add').to.be.a('function')
})
context('math.js', function () {
it('can add numbers', function () {
expect(add(1, 2)).to.eq(3)
})
it('can subtract numbers', function () {
expect(subtract(5, 12)).to.eq(-7)
})
it('can divide numbers', function () {
expect(divide(27, 9)).to.eq(3)
})
it('can muliple numbers', function () {
expect(multiply(5, 4)).to.eq(20)
})
})
})