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

152
Views
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
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!