In the provided code, I have a class with a method that concats a string. I have a function that creates an instance of the class and returns the result of the method. My goal is to create a jest mock implementation of methodOne so that it concats the strings in reverse order (Hello, World) becomes WorldHello. Can anyone help me figure out how to create a mock to accomplish this? The goal is for the second test to pass and for the serviceFunction to return the mocked methods implemented return.
handler.ts:
class Service {
constructor() {}
methodOne(a, b) {
return a + b
}
}
const serviceFunction = (a, b) => {
const obj = new Service()
return obj.methodOne(a, b)
}
module.exports = { Service, serviceFunction }
handler.test.ts:
const myModule = require('./handler.ts')
describe('Test Suite One', () => {
it('should concat Strings', () => {
const service = new myModule.Service()
const result = myModule.serviceFunction("Hello", "World")
expect(result).toEqual("HelloWorld")
})
it('should mock methodOne to reverse concat strings', () => {
const service = new myModule.Service()
const result = myModule.serviceFunction("Hello", "World")
expect(result).toEqual('WorldHello')
})
})