Coder Social home page Coder Social logo

Comments (1)

zomnium avatar zomnium commented on June 3, 2024

Hey!

I was looking into doing something similar, sadly deepMock doesn't seem to support method chaining - which is used here. It would be cool to build something for that. Have you ever looked into that @marchaos?

For now I could recommend you to look into:

  • The concept of dependency injection for your class
  • The mock function mockReturnThis() from Jest

When you combine those you can build something that injects the dependencies into your class. That gives you the ability to replace the actual library with a mocked version. That mocked version returns the object itself, so you can do method chaining just like the library.

The downside of doing this manually is that if the library api changes you have to update your mocks as well. Good to keep that in mind. That would make a mockDeep version for method chaining very useful.

I wasn't sure about mocking goo in your example, but I hope you get the concept with the rough code draft below:

// myModule.ts
import type { myPackage } from '@myScope/myPackage'

export class MyClass {
  constructor(private foo, private goo) {}

   toTest(): string {
      const a = foo().moo().koo() // that is why I want deepMock
      return goo(a + 'test')
   }
}

// app.ts
import { MyClass } from 'myModule.ts'
import { foo, goo } from '@myScope/myPackage'

const app = MyClass(foo, goo)

// myModule.test.ts
import { MyClass } from './myModule'
import { goo } from '@myScope/myPackage'

function createFooMock() {
  return () => ({
    moo: jest.fn().mockReturnThis(),
    moo: jest.fn().mockReturnThis(),
  })
}

describe('', () => {
  let myClass: MyClass

  beforeEach(() => {
    myClass = new MyClass(foo(), goo)
  })

  it('should return an empty string', async () => {
    // Doesn't look like you want to mock this one: goo.mockReturnValue('')
    const result = myClass.toTest()
    expect(result).toEqual('')
  })
})

from jest-mock-extended.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.