<commit_before>/* eslint-env jest */

import pRetry from './_pRetry'

describe('pRetry()', () => {
  it('retries until the function succeeds', async () => {
    let i = 0
    expect(
      await pRetry(
        () => {
          if (++i < 3) {
            throw new Error()
          }
          return 'foo'
        },
        { delay: 0 }
      )
    ).toBe('foo')
    expect(i).toBe(3)
  })

  it('returns the last error', async () => {
    let tries = 5
    const e = new Error()
    await expect(
      pRetry(
        () => {
          throw --tries > 0 ? new Error() : e
        },
        { delay: 0, tries }
      )
    ).rejects.toBe(e)
  })
  ;[ReferenceError, TypeError].forEach(ErrorType => {
    it(`does not retry if a ${ErrorType.name} is thrown`, async () => {
      let i = 0
      await expect(
        pRetry(() => {
          ++i
          throw new ErrorType()
        })
      ).rejects.toBeInstanceOf(ErrorType)
      expect(i).toBe(1)
    })
  })
})
<commit_msg>chore(xo-server/pRetry): more tests<commit_after>/* eslint-env jest */

import { forOwn } from 'lodash'

import pRetry from './_pRetry'

describe('pRetry()', () => {
  it('retries until the function succeeds', async () => {
    let i = 0
    expect(
      await pRetry(
        () => {
          if (++i < 3) {
            throw new Error()
          }
          return 'foo'
        },
        { delay: 0 }
      )
    ).toBe('foo')
    expect(i).toBe(3)
  })

  it('returns the last error', async () => {
    let tries = 5
    const e = new Error()
    await expect(
      pRetry(
        () => {
          throw --tries > 0 ? new Error() : e
        },
        { delay: 0, tries }
      )
    ).rejects.toBe(e)
  })
  ;[ReferenceError, TypeError].forEach(ErrorType => {
    it(`does not retry if a ${ErrorType.name} is thrown`, async () => {
      let i = 0
      await expect(
        pRetry(() => {
          ++i
          throw new ErrorType()
        })
      ).rejects.toBeInstanceOf(ErrorType)
      expect(i).toBe(1)
    })
  })

  it('does not retry if `stop` callback is called', async () => {
    const e = new Error()
    let i = 0
    await expect(
      pRetry(stop => {
        ++i
        stop(e)
      })
    ).rejects.toBe(e)
    expect(i).toBe(1)
  })

  describe('`when` option', () => {
    forOwn(
      {
        'with function predicate': _ => _.message === 'foo',
        'with object predicate': { message: 'foo' },
      },
      (when, title) =>
        describe(title, () => {
          it('retries when error matches', async () => {
            let i = 0
            await pRetry(
              () => {
                ++i
                throw new Error('foo')
              },
              { when, tries: 2 }
            ).catch(Function.prototype)
            expect(i).toBe(2)
          })

          it('does not retry when error does not match', async () => {
            let i = 0
            await pRetry(
              () => {
                ++i
                throw new Error('bar')
              },
              { when, tries: 2 }
            ).catch(Function.prototype)
            expect(i).toBe(1)
          })
        })
    )
  })
})

{'fraction_non_alphanumeric': 0.1594705326189726, 'fraction_numerical': 0.008194138039710053, 'mean_word_length': 1.411854103343465, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3854425', 'n_tokens_mistral': 1011, 'n_tokens_neox': 1000, 'n_words': 247}