0.1.1 • Published 5 years ago

cypress-jest-adapter v0.1.1

Weekly downloads
18,743
License
MIT
Repository
github
Last release
5 years ago

Jest assertions for Cypress

cypress-jest-adapter provides set of custom assertions using jest assertion style to Cypress expect command

Allow to use jest style commands to do assertion:

expect(1).toEqual(1)

besides chai assertions style of

expect(1).to.be.equal(1)

Also, can do assertions for jQuery elements return from cy.get() function directly:

expect($el).toHaveId
expect($el).toHaveClass

Example

Installation

yarn add cypress-jest-adapter

or

npm install --save cypress-jest-adapter

Add cypress-jest-adapter to cypress support/index.js file

import 'cypress-jest-adapter'

Assertions

Available assertions:

Value assertion (using jest matchers)

methodassertions
toBeexpect(sth).toBe
toBeCloseToexpect(sth).toBeCloseTo
toBeDefinedexpect(sth).toBeDefined
toBeFalsyexpect(sth).toBeFalsy
toBeGreaterThanexpect(sth).toBeGreaterThan
toBeGreaterThanOrEqualexpect(sth).toBeGreaterThanOrEqual
toBeLessThanexpect(sth).toBeLessThan
toBeLessThanOrEqualexpect(sth).toBeLessThanOrEqual
toBeInstanceOfexpect(sth).toBeInstanceOf
toBeNullexpect(sth).toBeNull
toBeTruthyexpect(sth).toBeTruthy
toBeUndefinedexpect(sth).toBeUndefined
toBeNaNexpect(sth).toBeNaN
toContainexpect(sth).toContain
toContainEqualexpect(sth).toContainEqual
toEqualexpect(sth).toEqual
toHaveLengthexpect(sth).toHaveLength
toMatchexpect(sth).toMatch
toMatchObjectexpect(sth).toMatchObject
toHavePropertyexpect(sth).toHaveProperty
toStrictEqualexpect(sth).toStrictEqual
toThrowexpect(sth).toThrow

Promise handlers

methodassertions
resolvesexpect(sth).resolves.toBe(sth)
rejectsexpect(sth).rejects.toThrow(error)

Mock assertions (using chai, chai-sinon and jest equal assertion)

methodassertions
toHaveBeenCalledexpect(spy).toHaveBeenCalled
toHaveBeenCalledTimesexpect(spy).toHaveBeenCalledTimes
toHaveBeenCalledWithexpect(spy).toHaveBeenCalledWith
toHaveBeenNthCalledWithexpect(spy).toHaveBeenNthCalledWith
toHaveReturnedexpect(spy).toHaveReturned
toHaveReturnedTimesexpect(spy).toHaveReturnedTimes
toHaveReturnedWithexpect(spy).toHaveReturnedWith
toHaveLastReturnedWithexpect(spy).toHaveLastReturnedWith
toHaveNthReturnedWithexpect(spy).toHaveNthReturnedWith

Assumptions

methodassertions
anythingexpect(sth).toBe(expect.anything())
anyexpect(sth).toBe(expect.any())
arrayContainingexpect(sth).toBe(expect.arrayContaining())
objectContainingexpect(sth).toBe(expect.objectContaining())
stringContainingexpect(sth).toBe(expect.stringContaining())
stringMatchingexpect(sth).toBe(expect.stringMatching())

Not implement Jest functions

method
toMatchSnapshot
toMatchInlineSnapshot
toThrowErrorMatchingSnapshot
toThrowErrorMatchingInlineSnapshot
assertions
hasAsertions

For detailed usages, go to jest expect documentation

Jest-Jquery assertions

methodassertionschai-jquery equivalent
toHaveAttrexpect($ele).toHaveAttr(attribute: string, value?: string)to.have.attr
toHavePropexpect($ele).toHaveProp(property: string, value?: string)to.have.prop
toHaveCssexpect($ele).toHaveCss(css: string/object, value?: string)to.have.css
toHaveDataexpect($ele).toHaveData(key: string, data?: string)to.have.data
toHaveClassexpect($ele).toHaveClass(className: string)to.have.class
toHaveIdexpect($ele).toHaveId(id: string)to.have.id
toHaveHtmlexpect($ele).toHaveHtml(html: string)to.have.html
toHaveTextexpect($ele).toHaveText(text: string)to.have.text
toHaveValueexpect($ele).toHaveValue(value: string)to.have.value
toBeVisibleexpect($ele).toBeVisible()to.be.visible
toBeHiddenexpect($ele).toBeHidden()to.be.hidden
toBeSelectedexpect($ele).toBeSelected()to.be.selected
toBeCheckedexpect($ele).toBeChecked()to.be.checked
toBeEnabledexpect($ele).toBeEnabled()to.be.enabled
toBeDisabledexpect($ele).toBeDisabled()to.be.disabled
toBeEmptyexpect($ele).toBeEmpty()to.be.empty
toExistexpect($ele).toExist()to.exist
toBeMatchedByexpect($ele).toBeMatchedBy(selector: string)to.match
toMatchSelectorexpect($ele).toMatchSelector(selector: string)to.match
toContainTextexpect($ele).toContainText(text: string)to.contain
toHaveDescendantexpect($ele).toHaveDescendant(selector: string)to.have.descendants
toHaveDescendantWithTextexpect($ele).toHaveDescendantWithText(selector: string, text: string)
toHaveQuantityexpect($ele).toHaveQuantity(length: number)
toHaveTagexpect($ele).toHaveTag(tag: string)
toBeFocusedexpect($ele).toBeFocused()

Used library:

License

MIT