Gathering detailed insights and metrics for cypress-thenify
Gathering detailed insights and metrics for cypress-thenify
Gathering detailed insights and metrics for cypress-thenify
Gathering detailed insights and metrics for cypress-thenify
npm install cypress-thenify
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
18 Stars
9 Commits
2 Forks
3 Watching
1 Branches
2 Contributors
Updated on 20 Aug 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-57.1%
3
Compared to previous day
Last week
36.8%
52
Compared to previous week
Last month
-30.4%
188
Compared to previous month
Last year
17%
4,243
Compared to previous year
2
Cypress commands are asynchronous. It's a common pattern to use a then
callback to get the value of a cypress command.
However, any callback, especially nested one, makes the code less readable.
This plugin allows wrapping a cypress command call statement into a then
callback under the hood and keep your code clean and readable.
Given code
1 let a = cy.get("foo").thenify() 2 cy.log(a.text()) 3 let b = cy.get("bar").thenify() 4 cy.log(a.text()) 5 b.click() // using jQuery click method
will be transpiled into this one:
1 cy.get("foo").then(__cypressSyncVar__ => { 2 let a = __cypressSyncVar__; 3 cy.log(a.text()); 4 cy.get("bar").then(__cypressSyncVar__ => { 5 let b = __cypressSyncVar__; 6 cy.log(a.text()); 7 b.click(); // using jQuery click method 8 }); 9 });
In this mode, the plugin will thenify all the cy
statements. To enable this mode, use total_thenify = 'true'
option, see Plugin options section for details.
Given code
1 let a = cy.get("foo") 2 cy.log(a.text()) 3 let b = cy.get("bar") 4 cy.log(a.text()) 5 b.click() // using jQuery click method
will be transpiled into this one:
1cy.get("foo").then(__cypressSyncVar__ => { 2 let a = __cypressSyncVar__; 3 cy.log(a.text()); 4 cy.then(() => { 5 cy.get("bar").then(__cypressSyncVar__ => { 6 let b = __cypressSyncVar__; 7 cy.log(a.text()); 8 cy.then(() => { 9 b.click(); // using jQuery click method 10 }); 11 }); 12 }); 13});
thenify
call per statement is currently supported. So1 let a = cy.wrap(1).thenify() + cy.wrap(2).thenify()
will produce an error.
cy.thenify()
to 'synchronize' the execution context with the cypress event loop:1 let myValue 2 if (myCondition) { // suppose myCondition is true 3 myValue = cy.wrap("foo").thenify() 4 someOtherCode() 5 console.log(myValue) // will print 'foo' as we are inside the same block of code as the `thenify` call 6 } else { 7 myValue = "bar" 8 } 9 console.log(myValue) // will print `undefined` as we are out of the initial block of code 10 cy.thenify() 11 console.log(myValue) // will print 'foo' as we get in sync with the cypress event loop 12 // because this and all below statements of the current block will be executed under a `then` callback
thenify
call only by its name, no deep semantic analysis is performed. So it will transpile any of myObj.thenify()
calls.
Use the plugin options to define your own unique function name if this default name clashes with any of your existing function (see Plugin options
section).1npm i cypress-thenify -D
If you do not have webpack and babel installed, add them like so per the webpack documentation:
1npm i babel-loader @babel/core @babel/preset-env webpack -D
After that, we need to add this plugin as a babel plugin for Cypress.
Put this into your plugin/index.js
if you have Cypress < 10 version:
1const thenify = require("cypress-thenify") 2const webpackPreprocessor = require('@cypress/webpack-preprocessor') 3 4module.exports = (on) => { 5 const options = webpackPreprocessor.defaultOptions 6 options.webpackOptions.module.rules[0].use[0].options.plugins = [[thenify, { total_thenify: 'true' }]] // The 'Total' mode is enabled 7 on('file:preprocessor', webpackPreprocessor(options)) 8}
Or put this into your setupNodeEvents
within the configuration file if you have Cypress >= 10 version:
1 setupNodeEvents(on, config) { 2 const thenify = require("cypress-thenify") 3 const webpackPreprocessor = require('@cypress/webpack-preprocessor') 4 const options = webpackPreprocessor.defaultOptions 5 options.webpackOptions.module.rules[0].use[0].options.plugins = [[thenify, { total_thenify: 'true' }]] // The 'Total' mode is enabled 6 on('file:preprocessor', webpackPreprocessor(options)) 7 },
You can define a custom thenify
function name by passing options to the plugin:
1 options.webpackOptions.module.rules[0].use[0].options.plugins = [[thenify, { thenify_function_name: 'cyEval'}]]
This project is in alpha stage. Some bugs are highly likely to be found. Please report any issues or suggestions into the tracker
If you use IDEA, Webstorm or any other IntelliJ IDEs to develop your Cypress tests, please consider using Cypress Support or Cypress Support Pro plugins to increase your productivity.
No vulnerabilities found.
No security vulnerabilities found.