Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Cypress.Commands.overwriteQuery when adding a command that already exists #256

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/__tests__/add-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,45 @@ import {commands} from '../'
test('adds commands to Cypress', () => {
const addMock = jest.fn().mockName('Cypress.Commands.add')
const addQueryMock = jest.fn().mockName('Cypress.Commands.addQuery')
global.Cypress = {Commands: {add: addMock, addQuery: addQueryMock}}
global.cy = {}
const overwriteQueryMock = jest
.fn()
.mockName('Cypress.Commands.overwriteQuery')
global.Cypress = {
Commands: {
add: addMock,
addQuery: addQueryMock,
overwriteQuery: overwriteQueryMock,
},
}
global.cy = {
findAllByLabelText: jest.fn(),
}

require('../add-commands')

expect(addQueryMock).toHaveBeenCalledTimes(commands.length)
/** Commands that we're adding to the cy. namespace */
const newCommands = commands.filter(({name}) => !global.cy[name])
/** Commands that we're overwriting in the cy. namespace */
const overwrittenCommands = commands.filter(({name}) => global.cy[name])

expect(addQueryMock).toHaveBeenCalledTimes(newCommands.length)
expect(overwriteQueryMock).toHaveBeenCalledTimes(overwrittenCommands.length)
expect(addMock).toHaveBeenCalledTimes(1) // we're also adding a configuration command
commands.forEach(({name}, index) => {

newCommands.forEach(({name}, index) => {
expect(addQueryMock.mock.calls[index]).toMatchObject([
name,
// We get a new function that is `command.bind(null, cy)` i.e. global `cy` passed into the first argument.
// The commands themselves will be tested separately in the Cypress end-to-end tests.
expect.any(Function),
])
})
overwrittenCommands.forEach(({name}, index) => {
expect(overwriteQueryMock.mock.calls[index]).toMatchObject([
name,
// We get a new function that is `command.bind(null, cy)` i.e. global `cy` passed into the first argument.
// The commands themselves will be tested separately in the Cypress end-to-end tests.
expect.any(Function),
])
})
})
6 changes: 5 additions & 1 deletion src/add-commands.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {configure, commands} from './'

commands.forEach(({name, command}) => {
Cypress.Commands.addQuery(name, command)
if (cy[name]) {
Cypress.Commands.overwriteQuery(name, command)
} else {
Cypress.Commands.addQuery(name, command)
}
})

Cypress.Commands.add('configureCypressTestingLibrary', config => {
Expand Down