Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Or-Geva committed Sep 21, 2023
1 parent 253a7d9 commit e192dd7
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/main/treeDataProviders/utils/yarnImpactGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IImpactGraph, IImpactGraphNode } from 'jfrog-ide-webview';
import { RootNode } from '../dependenciesTree/dependenciesRoot/rootTree';
import { ScanUtils } from '../../utils/scanUtils';

type YarnWhyItem = StepItem | InfoItem;
export type YarnWhyItem = StepItem | InfoItem;

/**
* Represents a step item in the "yarn why" output.
Expand Down Expand Up @@ -68,7 +68,8 @@ export class YarnImpactGraphUtil {
*/
private findDependencyChain(output: YarnWhyItem[]): string[] {
const startIndex: number | undefined = this.findDependencyPosition(this.dependencyVersion, output);
if (!startIndex) {
// Zero could be a valid index
if (startIndex === undefined) {
return [];
}
for (let i: number = startIndex + 1; i < output.length; i++) {
Expand Down Expand Up @@ -254,7 +255,7 @@ export class YarnImpactGraphUtil {
/**
* Executes the "yarn why" command and parses its JSON output.
*/
private runYarnWhy(): YarnWhyItem[] {
protected runYarnWhy(): YarnWhyItem[] {
const output: string = ScanUtils.executeCmd('yarn why --json --no-progress ' + this.dependencyName, this.workspaceFolder).toString();
return output
.split('\n')
Expand Down
144 changes: 144 additions & 0 deletions src/test/tests/yarnImpactGraph.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { IImpactGraph } from 'jfrog-ide-webview';
import { YarnImpactGraphUtil, YarnWhyItem } from '../../main/treeDataProviders/utils/yarnImpactGraph';
import { assert } from 'chai';
import { RootNode } from '../../main/treeDataProviders/dependenciesTree/dependenciesRoot/rootTree';

describe('Yarn impact graph util', async () => {
it('Build single impact graph', async () => {
const results: IImpactGraph = new YarnImpactGraphUtilMock('minimist', '0.0.8', 'Mock-Project', '').create();
assert.deepEqual(results, generateExpectedSingleImpactGraph());
});

it.only('Build multiple impact graphs', async () => {
const results: IImpactGraph = new YarnImpactGraphUtilMock('minimist', '1.2.0', 'Mock-Project', '').create();
assert.deepEqual(results, generateExpectedMultipleImpactGraphs());
});
});

class YarnImpactGraphUtilMock extends YarnImpactGraphUtil {
protected runYarnWhy(): YarnWhyItem[] {
const yarnWhyOutput: YarnWhyItem[] = [
{
type: 'info',
data: '\r=> Found "[email protected]"'
},
{
type: 'info',
data: 'Has been hoisted to "minimist"'
},
{
type: 'info',
data: 'Reasons this module exists'
},
{
type: 'list',
data: {
type: 'reasons',
items: [
'Specified in "dependencies"',
'Hoisted from "jest-cli#node-notifier#minimist"',
'Hoisted from "jest-cli#sane#minimist"',
'Hoisted from "jest-cli#istanbul-lib-instrument#babel-generator#detect-indent#minimist"'
]
}
},
{
type: 'info',
data: 'Disk size without dependencies: "96KB"'
},
{
type: 'info',
data: '\r=> Found "mkdirp#[email protected]"'
},
{
type: 'info',
data: 'This module exists because "jest-cli#istanbul-api#mkdirp" depends on it.'
}
];
return yarnWhyOutput;
}
}

function generateExpectedSingleImpactGraph(): IImpactGraph {
return {
root: {
name: 'Mock-Project',
children: [
{
name: 'jest-cli',
children: [
{
name: 'istanbul-api',
children: [
{
name: 'mkdirp',
children: [
{
name: 'minimist:0.0.8'
}
]
}
]
}
]
}
]
},
pathsCount: 1,
pathsLimit: RootNode.IMPACT_PATHS_LIMIT
};
}

function generateExpectedMultipleImpactGraphs(): IImpactGraph {
return {
root: {
name: 'Mock-Project',
children: [
{
name: 'minimist:1.2.0'
},
{
name: 'jest-cli',
children: [
{
name: 'node-notifier',
children: [
{
name: 'minimist:1.2.0'
}
]
},
{
name: 'sane',
children: [
{
name: 'minimist:1.2.0'
}
]
},
{
name: 'istanbul-lib-instrument',
children: [
{
name: 'babel-generator',
children: [
{
name: 'detect-indent',
children: [
{
name: 'minimist:1.2.0'
}
]
}
]
}
]
}
]
}
]
},
pathsCount: 4,
pathsLimit: RootNode.IMPACT_PATHS_LIMIT
};
}

0 comments on commit e192dd7

Please sign in to comment.