title: How to nest Jest tests
body: <p>I have a situation where I need to create a test that calls a function and checks its return value.  It returns an object so I have to iterate through it and check 100 or so values for correctness.  If one of them fails I want to know which one.</p>
  <p>I cannot work out how to do this with vanilla Jest such that the test is self-contained and I get a meaningful error message on a failure.</p>
  <p>For example, I can do this: (pseudocode to illustrate, not actual code)</p>
  <pre><code>describe('Test for function A', () =&gt; {
      beforeAll('Create class instance', () =&gt; {
          this.inst = new MyClass();
      });
  
      test('Call function with no parameters', () =&gt; {
          const value = this.inst.run();
  
          for (each key=value) {
              expect(value).toBe(correct); // on failure, doesn't tell me the key, only the value
          }
      });
  });
  </code></pre>
  <p>The problem with this is that if <code>value</code> is not <code>correct</code> then the error message is not very helpful, as it doesn't tell me which of the 100 values has the problem.</p>
  <p>I can't change to <code>test.each()</code> because then I get an error saying I have nested <code>test()</code> calls which is not allowed.</p>
  <p>If I use an inner <code>test()</code> and change the parent <code>test()</code> to <code>describe()</code> then the code becomes this:</p>
  <pre><code>describe('Test for function A', () =&gt; {
      beforeAll('Create class instance', () =&gt; {
          this.inst = new MyClass();
      });
  
      describe('Call function with no parameters', () =&gt; {
          const value = this.inst.run();
  
          for (each value) {
              test(`Checking ${value.name}`, () =&gt; {
                  expect(value).toBe(correct);
              });
          }
      });
  });
  </code></pre>
  <p>This would give me a detailed error message except <code>this.inst.run()</code> is called during test set up, before <code>this.inst</code> has been set by <code>beforeAll()</code>, so it fails.  (Jest runs all the <code>describe()</code> blocks first, then <code>beforeAll()</code>, then <code>test()</code>.  This means I call <code>this.inst.run()</code> first in a <code>describe()</code> block before the <code>beforeAll()</code> block creates the class instance.)</p>
  <p>Is there any way that this is possible to achieve?  To have a test that requires an object created and shared amongst all the child tests, a test group that calls a function to get data for that group, then a bunch of tests within the group?</p>