bluzi/jest-retries

beforeEach / afterEach hooks

vvscode opened this issue · 2 comments

I noticed, that hooks doesn't applied to retry-tests. And according to the implementation - it doesn't .

Is there any plan to do that? Or any idea how to do that?

So I would expect have working next code

const test = require('jest-retries');

function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min)) + min;
}

let counter = 0;
beforeEach(() => {
  counter ++;
});

test('Random value should eventually resolve to 1', 100, () => {
  const random = getRandomInt(0, 4);
  console.log('Random:', random, 'Counter: ', counter);
    expect(random).toBe(1);
    expect(counter > 3).toBe(true);
});

I can do that with something like next:

async function retry(description, retries, handler) {
    if (!description || typeof description !== 'string') {
        throw new Error('Invalid argument, description must be a string')
    }

    if (typeof retries === 'function' && !handler) {
        handler = retries;
        retries = 1;
    }

    if (!retries || typeof retries !== 'number' || retries < 1) {
        throw new Error('Invalid argument, retries must be a greather than 0')
    }

    let latestError = null;
    let isPassed = false;

    for(let i = 0; i< retries; i++) {
        test(`${description} #${i + 1}`, async () => {
            if (isPassed) {
                expect(true).toBe(true);
                return;
            }
            try {
                await runTest(handler);
                isPassed = true;
                return;
            } catch(error) {
                latestError = error;
            }
            if (i === retries - 1) {
                throw latestError;
            }
        });
    }
}

and it works, BUT! it pollutes output

