Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add unit testing #2

Merged
merged 1 commit into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
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
9 changes: 8 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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
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 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",
},
]
`;
11 changes: 2 additions & 9 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -10,13 +9,7 @@ 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);
}
const result = await analyze(target);
console.log(JSON.stringify(result, null, 2));
process.exit(0);
});
Expand Down
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 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();
});
});
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 analyze 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 analyze 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 analyze 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 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('[email protected],[email protected]')).resolves.toMatchSnapshot();
});
});
81 changes: 1 addition & 80 deletions src/platforms/workspaces/yarn/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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",
},
]
`;
2 changes: 1 addition & 1 deletion src/platforms/workspaces/yarn/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export interface Dep {
stability: number;
label: string;
}

export interface NpmDependency {
name: string;
version: string;
Expand Down
Loading