From 2a786795afa8b95efc80ad0b5b891f3228888e07 Mon Sep 17 00:00:00 2001 From: narol Date: Sun, 2 Jun 2024 00:35:51 +0800 Subject: [PATCH] fix: add unit testing --- .DS_Store | Bin 6148 -> 0 bytes README.md | 4 +- jest.config.js | 9 +- src/__snapshots__/index.spec.ts.snap | 53 ++++++++++++ src/cli/index.ts | 11 +-- src/index.spec.ts | 14 +++ src/index.ts | 14 ++- .../__snapshots__/index.spec.ts.snap | 56 +----------- src/platforms/npm-package/index.spec.ts | 6 +- .../yarn/__snapshots__/index.spec.ts.snap | 81 +----------------- src/platforms/workspaces/yarn/index.spec.ts | 2 +- src/types.ts | 1 - 12 files changed, 99 insertions(+), 152 deletions(-) delete mode 100644 .DS_Store create mode 100644 src/__snapshots__/index.spec.ts.snap create mode 100644 src/index.spec.ts diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index cc9193c319e3cfa225b93517e06a4194b260d254..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKJ5EC}5S$Guf@o4w`UH`A)nZuE8E=)>3&+Hy!{TP1Q#V_6C>FOf z-Xa~=6E#W!DR8R5MJ|_K|2Om>`u|gsR#HF;{3``)K0FR/src/**/*.ts'], - coveragePathIgnorePatterns: ['/dist/', '/node_modules/', '/scripts', '/tools'], + coveragePathIgnorePatterns: [ + '/dist/', + '/node_modules/', + '/scripts', + '/tools', + '/src/types.ts', + '/src/globals.d.ts', + ], coverageProvider: 'v8', coverageReporters: isCI ? ['json'] : ['text'], testTimeout: 120 * 1000, diff --git a/src/__snapshots__/index.spec.ts.snap b/src/__snapshots__/index.spec.ts.snap new file mode 100644 index 0000000..957a233 --- /dev/null +++ b/src/__snapshots__/index.spec.ts.snap @@ -0,0 +1,53 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Main Tests can analyze a valid npm package 1`] = ` +Array [ + Object { + "fanIn": 10000, + "fanOut": 1, + "label": "Stable", + "name": "react", + "stability": 0.00009999000099990002, + }, +] +`; + +exports[`Main Tests can analyze for local packages 1`] = ` +Array [ + Object { + "fanIn": 1, + "fanOut": 0, + "instable": 0, + "label": "Stable", + "name": "a", + }, + Object { + "fanIn": 1, + "fanOut": 1, + "instable": 0.5, + "label": "Normal", + "name": "b", + }, + Object { + "fanIn": 1, + "fanOut": 2, + "instable": 0.6666666666666666, + "label": "Flexible", + "name": "c", + }, + Object { + "fanIn": 1, + "fanOut": 3, + "instable": 0.75, + "label": "Flexible", + "name": "d", + }, + Object { + "fanIn": 1, + "fanOut": 4, + "instable": 0.8, + "label": "Instable", + "name": "e", + }, +] +`; diff --git a/src/cli/index.ts b/src/cli/index.ts index 819894e..b5b2a7e 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -1,7 +1,6 @@ #!/usr/bin/env node import { Command } from 'commander'; -import { analyzeNpmPackage, analyzeYarnWorkspaces } from '../index'; -import { isLocalPath } from '../utils/checkLocalPath'; +import { analyze } from '../index'; const program = new Command(); @@ -10,13 +9,7 @@ program .command('analyze') .argument('', 'the local package path or a npm package name') .action(async target => { - let result; - if (isLocalPath(target)) { - // default to yarn workspaces - result = await analyzeYarnWorkspaces(target); - } else { - result = await analyzeNpmPackage(target); - } + const result = await analyze(target); console.log(JSON.stringify(result, null, 2)); process.exit(0); }); diff --git a/src/index.spec.ts b/src/index.spec.ts new file mode 100644 index 0000000..9e04a50 --- /dev/null +++ b/src/index.spec.ts @@ -0,0 +1,14 @@ +/** + * This is a sample test suite. + * Replace this with your implementation. + */ +import { analyze } from './index'; + +describe('Main Tests', () => { + it('can analyze a valid npm package', async () => { + await expect(analyze('react')).resolves.toMatchSnapshot(); + }); + it('can analyze for local packages', async () => { + await expect(analyze('./src/platforms/workspaces/yarn/fixture')).resolves.toMatchSnapshot(); + }); +}); diff --git a/src/index.ts b/src/index.ts index b058d02..6696a55 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,12 @@ -export { analyze as analyzeNpmPackage } from './platforms/npm-package'; -export { analyze as analyzeYarnWorkspaces } from './platforms/workspaces/yarn'; +import { isLocalPath } from './utils/checkLocalPath'; + +import { analyze as analyzeNpmPackage } from './platforms/npm-package'; +import { analyze as analyzeYarnWorkspaces } from './platforms/workspaces/yarn'; + +export function analyze(target: string) { + if (isLocalPath(target)) { + // default to yarn workspaces + return analyzeYarnWorkspaces(target); + } + return analyzeNpmPackage(target); +} diff --git a/src/platforms/npm-package/__snapshots__/index.spec.ts.snap b/src/platforms/npm-package/__snapshots__/index.spec.ts.snap index bd597db..7f72fdf 100644 --- a/src/platforms/npm-package/__snapshots__/index.spec.ts.snap +++ b/src/platforms/npm-package/__snapshots__/index.spec.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Npm pacakge Tests should return a dep array for multiple packages 1`] = ` +exports[`Npm pacakge Tests can analyze for multiple packages 1`] = ` Array [ Object { "fanIn": 10000, @@ -19,19 +19,7 @@ Array [ ] `; -exports[`Npm pacakge Tests should return a dep array for single package 1`] = ` -Array [ - Object { - "fanIn": 10000, - "fanOut": 1, - "label": "Stable", - "name": "react", - "stability": 0.00009999000099990002, - }, -] -`; - -exports[`Npm pacakge Tests should return a dep array with the package version 1`] = ` +exports[`Npm pacakge Tests can analyze for multiple packages with the package version 1`] = ` Array [ Object { "fanIn": 10000, @@ -50,7 +38,7 @@ Array [ ] `; -exports[`Npm pacakge Tests should return a dep array for multiple packages 1`] = ` +exports[`Npm pacakge Tests can analyze for single package 1`] = ` Array [ Object { "fanIn": 10000, @@ -59,43 +47,5 @@ Array [ "name": "react", "stability": 0.00009999000099990002, }, - Object { - "fanIn": 10000, - "fanOut": 5, - "label": "Stable", - "name": "vue", - "stability": 0.0004997501249375312, - }, -] -`; - -exports[`Npm pacakge Tests should return a dep array for single package 1`] = ` -Array [ - Object { - "fanIn": 10000, - "fanOut": 1, - "label": "Stable", - "name": "react", - "stability": 0.00009999000099990002, - }, -] -`; - -exports[`Npm pacakge Tests should return a dep array with the package version 1`] = ` -Array [ - Object { - "fanIn": 10000, - "fanOut": 1, - "label": "Stable", - "name": "react", - "stability": 0.00009999000099990002, - }, - Object { - "fanIn": 10000, - "fanOut": 5, - "label": "Stable", - "name": "vue", - "stability": 0.0004997501249375312, - }, ] `; diff --git a/src/platforms/npm-package/index.spec.ts b/src/platforms/npm-package/index.spec.ts index 20e1c98..77d7f52 100644 --- a/src/platforms/npm-package/index.spec.ts +++ b/src/platforms/npm-package/index.spec.ts @@ -15,13 +15,13 @@ describe('Npm pacakge Tests', () => { { name: 'loose-envify', version: '^1.1.0' }, ]); }); - it('should return a dep array for single package', async () => { + it('can analyze for single package', async () => { await expect(analyze('react')).resolves.toMatchSnapshot(); }); - it('should return a dep array for multiple packages', async () => { + it('can analyze for multiple packages', async () => { await expect(analyze('react,vue')).resolves.toMatchSnapshot(); }); - it('should return a dep array with the package version', async () => { + it('can analyze for multiple packages with the package version', async () => { await expect(analyze('react@16.0.0,vue@3.0.0')).resolves.toMatchSnapshot(); }); }); diff --git a/src/platforms/workspaces/yarn/__snapshots__/index.spec.ts.snap b/src/platforms/workspaces/yarn/__snapshots__/index.spec.ts.snap index 4722a9d..aa7c506 100644 --- a/src/platforms/workspaces/yarn/__snapshots__/index.spec.ts.snap +++ b/src/platforms/workspaces/yarn/__snapshots__/index.spec.ts.snap @@ -1,45 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Workspaces Tests should be that the package.json can be found 1`] = ` -Array [ - Object { - "dependencies": Object {}, - "name": "a", - }, - Object { - "dependencies": Object { - "a": "^1.0.0", - }, - "name": "b", - }, - Object { - "dependencies": Object { - "a": "^1.0.0", - "b": "^1.0.0", - }, - "name": "c", - }, - Object { - "dependencies": Object { - "a": "^1.0.0", - "b": "^1.0.0", - "c": "^1.0.0", - }, - "name": "d", - }, - Object { - "dependencies": Object { - "a": "^1.0.0", - "b": "^1.0.0", - "c": "^1.0.0", - "e": "^1.0.0", - }, - "name": "e", - }, -] -`; - -exports[`Workspaces Tests should return a dep tree 1`] = ` +exports[`Workspaces Tests can analyze for local packages 1`] = ` Array [ Object { "fanIn": 1, @@ -117,43 +78,3 @@ Array [ }, ] `; - -exports[`Workspaces Tests should return a dep tree 1`] = ` -Array [ - Object { - "fanIn": 1, - "fanOut": 0, - "instable": 0, - "label": "Stable", - "name": "a", - }, - Object { - "fanIn": 1, - "fanOut": 1, - "instable": 0.5, - "label": "Normal", - "name": "b", - }, - Object { - "fanIn": 1, - "fanOut": 2, - "instable": 0.6666666666666666, - "label": "Flexible", - "name": "c", - }, - Object { - "fanIn": 1, - "fanOut": 3, - "instable": 0.75, - "label": "Flexible", - "name": "d", - }, - Object { - "fanIn": 1, - "fanOut": 4, - "instable": 0.8, - "label": "Instable", - "name": "e", - }, -] -`; diff --git a/src/platforms/workspaces/yarn/index.spec.ts b/src/platforms/workspaces/yarn/index.spec.ts index 6040acb..46e78b9 100644 --- a/src/platforms/workspaces/yarn/index.spec.ts +++ b/src/platforms/workspaces/yarn/index.spec.ts @@ -13,7 +13,7 @@ describe('Workspaces Tests', () => { it('should be that the package.json can be found', () => { expect(readWorkspacesDependencies('./src/platforms/workspaces/yarn/fixture')).toMatchSnapshot(); }); - it('should return a dep tree', async () => { + it('can analyze for local packages', async () => { await expect(analyze('./src/platforms/workspaces/yarn/fixture')).resolves.toMatchSnapshot(); }); }); diff --git a/src/types.ts b/src/types.ts index 6c222a9..9bdff93 100644 --- a/src/types.ts +++ b/src/types.ts @@ -5,7 +5,6 @@ export interface Dep { stability: number; label: string; } - export interface NpmDependency { name: string; version: string;