Skip to content

Shared testing utils to simplify unit testing #310

@nickofthyme

Description

@nickofthyme

Is your feature request related to a problem? Please describe.
Hard to unit test in isolation

Describe the solution you'd like
Ability to mock functions and get proper type for example

const double: (n: number): number => n * 2;
const doubleMock: ((n: number) => number) & jest.Mock  = mockIt(double);

// such that doubleMock extends the jest.mock type so this 👇 works
doubleMock.mockReturnValue(12);

Note: Should work for mocking modules, classes and individual functions.

Describe alternatives you've considered
Not having to do (scale.scale as jest.Mock) to mock the functions

describe('getPosition', () => {
  // @ts-ignore
  const scale: Scale = {
    scale: jest.fn(),
  };

  beforeEach(() => {
    (scale.scale as jest.Mock).mockClear();
  });

  it('should return value from scale', () => {
    (scale.scale as jest.Mock).mockReturnValue(20);
    const result = getPosition(10, scale);
    expect(result).toBe(20);
  });

  it('should call scale with correct args', () => {
    getPosition(10, scale);
    expect(scale.scale).toBeCalledWith(10);
  });
});

Unit test coverage is very good but I feel like the current tests don’t fully cover each scenario and enforce bug fixes. Every bug that is fixed, in my mind, should have a unit test that enforces that use case.

There is also a lack of component test coverage.

There is a lot of boilerplate code that is used to make assertions for just one use case then repeated for another. I think this could be DRY’d up but creating helper functions as well as default configurations constants that are then changed to make a given assertion. This can then be enhanced using property based testing via jsverify or others.

In my last job, I had a difficult time testing code in typescript as the assertions require each type to be fully built out or it will complain. Using ts-ignore is a useful workaround but employing helper functions to make partial assertions would be very beneficial. This is most common with mocked functions. I don’t care about the type I just want to assert that the function is called with the given arguments. Mocks are a MUST in jest testing and they are seldom used requiring a full build out of required parameters to make a given assertion.

Mocking canvas

Mocking the canvas element in unit tests is a good way to make assertions on the api level but it’s hard to capture all that a visual testing system would.

Metadata

Metadata

Assignees

No one assigned

    Labels

    :testMissing or to be fixed testchorediscussTo be discussedenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions