Skip to content

Commit a045f84

Browse files
authored
Update cypress.md
1 parent 257cef0 commit a045f84

File tree

1 file changed

+17
-55
lines changed

1 file changed

+17
-55
lines changed

docs/testing/cypress.md

Lines changed: 17 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Cypress is a great E2E testing tool. Here are a few great reasons to consider it:
33

44
* Isolated installation possible.
5-
* Ships with TypeScript definitions out of the box.
5+
* Ships with TypeScript support out of the box.
66
* Provides a nice interactive google chrome debug experience. This is very similar to how UI devs mostly work manually.
77
* Has command - execution separation which allows for more powerful debugging and test stability (more on this below).
88
* Has implicit assertions to provide more meaningful debug experience with less brittle tests (more on this in the tips below).
@@ -20,74 +20,36 @@ Create an e2e directory and install cypress and its dependencies for TypeScript
2020
mkdir e2e
2121
cd e2e
2222
npm init -y
23-
npm install cypress webpack @cypress/webpack-preprocessor typescript ts-loader
23+
npm install cypress typescript
24+
npx tsc --init --types cypress --lib dom,es6
25+
echo {} > cypress.json
2426
```
2527

2628
> Here are a few reasons for creating a separate `e2e` folder especially for cypress:
2729
* Creating a separate directory or `e2e` makes it easier to isolate its `package.json` dependencies from the rest of your project. This results in less dependency conflicts.
2830
* Testing frameworks have a habit of polluting the global namespace with stuff like `describe` `it` `expect`. It is best to keep the e2e `tsconfig.json` and `node_modules` in this special `e2e` folder to prevent global type definition conflicts.
2931

30-
Setup TypeScript `tsconfig.json` e.g.
32+
Add a few scripts to the `e2e/package.json` file:
3133

3234
```json
33-
{
34-
"compilerOptions": {
35-
"strict": true,
36-
"sourceMap": true,
37-
"module": "commonjs",
38-
"target": "es5",
39-
"lib": [
40-
"dom",
41-
"es6"
42-
],
43-
"types": ["cypress"],
44-
"jsx": "react",
45-
"experimentalDecorators": true
35+
"scripts": {
36+
"cypress:open": "cypress open",
37+
"cypress:run": "cypress run"
4638
},
47-
"compileOnSave": false
48-
}
4939
```
5040

51-
Do a first dry run of cypress to prime the cypress folder structure. The Cypress IDE will open. You can close it after you see the welcome message.
41+
Write your first test under `cypress/integration/basic.ts`:
5242

53-
```sh
54-
npx cypress open
55-
```
56-
57-
Setup cypress for transpiling typescript by editing `e2e/cypress/plugins/index.js` to match the following:
58-
59-
```js
60-
const wp = require('@cypress/webpack-preprocessor')
61-
module.exports = (on) => {
62-
const options = {
63-
webpackOptions: {
64-
resolve: {
65-
extensions: [".ts", ".tsx", ".js"]
66-
},
67-
module: {
68-
rules: [
69-
{
70-
test: /\.tsx?$/,
71-
loader: "ts-loader",
72-
options: { transpileOnly: true }
73-
}
74-
]
75-
}
76-
},
77-
}
78-
on('file:preprocessor', wp(options))
79-
}
43+
```ts
44+
it('should perform basic google search', () => {
45+
cy.visit('https://google.com');
46+
cy.get('[name="q"]')
47+
.type('subscribe')
48+
.type('{enter}');
49+
});
8050
```
8151

82-
83-
Optionally add a few scripts to the `e2e/package.json` file:
84-
85-
```json
86-
"scripts": {
87-
"cypress:open": "cypress open",
88-
"cypress:run": "cypress run"
89-
},
90-
```
52+
Now run `npm run cypress:open` during development and `npm run cypress:run` on your build server 🌹
9153

9254
## More description of key Files
9355
Under the `e2e` folder you now have these files:

0 commit comments

Comments
 (0)