Home > Enterprise >  How to correctly export and import functions for cypress plugin index.ts?
How to correctly export and import functions for cypress plugin index.ts?

Time:01-25

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 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

CodePudding user response:

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'

CodePudding user response:

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.

  •  Tags:  
  • Related