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

Commit 6115af6

Browse files
authored
Better ts commands (#7)
* .. * much better TS commands thanks to declare global * comment out TS linting for now * update travis file * add package lock file * update readme file
1 parent a89499b commit 6115af6

14 files changed

+6162
-163
lines changed

.npmrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
registry=http://registry.npmjs.org/
2+
save-exact=true
3+
progress=false
4+
package-lock=true

.travis.yml

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
## our React TodoMVC app is written
2-
## in node so thats what we select.
3-
## Cypress is agnostic to your apps
4-
## backend language though.
51
language: node_js
62

73
node_js:
8-
- 8
4+
- 10
95

106
cache:
117
directories:
12-
- node_modules
8+
- ~/.npm
9+
- ~/.cache
1310

1411
install:
15-
- npm install
12+
- npm ci
1613

1714
script:
18-
- npm run lint
15+
# comment out for now - Cypress v3.1.1 should have better TS definition for jQuery
16+
# to pass linting
17+
# - npm run lint
1918
- npm run cypress:run

README.md

+13-55
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,26 @@
11
[![Build Status](https://travis-ci.org/cypress-io/add-cypress-custom-command-in-typescript.svg?branch=master)](https://travis-ci.org/cypress-io/add-cypress-custom-command-in-typescript)
22

3-
* install typescript compiler v2.6.2
4-
* install Cypress v1.4.1
5-
* open Cypress once to scaffold tests
6-
* rename [cypress/integration/spec.ts](cypress/integration/spec.ts)
7-
* created `tsconfig.json` file
3+
To add TypeScript support to Cypress
84

9-
```json
10-
{
11-
"include": [
12-
"node_modules/cypress",
13-
"cypress/*/*.ts"
14-
]
15-
}
16-
```
5+
* `npm i -D @bahmutov/add-typescript-to-cypress`
6+
* start using TypeScript in your [cypress/integration/spec.ts](cypress/integration/spec.ts)
7+
* you can write [custom Cypress commands](https://on.cypress.io/custom-commands) in TypeScript, see example in [cypress/support/plugins.ts](cypress/support/plugins.ts) to add custom commands.
178

18-
* inside [cypress/support/commands.ts](cypress/support/commands.ts) create new function `foo`
9+
## Screenshots
1910

20-
```ts
21-
function foo() {
22-
return 'foo'
23-
}
24-
```
25-
26-
* then add this function Cypress commands
27-
28-
```ts
29-
Cypress.Commands.add('foo', foo)
30-
```
31-
32-
* and add to Cypress TS chainer interface
33-
34-
```ts
35-
declare namespace Cypress {
36-
interface Chainable {
37-
/**
38-
* Yields "foo"
39-
*
40-
* @returns {typeof foo}
41-
* @memberof Chainable
42-
* @example
43-
* cy.foo().then(f => ...) // f is "foo"
44-
*/
45-
foo(): typeof foo
46-
}
47-
}
48-
```
49-
50-
* use this in the test [cypress/integration/spec.ts](cypress/integration/spec.ts)
51-
52-
```ts
53-
it('has new command foo', () => {
54-
cy.foo().should('equal', 'foo')
55-
})
56-
```
57-
58-
* add TypeScript preprocessor to Cypress using `npm i -D @bahmutov/add-typescript-to-cypress` v 1.1.1
59-
60-
* run Cypress to make sure the test passes
11+
* passing tests that exercise custom commands
6112

6213
![test](images/cy-foo-works.png)
6314

6415
* VSCode IntelliSense correctly shows `cy.foo()` docs
6516

6617
![IntelliSense](images/cy-foo-intellisense.png)
6718

19+
## Problems
20+
21+
If you hit a problem, please open an issue either in this repo or in [cypress-io/cypress](https://github.com/cypress-io/cypress) with reproducible source code. The best issues are the ones we can "git clone ...", install dependencies and run to see the problem. This ensures we can fix it quicker.
22+
23+
## Additional information
6824

25+
- [bahmutov/add-typescript-to-cypress](https://github.com/bahmutov/add-typescript-to-cypress)
26+
- [Cypress Tooling: TypeScript](https://on.cypress.io/typescript-support)

cypress.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"ignoreTestFiles": "*.js",
3-
"videoRecording": false
3+
"video": false
44
}

cypress/integration/spec.ts

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
;(() => {
2-
// try to have name constant in test as in commands.ts
3-
const _ = Cypress._
4-
const name = 'Cypress-command'
5-
console.log('name in commands is', name)
1+
it('works', () => {
2+
expect(Cypress.version).to.be.a('string')
3+
})
64

7-
it('works', () => {
8-
expect(Cypress.version).to.be.a('string')
9-
})
5+
it('has new command foo', () => {
6+
cy.foo().should('equal', 'foo')
7+
})
108

11-
it('has new command foo', () => {
12-
cy.foo().should('equal', 'foo')
13-
})
14-
15-
it('has new command foo2 that uses foo', () => {
16-
cy.foo2().should('equal', 'foo')
17-
})
18-
})()
9+
it('has new command foo2 that uses foo', () => {
10+
cy.foo2().should('equal', 'foo')
11+
})

cypress/plugins/cy-ts-preprocessor.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const wp = require('@cypress/webpack-preprocessor')
2+
3+
const webpackOptions = {
4+
resolve: {
5+
extensions: ['.ts', '.js']
6+
},
7+
module: {
8+
rules: [
9+
{
10+
test: /\.ts$/,
11+
exclude: [/node_modules/],
12+
use: [
13+
{
14+
loader: 'ts-loader'
15+
}
16+
]
17+
}
18+
]
19+
}
20+
}
21+
22+
const options = {
23+
webpackOptions
24+
}
25+
26+
module.exports = wp(options)

cypress/plugins/index.js

+2-23
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,5 @@
1-
const wp = require('@cypress/webpack-preprocessor')
2-
3-
const webpackOptions = {
4-
resolve: {
5-
extensions: ['.ts'],
6-
},
7-
module: {
8-
rules: [
9-
{
10-
test: /\.ts$/,
11-
exclude: [/node_modules/],
12-
loader: 'ts-loader',
13-
options: {
14-
configFile: '../../tsconfig.json',
15-
},
16-
},
17-
],
18-
},
19-
}
1+
const cypressTypeScriptPreprocessor = require('./cy-ts-preprocessor')
202

213
module.exports = on => {
22-
const options = {
23-
webpackOptions,
24-
}
25-
on('file:preprocessor', wp(options))
4+
on('file:preprocessor', cypressTypeScriptPreprocessor)
265
}

cypress/support/actions.ts

-22
This file was deleted.

cypress/support/commands.ts

+38-30
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
1-
// importing into the TS module that merged module interface does not work
2-
// https://github.com/cypress-io/add-cypress-custom-command-in-typescript/issues/2
3-
// import {foo, foo2} from './actions'
4-
// but requiring in CommonJS style works
5-
;(() => {
6-
const { foo, foo2 } = require('./actions')
7-
const _ = Cypress._
8-
9-
// add commands to Cypress like "cy.foo()" and "cy.foo2()"
10-
Cypress.Commands.add('foo', foo)
11-
Cypress.Commands.add('foo2', foo2)
12-
13-
// unrelated constant - does it conflict with a constant with same name
14-
// in your tests?
15-
const name = 'Cypress-command'
16-
console.log('name in commands is', name)
17-
})()
18-
191
// add new command to the existing Cypress interface
20-
declare namespace Cypress {
21-
interface Chainable {
22-
/**
23-
* Yields "foo"
24-
*
25-
* @returns {typeof foo}
26-
* @memberof Chainable
27-
* @example
28-
* cy.foo().then(f = ...) // f is "foo"
29-
*/
30-
foo: () => string
31-
foo2: () => string
2+
declare global {
3+
namespace Cypress {
4+
interface Chainable {
5+
/**
6+
* Yields "foo"
7+
*
8+
* @returns {typeof foo}
9+
* @memberof Chainable
10+
* @example
11+
* cy.foo().then(f = ...) // f is "foo"
12+
*/
13+
foo: typeof foo
14+
foo2: typeof foo2
15+
}
3216
}
3317
}
18+
19+
/**
20+
* An example function "foo()"
21+
*
22+
* @returns {string} "foo"
23+
* @example
24+
* foo() // "foo"
25+
*/
26+
export function foo() {
27+
return 'foo'
28+
}
29+
30+
/**
31+
* Uses cy.foo() internally
32+
*
33+
* @returns
34+
*/
35+
export function foo2() {
36+
return cy.foo()
37+
}
38+
39+
// add commands to Cypress like "cy.foo()" and "cy.foo2()"
40+
Cypress.Commands.add('foo', foo)
41+
Cypress.Commands.add('foo2', foo2)

images/cy-foo-intellisense.png

28.7 KB
Loading

images/cy-foo-works.png

31.3 KB
Loading

0 commit comments

Comments
 (0)