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

docs: add workaround for TS custom commands #5454

Conversation

marmaladebacon
Copy link

@marmaladebacon marmaladebacon commented Aug 31, 2023

closes #27641

@netlify
Copy link

netlify bot commented Aug 31, 2023

‼️ Deploy request for cypress-docs rejected.

Name Link
🔨 Latest commit 91572f5

@CLAassistant
Copy link

CLAassistant commented Aug 31, 2023

CLA assistant check
All committers have signed the CLA.

@netlify
Copy link

netlify bot commented Aug 31, 2023

Deploy Preview for benevolent-cat-040f48 ready!

Name Link
🔨 Latest commit 91572f5
🔍 Latest deploy log https://app.netlify.com/sites/benevolent-cat-040f48/deploys/6513f03fd009550008c2a567
😎 Deploy Preview https://deploy-preview-5454--benevolent-cat-040f48.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@cypress-app-bot
Copy link
Collaborator

@nagash77
Copy link
Contributor

nagash77 commented Sep 1, 2023

Hi @marmaladebacon you will need to sign the CLA before this PR can be merged.

Comment on lines 902 to 925
From [email protected], if you are using TypeScript and have `sideEffects:false` set in your package.json, custom Cypress commands will not be registered by the common pattern of writing the commands in one file and having them be run as a side effect of importing the file. For example:

```typescript title="/cypress/support/commands.ts"
Cypress.Commands.add("login", (email, password) => { ... })
```

```typescript title="/cypress/support/e2e.ts"
import './commands'
```

To work around this issue, you can wrap the Cypress commands in a function that will be imported by the support file.

```typescript title="/cypress/support/commands.ts"
export function registerCommands(){
Cypress.Commands.add("login", (email, password) => { ... })
}
```

```typescript title="/cypress/support/e2e.ts"
import { registerCommands } from './commands'
registerCommands()
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "@" actually creates a mailto: link in the markdown. Here are some edits to give more context

Suggested change
From [email protected], if you are using TypeScript and have `sideEffects:false` set in your package.json, custom Cypress commands will not be registered by the common pattern of writing the commands in one file and having them be run as a side effect of importing the file. For example:
```typescript title="/cypress/support/commands.ts"
Cypress.Commands.add("login", (email, password) => { ... })
```
```typescript title="/cypress/support/e2e.ts"
import './commands'
```
To work around this issue, you can wrap the Cypress commands in a function that will be imported by the support file.
```typescript title="/cypress/support/commands.ts"
export function registerCommands(){
Cypress.Commands.add("login", (email, password) => { ... })
}
```
```typescript title="/cypress/support/e2e.ts"
import { registerCommands } from './commands'
registerCommands()
```
Cypress [12.17.4](/guides/references/changelog#12-17-4) includes a webpack upgrade (v4 to v5), which [tree shakes out](https://webpack.js.org/blog/2020-10-10-webpack-5-release/#inner-module-tree-shaking) any side-effects or files that only include side-effects.
If you are using TypeScript and have `sideEffects:false` set in your package.json, custom Cypress commands will not be registered by the common pattern of writing the commands in one file and having them be run as a side effect of importing the file. For example:
```typescript
// cypress/support/commands.ts
Cypress.Commands.add("login", (email, password) => { ... })
// cypress/support/e2e.ts

import './commands'

To support sideEffects:false, you can wrap the Cypress commands in a function that will be imported by the support file.

// cypress/support/commands.ts

export function registerCommands(){
  Cypress.Commands.add("login", (email, password) => { ... })
}
// cypress/support/e2e.ts

import { registerCommands } from './commands'
registerCommands()

Copy link
Author

@marmaladebacon marmaladebacon Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, i've added the suggested changes in the latest commit. Let me know if i missed anything, i had to do it manually because the markdown syntax probably broke halfway through the suggestion.

@marmaladebacon marmaladebacon force-pushed the document-specific-ts-custom branch from d9a3be1 to 1d4f735 Compare September 25, 2023 09:01
@marmaladebacon marmaladebacon force-pushed the document-specific-ts-custom branch from 1d4f735 to 83ecd6a Compare September 25, 2023 09:05
@jaffrepaul
Copy link
Contributor

Thank you for the contribution @marmaladebacon This was getting crazier and crazier with the formatting. Instead of wrestling with the markdown issues in the GH web UI, I just ported this over to this PR and merged it. Much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change in behaviour from 12.17.3 to 12.17.4
5 participants