Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared testing utils to simplify unit testing #310

Closed
nickofthyme opened this issue Aug 14, 2019 · 2 comments
Closed

Shared testing utils to simplify unit testing #310

nickofthyme opened this issue Aug 14, 2019 · 2 comments
Labels
chore discuss To be discussed enhancement New feature or request :test Missing or to be fixed test

Comments

@nickofthyme
Copy link
Collaborator

nickofthyme commented Aug 14, 2019

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.

@nickofthyme nickofthyme added enhancement New feature or request chore :test Missing or to be fixed test labels Aug 14, 2019
@nickofthyme nickofthyme added discuss To be discussed and removed chore :test Missing or to be fixed test labels Sep 13, 2019
@markov00 markov00 added chore :test Missing or to be fixed test labels Mar 26, 2020
@markov00
Copy link
Member

I'm not fully understood this issue @nickofthyme. Do you think we should keep this issue open?

@nickofthyme
Copy link
Collaborator Author

Yeah, I think all the testing XxxMock additions address the majority of this issue. The mockIt function may be helpful but that's easy to add if we see need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore discuss To be discussed enhancement New feature or request :test Missing or to be fixed test
Projects
None yet
Development

No branches or pull requests

2 participants