Files
cypress/packages/server/test/spec_helper.js
Jennifer Shehane 90310ae7f9 feat: extend Cypress.Keyboard.Keys and cy.press to support (almost) all keyboard keys (#31496)
* feat: extend Cypress.Keyboard.Keys and cy.press to support all keyboard keys

* Update cli/types/cypress.d.ts

* reference urls for codepoints

* type defs for expanded keys; tests

* changelog

* rm modifier keys temporarily - see WIP @ feat/key-press-modifier-keys

* Add pr link to changelog

* reduce repetition with key codes; remove unsupported capslock keycode

* only "key" special function keys in Cypress.Keyboard.Keys - otherwise, support any single character even if multiple codepoints

* do not refocus on f6 - f6 behavior keyup is indeterminate, so do not assert in that special case

* clean up keyboard key types, clean up duplicate def from merge

* various cleanup - wip - need multi-codepoint fix

* support multi-codepoint characters

* properly dispatch each part of a multipart utf-8 character

* fix import

* fix dtslint

* tscheck

* fix spacing

* Apply suggestions from code review

* changelog

* changelog

* ensure input actions are released in bidi; add test for keypress and input events for firefox

* fix keypress & input events in chrome

* consistent debug

* rm debug logging from input fixture

* some typos, changelog version

* fix toSupportedKey guard fn to properly reject non-strings

* add Space as named key, remove warnings re legacy firefox

* fix space

* support single-digit number keys

* remove support for F-keys

* add test cmd to types pkg

* rm failing vitest project config

* fix changelog

* clean up types a bit for single digit numbers

* more updates

---------

Co-authored-by: Cacie Prins <cacie@cypress.io>
Co-authored-by: Cacie Prins <cacieprins@users.noreply.github.com>
2025-08-28 10:31:49 -04:00

143 lines
2.8 KiB
JavaScript

/* eslint-disable no-console */
require('../lib/environment')
const { enable, mockElectron } = require('./mockery_helper')
const chai = require('chai')
chai.use(require('chai-subset'))
global.IS_TEST = true
global.supertest = require('supertest')
global.nock = require('nock')
global.expect = chai.expect
global.mockery = require('mockery')
global.proxyquire = require('proxyquire')
global.sinon = require('sinon')
const _ = require('lodash')
const Promise = require('bluebird')
const cache = require('../lib/cache').cache
require('chai')
.use(require('@cypress/sinon-chai'))
.use(require('chai-uuid'))
.use(require('chai-as-promised'))
if (process.env.UPDATE) {
throw new Error('You\'re using UPDATE=1 which is the old way of updating snapshots.\n\nThe correct environment variable is SNAPSHOT_UPDATE=1')
}
if (process.env.UPDATE_SNAPSHOT) {
throw new Error('You\'re using UPDATE_SNAPSHOT=1\n\nThe correct environment variable is SNAPSHOT_UPDATE=1')
}
if (process.env.UPDATE_SNAPSHOTS) {
throw new Error('You\'re using UPDATE_SNAPSHOTS=1\n\nThe correct environment variable is SNAPSHOT_UPDATE=1')
}
let hasOnly = false;
// hack for older version of mocha so that
// snap-shot-it can find suite._onlyTests
['it', 'describe', 'context'].forEach((prop) => {
const backup = global[prop].only
global[prop].only = function (...args) {
hasOnly = true
return backup.apply(this, args)
}
})
const originalEnv = process.env
const env = _.clone(process.env)
sinon.usingPromise(Promise)
// backup these originals
const {
restore,
useFakeTimers,
} = sinon
sinon.useFakeTimers = function (...args) {
const clock = useFakeTimers.apply(sinon, args)
sinon._clock = clock
return clock
}
sinon.restore = function (...args) {
let c
c = sinon._clock
if (c) {
c.restore()
}
return restore.apply(sinon, args)
}
enable(mockery)
mockElectron(mockery)
before(function () {
if (hasOnly) {
this.test.parent._onlyTests = [true]
}
})
// appData.ensure()
const { setCtx, getCtx, clearCtx, makeDataContext } = require('../lib/makeDataContext')
before(async () => {
await clearCtx()
setCtx(makeDataContext({}))
})
beforeEach(async function () {
await clearCtx()
setCtx(makeDataContext({}))
this.originalEnv = originalEnv
if (!nock.isActive()) {
nock.activate()
}
nock.disableNetConnect()
nock.enableNetConnect(/localhost/)
// always clean up the cache
// before each test
return cache.remove()
})
afterEach(async () => {
try {
await getCtx()._reset()
} catch (e) {
console.error('CAUGHT ERROR calling ctx._reset:')
console.error(e)
}
await clearCtx()
sinon.restore()
nock.cleanAll()
nock.enableNetConnect()
process.env = _.clone(env)
})
module.exports = {
expect: global.expect,
nock: global.nock,
proxyquire: global.proxyquire,
sinon: global.sinon,
root: global.root,
}