Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 4237f68

Browse files
Merge pull request #313 from chakra-ui/fix/cypress-tests-setup
2 parents 2568cb5 + cc47753 commit 4237f68

File tree

112 files changed

+532
-477
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+532
-477
lines changed

.github/workflows/quality.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ jobs:
2727
run: pnpm lint
2828

2929
- name: Run Component Tests
30-
run: pnpm test:ci
30+
run:
31+
pnpm test:ci && pnpm cy:run --component --record --key
32+
$CYPRESS_COMPONENT_TESTING_KEY
3133
env:
34+
CYPRESS_COMPONENT_TESTING_KEY:
35+
${{ secrets.CYPRESS_COMPONENT_TESTING_KEY }}
3236
CI: true
3337

3438
- name: Build packages

@types/components.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
2-
* Typescript support for @chakra-ui/vue-next2.1.0-beta.10 auto-imported
2+
* Typescript support for @chakra-ui/vue-next2.1.0-beta.11 auto-imported
33
* components using `unplugin-vue-components,`
44
*
55
* @see: https://github.com/antfu/unplugin-vue-components/#typescript
66
*
77
* This is a generated file. Do not edit it's contents.
88
*
9-
* This file was generated on 2023-03-28T18:38:28.043Z
9+
* This file was generated on 2023-04-22T14:04:49.473Z
1010
*/
1111

1212
import { ChakraProps, chakra } from "@chakra-ui/vue-system"

@types/cypress-shims.d.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

@types/cypress.d.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/// <reference types="cypress" />
2+
3+
import { mount } from "cypress/vue"
4+
5+
import type { mount as cyMount } from "cypress/vue"
6+
import type { MountingOptions, VueWrapper } from "@vue/test-utils"
7+
import type {
8+
AllowedComponentProps,
9+
ComponentPublicInstance,
10+
FunctionalComponent,
11+
VNodeProps,
12+
} from "vue"
13+
14+
type Swipe = number[] | string
15+
16+
type StripProps =
17+
| keyof VNodeProps
18+
| keyof AllowedComponentProps
19+
| "v-slots"
20+
| "$children"
21+
| `v-slot:${string}`
22+
23+
type Events<T> = T extends { $props: infer P extends object }
24+
? {
25+
[K in Exclude<keyof P, StripProps> as K extends `on${infer N}`
26+
? Uncapitalize<N>
27+
: never]: P[K] extends ((...args: any[]) => any) | undefined
28+
? Parameters<NonNullable<P[K]>>[]
29+
: never
30+
}
31+
: never
32+
33+
type MountParams = Parameters<typeof mount>
34+
type OptionsParam = MountParams[1]
35+
36+
declare global {
37+
namespace Cypress {
38+
interface Chainable {
39+
mount: typeof mount
40+
emitted<
41+
T extends new (...args: any) => any,
42+
E extends Events<InstanceType<T>>,
43+
K extends keyof E
44+
>(
45+
selector: T,
46+
event?: K
47+
): Chainable<E[K]>
48+
/**
49+
* Run a11y tests or only a subset of all tests
50+
* @see https://github.com/avanslaars/cypress-axe
51+
* @example
52+
* cy.checkA11y()
53+
*/
54+
checkA11y(options?: any, params?: object): Chainable
55+
/**
56+
* Triggers tab action
57+
* @param options
58+
*/
59+
tab(options?: { shift: boolean }): Chainable
60+
}
61+
}
62+
}
63+
64+
declare module "cypress/vue" {
65+
export function mount(
66+
component: JSX.Element,
67+
options?: MountingOptions<any> | null
68+
): Cypress.Chainable
69+
}

@types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import "./components"
2-
import "./cypress-shims"
2+
import "./cypress"
33
import "./env"
44
import "./vue-shims"

CONTRIBUTING.md

