1

I'm adding tests using Jest to my React project, and I need to run a large sequence of tests against where order matters (I'm going to be using an emulator for storing data, which later tests will depend on). I need to log in, and test app state changing over time as I add/remove data and interact with it. So more a large integration test instead of unit tests.

I don't want to have one giant App.test.ts file (Or App.ts file in __tests__). How can I split the test across multiple files, without each of them being run as their own test?

I'm new to Jest, so still building a mental model for it.

Nathan
  • 73,987
  • 14
  • 40
  • 69
  • "a large sequence of tests against where order matters (I'm going to be using an emulator for storing data, which later tests will depend on)" -- you may be aware, but all tests should be idempotent (able to be run in isolation). Carrying state between tests is a brittle design, to say the least. Tear down all of your state and rebuild it per test unless there's a _very_ compelling reason to do otherwise (I don't see one here). Most test runners don't make ordering guarantees, and will often run your tests in parallel to improve* performance. (sometimes single-threaded is faster for slow CPUs) – ggorlen Jul 14 '22 at 17:52
  • I'm using Firebase, and need to use the emulator suite to test user creation, logging in with those users, authz, creating data, other users operating on that data, and cloud function hooks for data changes. Each test progressively builds on the others. It takes a while to spin the emulator up, and I can't use a different emulator for each test (and even if I did, rebuilding the needed state would be a huge task). – Nathan Jul 14 '22 at 18:00
  • And you can't mock the external servies? Are these integration tests? If these external services aren't being tested and just happen to be involved out of necessity, the typical approach is to mock them. If you do proceed, consider yourself warned, and I would try to clean up as much state between tests as possible regardless. Jest has a `--runInBand` option to force single threaded, for starters, but everything about it is pretty much designed to be in diametric opposition to your use case. You may need to run each file one after the other programmatically, making multiple `jest` invocations. – ggorlen Jul 14 '22 at 18:01
  • Yeah, I'm looking to run a large integration test essentially. And no, I can't really mock it out-- Firebase documentation recommends using the emulator. There's a lot of things like cloud function hooks that change data upon data insertion, etc, that would be very difficult to mock. – Nathan Jul 14 '22 at 18:03
  • Though to be fair, testing is my #1 weakness as a programmer, so I could be approaching this all wrong. If I go this approach, --runInBand is probably going to be what I want – Nathan Jul 14 '22 at 18:04
  • 1
    Thanks for the context. Check out [this GH issue](https://github.com/facebook/jest/issues/6194) and see many answers in [How to run Jest tests sequentially?](https://stackoverflow.com/questions/32751695/how-to-run-jest-tests-sequentially) that discuss multiple files. – ggorlen Jul 14 '22 at 18:06
  • So it seems like the tests are actually running sequentially even w/o --runInBand, strangely. It's possible it's just due to the low volume of tests I have, we'll see if it stays that way. Anyhow, I've gotten what I need pretty much working-- I preserve render context between tests using RTL_SKIP_AUTO_CLEANUP=true, and I'm planning on having functions that lead to describe blocks in various files, and then import and using those functions in the App.test.ts file to compose the full test. – Nathan Jul 14 '22 at 19:20

0 Answers0