Skip to content

Commit

Permalink
fix: add unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
narol1024 committed Jun 2, 2024
1 parent f5f94cc commit e1aa768
Show file tree
Hide file tree
Showing 32 changed files with 217 additions and 209 deletions.
Binary file removed .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,4 @@ dist
.idea/
.vscode/
tools/dev
.DS_Store
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ npx sdp-analyzer analyze react,vue,svelte,audio-analyser-cli
### API Reference

```javascript
const { analyzeNpmPackage } = require("sdp-analyzer");
const { analyze } = require("sdp-analyzer");
try {
const deps = await sdpAnalyer.analyze("react");
const deps = await analyze("react");
} catch (error) {
// handle error
}
Expand Down
18 changes: 14 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import 'dotenv/config';
require('dotenv/config');

const isCI = process.env.CI === 'true';

export default {
module.exports = {
verbose: true,
collectCoverage: false,
resetModules: true,
restoreMocks: true,
testEnvironment: 'node',
transform: {},
transform: {
'^.+\\\\\\\\.tsx?$': 'ts-jest',
},
preset: 'ts-jest/presets/default-esm',
extensionsToTreatAsEsm: ['.ts'],
testMatch: ['**/?(*.)+(spec|test).[jt]s?(x)'],
// testMatch: ['<rootDir>/src/cli/*.spec.ts'],
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
Expand All @@ -23,7 +26,14 @@ export default {
__JEST_TEST_ENV__: true,
},
collectCoverageFrom: ['<rootDir>/src/**/*.ts'],
coveragePathIgnorePatterns: ['<rootDir>/dist/', '/node_modules/', '<rootDir>/scripts', '<rootDir>/tools'],
coveragePathIgnorePatterns: [
'<rootDir>/dist/',
'/node_modules/',
'<rootDir>/scripts',
'<rootDir>/tools',
'<rootDir>/src/types.ts',
'<rootDir>/src/globals.d.ts',
],
coverageProvider: 'v8',
coverageReporters: isCI ? ['json'] : ['text'],
testTimeout: 120 * 1000,
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{
"name": "sdp-analyzer",
"type": "module",
"main": "dist/index.js",
"bin": "dist/cli/index.js",
"bin": "dist/bin/cli.js",
"types": "dist/main.d.ts",
"version": "1.0.0",
"scripts": {
"prepare": "husky install",
"build": "tsc --project tsconfig.build.json",
"build:clean": "rm -rf tsconfig.build.tsbuildinfo && rm -rf ./dist",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
"test": "jest",
"test:coverage": "npm run test -- --coverage",
"test:ci": "npm run test -- --colors --coverage --ci --coverageReporters=\"json-summary\"",
"lint": "eslint --ext .ts,.js .",
Expand Down
53 changes: 53 additions & 0 deletions src/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Main Tests can be analyzed for a valid npm package 1`] = `
Array [
Object {
"fanIn": 10000,
"fanOut": 1,
"label": "Stable",
"name": "react",
"stability": 0.00009999000099990002,
},
]
`;

exports[`Main Tests can be analyzed 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",
},
]
`;
5 changes: 5 additions & 0 deletions src/bin/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

import { bootstrap } from '../cli';

bootstrap();
29 changes: 29 additions & 0 deletions src/cli/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* This is a sample test suite.
* Replace this with your implementation.
*/
import { bootstrap } from './index';

describe('CLI tool Tests', () => {
it('main', async () => {
const spy = jest.spyOn(console, 'log');
const program = await bootstrap(['node', 'index.js', 'analyze', 'react']);
await expect(program.commands.map((i: any) => i._name)).toEqual(['analyze']);
const result = spy.mock.calls[0][0];
expect(result).toEqual(
JSON.stringify(
[
{
name: 'react',
fanIn: 10000,
fanOut: 1,
stability: 0.00009999000099990002,
label: 'Stable',
},
],
null,
2,
),
);
});
});
33 changes: 14 additions & 19 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
#!/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();

program.name('sdp-analyzer').description('A cli tool for analyzing package stability').version('1.0.0');
program
.command('analyze')
.argument('<string>', '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);
}
console.log(JSON.stringify(result, null, 2));
process.exit(0);
});
async function bootstrap(argv = process.argv) {
program.name('sdp-analyzer').description('A cli tool for analyzing package stability').version('1.0.0');
program
.command('analyze')
.argument('<string>', 'the local package path or a npm package name')
.action(async target => {
const result = await analyze(target);
console.log(JSON.stringify(result, null, 2));
});
await program.parseAsync(argv);
return program;
}

program.parse();
export { bootstrap };
20 changes: 18 additions & 2 deletions src/core/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,23 @@
import { evaluate } from './index';

describe('Core Tests', () => {
it('should be return 0.2', () => {
expect(evaluate(1, 4)).toEqual(0.2);
test('should return correct SDP value with non-zero fan-in and fan-out', () => {
expect(evaluate(4, 4)).toBe(0.5);
});

test('should return 1 when fan-in is 0', () => {
expect(evaluate(5, 0)).toBe(1);
});

test('should return 0 when fan-out is 0', () => {
expect(evaluate(0, 3)).toBe(0);
});

test('should return 0 when both fan-in and fan-out are 0', () => {
expect(evaluate(0, 0)).toBe(0);
});

test('should handle large numbers', () => {
expect(evaluate(1000, 500)).toBe(2 / 3);
});
});
14 changes: 14 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* This is a sample test suite.
* Replace this with your implementation.
*/
import { analyze } from './index';

describe('Main Tests', () => {
it('can be analyzed for a valid npm package', async () => {
await expect(analyze('react')).resolves.toMatchSnapshot();
});
it('can be analyzed for local packages', async () => {
await expect(analyze('./src/platforms/workspaces/yarn/fixture/valid-packages')).resolves.toMatchSnapshot();
});
});
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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);
}
56 changes: 3 additions & 53 deletions src/platforms/npm-package/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -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 be analyzed for multiple packages 1`] = `
Array [
Object {
"fanIn": 10000,
Expand All @@ -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 be analyzed for multiple packages with the package version 1`] = `
Array [
Object {
"fanIn": 10000,
Expand All @@ -50,7 +38,7 @@ Array [
]
`;

exports[`Npm pacakge Tests should return a dep array for multiple packages 1`] = `
exports[`Npm pacakge Tests can be analyzed for single package 1`] = `
Array [
Object {
"fanIn": 10000,
Expand All @@ -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,
},
]
`;
6 changes: 3 additions & 3 deletions src/platforms/npm-package/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 be analyzed for single package', async () => {
await expect(analyze('react')).resolves.toMatchSnapshot();
});
it('should return a dep array for multiple packages', async () => {
it('can be analyzed for multiple packages', async () => {
await expect(analyze('react,vue')).resolves.toMatchSnapshot();
});
it('should return a dep array with the package version', async () => {
it('can be analyzed for multiple packages with the package version', async () => {
await expect(analyze('[email protected],[email protected]')).resolves.toMatchSnapshot();
});
});
Loading

0 comments on commit e1aa768

Please sign in to comment.