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

toHaveBeenCalledBefore throw an error #147

Closed
nolazybits opened this issue Jul 10, 2018 · 8 comments
Closed

toHaveBeenCalledBefore throw an error #147

nolazybits opened this issue Jul 10, 2018 · 8 comments

Comments

@nolazybits
Copy link

nolazybits commented Jul 10, 2018

Bug

  • package version: 0.7.2
  • node version: 10
  • npm (or yarn) version: 1.7

From the documentation example

describe("toHaveBeenCalledBefore", async () =>
{
    it('calls mock1 before mock2', () => {
      const mock1 = jest.fn();
      const mock2 = jest.fn();

      mock1();
      mock2();
      mock1();

      expect(mock1).toHaveBeenCalledBefore(mock2);
    });
}

you will get the following error

   TypeError: Cannot read property 'length' of undefined

      170 |         mock1();
      171 | 
    > 172 |         expect(mock1).toHaveBeenCalledBefore(mock2);
          |                       ^
      173 |     });
      174 | 

      at Object.<anonymous>.exports.default (../../node_modules/jest-extended/dist/matchers/toHaveBeenCalledBefore/predicate.js:9:23)
      at Object.toHaveBeenCalledBefore (../../node_modules/jest-extended/dist/matchers/toHaveBeenCalledBefore/index.js:23:42)
      at Object.<anonymous>.it (tests/unit/BugReport.test.ts:172:23)
@benjaminkay93
Copy link
Contributor

Your using jest 23 right? this has been fixed in a PR that been merged in #132, However this has not yet been published, until then you will have to use either jest 22, or not use this matcher :P

@benjaminkay93
Copy link
Contributor

However the docs on here do say jest 22=< so its expected to be working in jest 23, I know the docs have been updated in #132 but is it worth keeping them compatible with 22 as well?

@nolazybits
Copy link
Author

Awesome thank you much for the feedback.
will keep an eye on the PR

Came up with a bad workaround

...
        const moduleOrder: number[] = [];
        const sut: ISauceCore = await Container.GetFromContainer(SauceCore).isReady;

        const spyModuleStart: SpyInstance = jest.spyOn(Module.prototype, "start");
        const spyModule2Start: SpyInstance = jest.spyOn(Module2.prototype, "start");
        const spyModule3Start: SpyInstance = jest.spyOn(Module3.prototype, "start");
        spyModuleStart.mockImplementation(async () =>
        {
            moduleOrder.push(1);
        });
        spyModule2Start.mockImplementation(async () =>
        {
            moduleOrder.push(2);
        });
        spyModule3Start.mockImplementation(async () =>
        {
            moduleOrder.push(3);
        });

        await sut.register(Module);
        await sut.register(Module2);
        await sut.register(Module3);
        await sut.start();

        expect(moduleOrder).toEqual(expect.arrayContaining([1, 2, 3]));

        spyModuleStart.mockRestore();
        spyModule2Start.mockRestore();
        spyModule3Start.mockRestore();
...

mocking my function and pushing to an array and checking the array against the expected :/

@benjaminkay93
Copy link
Contributor

benjaminkay93 commented Jul 13, 2018

you could do

const mock1 = jest.fn()
const mock2 = jest.fn()

expect(mock1.mock.invocationCallOrder).toBeLessThan(mock2.mock.invocationCallOrder)

@benjaminkay93
Copy link
Contributor

The jest api changed from timestamp to invocationCallOrder so you dont have to mock out a timeout function inside the jest.fn() to get the innvocation order.

@nolazybits
Copy link
Author

Thank you for the feedback
I'll give it a try on Monday :)

@jonohayon
Copy link

jonohayon commented Jul 18, 2018

Having the same problem...
@mattphillips can you please publish the fixed version to npm?

@mattphillips
Copy link
Member

Hey thanks for raising this, sorry about the delay in getting this published! It is now out in v0.8.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants