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

feat: Add TypeScript example. #515

Merged
merged 19 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Recipe | Description
[Cypress module API](./examples/fundamentals__module-api) | Run Cypress via its module API
[Custom browsers](./examples/fundamentals__custom-browsers) | Control which browsers the project can use, or even add a custom browser into the list
[use Chrome Remote Interface](./examples/fundamentals__chrome-remote-debugging) | Use Chrome debugger protocol to trigger hover state and print media style
[Out-of-the-box TypeScript](./examples/fundamentals__typescript) | Write tests in TypeScript without setting up preprocessors

## Testing the DOM

Expand Down
6 changes: 6 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ jobs:
<<: *defaults
fundamentals__chrome-remote-debugging:
<<: *defaults
fundamentals__typescript:
<<: *defaults

# list all jobs to run and their dependencies here
# and then use this list from workflow definition
Expand Down Expand Up @@ -487,6 +489,9 @@ all_jobs: &all_jobs
- fundamentals__module-api:
requires:
- build
- fundamentals__typescript:
requires:
- build

# to avoid constantly tweaking required jobs on CircleCI
# we can have a single job wait on all other test jobs here.
Expand Down Expand Up @@ -516,6 +521,7 @@ all_jobs: &all_jobs
- fundamentals__dynamic-tests
- fundamentals__module-api
- fundamentals__add-custom-command
- fundamentals__typescript
- logging-in__csrf-tokens
- logging-in__html-web-forms
- logging-in__single-sign-on
Expand Down
10 changes: 10 additions & 0 deletions examples/fundamentals__typescript/README.Md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Out-of-the-box TypeScript
> Write tests in TypeScript without setting up preprocessors

From Cypress 4.4.0, you can write tests in TypeScript without setting up preprocessors. See [the official doc](https://on.cypress.io/typescript-support) for more details.

- Use out-of-the-box TypeScript.
- Write spec, plugin, support files in TypeScript.
- Define type for the custom commands.
- Check types in the spec files.
- Show difference between Test Runner `window` and `AUTWindow` types. And how to extend `AUTWindow` type.
1 change: 1 addition & 0 deletions examples/fundamentals__typescript/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
12 changes: 12 additions & 0 deletions examples/fundamentals__typescript/cypress/fixtures/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<a href="#">click me</a>
</body>
<script>
window.add = (a, b) => a + b
</script>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* global window */
/// <reference path="../types.d.ts" />

describe('tests', () => {
it('test custom command', () => {
cy.visit('cypress/fixtures/test.html')
cy.clickLink('click me')
sainthkh marked this conversation as resolved.
Show resolved Hide resolved
})

it('test extending AUTWindow', () => {
// Test Runner window object doesn't have add() function.
// So, it should fail the type check.
// @ts-expect-error
window.add = (a, b) => a + b

cy.window().then((win) => {
// AUT add() is defined in the fixture, test.html.
// So, it should pass the type check.
return win.add(2, 3)
})
})
})
5 changes: 5 additions & 0 deletions examples/fundamentals__typescript/cypress/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="cypress" />

export default (on: Cypress.PluginEvents, config: Cypress.PluginConfigOptions) => {

}
6 changes: 6 additions & 0 deletions examples/fundamentals__typescript/cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// <reference types="cypress" />

// Copied an example command from https://on.cypress.io/custom-commands
Cypress.Commands.add('clickLink', (label: string | number | RegExp) => {
cy.get('a').contains(label).click()
})
20 changes: 20 additions & 0 deletions examples/fundamentals__typescript/cypress/support/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ***********************************************************
// This example support/index.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands'

// Alternatively you can use CommonJS syntax:
// require('./commands')
25 changes: 25 additions & 0 deletions examples/fundamentals__typescript/cypress/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// reference code is written like below to avoid the clash in mocha types.
// in most of the cases, simple <reference types="cypress" /> will do.
/// <reference path="../../../node_modules/cypress/types/cy-blob-util.d.ts" />
/// <reference path="../../../node_modules/cypress/types/cy-bluebird.d.ts" />
/// <reference path="../../../node_modules/cypress/types/cy-moment.d.ts" />
/// <reference path="../../../node_modules/cypress/types/cy-minimatch.d.ts" />
/// <reference path="../../../node_modules/cypress/types/lodash/index.d.ts" />
/// <reference path="../../../node_modules/cypress/types/sinon/index.d.ts" />
/// <reference path="../../../node_modules/cypress/types/jquery/index.d.ts" />
/// <reference path="../../../node_modules/cypress/types/cypress.d.ts" />
/// <reference path="../../../node_modules/cypress/types/cypress-type-helpers.d.ts" />
/// <reference path="../../../node_modules/cypress/types/cypress-global-vars.d.ts" />

declare namespace Cypress {
interface Chainable<Subject=any> {
bahmutov marked this conversation as resolved.
Show resolved Hide resolved
// let TS know we have a custom command cy.clickLink(...)
clickLink(label: string | number | RegExp): void
bahmutov marked this conversation as resolved.
Show resolved Hide resolved
}

interface ApplicationWindow {
bahmutov marked this conversation as resolved.
Show resolved Hide resolved
// let TS know the application's code will add
// method window.add with the following signature
add(a: number, b: number): number
bahmutov marked this conversation as resolved.
Show resolved Hide resolved
}
}
15 changes: 15 additions & 0 deletions examples/fundamentals__typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "with-typescript",
"version": "1.0.0",
"description": "Use out-of-the-box TypeScript with TypeScript",
"scripts": {
"cypress:open": "../../node_modules/.bin/cypress open",
"precypress:run": "npm run lint",
"cypress:run": "../../node_modules/.bin/cypress run",
"lint": "../../node_modules/.bin/tslint --project ./tsconfig.json",
"postlint": "npm run tsc",
"test:ci": "../../node_modules/.bin/cypress run",
"test:ci:windows": "bin-up cypress run",
"tsc": "../../node_modules/.bin/tsc --pretty --noEmit"
}
}
7 changes: 7 additions & 0 deletions examples/fundamentals__typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// This file is required to make `npm run tsc` work.
{
bahmutov marked this conversation as resolved.
Show resolved Hide resolved
"include": [
"cypress/integration/*.ts",
"cypress/integration/**/*.ts",
]
}
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"stop-only": "3.0.1",
"terminal-banner": "1.1.0",
"tslint": "5.9.1",
"typescript": "3.3.3",
"typescript": "3.9.5",
"vue-loader": "14.2.4",
"vue-template-compiler": "2.6.10",
"wait-on": "3.2.0",
Expand Down