✓ Random value should eventually resolve to 1 #1 (10 ms)
  ✓ Random value should eventually resolve to 1 #2 (5 ms)
  ✓ Random value should eventually resolve to 1 #3 (1 ms)
  ✓ Random value should eventually resolve to 1 #4 (2 ms)
  ✓ Random value should eventually resolve to 1 #5 (1 ms)
  ✓ Random value should eventually resolve to 1 #6 (1 ms)
  ✓ Random value should eventually resolve to 1 #7 (1 ms)
  ✓ Random value should eventually resolve to 1 #8 (4 ms)
  ✓ Random value should eventually resolve to 1 #9 (1 ms)
  ✓ Random value should eventually resolve to 1 #10 (1 ms)
  ✓ Random value should eventually resolve to 1 #11 (1 ms)
  ✓ Random value should eventually resolve to 1 #12 (1 ms)
  ✓ Random value should eventually resolve to 1 #13 (1 ms)
  ✓ Random value should eventually resolve to 1 #14 (1 ms)
  ✓ Random value should eventually resolve to 1 #15
  ✓ Random value should eventually resolve to 1 #16 (1 ms)
  ✓ Random value should eventually resolve to 1 #17
  ✓ Random value should eventually resolve to 1 #18
  ✓ Random value should eventually resolve to 1 #19
  ✓ Random value should eventually resolve to 1 #20
  ✓ Random value should eventually resolve to 1 #21 (1 ms)
  ✓ Random value should eventually resolve to 1 #22
  ✓ Random value should eventually resolve to 1 #23
  ✓ Random value should eventually resolve to 1 #24
  ✓ Random value should eventually resolve to 1 #25
  ✓ Random value should eventually resolve to 1 #26 (1 ms)
  ✓ Random value should eventually resolve to 1 #27
  ✓ Random value should eventually resolve to 1 #28
  ✓ Random value should eventually resolve to 1 #29
  ✓ Random value should eventually resolve to 1 #30
  ✓ Random value should eventually resolve to 1 #31
  ✓ Random value should eventually resolve to 1 #32
  ✓ Random value should eventually resolve to 1 #33 (1 ms)
  ✓ Random value should eventually resolve to 1 #34
  ✓ Random value should eventually resolve to 1 #35
  ✓ Random value should eventually resolve to 1 #36
  ✓ Random value should eventually resolve to 1 #37
  ✓ Random value should eventually resolve to 1 #38
  ✓ Random value should eventually resolve to 1 #39 (1 ms)
  ✓ Random value should eventually resolve to 1 #40
  ✓ Random value should eventually resolve to 1 #41
  ✓ Random value should eventually resolve to 1 #42
  ✓ Random value should eventually resolve to 1 #43
  ✓ Random value should eventually resolve to 1 #44 (1 ms)
  ✓ Random value should eventually resolve to 1 #45
  ✓ Random value should eventually resolve to 1 #46
  ✓ Random value should eventually resolve to 1 #47
  ✓ Random value should eventually resolve to 1 #48
  ✓ Random value should eventually resolve to 1 #49 (1 ms)
  ✓ Random value should eventually resolve to 1 #50
  ✓ Random value should eventually resolve to 1 #51
  ✓ Random value should eventually resolve to 1 #52
  ✓ Random value should eventually resolve to 1 #53
  ✓ Random value should eventually resolve to 1 #54
  ✓ Random value should eventually resolve to 1 #55
  ✓ Random value should eventually resolve to 1 #56 (1 ms)
  ✓ Random value should eventually resolve to 1 #57
  ✓ Random value should eventually resolve to 1 #58
  ✓ Random value should eventually resolve to 1 #59
  ✓ Random value should eventually resolve to 1 #60
  ✓ Random value should eventually resolve to 1 #61
  ✓ Random value should eventually resolve to 1 #62
  ✓ Random value should eventually resolve to 1 #63 (1 ms)
  ✓ Random value should eventually resolve to 1 #64
  ✓ Random value should eventually resolve to 1 #65
  ✓ Random value should eventually resolve to 1 #66
  ✓ Random value should eventually resolve to 1 #67
  ✓ Random value should eventually resolve to 1 #68
  ✓ Random value should eventually resolve to 1 #69
  ✓ Random value should eventually resolve to 1 #70
  ✓ Random value should eventually resolve to 1 #71 (1 ms)
  ✓ Random value should eventually resolve to 1 #72
  ✓ Random value should eventually resolve to 1 #73
  ✓ Random value should eventually resolve to 1 #74
  ✓ Random value should eventually resolve to 1 #75
  ✓ Random value should eventually resolve to 1 #76
  ✓ Random value should eventually resolve to 1 #77
  ✓ Random value should eventually resolve to 1 #78 (1 ms)
  ✓ Random value should eventually resolve to 1 #79
  ✓ Random value should eventually resolve to 1 #80
  ✓ Random value should eventually resolve to 1 #81
  ✓ Random value should eventually resolve to 1 #82
  ✓ Random value should eventually resolve to 1 #83
  ✓ Random value should eventually resolve to 1 #84
  ✓ Random value should eventually resolve to 1 #85
  ✓ Random value should eventually resolve to 1 #86 (1 ms)
  ✓ Random value should eventually resolve to 1 #87
  ✓ Random value should eventually resolve to 1 #88
  ✓ Random value should eventually resolve to 1 #89
  ✓ Random value should eventually resolve to 1 #90
  ✓ Random value should eventually resolve to 1 #91
  ✓ Random value should eventually resolve to 1 #92
  ✓ Random value should eventually resolve to 1 #93
  ✓ Random value should eventually resolve to 1 #94
  ✓ Random value should eventually resolve to 1 #95
  ✓ Random value should eventually resolve to 1 #96
  ✓ Random value should eventually resolve to 1 #97
  ✓ Random value should eventually resolve to 1 #98
  ✓ Random value should eventually resolve to 1 #99
  ✓ Random value should eventually resolve to 1 #100

  console.log
    Random: 1 Counter:  1

      at index.test.js:16:11

  console.log
    Random: 0 Counter:  2

      at index.test.js:16:11

  console.log
    Random: 0 Counter:  3

      at index.test.js:16:11

  console.log
    Random: 3 Counter:  4

      at index.test.js:16:11

  console.log
    Random: 0 Counter:  5

      at index.test.js:16:11

  console.log
    Random: 2 Counter:  6

      at index.test.js:16:11

  console.log
    Random: 3 Counter:  7

      at index.test.js:16:11

  console.log
    Random: 0 Counter:  8

      at index.test.js:16:11

  console.log
    Random: 2 Counter:  9

      at index.test.js:16:11

  console.log
    Random: 2 Counter:  10

      at index.test.js:16:11

  console.log
    Random: 3 Counter:  11

      at index.test.js:16:11

  console.log
    Random: 3 Counter:  12

      at index.test.js:16:11

  console.log
    Random: 0 Counter:  13

      at index.test.js:16:11

  console.log
    Random: 2 Counter:  14

      at index.test.js:16:11

  console.log
    Random: 1 Counter:  15

      at index.test.js:16:11

Test Suites: 1 passed, 1 total
Tests:       100 passed, 100 total
Snapshots:   0 total
Time:        1.343 s
Ran all test suites.