Lines changed: 93 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,43 @@ cd chakra-ui
1919
```
2020

2121
3. Install dependencies and bootstrap the project
22+
2223
```sh
2324
pnpm install
2425
```
2526

2627
4. Start development server for packages
28+
2729
```sh
2830
pnpm dev
2931
```
3032

3133
5. Open component playground
34+
3235
```sh
3336
pnpm playground:dev
3437
```
3538

36-
> If you run into any issues during these steps, kindly reach out to the Chakra UI
37-
> Vue team here:[https://discord.gg/cMpMfvxa](https://discord.gg/cMpMfvxa)
39+
> If you run into any issues during these steps, kindly reach out to the Chakra
40+
> UI Vue team here:[https://discord.gg/cMpMfvxa](https://discord.gg/cMpMfvxa)
3841
3942
## For Windows OS Users
40-
There may be some trouble specific to the local setup in Windows. The following are suggestions in ensuring the local environment boots up successfully:
4143

42-
- The package dependencies and scripts should work with Node `v16.16.0 and higher`
44+
There may be some trouble specific to the local setup in Windows. The following
45+
are suggestions in ensuring the local environment boots up successfully:
4346

47+
- The package dependencies and scripts should work with Node
48+
`v16.16.0 and higher`
4449

4550
## Development
4651

4752
To improve our development process, we've set up tooling and systems. Chakra UI
4853
uses a monorepo structure and we treat each component as an independent package
4954
that can be consumed in isolation.
5055

51-
If you are looking to build a new component, and it has been approved by the team, head over to the [components-guide.md](./docs/guides/component-guide.md) to help you get started!
56+
If you are looking to build a new component, and it has been approved by the
57+
team, head over to the [components-guide.md](./docs/guides/component-guide.md)
58+
to help you get started!
5259

5360
### Tooling
5461

@@ -66,7 +73,8 @@ If you are looking to build a new component, and it has been approved by the tea
6673
**`pnpm install`**: bootstraps the entire project, symlinks all dependencies for
6774
cross-component development and builds all components.
6875

69-
**`pnpm playground:dev`**: starts components playground server and loads stories in SFCs in the `packages/**/examples/*.vue` file.
76+
**`pnpm playground:dev`**: starts components playground server and loads stories
77+
in SFCs in the `packages/**/examples/*.vue` file.
7078

7179
**`pnpm docs:dev`**: run the documentation site locally.
7280

@@ -87,8 +95,9 @@ to run commands within component packages directly from the root.
8795
Each component is named this way: `@chakra-ui/[component]`. Let's say we want to
8896
build the button component. Here's how to do it:
8997

90-
> *Take note that in order to prevent template tags name clashing with HTML elements or other Vue library components,*
91-
> *we prefix all component names with a `c-` in kebab-case or a capital `C` in PascalCase.*
98+
> _Take note that in order to prevent template tags name clashing with HTML
99+
> elements or other Vue library components,_ > _we prefix all component names
100+
> with a `c-` in kebab-case or a capital `C` in PascalCase._
92101
93102
```bash
94103
pnpm workspace @chakra-ui/c-button build
@@ -99,6 +108,7 @@ lerna run build --scope @chakra-ui/c-button
99108
```
100109

101110
**Shortcut:**
111+
102112
```bash
103113
# to build
104114
pnpm pkg @chakra-ui/c-tabs build
@@ -114,14 +124,15 @@ and want to avoid running the command for all components.
114124

115125
### Documentation
116126

117-
The documentation site is built with Vite.js on SSR. If you'd like to contribute to the
118-
docs, simply run `pnpm build`, and `pnpm docs:dev`
127+
The documentation site is built with Vite.js on SSR. If you'd like to contribute
128+
to the docs, simply run `pnpm build`, and `pnpm docs:dev`
119129

120130
### Components Development Playground
121131

122132
Build components in isolation with Vite using `pnpm playground:dev`
123133

124-
Run `pnpm start` in a separate terminal first so the packages are built and a watcher set up for changes.
134+
Run `pnpm start` in a separate terminal first so the packages are built and a
135+
watcher set up for changes.
125136

126137
## Think you found a bug?
127138

@@ -131,8 +142,8 @@ link.
131142

132143
You may wish to use our starters to help you get going:
133144

134-
TODO: Add Typescript starter for `@chakra-ui/vue` v1
135-
TODO: Add Javascript starter for `@chakra-ui/vue` v1
145+
TODO: Add Typescript starter for `@chakra-ui/vue` v1 TODO: Add Javascript
146+
starter for `@chakra-ui/vue` v1
136147

137148
## Proposing new or changed API?
138149

@@ -200,9 +211,76 @@ https://www.conventionalcommits.org/ or check out the
200211
> `pnpm changeset add --empty` to generate an empty changeset file to document
201212
> your changes.
202213
203-
### Tests
214+
## Writing Tests
215+
216+
We recommend all new code that is added to the codebase or any refactors that
217+
may modify the behaviour of a given component or module are accompanied with
218+
some simple tests. This ensures that the behaviour of the component is protected
219+
against any accidental regressions in the future, or ensures that the new
220+
refactor does not change the behaviour of a given component.
221+
222+
At the time of writing this this document, unit tests authored in the Chakra UI
223+
Vue codebase are categorized into two types:
224+
225+
1. Component Tests (Behavioural Focused)
226+
2. Black Box Tests (Input-Output Focused)
227+
228+
### Component Tests
204229

205-
All commits that fix bugs or add features need a test.
230+
Authoring component tests typically is aimed at ensuring the behaviour of a
231+
given component based on its public API. Depending on the nature of the
232+
component you are testing, you may want to use either
233+
[Cypress component testing]() which excels this kind of behavioural testing of
234+
with [Jest](), which is mostly used for black-box testing.
235+
236+
#### Testing with Cypress
237+
238+
1. Create your `<COMPONENT>.cy.tsx` file in the
239+
`packages/<COMPONENT>/tests/component/` directory; where `<COMPONENT>` is the
240+
name of the component you are testing. **Please take note that the Cypress
241+
tests are to be put under the `tests/component/**` directory. This is to
242+
prevent type conflicts between Jest and Cypress globals.
243+
2. Add the Cypress type references at the top of the file as shown in the
244+
example below. This will allow typescript to correctly reference the Cypress
245+
globals.
246+
247+
```tsx
248+
/// <reference types="../../../../@types/cypress" />
249+
250+
import { MyComponent } from "../../src/my-component"
251+
252+
it("<MyComponent /> is accessible", () => {
253+
cy.mount(() => <MyComponent>Hello World</MyComponent>)
254+
cy.checkA11y()
255+
})
256+
```
257+
258+
#### Testing with Jest
259+
260+
Create your `<COMPONENT>.test.tsx` file in the `packages/<COMPONENT>/tests/`
261+
directory; where `<COMPONENT>` is the name of the component you are testing.
262+
263+
```tsx
264+
import { MyComponent } from "../src"
265+
import {
266+
render,
267+
userEvent,
268+
testA11y,
269+
TestRenderProps,
270+
} from "@chakra-ui/vue-test-utils"
271+
272+
const renderComponent = (options: Component = {}) =>
273+
render({
274+
components: { MyComponent },
275+
template: `<MyComponent>Hello World</MyComponent>`,
276+
...options,
277+
})
278+
279+
it("should render properly", () => {
280+
const { asFragment } = renderComponent()
281+
expect(asFragment()).toMatchSnapshot()
282+
})
283+
```
206284

207285
> **Dear Chakra UI Vue team:** Please do not merge code without tests
208286

components.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
2-
* Typescript support for @chakra-ui/vue-next2.1.0-beta.10 auto-imported
2+
* Typescript support for @chakra-ui/vue-next2.1.0-beta.11 auto-imported
33
* components using `unplugin-vue-components,`
44
*
55
* @see: https://github.com/antfu/unplugin-vue-components/#typescript
66
*
77
* This is a generated file. Do not edit it's contents.
88
*
9-
* This file was generated on 2023-03-28T18:38:28.043Z
9+
* This file was generated on 2023-04-22T14:04:49.473Z
1010
*/
1111

