diff --git a/jest-global-mock.ts b/jest-global-mock.ts index 50412b2..34ace52 100644 --- a/jest-global-mock.ts +++ b/jest-global-mock.ts @@ -1,7 +1,3 @@ -/** - * This is a sample test suite. - * Replace this with your implementation. - */ import { exec } from 'child_process'; import fetch from 'node-fetch'; diff --git a/src/platforms/npm-package/index.spec.ts b/src/platforms/npm-package/index.spec.ts index 8411be6..60719e5 100644 --- a/src/platforms/npm-package/index.spec.ts +++ b/src/platforms/npm-package/index.spec.ts @@ -4,6 +4,14 @@ */ import { readNpmPackageDependencies, countNpmPackageDependants, analyze } from './index'; +const originalConsoleError = console.error; +beforeAll(() => { + console.error = jest.fn(); +}); +afterAll(() => { + console.error = originalConsoleError; +}); + describe('Npm package tests', () => { it('should not read a non-existent package', async () => { await expect(readNpmPackageDependencies('is-a-non-existent-package')).rejects.toEqual( diff --git a/src/platforms/npm-package/index.ts b/src/platforms/npm-package/index.ts index c8adec1..ad5d93f 100644 --- a/src/platforms/npm-package/index.ts +++ b/src/platforms/npm-package/index.ts @@ -29,7 +29,7 @@ export async function readNpmPackageDependencies(packageName: string): Promise ({ @@ -39,12 +39,13 @@ export async function readNpmPackageDependencies(packageName: string): Promise { + .catch(error => { + console.error(error); if (retriedTimes < 5) { retriedTimes += 1; doReadTask(); @@ -84,13 +85,14 @@ export async function countNpmPackageDependants(packageName: string, version: st resolve(parseInt(match.replace(/,/g, ''), 10)); return Promise.resolve(); } - throw new Error(`Operation failed`); + throw new Error(`Cannot matches target text`); } catch (error) { - return Promise.reject(new Error(`Operation failed`)); + return Promise.reject(error); } }; Promise.race([fetchPromise(), timeoutPromise]) - .catch(() => { + .catch(error => { + console.error(error); if (retriedTimes < 5) { retriedTimes += 1; doCountTask();