-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add TypeScript example. (#515)
Co-authored-by: Gleb Bahmutov <gleb.bahmutov@gmail.com> Co-authored-by: Chris Breiding <chrisbreiding@gmail.com>
- Loading branch information
1 parent
6fe0687
commit 86d0f7d
Showing
14 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
12 changes: 12 additions & 0 deletions
12
examples/fundamentals__typescript/cypress/fixtures/test.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
22 changes: 22 additions & 0 deletions
22
examples/fundamentals__typescript/cypress/integration/tests.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
|
||
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) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
6
examples/fundamentals__typescript/cypress/support/commands.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
examples/fundamentals__typescript/cypress/support/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// 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 { | ||
// add custom Cypress command to the interface Chainable<Subject> | ||
interface Chainable<Subject=any> { | ||
// let TS know we have a custom command cy.clickLink(...) | ||
clickLink(label: string | number | RegExp): void | ||
} | ||
|
||
// add properties the application adds to its "window" object | ||
// by adding them to the interface ApplicationWindow | ||
interface ApplicationWindow { | ||
// let TS know the application's code will add | ||
// method window.add with the following signature | ||
add(a: number, b: number): number | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// This file is required to make `npm run tsc` work. | ||
{ | ||
"include": [ | ||
"cypress/integration/*.ts", | ||
"cypress/integration/**/*.ts", | ||
] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters