Repository: facebook/jest
File: packages/expect/src/__tests__/toThrowMatchers.test.ts (TypeScript)

Code before:
        ).toThrowErrorMatchingSnapshot();
      });
    });
  });

  describe('error message and cause', () => {
    const errorA = new Error('A');
    const errorB = new Error('B', {cause: errorA});
    const expected = new Error('good', {cause: errorB});

    describe('pass', () => {
      test('isNot false', () => {
        jestExpect(() => {
          throw new Error('good', {cause: errorB});
        })[toThrow](expected);
      });

      test('isNot true, incorrect message', () => {
        jestExpect(() => {
          throw new Error('bad', {cause: errorB});
        }).not[toThrow](expected);
      });

      test('isNot true, incorrect cause', () => {
        // only v16 or higher support Error.cause
        if (Number(process.version.split('.')[0].slice(1)) >= 16) {
          jestExpect(() => {
            throw new Error('good', {cause: errorA});
          }).not[toThrow](expected);
        }
      });
    });

    describe('fail', () => {
      // only v16 or higher support Error.cause
      if (Number(process.version.split('.')[0].slice(1)) >= 16) {
        test('isNot false, incorrect message', () => {
          expect(() =>
            jestExpect(() => {
              throw new Error('bad', {cause: errorB});
            })[toThrow](expected),
          ).toThrow(
            /^(?=.*Expected message and cause: ).*Received message and cause: /s,
          );
        });

        test('isNot true, incorrect cause', () => {
          expect(() =>
            jestExpect(() => {
              throw new Error('good', {cause: errorA});
            })[toThrow](expected),

Review comment (refactor):
can you use the helper? https://github.com/facebook/jest/blob/74776d8b581fabeb5c3993b6499643b8c6ffedf6/packages/test-utils/src/ConditionalTest.ts#L34

Code after:
        ).toThrowErrorMatchingSnapshot();
      });
    });
  });

  describe('error message and cause', () => {
    const errorA = new Error('A');
    const errorB = new Error('B', {cause: errorA});
    const expected = new Error('good', {cause: errorB});

    describe('pass', () => {
      test('isNot false', () => {
        jestExpect(() => {
          throw new Error('good', {cause: errorB});
        })[toThrow](expected);
      });

      test('isNot true, incorrect message', () => {
        jestExpect(() => {
          throw new Error('bad', {cause: errorB});
        }).not[toThrow](expected);
      });

      onNodeVersions('>=16.9.0', () => {
        test('isNot true, incorrect cause', () => {
          jestExpect(() => {
            throw new Error('good', {cause: errorA});
          }).not[toThrow](expected);
        });
      });
    });

    describe('fail', () => {
      onNodeVersions('>=16.9.0', () => {
        test('isNot false, incorrect message', () => {
          expect(() =>
            jestExpect(() => {
              throw new Error('bad', {cause: errorB});
            })[toThrow](expected),
          ).toThrow(
            /^(?=.*Expected message and cause: ).*Received message and cause: /s,
          );
        });

        test('isNot true, incorrect cause', () => {
          expect(() =>
            jestExpect(() => {
              throw new Error('good', {cause: errorA});
            })[toThrow](expected),
          ).toThrow(
            /^(?=.*Expected message and cause: ).*Received message and cause: /s,