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

154
Visualizações
Export a Monkey Patch library and Import it into a separate file in Javascript

I'm working in WebDriverIO w/Mocha, and there's some monkey patches I've written to make my code easier to read. Things like:

  • Object.prototype.findEntries(...arr) for getting a subset of Object.entries(),
  • String.prototype.padding(int), adding spacing to the right of a string,
  • Array.prototype.sortByKey(key) to sort an array of similar objects by the value of a common key, and
  • Array.prototype.last(), the classic.

Since I have many spec files with even more tests, I'd like to have a MonkeyPatchLib.js in my project that I can import into my spec files, so I can call these custom functions when needed:

myTest.js:

import { all } from './MonkeyPatchLib.js' // <- line in question

describe('My First Spec File', async() => {
    it('First Test', async() => {
        let myArr=[1,2,3]
        console.log(myArr.last())
    })
})

I've screwed around with module.exports=[func1(){...},func2(){...}] to no avail. I'm aware that I can simply create custom classes (a la the solution to this question) and export those, but that would require an annoying refactor of my codebase. How should I format the library, and how would one import said library?

about 4 years ago · Juan Pablo Isaza
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