Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

424
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda