Passing data from the test context to a Jest mocked ES6 class

Viewed 72

I have a test framework that I'm building for my project. In the test file, we have something like this:

//myTest.tsx
import {MyFooDependency} from 'path/to/MyFooDependency';
jest.mock('path/to/MyFooDependency');

describe('foo tests', () => {
  it('foo', async () => {
    const myObjUnderTest = new MyObj();
    const res = await myObjUnderTest.testMethod();
    expect(res).toBe(expectedResult);
  }
}

I have a manual mock like so:

//path/to/__mocks__/MyFooDependency.tsx

export class MyFooDependency {
  //mock implementation of class
  const mockData = [
      {usefulDataKey: "fooVal1"},
      {usefulDataKey: "fooVal2"},
      {usefulDataKey: "fooVal3"},
    ];

  let calls = 0;
  getData() {
    const ret = mockData[calls];
    calls++;
    return ret;
  }
}

the object under test eventually calls into MyFooDependency and gets some data returned. With the above code, the MyFooDependency that is used is my manual mock.

I would like to inject data from my test into the MyFooDependency mock, to drive the behavior. So in this example I'd like the test to know about the mockData object, and inject that (which will change per test) into the MyFooDependency manual mock.

How can I inject this data into the manual mock?

0 Answers
Related