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 76c075f commit b131e22
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/platforms/npm-package/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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();
});
Expand Down
6 changes: 3 additions & 3 deletions src/platforms/npm-package/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -129,7 +129,7 @@ export async function analyze(packageNames: string): Promise<Dep[]> {

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}`));
}
}
4 changes: 3 additions & 1 deletion src/platforms/workspaces/yarn/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/platforms/workspaces/yarn/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`));
}
}

0 comments on commit b131e22

Please sign in to comment.