cypress 16.2.1 → 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.
- package/angular/dist/index.js +4 -5
- package/dist/bin/cypress +8 -8
- package/dist/{cli-DoJnc1F1.js → cli-y9Bb9Y77.js} +212 -38
- package/dist/cli.js +6 -6
- package/dist/{cypress-BTgp4zGi.js → cypress-CvEs5adm.js} +2 -2
- package/dist/cypress.js +8 -8
- package/dist/exec/spawn.js +4 -4
- package/dist/exec/xvfb.js +7 -4
- package/dist/index.js +8 -8
- package/dist/{spawn-zPuxj7Kz.js → spawn-Bx99tBTm.js} +527 -67
- package/dist/{tap-D2mHgoNE.js → tap-BMFGLoCT.js} +108 -26
- package/dist/{xvfb-CbW2_YXR.js → xvfb-DxbcANVl.js} +238 -25
- package/mount-utils/dist/index.js +1 -0
- package/mount-utils/package.json +1 -0
- package/package.json +4 -4
- package/react/dist/cypress-react.cjs.js +4 -1
- package/react/dist/cypress-react.esm-bundler.js +4 -1
- package/svelte/dist/cypress-svelte.cjs.js +3 -1
- package/svelte/dist/cypress-svelte.esm-bundler.js +3 -1
- package/types/cy-bluebird.d.ts +1 -0
- package/types/cypress.d.ts +8 -1
- package/types/net-stubbing.d.ts +1 -1
- package/vue/dist/cypress-vue.cjs.js +3 -0
- package/vue/dist/cypress-vue.esm-bundler.js +3 -0
- package/vue/dist/index.d.ts +3 -3
- package/vue/package.json +1 -1
- package/angular/angular/README.md +0 -18
- package/angular/angular/dist/index.d.ts +0 -120
- package/angular/angular/dist/index.js +0 -478
- package/angular/angular/package.json +0 -67
- package/mount-utils/mount-utils/README.md +0 -140
- package/mount-utils/mount-utils/dist/index.d.ts +0 -12
- package/mount-utils/mount-utils/dist/index.js +0 -40
- package/mount-utils/mount-utils/package.json +0 -52
- package/react/react/README.md +0 -19
- package/react/react/dist/cypress-react.cjs.js +0 -292
- package/react/react/dist/cypress-react.esm-bundler.js +0 -267
- package/react/react/dist/index.d.ts +0 -95
- package/react/react/package.json +0 -93
- package/svelte/svelte/README.md +0 -19
- package/svelte/svelte/dist/cypress-svelte.cjs.js +0 -121
- package/svelte/svelte/dist/cypress-svelte.esm-bundler.js +0 -119
- package/svelte/svelte/dist/index.d.ts +0 -31
- package/svelte/svelte/package.json +0 -65
- package/vue/vue/README.md +0 -14
- package/vue/vue/dist/cypress-vue.cjs.js +0 -8691
- package/vue/vue/dist/cypress-vue.esm-bundler.js +0 -8670
- package/vue/vue/dist/index.d.ts +0 -1413
- package/vue/vue/package.json +0 -90
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@cypress/angular",
|
|
3
|
-
"version": "0.0.0-development",
|
|
4
|
-
"description": "Test Angular Components with Cypress",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"prebuild": "rimraf dist",
|
|
8
|
-
"build": "rollup -c rollup.config.mjs",
|
|
9
|
-
"postbuild": "node ../../scripts/sync-exported-npm-with-cli.js",
|
|
10
|
-
"check-ts": "tsc --noEmit",
|
|
11
|
-
"dev": "rollup -c rollup.config.mjs -w",
|
|
12
|
-
"lint": "eslint"
|
|
13
|
-
},
|
|
14
|
-
"dependencies": {},
|
|
15
|
-
"devDependencies": {
|
|
16
|
-
"@angular/common": "^21.0.0",
|
|
17
|
-
"@angular/core": "^21.0.0",
|
|
18
|
-
"@angular/platform-browser": "^21.0.0",
|
|
19
|
-
"@cypress/mount-utils": "0.0.0-development",
|
|
20
|
-
"@packages/eslint-config": "0.0.0-development",
|
|
21
|
-
"eslint": "^9.31.0",
|
|
22
|
-
"jiti": "^2.4.2",
|
|
23
|
-
"rollup": "^4.60.2",
|
|
24
|
-
"typescript": "~5.9.2"
|
|
25
|
-
},
|
|
26
|
-
"peerDependencies": {
|
|
27
|
-
"@angular/common": ">=21.0.0",
|
|
28
|
-
"@angular/core": ">=21.0.0",
|
|
29
|
-
"@angular/platform-browser": ">=21.0.0",
|
|
30
|
-
"rxjs": ">=7.8.0"
|
|
31
|
-
},
|
|
32
|
-
"files": [
|
|
33
|
-
"dist"
|
|
34
|
-
],
|
|
35
|
-
"types": "dist/index.d.ts",
|
|
36
|
-
"license": "MIT",
|
|
37
|
-
"repository": {
|
|
38
|
-
"type": "git",
|
|
39
|
-
"url": "git+https://github.com/cypress-io/cypress.git"
|
|
40
|
-
},
|
|
41
|
-
"homepage": "https://github.com/cypress-io/cypress/blob/develop/npm/angular/#readme",
|
|
42
|
-
"bugs": "https://github.com/cypress-io/cypress/issues/new?assignees=&labels=npm%3A%20%40cypress%2Fangular&template=1-bug-report.md&title=",
|
|
43
|
-
"keywords": [
|
|
44
|
-
"angular",
|
|
45
|
-
"cypress",
|
|
46
|
-
"cypress-io",
|
|
47
|
-
"test",
|
|
48
|
-
"testing",
|
|
49
|
-
"zoneless"
|
|
50
|
-
],
|
|
51
|
-
"module": "dist/index.js",
|
|
52
|
-
"publishConfig": {
|
|
53
|
-
"access": "public"
|
|
54
|
-
},
|
|
55
|
-
"nx": {
|
|
56
|
-
"targets": {
|
|
57
|
-
"build": {
|
|
58
|
-
"outputs": [
|
|
59
|
-
"{workspaceRoot}/cli/angular"
|
|
60
|
-
]
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
},
|
|
64
|
-
"lint-staged": {
|
|
65
|
-
"**/*.{js,jsx,ts,tsx,json}": "eslint --fix"
|
|
66
|
-
}
|
|
67
|
-
}
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
# @cypress/mount-utils
|
|
2
|
-
|
|
3
|
-
> **Note** this package is not meant to be used outside of cypress component testing.
|
|
4
|
-
|
|
5
|
-
This library exports some shared types and utility functions designed to build adapters for components frameworks.
|
|
6
|
-
|
|
7
|
-
It is used in:
|
|
8
|
-
|
|
9
|
-
- [`@cypress/react`](https://github.com/cypress-io/cypress/tree/develop/npm/react)
|
|
10
|
-
- [`@cypress/vue`](https://github.com/cypress-io/cypress/tree/develop/npm/vue)
|
|
11
|
-
- [`@cypress/svelte`](https://github.com/cypress-io/cypress/tree/develop/npm/svelte)
|
|
12
|
-
- [`@cypress/angular`](https://github.com/cypress-io/cypress/tree/develop/npm/angular)
|
|
13
|
-
|
|
14
|
-
## What is a Mount Adapter?
|
|
15
|
-
|
|
16
|
-
All Component Tests require a component to be **mounted**. This is generally done with a custom command, `cy.mount` by default.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
### Requirements
|
|
20
|
-
|
|
21
|
-
All the functionality used to create the first party Mount adapters is available to support third parties adapters. At minimum, a Mount Adapter must:
|
|
22
|
-
|
|
23
|
-
- Receive a component as the first argument. This could be class, function etc - depends on the framework.
|
|
24
|
-
- Return a Cypress Chainable (for example using `cy.wrap`) that resolves whatever is idiomatic for your framework
|
|
25
|
-
- Call `getContainerEl` to access the root DOM element
|
|
26
|
-
|
|
27
|
-
In addition, we recommend that Mount Adapters:
|
|
28
|
-
|
|
29
|
-
- call `setupHooks` to register the required lifecycle hooks for `@cypress/mount-utils` to work
|
|
30
|
-
|
|
31
|
-
### Example Mount Adapter: Web Components
|
|
32
|
-
|
|
33
|
-
Here's a simple yet realistic example of Mount Adapter targeting Web Components. It uses utilities from this package (`@cypress/mount-utils`) This is recommended, so your adapter behaves similar to the first party Mount Adapters.
|
|
34
|
-
|
|
35
|
-
```ts
|
|
36
|
-
import {
|
|
37
|
-
ROOT_SELECTOR,
|
|
38
|
-
setupHooks,
|
|
39
|
-
getContainerEl
|
|
40
|
-
} from "@cypress/mount-utils";
|
|
41
|
-
|
|
42
|
-
Cypress.on("run:start", () => {
|
|
43
|
-
// Consider doing a check to ensure your adapter only runs in Component Testing mode.
|
|
44
|
-
if (Cypress.testingType !== "component") {
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
Cypress.on("test:before:run", () => {
|
|
49
|
-
// Do some cleanup from previous test - for example, clear the DOM.
|
|
50
|
-
getContainerEl().innerHTML = "";
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
function maybeRegisterComponent<T extends CustomElementConstructor>(
|
|
55
|
-
name: string,
|
|
56
|
-
webComponent: T
|
|
57
|
-
) {
|
|
58
|
-
// Avoid double-registering a Web Component.
|
|
59
|
-
if (window.customElements.get(name)) {
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
window.customElements.define(name, webComponent);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function mount(
|
|
67
|
-
webComponent: CustomElementConstructor
|
|
68
|
-
): Cypress.Chainable {
|
|
69
|
-
// Get root selector defined in `cypress/support.component-index.html
|
|
70
|
-
const $root = document.querySelector(ROOT_SELECTOR)!;
|
|
71
|
-
|
|
72
|
-
// Change to kebab-case to comply with Web Component naming convention
|
|
73
|
-
const name = webComponent.name
|
|
74
|
-
.replace(/([a-z0–9])([A-Z])/g, "$1-$2")
|
|
75
|
-
.toLowerCase();
|
|
76
|
-
|
|
77
|
-
/// Register Web Component
|
|
78
|
-
maybeRegisterComponent(name, webComponent);
|
|
79
|
-
|
|
80
|
-
// Render HTML containing component.
|
|
81
|
-
$root.innerHTML = `<${name} id="root"></${name}>`;
|
|
82
|
-
|
|
83
|
-
// Log a messsage in the Command Log.
|
|
84
|
-
Cypress.log({
|
|
85
|
-
name: "mount",
|
|
86
|
-
message: [`<${name} ... />`],
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
// Return a `Cypress.Chainable` that returns whatever is idiomatic
|
|
90
|
-
// in the framework your mount adapter targets.
|
|
91
|
-
return cy.wrap(document.querySelector("#root"), { log: false });
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// Setup Cypress lifecycle hooks.
|
|
95
|
-
setupHooks();
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
Usage:
|
|
99
|
-
|
|
100
|
-
```ts
|
|
101
|
-
// User will generally register a `cy.mount` command in `cypress/support/component.js`:
|
|
102
|
-
|
|
103
|
-
import { mount } from '@package/cypress-web-components'
|
|
104
|
-
|
|
105
|
-
Cypress.Commands.add('mount', mount)
|
|
106
|
-
|
|
107
|
-
// Test
|
|
108
|
-
export class WebCounter extends HTMLElement {
|
|
109
|
-
constructor() {
|
|
110
|
-
super();
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
connectedCallback() {
|
|
114
|
-
this.innerHTML = `
|
|
115
|
-
<div>
|
|
116
|
-
<button>Counter</button>
|
|
117
|
-
</div>`;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
describe('web-component.cy.ts', () => {
|
|
123
|
-
it('playground', () => {
|
|
124
|
-
cy.mount(WebCounter)
|
|
125
|
-
})
|
|
126
|
-
})
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
For more robust, production ready examples, check out our first party adapters.
|
|
130
|
-
|
|
131
|
-
## Compatibility
|
|
132
|
-
|
|
133
|
-
| @cypress/mount-utils | cypress |
|
|
134
|
-
| -------------------- | ------- |
|
|
135
|
-
| <= v1 | <= v9 |
|
|
136
|
-
| >= v2 | >= v10 |
|
|
137
|
-
|
|
138
|
-
## Changelog
|
|
139
|
-
|
|
140
|
-
[Changelog](./CHANGELOG.md)
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export declare const ROOT_SELECTOR = "[data-cy-root]";
|
|
2
|
-
/**
|
|
3
|
-
* Gets the root element used to mount the component.
|
|
4
|
-
* @returns {HTMLElement} The root element
|
|
5
|
-
* @throws {Error} If the root element is not found
|
|
6
|
-
*/
|
|
7
|
-
export declare const getContainerEl: () => HTMLElement;
|
|
8
|
-
/**
|
|
9
|
-
* Utility function to register CT side effects and run cleanup code during the "test:before:run" Cypress hook
|
|
10
|
-
* @param optionalCallback Callback to be called before the next test runs
|
|
11
|
-
*/
|
|
12
|
-
export declare function setupHooks(optionalCallback?: Function): void;
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
export const ROOT_SELECTOR = '[data-cy-root]';
|
|
2
|
-
/**
|
|
3
|
-
* Gets the root element used to mount the component.
|
|
4
|
-
* @returns {HTMLElement} The root element
|
|
5
|
-
* @throws {Error} If the root element is not found
|
|
6
|
-
*/
|
|
7
|
-
export const getContainerEl = () => {
|
|
8
|
-
const el = document.querySelector(ROOT_SELECTOR);
|
|
9
|
-
if (el) {
|
|
10
|
-
return el;
|
|
11
|
-
}
|
|
12
|
-
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.`);
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* Utility function to register CT side effects and run cleanup code during the "test:before:run" Cypress hook
|
|
16
|
-
* @param optionalCallback Callback to be called before the next test runs
|
|
17
|
-
*/
|
|
18
|
-
export function setupHooks(optionalCallback) {
|
|
19
|
-
// We don't want CT side effects to run when e2e
|
|
20
|
-
// testing so we early return.
|
|
21
|
-
// System test to verify CT side effects do not pollute e2e: system-tests/test/e2e_with_mount_import_spec.ts
|
|
22
|
-
if (Cypress.testingType !== 'component') {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
// When running component specs, we cannot allow "cy.visit"
|
|
26
|
-
// because it will wipe out our preparation work, and does not make much sense
|
|
27
|
-
// thus we overwrite "cy.visit" to throw an error
|
|
28
|
-
Cypress.Commands.overwrite('visit', () => {
|
|
29
|
-
throw new Error('cy.visit from a component spec is not allowed');
|
|
30
|
-
});
|
|
31
|
-
Cypress.Commands.overwrite('session', () => {
|
|
32
|
-
throw new Error('cy.session from a component spec is not allowed');
|
|
33
|
-
});
|
|
34
|
-
Cypress.Commands.overwrite('origin', () => {
|
|
35
|
-
throw new Error('cy.origin from a component spec is not allowed');
|
|
36
|
-
});
|
|
37
|
-
Cypress.on('test:before:after:run:async', () => {
|
|
38
|
-
optionalCallback?.();
|
|
39
|
-
});
|
|
40
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@cypress/mount-utils",
|
|
3
|
-
"version": "0.0.0-development",
|
|
4
|
-
"description": "Shared utilities for the various component testing adapters",
|
|
5
|
-
"homepage": "https://github.com/cypress-io/cypress/tree/develop/npm/mount-utils#readme",
|
|
6
|
-
"bugs": "https://github.com/cypress-io/cypress/issues/new?template=1-bug-report.md",
|
|
7
|
-
"repository": {
|
|
8
|
-
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/cypress-io/cypress.git"
|
|
10
|
-
},
|
|
11
|
-
"license": "MIT",
|
|
12
|
-
"main": "dist/index.js",
|
|
13
|
-
"types": "dist/index.d.ts",
|
|
14
|
-
"files": [
|
|
15
|
-
"dist"
|
|
16
|
-
],
|
|
17
|
-
"scripts": {
|
|
18
|
-
"build": "tsc || echo 'built, with type errors'",
|
|
19
|
-
"postbuild": "node ../../scripts/sync-exported-npm-with-cli.js",
|
|
20
|
-
"check-ts": "tsc --noEmit",
|
|
21
|
-
"lint": "eslint",
|
|
22
|
-
"watch": "tsc -w"
|
|
23
|
-
},
|
|
24
|
-
"lint-staged": {
|
|
25
|
-
"**/*.{js,jsx,ts,tsx,json}": "eslint --fix"
|
|
26
|
-
},
|
|
27
|
-
"dependencies": {},
|
|
28
|
-
"devDependencies": {
|
|
29
|
-
"@packages/eslint-config": "0.0.0-development",
|
|
30
|
-
"@rollup/plugin-commonjs": "^29.0.2",
|
|
31
|
-
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
32
|
-
"eslint": "^9.31.0",
|
|
33
|
-
"lodash": "^4.17.21",
|
|
34
|
-
"rollup": "4.60.2",
|
|
35
|
-
"rollup-plugin-dts": "6.4.1",
|
|
36
|
-
"rollup-plugin-typescript2": "^0.37.0",
|
|
37
|
-
"typescript": "~5.4.5"
|
|
38
|
-
},
|
|
39
|
-
"publishConfig": {
|
|
40
|
-
"access": "public"
|
|
41
|
-
},
|
|
42
|
-
"nx": {
|
|
43
|
-
"targets": {
|
|
44
|
-
"build": {
|
|
45
|
-
"outputs": [
|
|
46
|
-
"{workspaceRoot}/cli/mount-utils",
|
|
47
|
-
"{projectRoot}/dist"
|
|
48
|
-
]
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
package/react/react/README.md
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
# @cypress/react
|
|
2
|
-
|
|
3
|
-
Mount React 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 [React Component Testing Docs](https://docs.cypress.io/guides/component-testing/react/overview) for mounting React components. Installing and importing `mount` from `@cypress/react` should only be done for advanced use-cases.
|
|
6
|
-
|
|
7
|
-
## Requirements
|
|
8
|
-
|
|
9
|
-
- React 18.0.0+ (Cypress 13 and under supports React 16 and 17)
|
|
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,292 +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
|
-
'use strict';
|
|
9
|
-
|
|
10
|
-
var React = require('react');
|
|
11
|
-
var ReactDOM = require('react-dom/client');
|
|
12
|
-
|
|
13
|
-
function _interopNamespaceDefault(e) {
|
|
14
|
-
var n = Object.create(null);
|
|
15
|
-
if (e) {
|
|
16
|
-
Object.keys(e).forEach(function (k) {
|
|
17
|
-
if (k !== 'default') {
|
|
18
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
19
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
20
|
-
enumerable: true,
|
|
21
|
-
get: function () { return e[k]; }
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
n.default = e;
|
|
27
|
-
return Object.freeze(n);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Gets the display name of the component when possible.
|
|
34
|
-
* @param type {JSX} The type object returned from creating the react element.
|
|
35
|
-
* @param fallbackName {string} The alias, or fallback name to use when the name cannot be derived.
|
|
36
|
-
* @link https://github.com/facebook/react-devtools/blob/master/backend/getDisplayName.js
|
|
37
|
-
*/
|
|
38
|
-
function getDisplayName(node, fallbackName = 'Unknown') {
|
|
39
|
-
const type = node === null || node === void 0 ? void 0 : node.type;
|
|
40
|
-
if (!type) {
|
|
41
|
-
return fallbackName;
|
|
42
|
-
}
|
|
43
|
-
let displayName = null;
|
|
44
|
-
// The displayName property is not guaranteed to be a string.
|
|
45
|
-
// It's only safe to use for our purposes if it's a string.
|
|
46
|
-
// github.com/facebook/react-devtools/issues/803
|
|
47
|
-
if (typeof type.displayName === 'string') {
|
|
48
|
-
displayName = type.displayName;
|
|
49
|
-
}
|
|
50
|
-
if (!displayName) {
|
|
51
|
-
displayName = type.name || fallbackName;
|
|
52
|
-
}
|
|
53
|
-
// Facebook-specific hack to turn "Image [from Image.react]" into just "Image".
|
|
54
|
-
// We need displayName with module name for error reports but it clutters the DevTools.
|
|
55
|
-
const match = displayName.match(/^(.*) \[from (.*)\]$/);
|
|
56
|
-
if (match) {
|
|
57
|
-
const componentName = match[1];
|
|
58
|
-
const moduleName = match[2];
|
|
59
|
-
if (componentName && moduleName) {
|
|
60
|
-
if (moduleName === componentName ||
|
|
61
|
-
moduleName.startsWith(`${componentName}.`)) {
|
|
62
|
-
displayName = componentName;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return displayName;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const ROOT_SELECTOR = '[data-cy-root]';
|
|
70
|
-
/**
|
|
71
|
-
* Gets the root element used to mount the component.
|
|
72
|
-
* @returns {HTMLElement} The root element
|
|
73
|
-
* @throws {Error} If the root element is not found
|
|
74
|
-
*/
|
|
75
|
-
const getContainerEl = () => {
|
|
76
|
-
const el = document.querySelector(ROOT_SELECTOR);
|
|
77
|
-
if (el) {
|
|
78
|
-
return el;
|
|
79
|
-
}
|
|
80
|
-
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.`);
|
|
81
|
-
};
|
|
82
|
-
/**
|
|
83
|
-
* Utility function to register CT side effects and run cleanup code during the "test:before:run" Cypress hook
|
|
84
|
-
* @param optionalCallback Callback to be called before the next test runs
|
|
85
|
-
*/
|
|
86
|
-
function setupHooks(optionalCallback) {
|
|
87
|
-
// We don't want CT side effects to run when e2e
|
|
88
|
-
// testing so we early return.
|
|
89
|
-
// System test to verify CT side effects do not pollute e2e: system-tests/test/e2e_with_mount_import_spec.ts
|
|
90
|
-
if (Cypress.testingType !== 'component') {
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
// When running component specs, we cannot allow "cy.visit"
|
|
94
|
-
// because it will wipe out our preparation work, and does not make much sense
|
|
95
|
-
// thus we overwrite "cy.visit" to throw an error
|
|
96
|
-
Cypress.Commands.overwrite('visit', () => {
|
|
97
|
-
throw new Error('cy.visit from a component spec is not allowed');
|
|
98
|
-
});
|
|
99
|
-
Cypress.Commands.overwrite('session', () => {
|
|
100
|
-
throw new Error('cy.session from a component spec is not allowed');
|
|
101
|
-
});
|
|
102
|
-
Cypress.Commands.overwrite('origin', () => {
|
|
103
|
-
throw new Error('cy.origin from a component spec is not allowed');
|
|
104
|
-
});
|
|
105
|
-
Cypress.on('test:before:after:run:async', () => {
|
|
106
|
-
optionalCallback?.();
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
let mountCleanup;
|
|
111
|
-
/**
|
|
112
|
-
* Create an `mount` function. Performs all the non-React-version specific
|
|
113
|
-
* behavior related to mounting. The React-version-specific code
|
|
114
|
-
* is injected. This helps us to maintain a consistent public API
|
|
115
|
-
* and handle breaking changes in React's rendering API.
|
|
116
|
-
*
|
|
117
|
-
* This is designed to be consumed by `npm/react{16,17,18}`, and other React adapters,
|
|
118
|
-
* or people writing adapters for third-party, custom adapters.
|
|
119
|
-
*/
|
|
120
|
-
const makeMountFn = (type, jsx, options = {}, rerenderKey, internalMountOptions) => {
|
|
121
|
-
if (!internalMountOptions) {
|
|
122
|
-
throw Error('internalMountOptions must be provided with `render` and `reactDom` parameters');
|
|
123
|
-
}
|
|
124
|
-
mountCleanup = internalMountOptions.cleanup;
|
|
125
|
-
return cy
|
|
126
|
-
.then(() => {
|
|
127
|
-
var _a, _b, _c;
|
|
128
|
-
const reactDomToUse = internalMountOptions.reactDom;
|
|
129
|
-
const el = getContainerEl();
|
|
130
|
-
if (!el) {
|
|
131
|
-
throw new Error([
|
|
132
|
-
`[@cypress/react] 🔥 Hmm, cannot find root element to mount the component. Searched for ${ROOT_SELECTOR}`,
|
|
133
|
-
].join(' '));
|
|
134
|
-
}
|
|
135
|
-
const key = rerenderKey !== null && rerenderKey !== void 0 ? rerenderKey :
|
|
136
|
-
// @ts-expect-error - `mocha` is not in the public Cypress types. Provide unique key to the wrapped component to make sure we are rerendering between tests
|
|
137
|
-
(((_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();
|
|
138
|
-
const props = {
|
|
139
|
-
key,
|
|
140
|
-
};
|
|
141
|
-
const reactComponent = React__namespace.createElement(options.strict ? React__namespace.StrictMode : React__namespace.Fragment, props, jsx);
|
|
142
|
-
// since we always surround the component with a fragment
|
|
143
|
-
// let's get back the original component
|
|
144
|
-
const userComponent = reactComponent.props.children;
|
|
145
|
-
internalMountOptions.render(reactComponent, el, reactDomToUse);
|
|
146
|
-
return (cy.wrap(userComponent, { log: false })
|
|
147
|
-
.then(() => {
|
|
148
|
-
return cy.wrap({
|
|
149
|
-
component: userComponent,
|
|
150
|
-
rerender: (newComponent) => makeMountFn('rerender', newComponent, options, key, internalMountOptions),
|
|
151
|
-
}, { log: false });
|
|
152
|
-
})
|
|
153
|
-
// by waiting, we delaying test execution for the next tick of event loop
|
|
154
|
-
// and letting hooks and component lifecycle methods to execute mount
|
|
155
|
-
// https://github.com/bahmutov/cypress-react-unit-test/issues/200
|
|
156
|
-
.wait(0, { log: false })
|
|
157
|
-
.then(() => {
|
|
158
|
-
if (options.log !== false) {
|
|
159
|
-
// Get the display name property via the component constructor
|
|
160
|
-
const componentName = getDisplayName(jsx);
|
|
161
|
-
const jsxComponentName = `<${componentName} ... />`;
|
|
162
|
-
Cypress.log({
|
|
163
|
-
name: type,
|
|
164
|
-
type: 'parent',
|
|
165
|
-
message: [jsxComponentName],
|
|
166
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment -- @ts-expect-error would be unused once the cli typedefs are generated
|
|
167
|
-
// @ts-ignore - JQuery comes from cli/types/jquery, which is generated by the cli build and missing when this package builds first
|
|
168
|
-
$el: el.children.item(0),
|
|
169
|
-
consoleProps: () => {
|
|
170
|
-
return {
|
|
171
|
-
// @ts-expect-error - `props` is not on every `ReactNode` member, such as string
|
|
172
|
-
props: jsx === null || jsx === void 0 ? void 0 : jsx.props,
|
|
173
|
-
description: type === 'mount' ? 'Mounts React component' : 'Rerenders mounted React component',
|
|
174
|
-
home: 'https://github.com/cypress-io/cypress',
|
|
175
|
-
};
|
|
176
|
-
},
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
}));
|
|
180
|
-
// Bluebird types are terrible. I don't think the return type can be carried without this cast
|
|
181
|
-
});
|
|
182
|
-
};
|
|
183
|
-
/**
|
|
184
|
-
* Create an `unmount` function. Performs all the non-React-version specific
|
|
185
|
-
* behavior related to unmounting.
|
|
186
|
-
*
|
|
187
|
-
* This is designed to be consumed by `npm/react{16,17,18}`, and other React adapters,
|
|
188
|
-
* or people writing adapters for third-party, custom adapters.
|
|
189
|
-
*
|
|
190
|
-
* @param {UnmountArgs} options used during unmounting
|
|
191
|
-
*/
|
|
192
|
-
const makeUnmountFn = (options) => {
|
|
193
|
-
return cy.then(() => {
|
|
194
|
-
var _a;
|
|
195
|
-
const wasUnmounted = mountCleanup === null || mountCleanup === void 0 ? void 0 : mountCleanup();
|
|
196
|
-
if (wasUnmounted && options.log) {
|
|
197
|
-
Cypress.log({
|
|
198
|
-
name: 'unmount',
|
|
199
|
-
type: 'parent',
|
|
200
|
-
message: [(_a = options.boundComponentMessage) !== null && _a !== void 0 ? _a : 'Unmounted component'],
|
|
201
|
-
consoleProps: () => {
|
|
202
|
-
return {
|
|
203
|
-
description: 'Unmounts React component',
|
|
204
|
-
parent: getContainerEl().parentNode,
|
|
205
|
-
home: 'https://github.com/cypress-io/cypress',
|
|
206
|
-
};
|
|
207
|
-
},
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
};
|
|
212
|
-
// Cleanup before each run
|
|
213
|
-
// NOTE: we cannot use unmount here because
|
|
214
|
-
// we are not in the context of a test
|
|
215
|
-
const preMountCleanup = () => {
|
|
216
|
-
mountCleanup === null || mountCleanup === void 0 ? void 0 : mountCleanup();
|
|
217
|
-
};
|
|
218
|
-
const _mount = (jsx, options = {}) => makeMountFn('mount', jsx, options);
|
|
219
|
-
const createMount = (defaultOptions) => {
|
|
220
|
-
return (element, options) => {
|
|
221
|
-
return _mount(element, Object.assign(Object.assign({}, defaultOptions), options));
|
|
222
|
-
};
|
|
223
|
-
};
|
|
224
|
-
// Side effects from "import { mount } from '@cypress/<my-framework>'" are annoying, we should avoid doing this
|
|
225
|
-
// by creating an explicit function/import that the user can register in their 'component.js' support file,
|
|
226
|
-
// such as:
|
|
227
|
-
// import 'cypress/<my-framework>/support'
|
|
228
|
-
// or
|
|
229
|
-
// import { registerCT } from 'cypress/<my-framework>'
|
|
230
|
-
// registerCT()
|
|
231
|
-
// Note: This would be a breaking change
|
|
232
|
-
// it is required to unmount component in beforeEach hook in order to provide a clean state inside test
|
|
233
|
-
// because `mount` can be called after some preparation that can side effect unmount
|
|
234
|
-
// @see npm/react/cypress/component/advanced/set-timeout-example/loading-indicator.cy.tsx
|
|
235
|
-
setupHooks(preMountCleanup);
|
|
236
|
-
|
|
237
|
-
let root;
|
|
238
|
-
const cleanup = () => {
|
|
239
|
-
if (root) {
|
|
240
|
-
root.unmount();
|
|
241
|
-
root = null;
|
|
242
|
-
return true;
|
|
243
|
-
}
|
|
244
|
-
return false;
|
|
245
|
-
};
|
|
246
|
-
/**
|
|
247
|
-
* Mounts a React component into the DOM.
|
|
248
|
-
* @param {import('react').JSX.Element} jsx The React component to mount.
|
|
249
|
-
* @param {MountOptions} options Options to pass to the mount function.
|
|
250
|
-
* @param {string} rerenderKey A key to use to force a rerender.
|
|
251
|
-
*
|
|
252
|
-
* @example
|
|
253
|
-
* import { mount } from '@cypress/react'
|
|
254
|
-
* import { Stepper } from './Stepper'
|
|
255
|
-
*
|
|
256
|
-
* it('mounts', () => {
|
|
257
|
-
* mount(<StepperComponent />)
|
|
258
|
-
* cy.get('[data-cy=increment]').click()
|
|
259
|
-
* cy.get('[data-cy=counter]').should('have.text', '1')
|
|
260
|
-
* }
|
|
261
|
-
*
|
|
262
|
-
* @see {@link https://on.cypress.io/mounting-react} for more details.
|
|
263
|
-
*
|
|
264
|
-
* @returns {Cypress.Chainable<MountReturn>} The mounted component.
|
|
265
|
-
*/
|
|
266
|
-
function mount(jsx, options = {}, rerenderKey) {
|
|
267
|
-
// Remove last mounted component if cy.mount is called more than once in a test
|
|
268
|
-
// React by default removes the last component when calling render, but we should remove the root
|
|
269
|
-
// to wipe away any state
|
|
270
|
-
cleanup();
|
|
271
|
-
const internalOptions = {
|
|
272
|
-
reactDom: ReactDOM,
|
|
273
|
-
render: (reactComponent, el) => {
|
|
274
|
-
if (!root) {
|
|
275
|
-
root = ReactDOM.createRoot(el);
|
|
276
|
-
}
|
|
277
|
-
return root.render(reactComponent);
|
|
278
|
-
},
|
|
279
|
-
unmount: internalUnmount,
|
|
280
|
-
cleanup,
|
|
281
|
-
};
|
|
282
|
-
return makeMountFn('mount', jsx, Object.assign({ ReactDom: ReactDOM }, options), rerenderKey, internalOptions);
|
|
283
|
-
}
|
|
284
|
-
function internalUnmount(options = { log: true }) {
|
|
285
|
-
return makeUnmountFn(options);
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
exports.createMount = createMount;
|
|
289
|
-
exports.getContainerEl = getContainerEl;
|
|
290
|
-
exports.makeMountFn = makeMountFn;
|
|
291
|
-
exports.makeUnmountFn = makeUnmountFn;
|
|
292
|
-
exports.mount = mount;
|