From b131e22741b610c9cbbdd94a4de866b2fc5402a3 Mon Sep 17 00:00:00 2001 From: narol Date: Sun, 2 Jun 2024 17:09:23 +0800 Subject: [PATCH] fix: add unit testing --- src/platforms/npm-package/index.spec.ts | 10 +++++++++- src/platforms/npm-package/index.ts | 6 +++--- src/platforms/workspaces/yarn/index.spec.ts | 4 +++- src/platforms/workspaces/yarn/index.ts | 4 ++-- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/platforms/npm-package/index.spec.ts b/src/platforms/npm-package/index.spec.ts index 66f25f0..564f323 100644 --- a/src/platforms/npm-package/index.spec.ts +++ b/src/platforms/npm-package/index.spec.ts @@ -2,7 +2,7 @@ * This is a sample test suite. * Replace this with your implementation. */ -import { readNpmPackageDependencies, analyze } from './index'; +import { readNpmPackageDependencies, countNpmPackageDependants, analyze } from './index'; describe('Npm pacakge Tests', () => { it('should not read a non-existent package', async () => { @@ -15,6 +15,14 @@ describe('Npm pacakge Tests', () => { { name: 'loose-envify', version: '^1.1.0' }, ]); }); + it('should count the depandants of a valid package', async () => { + await expect(countNpmPackageDependants('react', '18.3.1')).resolves.toEqual(10000); + }); + it('can not count the depandants of a invalid package', async () => { + await expect(countNpmPackageDependants('is-a-non-existent-package', '1.0.0')).rejects.toEqual( + new Error(`Cannot count the depandants of the is-a-non-existent-package.`), + ); + }); it('can be analyzed for single package', async () => { await expect(analyze('react')).resolves.toMatchSnapshot(); }); diff --git a/src/platforms/npm-package/index.ts b/src/platforms/npm-package/index.ts index 25cf272..199960c 100644 --- a/src/platforms/npm-package/index.ts +++ b/src/platforms/npm-package/index.ts @@ -92,7 +92,7 @@ export async function countNpmPackageDependants(packageName: string, version: st retriedTimes += 1; doCountTask(); } else { - reject(new Error(`Cannot read ${packageName}.`)); + reject(new Error(`Cannot count the depandants of the ${packageName}.`)); } }) .finally(() => { @@ -129,7 +129,7 @@ export async function analyze(packageNames: string): Promise { const deps = await Promise.all(depsPromises); return Promise.resolve(deps); - } catch (error) { - return Promise.reject(new Error(`Cannot analyze ${packageNames}`)); + } catch (error: any) { + return Promise.reject(new Error(`Cannot analyze ${packageNames}, the reason is ${error.message}`)); } } diff --git a/src/platforms/workspaces/yarn/index.spec.ts b/src/platforms/workspaces/yarn/index.spec.ts index 4a7e683..b63f69a 100644 --- a/src/platforms/workspaces/yarn/index.spec.ts +++ b/src/platforms/workspaces/yarn/index.spec.ts @@ -21,7 +21,9 @@ describe('Workspaces Tests', () => { ); }); it('can not analyze for the invalid local packages', async () => { - await expect(analyze('./error-path')).rejects.toEqual(new Error(`Cannot analyze ./error-path`)); + await expect(analyze('./error-path')).rejects.toEqual( + new Error(`Cannot analyze ./error-path, the reason is Cannot find package.json on ./error-path.`), + ); }); it('can be analyzed for the local packages', async () => { await expect(analyze('./src/platforms/workspaces/yarn/fixture/valid-packages')).resolves.toMatchSnapshot(); diff --git a/src/platforms/workspaces/yarn/index.ts b/src/platforms/workspaces/yarn/index.ts index 3eeaaac..c3bd7c9 100644 --- a/src/platforms/workspaces/yarn/index.ts +++ b/src/platforms/workspaces/yarn/index.ts @@ -61,7 +61,7 @@ export async function analyze(packagePath: string) { }; }), ); - } catch (error) { - return Promise.reject(new Error(`Cannot analyze ${packagePath}`)); + } catch (error: any) { + return Promise.reject(new Error(`Cannot analyze ${packagePath}, the reason is ${error.message}`)); } }