cypress 16.2.0 → 16.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/angular/dist/index.js +10 -12
  2. package/angular/package.json +7 -22
  3. package/dist/bin/cypress +8 -8
  4. package/dist/{cli-AgTnS5LO.js → cli-y9Bb9Y77.js} +215 -40
  5. package/dist/cli.js +6 -6
  6. package/dist/{cypress-CdDm9AVa.js → cypress-CvEs5adm.js} +2 -2
  7. package/dist/cypress.js +8 -8
  8. package/dist/exec/spawn.js +4 -4
  9. package/dist/exec/xvfb.js +7 -4
  10. package/dist/index.js +8 -8
  11. package/dist/{spawn-DElwyZHC.js → spawn-Bx99tBTm.js} +529 -69
  12. package/dist/{tap-WHD3nHJC.js → tap-BMFGLoCT.js} +108 -26
  13. package/dist/{xvfb-CbW2_YXR.js → xvfb-DxbcANVl.js} +238 -25
  14. package/mount-utils/dist/index.js +1 -1
  15. package/mount-utils/package.json +15 -14
  16. package/package.json +5 -5
  17. package/react/dist/cypress-react.cjs.js +9 -7
  18. package/react/dist/cypress-react.esm-bundler.js +9 -7
  19. package/react/package.json +26 -46
  20. package/svelte/dist/cypress-svelte.cjs.js +3 -2
  21. package/svelte/dist/cypress-svelte.esm-bundler.js +3 -2
  22. package/svelte/package.json +24 -22
  23. package/types/cy-bluebird.d.ts +1 -0
  24. package/types/cy-chai.d.ts +99 -2
  25. package/types/cypress.d.ts +216 -43
  26. package/types/net-stubbing.d.ts +1 -1
  27. package/vue/dist/cypress-vue.cjs.js +4 -2
  28. package/vue/dist/cypress-vue.esm-bundler.js +4 -2
  29. package/vue/dist/index.d.ts +3 -3
  30. package/vue/package.json +35 -42
  31. package/angular/angular/README.md +0 -18
  32. package/angular/angular/dist/index.d.ts +0 -120
  33. package/angular/angular/dist/index.js +0 -479
  34. package/angular/angular/package.json +0 -82
  35. package/mount-utils/mount-utils/README.md +0 -140
  36. package/mount-utils/mount-utils/dist/index.d.ts +0 -12
  37. package/mount-utils/mount-utils/dist/index.js +0 -41
  38. package/mount-utils/mount-utils/package.json +0 -52
  39. package/react/react/README.md +0 -19
  40. package/react/react/dist/cypress-react.cjs.js +0 -293
  41. package/react/react/dist/cypress-react.esm-bundler.js +0 -268
  42. package/react/react/dist/index.d.ts +0 -95
  43. package/react/react/package.json +0 -113
  44. package/svelte/svelte/README.md +0 -19
  45. package/svelte/svelte/dist/cypress-svelte.cjs.js +0 -122
  46. package/svelte/svelte/dist/cypress-svelte.esm-bundler.js +0 -120
  47. package/svelte/svelte/dist/index.d.ts +0 -31
  48. package/svelte/svelte/package.json +0 -63
  49. package/vue/vue/README.md +0 -14
  50. package/vue/vue/dist/cypress-vue.cjs.js +0 -8692
  51. package/vue/vue/dist/cypress-vue.esm-bundler.js +0 -8671
  52. package/vue/vue/dist/index.d.ts +0 -1413
  53. package/vue/vue/package.json +0 -97
