How to use p-map - 10 common examples

To help you get started, we’ve selected a few p-map examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github okonet / lint-staged / test / resolveTaskFn-chunked.spec.js View on Github external
it('should respect chunk size and concurrency', async () => {
      expect.assertions(6)

      const taskFn = resolveTaskFn({
        ...defaultOpts,
        pathsToLint: ['test1.js', 'test2.js'],
        subTaskConcurrency: 2
      })
      await taskFn()
      const [[chunks, mapper]] = pMap.mock.calls
      expect(mapper).toBeInstanceOf(Function)
      expect(pMap).toHaveBeenCalledWith([['test1.js'], ['test2.js']], mapper, { concurrency: 2 })

      // Check that calling the mapper invokes execa
      const [c1, c2] = chunks
      await mapper(c1)
      expect(execa).toHaveBeenCalledTimes(1)
      expect(execa).lastCalledWith('test', ['test1.js'], { reject: false })
      await mapper(c2)
      expect(execa).toHaveBeenCalledTimes(2)
      expect(execa).lastCalledWith('test', ['test2.js'], { reject: false })
    })
github algolia / docsearch / docs / scripts / lib / markdown.js View on Github external
async run() {
    // Convert markdown to HTML
    const markdownFiles = await helper.getFiles('*.md');
    await pMap(markdownFiles, async filepath => {
      await this.compile(filepath);
    });
  },
github okonet / lint-staged / test / resolveTaskFn-chunked.spec.js View on Github external
it('should invoke execa in mapper function', async () => {
      expect.assertions(3)

      const taskFn = resolveTaskFn({ ...defaultOpts })
      await taskFn()
      const [[[chunk], mapper]] = pMap.mock.calls
      expect(pMap).toHaveBeenCalledWith([['test.js']], mapper, { concurrency: 1 })
      await mapper(chunk)
      expect(execa).toHaveBeenCalledTimes(1)
      expect(execa).toHaveBeenCalledWith('test', ['test.js'], { reject: false })
    })
github okonet / lint-staged / test / runScript-mock-pMap.spec.js View on Github external
it('should handle unexpected error', async () => {
    expect.assertions(1)
    pMapMock.mockImplementation(() => Promise.reject(new Error('Unexpected Error')))

    const [linter] = runScript(['test'], ['test.js'])
    try {
      await linter.task()
    } catch (err) {
      expect(err.message).toMatch(dedent`
        ${logSymbols.error} test got an unexpected error.
        Unexpected Error
      `)
    }
  })
})
github okonet / lint-staged / test / resolveTaskFn-chunked.spec.js View on Github external
it('should handle unexpected error', async () => {
      expect.assertions(1)
      pMap.mockRejectedValueOnce(new Error('Unexpected Error'))

      const taskFn = resolveTaskFn({ ...defaultOpts })
      try {
        await taskFn()
      } catch (err) {
        expect(err.message).toMatch(dedent`
          ${logSymbols.error} test got an unexpected error.
          Unexpected Error
        `)
      }
    })
  })
github okonet / lint-staged / test / resolveTaskFn-chunked.spec.js View on Github external
describe('chunked tasks', () => {
    afterEach(() => {
      execa.mockClear()
      pMap.mockClear()
    })

    pMap.mockResolvedValue([
      {
        stdout: 'a-ok',
        stderr: '',
        code: 0,
        failed: false,
        cmd: 'mock cmd'
      }
    ])

    it('should invoke execa in mapper function', async () => {
      expect.assertions(3)

      const taskFn = resolveTaskFn({ ...defaultOpts })
      await taskFn()
      const [[[chunk], mapper]] = pMap.mock.calls
      expect(pMap).toHaveBeenCalledWith([['test.js']], mapper, { concurrency: 1 })
github okonet / lint-staged / test / resolveTaskFn-chunked.spec.js View on Github external
it('should throw error for failed linters', async () => {
      expect.assertions(1)
      pMap.mockResolvedValueOnce([
        {
          stdout: 'Mock error',
          stderr: '',
          code: 0,
          failed: true,
          cmd: 'mock cmd'
        }
      ])

      const taskFn = resolveTaskFn({
        ...defaultOpts,
        linter: 'mock-fail-linter'
      })
      try {
        await taskFn()
      } catch (err) {
github okonet / lint-staged / test / runScript-mock-pMap.spec.js View on Github external
{
          stdout: 'a-ok',
          stderr: '',
          code: 0,
          failed: false,
          cmd: 'mock cmd'
        }
      ])
    )

    const [linter] = runScript(['test'], ['test1.js', 'test2.js'], {
      chunkSize: 1,
      subTaskConcurrency: 1
    })
    await linter.task()
    const [[, mapper]] = pMapMock.mock.calls
    expect(mapper).toBeInstanceOf(Function)
    expect(pMapMock).toHaveBeenCalledWith([['test1.js'], ['test2.js']], mapper, { concurrency: 1 })
  })
github okonet / lint-staged / test / resolveTaskFn-chunked.spec.js View on Github external
afterEach(() => {
      execa.mockClear()
      pMap.mockClear()
    })

p-map

Map over promises concurrently

MIT
Latest version published 16 days ago

Package Health Score

85 / 100
Full package analysis