Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

423
Views
How to correctly export and import functions for cypress plugin index.ts?

I am developing plugin/index.ts file where I place async functions eg. clearing the database or uploading files to the app, but it starts to grow and I think about a way to keep it clean and structured.

In this video I see that there is a way to store functions in separate files and then export them using module.exports = { function } and then in the index.ts just import them using require.

But I can't have it working for my case.

This is a simplistic form of my plugins/index.ts file:

const uploadDocument = require('./documents');

module.exports = (on, config) => {
  // `on` is used to hook into various events Cypress emits
  // `config` is the resolved Cypress config

  on('task', {
    clearDatabase: clearDatabase,
    uploadDocument: uploadDocument,
  });
  
  async function clearDatabase() { ... }
}

I decided to move the code of function uploadDocument to the plugins/documents.ts file:

enter image description here

and this is how the file plugins/documents.ts looks like:

imports...

async function uploadDocument(fileName: string) { ... }

module.exports = { uploadDocument }

And when I run the test with a task this way:

cy.task("uploadDocument", 'Very_light_file.pdf')

I get this error in Cypress:

enter image description here

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

It looks like the problem is with your module.exports and the import.

Try this,

// document.ts

export async function uploadDocument(fileName: string) { ... }
// module.exports = { uploadDocument }
// plugins/index.ts

import { uploadDocument } =  from './documents'
about 4 years ago · Juan Pablo Isaza Report

0

From the post, I can tell TS is mixed JS there. The example is in JS, you're using TS, so there is no module.exports.

Try a pure JS version, then later convert it to TS.

about 4 years ago · Juan Pablo Isaza Report

0

If you look at cypress-io/cypress-realworld-app/blob/develop/cypress/plugins/index.ts you can see they change module.exports to:

// cypress/plugins/index.ts

export default (on, config) => {
  config.env.defaultPassword = process.env.SEED_DEFAULT_USER_PASSWORD;
  config.env.paginationPageSize = process.env.PAGINATION_PAGE_SIZE;
  // Auth0
  //
  //
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!