Cypress Typescript - biswajitsundara/Cypress GitHub Wiki
Cypress can be easily integrated with Type Script
mkdir demoproject
This will create an empty folder with name demoproject
npm init --y
This will create package.json file with default options.
npm install cypress
This will install the latest cypress version and the dependency will be added to package.json file.
node_modules/.bin/cypress open
The cypress test runner will be opened and it will populate the default cypress folder structure.
Create a test file under integration folder e.g cypress/integration/basic.js
describe('demo test suite', ()=>{
it('demo test case',()=>{
cy.log('Hello world');
})
})
npm install typescriptnpm install @types/node
This will install the typescript and add the dependency to package.json file
Change the file extension plugins/index.js to index.ts below code should be already available.
// cypress/plugins/index.ts
/// <reference types="cypress" />
/**
* @type {Cypress.PluginConfig}
*/
module.exports = (on, config) => {
}
tsc --init
This will generate tsconfig.json file with below codes.
{
"compilerOptions": {
"target": "es5",
"lib": ["es5", "dom"],
"types": ["cypress","node"]
},
"include": ["**/*.ts"]
}
- Change the file extension from
.jsto.tsfor the file created at step #5 - cypress/integration/basic.ts
- Launch the test runner and run the file.
- It should work fine.
"dependencies": {
"@types/node": "^14.6.2",
"cypress": "^4.12.1",
"typescript": "^4.0.2"
}
tsc --init --types cypress
This will generate the file as below.
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es5","dom"],
"types": ["cypress","node"],
"esModuleInterop": true,
/* Advanced Options */
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"compileOnSave": false,
"include": [
"**/*.ts"
]
}
it('should perform basic google search', () => {
cy.visit("https://google.co.in");
cy.get('[name="q"]')
.type('subscribe')
.type('{enter}');
});
});
https://docs.cypress.io/guides/tooling/typescript-support.html#Install-TypeScript
https://basarat.gitbook.io/typescript/intro-1/cypress
https://github.com/TheBrainFamily/cypress-cucumber-preprocessor#typeScript-support