Do you prefer Jest or Mocha for Meteor testing?

Hi, do you prefer Mocha (supported by Meteor) or Jest (probably faster) testing? What are pros/cons of Mocha/Jest with Meteor? I appreciate any experience.

Thanks a lot.

I like that the Meteor Community mocha package and test driver is all set up to take advantage of a running Meteor instance in the background, complete with your usual plugins, so you can test with a disposable MongoDB DB and everything quite easily.

It’s just a little slow IMO. But that’s more Meteor having to spin up.

I might be missing something, but to get Jest to work (which I last did maybe 4+ years ago) you need stubs for all the Meteor specific dependencies and you need to set up Jest to use an identical Babel config as your Meteor app for extra consistency. But it ran much faster and you got good integration with eg VSCode and with the right editor plugins you got a little list of tests with ticks or crosses depending on how successful they were, and if you made them run automatically on save it was quite satisfying for test driven development purposes.

Vitest I’d say is a nice “successor” (or rather competitor) to Jest and I’ve been trying it out just with unit tests only, no Meteor APIs, and it’s pretty fast. But I couldn’t figure out a way to get it working with the Meteor Vite plugin from Jorgenvatle, I think it would need a whole new test driver set up and change to the way the vite plugin builds for test environments. So while ideally I’d use that for integration and E2E tests as well, there’s a positive now in that I have my “fast” tests running on Vite and they’re virtually real time, and my “slow” tests running with Mocha.

(Otherwise… stubs stubs stubs like Jest :sweat_smile:)

I also like the Vite suite/test API instead of “describe/it”, I think that’s related to the new Node test APIs? But fundamentally it’s all the same until async/await is concerned. I also like that Vite has a range of assertions built in.

1 Like

Hello @ceigey, thanks a loot for your explanation! Only just to be clear, you mostly prefer Mocha and for “independent” code parts and faster testing Jest? And what about AI generated tests? Does exists any way to generate tests with local AI models (without share code somewhere else)?

So I would go:

If you like complexity:

  • unit tests, where code is mostly independent from Meteor: Jest/Vitest (Vitest for me).
  • everything else: Mocha

If you want 1x system for everything:

  • just use Mocha.

As for me, I fit either persona depending on the day of the week :upside_down_face:

Later on I can share my Vitest & Mocha configs for context and find the vscode plugin I use for Vitest. AI tools could try and help you convert that to Jest (sorry no recent Jest configs on hand)

For AI, ChatGPT should be able to generate tests for all the above if you tell it which libraries you’re using, and if you use TypeScript or JSDoc (or SimpleSchema etc), because then it can derive more context.

1 Like