Skip to content

Conversation

alexsch01
Copy link
Contributor

OPT-IN: adds type-safety and autocomplete to cy.task function


cypress.config.js

const { defineConfig } = require('cypress')
const CypressTasks = require('./tasks')

module.exports = {
  ...defineConfig({
    e2e: {
      supportFile: false,
      setupNodeEvents(on, config) {
        on('task', CypressTasks)
      },
    },
  }),
  CypressTasks,
}

tasks.js

module.exports = {
  log(args) {
    console.log(args)
    return null;
  },

  /**
   * @param {any[]} args
   */
  logMany(args) {
    console.log(...args)
    return null;
  },

  getData() {
    return {
      response: {
        body: 'OK',
        status: 200,
      }
    }
  },

  /**
   * @param {string} x 
   * @returns {Promise<string>}
   */
  getSelf(x) {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(x);
      }, 5000);
    });
  },
}

cypress/e2e/spec.cy.js

it('Example of cy.task', () => {
  cy.task('logMany', [1, 2, 3, null, 5])

  cy.task('getData').then(data => {
    expect(data.response.status).to.eq(200)
    expect(data.response.body).to.eq('OK')
  })

  cy.task('getSelf', "=========").then((x) => {
    cy.task('log', x)
  })
})

jsconfig.json

{
  "compilerOptions": {
    "types": ["cypress"],
    "checkJs": true,
    "lib": ["ES2021", "DOM"],
    "module": "preserve",
    "strictNullChecks": true,
  },
  "exclude": ["node_modules"]
}

@cypress-app-bot
Copy link
Collaborator

@alexsch01 alexsch01 marked this pull request as ready for review July 22, 2025 19:16
cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

cursor[bot]

This comment was marked as outdated.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Cypress Type Definitions Fail to Resolve User Config

The hardcoded relative import path ../../../cypress.config within the Cypress type definitions fails to resolve the user's cypress.config file. As the types file is located in node_modules, this path incorrectly attempts to resolve the config file from within the package directory instead of the user's project root. This causes TypeScript compilation errors, breaking the type-safe cy.task feature, especially for custom config names or locations.

cli/types/cypress.d.ts#L9-L10

type CypressConfig_Data = typeof import('../../../cypress.config');

Fix in CursorFix in Web


Was this report helpful? Give feedback by reacting with 👍 or 👎

@alexsch01 alexsch01 closed this Aug 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants