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, andArray.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?