npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cypress-thenify

v0.2.0

Published

Get rid of Cypress `then` callback hell

Downloads

369

Readme

cypress-thenify

Rationale

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.

Usage example

Strict mode (the default mode):

Given code

    let a = cy.get("foo").thenify()
    cy.log(a.text())
    let b = cy.get("bar").thenify()
    cy.log(a.text())
    b.click() // using jQuery click method

will be transpiled into this one:

  cy.get("foo").then(__cypressSyncVar__ => {
    let a = __cypressSyncVar__;
    cy.log(a.text());
    cy.get("bar").then(__cypressSyncVar__ => {
      let b = __cypressSyncVar__;
      cy.log(a.text());
      b.click(); // using jQuery click method
    });
  });

Total mode:

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

    let a = cy.get("foo")
    cy.log(a.text())
    let b = cy.get("bar")
    cy.log(a.text())
    b.click() // using jQuery click method

will be transpiled into this one:

cy.get("foo").then(__cypressSyncVar__ => {
   let a = __cypressSyncVar__;
   cy.log(a.text());
   cy.then(() => {
      cy.get("bar").then(__cypressSyncVar__ => {
         let b = __cypressSyncVar__;
         cy.log(a.text());
         cy.then(() => {
            b.click(); // using jQuery click method
         });
      });
   });
});

Limitations (Strict mode)

  1. The plugin wraps statements inside the current block of statements only. Use an empty call to cy.thenify() to 'synchronize' the execution context with the cypress event loop:
   let myValue 
   if (myCondition) { // suppose myCondition is true
       myValue = cy.wrap("foo").thenify()
       someOtherCode()
       console.log(myValue) // will print 'foo' as we are inside the same block of code as the `thenify` call
   } else {
       myValue = "bar"
   }
   console.log(myValue) // will print `undefined` as we are out of the initial block of code 
   cy.thenify()
   console.log(myValue) // will print 'foo' as we get in sync with the cypress event loop 
                        // because this and all below statements of the current block will be executed under a `then` callback   
  1. Only a single thenify call per statement is currently supported. So
 let a = cy.wrap(1).thenify() + cy.wrap(2).thenify()

will produce an error.

  1. The plugin searches for the 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)

Install and configure

npm i cypress-thenify -D

Put this into your plugin/index.js:

const browserify = require('@cypress/browserify-preprocessor')
const thenify = require("cypress-thenify")

module.exports = (on) => {
  const options = browserify.defaultOptions
  options.browserifyOptions.transform[1][1].plugins.push([thenify, { total_thenify: 'true' }]) // Total mode is enabled 
  on('file:preprocessor', browserify(options))
}

Plugin options

You can define a custom thenify function name by passing options to the plugin:

    options.browserifyOptions.transform[1][1].plugins.push([thenify, { thenify_function_name: 'cyEval' }])

Project status notice

This project is in alpha stage. Some bugs are highly likely to be found.