@@ -1,268 +0,0 @@
1
-
2
- /**
3
- * @cypress/react v0.0.0-development
4
- * (c) 2026 Cypress.io
5
- * Released under the MIT License
6
- */
7
-
8
- import * as React from 'react';
9
- import ReactDOM from 'react-dom/client';
10
-
11
- /**
12
- * Gets the display name of the component when possible.
13
- * @param type {JSX} The type object returned from creating the react element.
14
- * @param fallbackName {string} The alias, or fallback name to use when the name cannot be derived.
15
- * @link https://github.com/facebook/react-devtools/blob/master/backend/getDisplayName.js
16
- */
17
- function getDisplayName(node, fallbackName = 'Unknown') {
18
- const type = node === null || node === void 0 ? void 0 : node.type;
19
- if (!type) {
20
- return fallbackName;
21
- }
22
- let displayName = null;
23
- // The displayName property is not guaranteed to be a string.
24
- // It's only safe to use for our purposes if it's a string.
25
- // github.com/facebook/react-devtools/issues/803
26
- if (typeof type.displayName === 'string') {
27
- displayName = type.displayName;
28
- }
29
- if (!displayName) {
30
- displayName = type.name || fallbackName;
31
- }
32
- // Facebook-specific hack to turn "Image [from Image.react]" into just "Image".
33
- // We need displayName with module name for error reports but it clutters the DevTools.
34
- const match = displayName.match(/^(.*) \[from (.*)\]$/);
35
- if (match) {
36
- const componentName = match[1];
37
- const moduleName = match[2];
38
- if (componentName && moduleName) {
39
- if (moduleName === componentName ||
40
- moduleName.startsWith(`${componentName}.`)) {
41
- displayName = componentName;
42
- }
43
- }
44
- }
45
- return displayName;
46
- }
47
-
48
- const ROOT_SELECTOR = '[data-cy-root]';
49
- /**
50
- * Gets the root element used to mount the component.
51
- * @returns {HTMLElement} The root element
52
- * @throws {Error} If the root element is not found
53
- */
54
- const getContainerEl = () => {
55
- const el = document.querySelector(ROOT_SELECTOR);
56
- if (el) {
57
- return el;
58
- }
59
- throw Error(`No element found that matches selector ${ROOT_SELECTOR}. Please add a root element with data-cy-root attribute to your "component-index.html" file so that Cypress can attach your component to the DOM.`);
60
- };
61
- /**
62
- * Utility function to register CT side effects and run cleanup code during the "test:before:run" Cypress hook
63
- * @param optionalCallback Callback to be called before the next test runs
64
- */
65
- function setupHooks(optionalCallback) {
66
- // We don't want CT side effects to run when e2e
67
- // testing so we early return.
68
- // System test to verify CT side effects do not pollute e2e: system-tests/test/e2e_with_mount_import_spec.ts
69
- if (Cypress.testingType !== 'component') {
70
- return;
71
- }
72
- // When running component specs, we cannot allow "cy.visit"
73
- // because it will wipe out our preparation work, and does not make much sense
74
- // thus we overwrite "cy.visit" to throw an error
75
- Cypress.Commands.overwrite('visit', () => {
76
- throw new Error('cy.visit from a component spec is not allowed');
77
- });
78
- Cypress.Commands.overwrite('session', () => {
79
- throw new Error('cy.session from a component spec is not allowed');
80
- });
81
- Cypress.Commands.overwrite('origin', () => {
82
- throw new Error('cy.origin from a component spec is not allowed');
83
- });
84
- // @ts-ignore
85
- Cypress.on('test:before:after:run:async', () => {
86
- optionalCallback?.();
87
- });
88
- }
89
-
90
- let mountCleanup;
91
- /**
92
- * Create an `mount` function. Performs all the non-React-version specific
93
- * behavior related to mounting. The React-version-specific code
94
- * is injected. This helps us to maintain a consistent public API
95
- * and handle breaking changes in React's rendering API.
96
- *
97
- * This is designed to be consumed by `npm/react{16,17,18}`, and other React adapters,
98
- * or people writing adapters for third-party, custom adapters.
99
- */
100
- const makeMountFn = (type, jsx, options = {}, rerenderKey, internalMountOptions) => {
101
- if (!internalMountOptions) {
102
- throw Error('internalMountOptions must be provided with `render` and `reactDom` parameters');
103
- }
104
- mountCleanup = internalMountOptions.cleanup;
105
- return cy
106
- .then(() => {
107
- var _a, _b, _c;
108
- const reactDomToUse = internalMountOptions.reactDom;
109
- const el = getContainerEl();
110
- if (!el) {
111
- throw new Error([
112
- `[@cypress/react] 🔥 Hmm, cannot find root element to mount the component. Searched for ${ROOT_SELECTOR}`,
113
- ].join(' '));
114
- }
115
- const key = rerenderKey !== null && rerenderKey !== void 0 ? rerenderKey :
116
- // @ts-ignore provide unique key to the the wrapped component to make sure we are rerendering between tests
117
- (((_c = (_b = (_a = Cypress === null || Cypress === void 0 ? void 0 : Cypress.mocha) === null || _a === void 0 ? void 0 : _a.getRunner()) === null || _b === void 0 ? void 0 : _b.test) === null || _c === void 0 ? void 0 : _c.title) || '') + Math.random();
118
- const props = {
119
- key,
120
- };
121
- const reactComponent = React.createElement(options.strict ? React.StrictMode : React.Fragment, props, jsx);
122
- // since we always surround the component with a fragment
123
- // let's get back the original component
124
- const userComponent = reactComponent.props.children;
125
- internalMountOptions.render(reactComponent, el, reactDomToUse);
126
- return (cy.wrap(userComponent, { log: false })
127
- .then(() => {
128
- return cy.wrap({
129
- component: userComponent,
130
- rerender: (newComponent) => makeMountFn('rerender', newComponent, options, key, internalMountOptions),
131
- }, { log: false });
132
- })
133
- // by waiting, we delaying test execution for the next tick of event loop
134
- // and letting hooks and component lifecycle methods to execute mount
135
- // https://github.com/bahmutov/cypress-react-unit-test/issues/200
136
- .wait(0, { log: false })
137
- .then(() => {
138
- if (options.log !== false) {
139
- // Get the display name property via the component constructor
140
- // @ts-ignore FIXME
141
- const componentName = getDisplayName(jsx);
142
- const jsxComponentName = `<${componentName} ... />`;
143
- Cypress.log({
144
- name: type,
145
- type: 'parent',
146
- message: [jsxComponentName],
147
- // @ts-ignore
148
- $el: el.children.item(0),
149
- consoleProps: () => {
150
- return {
151
- // @ts-ignore protect the use of jsx functional components use ReactNode
152
- props: jsx === null || jsx === void 0 ? void 0 : jsx.props,
153
- description: type === 'mount' ? 'Mounts React component' : 'Rerenders mounted React component',
154
- home: 'https://github.com/cypress-io/cypress',
155
- };
156
- },
157
- });
158
- }
159
- }));
160
- // Bluebird types are terrible. I don't think the return type can be carried without this cast
161
- });
162
- };
163
- /**
164
- * Create an `unmount` function. Performs all the non-React-version specific
165
- * behavior related to unmounting.
166
- *
167
- * This is designed to be consumed by `npm/react{16,17,18}`, and other React adapters,
168
- * or people writing adapters for third-party, custom adapters.
169
- *
170
- * @param {UnmountArgs} options used during unmounting
171
- */
172
- const makeUnmountFn = (options) => {
173
- return cy.then(() => {
174
- var _a;
175
- const wasUnmounted = mountCleanup === null || mountCleanup === void 0 ? void 0 : mountCleanup();
176
- if (wasUnmounted && options.log) {
177
- Cypress.log({
178
- name: 'unmount',
179
- type: 'parent',
180
- message: [(_a = options.boundComponentMessage) !== null && _a !== void 0 ? _a : 'Unmounted component'],
181
- consoleProps: () => {
182
- return {
183
- description: 'Unmounts React component',
184
- parent: getContainerEl().parentNode,
185
- home: 'https://github.com/cypress-io/cypress',
186
- };
187
- },
188
- });
189
- }
190
- });
191
- };
192
- // Cleanup before each run
193
- // NOTE: we cannot use unmount here because
194
- // we are not in the context of a test
195
- const preMountCleanup = () => {
196
- mountCleanup === null || mountCleanup === void 0 ? void 0 : mountCleanup();
197
- };
198
- const _mount = (jsx, options = {}) => makeMountFn('mount', jsx, options);
199
- const createMount = (defaultOptions) => {
200
- return (element, options) => {
201
- return _mount(element, Object.assign(Object.assign({}, defaultOptions), options));
202
- };
203
- };
204
- // Side effects from "import { mount } from '@cypress/<my-framework>'" are annoying, we should avoid doing this
205
- // by creating an explicit function/import that the user can register in their 'component.js' support file,
206
- // such as:
207
- // import 'cypress/<my-framework>/support'
208
- // or
209
- // import { registerCT } from 'cypress/<my-framework>'
210
- // registerCT()
211
- // Note: This would be a breaking change
212
- // it is required to unmount component in beforeEach hook in order to provide a clean state inside test
213
- // because `mount` can be called after some preparation that can side effect unmount
214
- // @see npm/react/cypress/component/advanced/set-timeout-example/loading-indicator-spec.js
215
- setupHooks(preMountCleanup);
216
-
217
- let root;
218
- const cleanup = () => {
219
- if (root) {
220
- root.unmount();
221
- root = null;
222
- return true;
223
- }
224
- return false;
225
- };
226
- /**
227
- * Mounts a React component into the DOM.
228
- * @param {import('react').JSX.Element} jsx The React component to mount.
229
- * @param {MountOptions} options Options to pass to the mount function.
230
- * @param {string} rerenderKey A key to use to force a rerender.
231
- *
232
- * @example
233
- * import { mount } from '@cypress/react'
234
- * import { Stepper } from './Stepper'
235
- *
236
- * it('mounts', () => {
237
- * mount(<StepperComponent />)
238
- * cy.get('[data-cy=increment]').click()
239
- * cy.get('[data-cy=counter]').should('have.text', '1')
240
- * }
241
- *
242
- * @see {@link https://on.cypress.io/mounting-react} for more details.
243
- *
244
- * @returns {Cypress.Chainable<MountReturn>} The mounted component.
245
- */
246
- function mount(jsx, options = {}, rerenderKey) {
247
- // Remove last mounted component if cy.mount is called more than once in a test
248
- // React by default removes the last component when calling render, but we should remove the root
249
- // to wipe away any state
250
- cleanup();
251
- const internalOptions = {
252
- reactDom: ReactDOM,
253
- render: (reactComponent, el) => {
254
- if (!root) {
255
- root = ReactDOM.createRoot(el);
256
- }
257
- return root.render(reactComponent);
258
- },
259
- unmount: internalUnmount,
260
- cleanup,
261
- };
262
- return makeMountFn('mount', jsx, Object.assign({ ReactDom: ReactDOM }, options), rerenderKey, internalOptions);
263
- }
264
- function internalUnmount(options = { log: true }) {
265
- return makeUnmountFn(options);
266
- }
267
-
268
- export { createMount, getContainerEl, makeMountFn, makeUnmountFn, mount };
@@ -1,95 +0,0 @@
1
- /// <reference types="cypress" />
2
-
3
- import * as React from 'react';
4
- import React__default from 'react';
5
- import * as react_dom_client from 'react-dom/client';
6
-
7
- interface UnmountArgs {
8
- log: boolean;
9
- boundComponentMessage?: string;
10
- }
11
- type MountOptions = Partial<MountReactComponentOptions>;
12
- interface MountReactComponentOptions {
13
- ReactDom: typeof react_dom_client;
14
- /**
15
- * Log the mounting command into Cypress Command Log,
16
- * true by default.
17
- */
18
- log: boolean;
19
- /**
20
- * Render component in React [strict mode](https://reactjs.org/docs/strict-mode.html)
21
- * It activates additional checks and warnings for child components.
22
- */
23
- strict: boolean;
24
- }
25
- interface InternalMountOptions {
26
- reactDom: typeof react_dom_client;
27
- render: (reactComponent: ReturnType<typeof React__default.createElement>, el: HTMLElement, reactDomToUse: typeof react_dom_client) => void;
28
- unmount: (options: UnmountArgs) => void;
29
- cleanup: () => boolean;
30
- }
31
- interface MountReturn {
32
- /**
33
- * The component that was rendered.
34
- */
35
- component: React__default.ReactNode;
36
- /**
37
- * Rerenders the specified component with new props. This allows testing of components that store state (`setState`)
38
- * or have asynchronous updates (`useEffect`, `useLayoutEffect`).
39
- */
40
- rerender: (component: React__default.ReactNode) => globalThis.Cypress.Chainable<MountReturn>;
41
- }
42
-
43
- /**
44
- * Create an `mount` function. Performs all the non-React-version specific
45
- * behavior related to mounting. The React-version-specific code
46
- * is injected. This helps us to maintain a consistent public API
47
- * and handle breaking changes in React's rendering API.
48
- *
49
- * This is designed to be consumed by `npm/react{16,17,18}`, and other React adapters,
50
- * or people writing adapters for third-party, custom adapters.
51
- */
52
- declare const makeMountFn: (type: 'mount' | 'rerender', jsx: React.ReactNode, options?: MountOptions, rerenderKey?: string, internalMountOptions?: InternalMountOptions) => globalThis.Cypress.Chainable<MountReturn>;
53
- /**
54
- * Create an `unmount` function. Performs all the non-React-version specific
55
- * behavior related to unmounting.
56
- *
57
- * This is designed to be consumed by `npm/react{16,17,18}`, and other React adapters,
58
- * or people writing adapters for third-party, custom adapters.
59
- *
60
- * @param {UnmountArgs} options used during unmounting
61
- */
62
- declare const makeUnmountFn: (options: UnmountArgs) => Cypress.Chainable<undefined>;
63
- declare const createMount: (defaultOptions: MountOptions) => (element: React.ReactElement, options?: MountOptions) => Cypress.Chainable<MountReturn>;
64
-
65
- /**
66
- * Gets the root element used to mount the component.
67
- * @returns {HTMLElement} The root element
68
- * @throws {Error} If the root element is not found
69
- */
70
- declare const getContainerEl: () => HTMLElement;
71
-
72
- /**
73
- * Mounts a React component into the DOM.
74
- * @param {import('react').JSX.Element} jsx The React component to mount.
75
- * @param {MountOptions} options Options to pass to the mount function.
76
- * @param {string} rerenderKey A key to use to force a rerender.
77
- *
78
- * @example
79
- * import { mount } from '@cypress/react'
80
- * import { Stepper } from './Stepper'
81
- *
82
- * it('mounts', () => {
83
- * mount(<StepperComponent />)
84
- * cy.get('[data-cy=increment]').click()
85
- * cy.get('[data-cy=counter]').should('have.text', '1')
86
- * }
87
- *
88
- * @see {@link https://on.cypress.io/mounting-react} for more details.
89
- *
90
- * @returns {Cypress.Chainable<MountReturn>} The mounted component.
91
- */
92
- declare function mount(jsx: React__default.ReactNode, options?: MountOptions, rerenderKey?: string): Cypress.Chainable<MountReturn>;
93
-
94
- export { createMount, getContainerEl, makeMountFn, makeUnmountFn, mount };
95
- export type { InternalMountOptions, MountOptions, MountReactComponentOptions, MountReturn, UnmountArgs };
@@ -1,113 +0,0 @@
1
- {
2
- "name": "@cypress/react",
3
- "version": "0.0.0-development",
4
- "description": "Test React components using Cypress",
5
- "main": "dist/cypress-react.cjs.js",
6
- "scripts": {
7
- "build": "rimraf dist && rollup -c rollup.config.mjs",
8
- "postbuild": "node ../../scripts/sync-exported-npm-with-cli.js",
9
- "check-ts": "tsc --noEmit && tsc --noEmit -p cypress/tsconfig.json && tsc --noEmit -p tsconfig.cypress.json",
10
- "cy:open": "node ../../scripts/cypress.js open --component",
11
- "cy:open:debug": "node --inspect-brk ../../scripts/start.js --component-testing --run-project ${PWD}",
12
- "cy:run": "node ../../scripts/cypress.js run --component",
13
- "cy:run:debug": "node --inspect-brk ../../scripts/start.js --component-testing --run-project ${PWD}",
14
- "lint": "eslint",
15
- "test": "yarn cy:run",
16
- "watch": "yarn build --watch --watch.exclude ./dist/**/*"
17
- },
18
- "devDependencies": {
19
- "@packages/eslint-config": "0.0.0-development",
20
- "@cypress/mount-utils": "0.0.0-development",
21
- "@testing-library/cypress": "10.1.3",
22
- "@vitejs/plugin-react": "4.6.0",
23
- "axios": "^1.20.0",
24
- "cypress": "0.0.0-development",
25
- "eslint": "^9.31.0",
26
- "jiti": "^2.4.2",
27
- "prop-types": "15.8.1",
28
- "react": "18.3.1",
29
- "react-dom": "18.3.1",
30
- "react-router": "6.28.0",
31
- "react-router-dom": "6.28.0",
32
- "rollup": "^4.60.2",
33
- "typescript": "~5.4.5",
34
- "vite": "^8.0.9"
35
- },
36
- "peerDependencies": {
37
- "@types/react": "^18 || ^19",
38
- "@types/react-dom": "^18 || ^19",
39
- "cypress": "*",
40
- "react": "^18 || ^19",
41
- "react-dom": "^18 || ^19"
42
- },
43
- "files": [
44
- "dist"
45
- ],
46
- "types": "dist/index.d.ts",
47
- "license": "MIT",
48
- "repository": {
49
- "type": "git",
50
- "url": "https://github.com/cypress-io/cypress.git"
51
- },
52
- "homepage": "https://github.com/cypress-io/cypress/blob/develop/npm/react/#readme",
53
- "bugs": "https://github.com/cypress-io/cypress/issues/new?assignees=&labels=npm%3A%20%40cypress%2Freact&template=1-bug-report.md&title=",
54
- "keywords": [
55
- "react",
56
- "cypress",
57
- "cypress-io",
58
- "test",
59
- "testing"
60
- ],
61
- "contributors": [
62
- {
63
- "name": "Dmitriy Kovalenko",
64
- "social": "@dmtrKovalenko"
65
- },
66
- {
67
- "name": "Brian Mann",
68
- "social": "@brian-mann"
69
- },
70
- {
71
- "name": "Barthélémy Ledoux",
72
- "social": "@elevatebart"
73
- },
74
- {
75
- "name": "Lachlan Miller",
76
- "social": "@lmiller1990"
77
- },
78
- {
79
- "name": "Jessica Sachs",
80
- "social": "@_JessicaSachs"
81
- }
82
- ],
83
- "unpkg": "dist/cypress-react.browser.js",
84
- "module": "dist/cypress-react.esm-bundler.js",
85
- "peerDependenciesMeta": {
86
- "@types/react": {
87
- "optional": true
88
- }
89
- },
90
- "publishConfig": {
91
- "access": "public"
92
- },
93
- "nx": {
94
- "targets": {
95
- "build": {
96
- "outputs": [
97
- "{workspaceRoot}/cli/react",
98
- "{projectRoot}/dist"
99
- ]
100
- }
101
- }
102
- },
103
- "standard": {
104
- "globals": [
105
- "Cypress",
106
- "cy",
107
- "expect"
108
- ]
109
- },
110
- "lint-staged": {
111
- "**/*.{js,jsx,ts,tsx}": "eslint --fix"
112
- }
113
- }
@@ -1,19 +0,0 @@
1
- # @cypress/svelte
2
-
3
- Mount Svelte components in the open source [Cypress.io](https://www.cypress.io/) test runner
4
-
5
- > **Note:** This package is bundled with the `cypress` package and should not need to be installed separately. See the [Svelte Component Testing Docs](https://docs.cypress.io/guides/component-testing/svelte/overview) for mounting Svelte components. Installing and importing `mount` from `@cypress/svelte` should only be done for advanced use-cases or in the case you may require an older or non supported version of svelte.
6
-
7
- ## Requirements
8
-
9
- - Svelte 5+ (Cypress 13 and under supports Svelte 4 and under)
10
-
11
- ## Development
12
-
13
- Run `yarn build` to compile and sync packages to the `cypress` cli package.
14
-
15
- Run `yarn cy:open` to open Cypress component testing against real-world examples.
16
-
17
- Run `yarn test` to execute headless Cypress tests.
18
-
19
- ## [Changelog](./CHANGELOG.md)
@@ -1,122 +0,0 @@
1
-
2
- /**
3
- * @cypress/svelte v0.0.0-development
4
- * (c) 2026 Cypress.io
5
- * Released under the MIT License
6
- */
7
-
8
- 'use strict';
9
-
10
- var svelte = require('svelte');
11
-
12
- const ROOT_SELECTOR = '[data-cy-root]';
13
- /**
14
- * Gets the root element used to mount the component.
15
- * @returns {HTMLElement} The root element
16
- * @throws {Error} If the root element is not found
17
- */
18
- const getContainerEl = () => {
19
- const el = document.querySelector(ROOT_SELECTOR);
20
- if (el) {
21
- return el;
22
- }
23
- throw Error(`No element found that matches selector ${ROOT_SELECTOR}. Please add a root element with data-cy-root attribute to your "component-index.html" file so that Cypress can attach your component to the DOM.`);
24
- };
25
- /**
26
- * Utility function to register CT side effects and run cleanup code during the "test:before:run" Cypress hook
27
- * @param optionalCallback Callback to be called before the next test runs
28
- */
29
- function setupHooks(optionalCallback) {
30
- // We don't want CT side effects to run when e2e
31
- // testing so we early return.
32
- // System test to verify CT side effects do not pollute e2e: system-tests/test/e2e_with_mount_import_spec.ts
33
- if (Cypress.testingType !== 'component') {
34
- return;
35
- }
36
- // When running component specs, we cannot allow "cy.visit"
37
- // because it will wipe out our preparation work, and does not make much sense
38
- // thus we overwrite "cy.visit" to throw an error
39
- Cypress.Commands.overwrite('visit', () => {
40
- throw new Error('cy.visit from a component spec is not allowed');
41
- });
42
- Cypress.Commands.overwrite('session', () => {
43
- throw new Error('cy.session from a component spec is not allowed');
44
- });
45
- Cypress.Commands.overwrite('origin', () => {
46
- throw new Error('cy.origin from a component spec is not allowed');
47
- });
48
- // @ts-ignore
49
- Cypress.on('test:before:after:run:async', () => {
50
- optionalCallback?.();
51
- });
52
- }
53
-
54
- const DEFAULT_COMP_NAME = 'unknown';
55
- let componentInstance;
56
- const cleanup = () => {
57
- if (componentInstance) {
58
- svelte.unmount(componentInstance);
59
- }
60
- };
61
- // Extract the component name from the object passed to mount
62
- const getComponentDisplayName = (Component) => {
63
- if (Component.name) {
64
- const [, match] = /Proxy\<(\w+)\>/.exec(Component.name) || [];
65
- return match || Component.name;
66
- }
67
- return DEFAULT_COMP_NAME;
68
- };
69
- /**
70
- * Mounts a Svelte component inside the Cypress browser
71
- *
72
- * @param {Record<string, any>} Component Svelte component being mounted
73
- * @param {MountReturn<T extends SvelteComponent>} options options to customize the component being mounted
74
- * @returns Cypress.Chainable<MountReturn>
75
- *
76
- * @example
77
- * import Counter from './Counter.svelte'
78
- * import { mount } from 'cypress/svelte'
79
- *
80
- * it('should render', () => {
81
- * mount(Counter, { props: { count: 42 } })
82
- * cy.get('button').contains(42)
83
- * })
84
- *
85
- * @see {@link https://on.cypress.io/mounting-svelte} for more details.
86
- */
87
- function mount(Component, options = {}) {
88
- // In Svelte 5, the component name is no longer easily discoverable and logs as "wrapper"
89
- // so we default the logging of it to false as it doesn't provide a lot of value
90
- options.log = options.log || false;
91
- return cy.then(() => {
92
- // Remove last mounted component if cy.mount is called more than once in a test
93
- cleanup();
94
- const target = getContainerEl();
95
- const ComponentConstructor = (Component.default || Component);
96
- // @see https://svelte.dev/docs/svelte/v5-migration-guide#Components-are-no-longer-classes
97
- componentInstance = svelte.mount(ComponentConstructor, Object.assign({ target }, options));
98
- // by waiting, we are delaying test execution for the next tick of event loop
99
- // and letting hooks and component lifecycle methods to execute mount
100
- return cy.wait(0, { log: false }).then(() => {
101
- if (options.log !== false) {
102
- const mountMessage = `<${getComponentDisplayName(Component)} ... />`;
103
- Cypress.log({
104
- name: 'mount',
105
- message: [mountMessage],
106
- });
107
- }
108
- })
109
- .wrap({ component: componentInstance }, { log: false });
110
- });
111
- }
112
- // Side effects from "import { mount } from '@cypress/<my-framework>'" are annoying, we should avoid doing this
113
- // by creating an explicit function/import that the user can register in their 'component.js' support file,
114
- // such as:
115
- // import 'cypress/<my-framework>/support'
116
- // or
117
- // import { registerCT } from 'cypress/<my-framework>'
118
- // registerCT()
119
- // Note: This would be a breaking change
120
- setupHooks(cleanup);
121
-
122
- exports.mount = mount;