Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'p-map' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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 })
})
async run() {
// Convert markdown to HTML
const markdownFiles = await helper.getFiles('*.md');
await pMap(markdownFiles, async filepath => {
await this.compile(filepath);
});
},
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 })
})
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
`)
}
})
})
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
`)
}
})
})
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 })
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) {
{
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 })
})
afterEach(() => {
pMapMock.mockClear()
})
afterEach(() => {
execa.mockClear()
pMap.mockClear()
})