1212
import { ChakraProps, chakra } from "@chakra-ui/vue-system"

cypress.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineConfig } from "cypress"
22

33
export default defineConfig({
4+
projectId: 'rb8e7m',
45
video: false,
56
fixturesFolder: false,
67
reporter: "spec",

cypress.d.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { mount } from "cypress/vue"
2+
3+
import type { mount as cyMount } from "cypress/vue"
4+
import type { MountingOptions, VueWrapper } from "@vue/test-utils"
5+
import type {
6+
AllowedComponentProps,
7+
ComponentPublicInstance,
8+
FunctionalComponent,
9+
VNodeProps,
10+
} from "vue"
11+
12+
type Swipe = number[] | string
13+
14+
type StripProps =
15+
| keyof VNodeProps
16+
| keyof AllowedComponentProps
17+
| "v-slots"
18+
| "$children"
19+
| `v-slot:${string}`
20+
21+
type Events<T> = T extends { $props: infer P extends object }
22+
? {
23+
[K in Exclude<keyof P, StripProps> as K extends `on${infer N}`
24+
? Uncapitalize<N>
25+
: never]: P[K] extends ((...args: any[]) => any) | undefined
26+
? Parameters<NonNullable<P[K]>>[]
27+
: never
28+
}
29+
: never
30+
31+
type MountParams = Parameters<typeof mount>
32+
type OptionsParam = MountParams[1]
33+
34+
declare global {
35+
namespace Cypress {
36+
interface Chainable {
37+
mount: typeof mount
38+
emitted<
39+
T extends new (...args: any) => any,
40+
E extends Events<InstanceType<T>>,
41+
K extends keyof E
42+
>(
43+
selector: T,
44+
event?: K
45+
): Chainable<E[K]>
46+
}
47+
}
48+
}
49+
50+
declare module "cypress/vue" {
51+
export function mount(
52+
component: JSX.Element,
53+
options?: MountingOptions<any> | null
54+
): Cypress.Chainable
55+
}

0 commit comments

Comments
 (0)