From 2c24779cd6d1c21903e40484f933fde57ccd30f8 Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Thu, 11 May 2023 17:10:43 -0400 Subject: [PATCH] feat: add node API --- README.md | 16 +- lib/check.ts | 78 ++++++ lib/cli.ts | 83 ++----- lib/dependency-versions.ts | 71 +++--- lib/index.ts | 7 + lib/output.ts | 10 +- lib/package.ts | 26 +- package.json | 3 + test/lib/check-test.ts | 359 +++++++++++++++++++++++++++ test/lib/dependency-versions-test.ts | 24 +- test/lib/output-test.ts | 22 +- 11 files changed, 577 insertions(+), 122 deletions(-) create mode 100644 lib/check.ts create mode 100644 lib/index.ts create mode 100644 test/lib/check-test.ts diff --git a/README.md b/README.md index e18c6e1b..2181a2c2 100644 --- a/README.md +++ b/README.md @@ -112,8 +112,10 @@ Found 2 dependencies with mismatching versions across the workspace. Fix with `- ## Options +These options are available on the CLI and as parameters to the [Node API](#node-api). + | Name | Description | -| --- | --- | +| :-- | :-- | | `--fix` | Whether to autofix inconsistencies (using latest version present). | | `--ignore-dep` | Dependency to ignore mismatches for (option can be repeated). | | `--ignore-dep-pattern` | RegExp of dependency names to ignore mismatches for (option can be repeated). | @@ -122,6 +124,18 @@ Found 2 dependencies with mismatching versions across the workspace. Fix with `- | `--ignore-path` | Workspace-relative path of packages to ignore mismatches for (option can be repeated). | | `--ignore-path-pattern` | RegExp of workspace-relative path of packages to ignore mismatches for (option can be repeated). | +## Node API + +| Function | Description | +| :-- | :-- | +| `check(path, options)` | Checks for inconsistencies across a workspace. Optionally fixes them. Returns lists of inconsistencies: a complete list, a fixable list, and an un-fixable list. | +| `mismatchingVersionsToDetailedSummary(versions)` | Returns a string of human-readable tables describing mismatching dependency versions. | +| `mismatchingVersionsToFixedSummary(versions)` | Returns a string summary of the mismatching dependency versions that were fixed. | + +More information about parameters and return values can be found in the types and JSDoc comments. + +See an example of how these functions are used in [`lib/cli.ts`](./lib/cli.ts). + ## Related * [npm-package-json-lint](https://github.com/tclindner/npm-package-json-lint) — use this complementary tool to enforce that your dependency versions use consistent range types (i.e. [prefer-caret-version-dependencies](https://npmpackagejsonlint.org/docs/rules/dependencies/prefer-caret-version-dependencies), [prefer-caret-version-devDependencies](https://npmpackagejsonlint.org/docs/rules/dependencies/prefer-caret-version-devDependencies)) diff --git a/lib/check.ts b/lib/check.ts new file mode 100644 index 00000000..35b4f477 --- /dev/null +++ b/lib/check.ts @@ -0,0 +1,78 @@ +import { + calculateVersionsForEachDependency, + calculateMismatchingVersions, + filterOutIgnoredDependencies, + fixMismatchingVersions, +} from './dependency-versions.js'; +import { getPackages } from './workspace.js'; + +/** + * Checks for inconsistencies across a workspace. Optionally fixes them. + * @param path - path to the workspace root + * @param options + * @param options.fix - Whether to autofix inconsistencies (using latest version present) + * @param options.ignoreDep - Dependency(s) to ignore mismatches for + * @param options.ignoreDepPattern - RegExp(s) of dependency names to ignore mismatches for + * @param options.ignorePackage - Workspace package(s) to ignore mismatches for + * @param options.ignorePackagePattern - RegExp(s) of package names to ignore mismatches for + * @param options.ignorePath - Workspace-relative path(s) of packages to ignore mismatches for + * @param options.ignorePathPattern - RegExp(s) of workspace-relative path of packages to ignore mismatches for + * @returns an object with the following properties: + * - `mismatchingVersions`: All the mismatching versions found. + * - `mismatchingVersionsFixable`: The mismatching versions that are fixable (these will be fixed in `fix` mode) + * - `mismatchingVersionsNotFixable`: The mismatching versions that are not fixable (these will be skipped in `fix` mode). + */ +export function check( + path: string, + options?: { + fix?: boolean; + ignoreDep?: string[]; + ignoreDepPattern?: string[]; + ignorePackage?: string[]; + ignorePackagePattern?: string[]; + ignorePath?: string[]; + ignorePathPattern?: string[]; + } +) { + const optionsWithDefaults = { + fix: false, + ignoreDep: [], + ignoreDepPattern: [], + ignorePackage: [], + ignorePackagePattern: [], + ignorePath: [], + ignorePathPattern: [], + ...options, + }; + + // Calculate. + const packages = getPackages( + path, + optionsWithDefaults.ignorePackage, + optionsWithDefaults.ignorePackagePattern.map((s) => new RegExp(s)), + optionsWithDefaults.ignorePath, + optionsWithDefaults.ignorePathPattern.map((s) => new RegExp(s)) + ); + + const dependencyVersions = calculateVersionsForEachDependency(packages); + + const mismatchingVersions = filterOutIgnoredDependencies( + calculateMismatchingVersions(dependencyVersions), + optionsWithDefaults.ignoreDep, + optionsWithDefaults.ignoreDepPattern.map((s) => new RegExp(s)) + ); + + const resultsAfterFix = fixMismatchingVersions( + packages, + mismatchingVersions, + !optionsWithDefaults.fix // Do dry-run if not fixing. + ); + const mismatchingVersionsFixable = resultsAfterFix.fixable; + const mismatchingVersionsNotFixable = resultsAfterFix.notFixable; + + return { + mismatchingVersions, + mismatchingVersionsFixable, + mismatchingVersionsNotFixable, + }; +} diff --git a/lib/cli.ts b/lib/cli.ts index bc1d3076..460b3baf 100644 --- a/lib/cli.ts +++ b/lib/cli.ts @@ -1,20 +1,13 @@ import { Command, Argument } from 'commander'; import { readFileSync } from 'node:fs'; -import { - calculateVersionsForEachDependency, - calculateMismatchingVersions, - filterOutIgnoredDependencies, - fixMismatchingVersions, - MismatchingDependencyVersions, -} from '../lib/dependency-versions.js'; -import { getPackages } from '../lib/workspace.js'; -import { - mismatchingVersionsToOutput, - mismatchingVersionsFixedToOutput, -} from '../lib/output.js'; import { join, dirname } from 'node:path'; import type { PackageJson } from 'type-fest'; import { fileURLToPath } from 'node:url'; +import { + mismatchingVersionsToDetailedSummary, + mismatchingVersionsToFixedSummary, +} from './output.js'; +import { check } from './check.js'; const __dirname = dirname(fileURLToPath(import.meta.url)); @@ -81,53 +74,31 @@ export function run() { collect, [] ) - .action(function ( - path, - options: { - ignoreDep: string[]; - ignoreDepPattern: string[]; - ignorePackage: string[]; - ignorePackagePattern: string[]; - ignorePath: string[]; - ignorePathPattern: string[]; - fix: boolean; - } - ) { - // Calculate. - const packages = getPackages( - path, - options.ignorePackage, - options.ignorePackagePattern.map((s) => new RegExp(s)), - options.ignorePath, - options.ignorePathPattern.map((s) => new RegExp(s)) - ); - - const dependencyVersions = calculateVersionsForEachDependency(packages); - - let mismatchingVersions = filterOutIgnoredDependencies( - calculateMismatchingVersions(dependencyVersions), - options.ignoreDep, - options.ignoreDepPattern.map((s) => new RegExp(s)) - ); - let mismatchingVersionsFixed: MismatchingDependencyVersions = []; + .action((path, options) => { + const { + mismatchingVersions, + mismatchingVersionsFixable, + mismatchingVersionsNotFixable, + } = check(path, options); if (options.fix) { - const resultsAfterFix = fixMismatchingVersions( - packages, - mismatchingVersions - ); - mismatchingVersions = resultsAfterFix.notFixed; - mismatchingVersionsFixed = resultsAfterFix.fixed; - } - - // Show output for dependencies we fixed. - if (mismatchingVersionsFixed.length > 0) { - console.log(mismatchingVersionsFixedToOutput(mismatchingVersionsFixed)); - } + // Show output for dependencies we fixed. + if (mismatchingVersionsFixable.length > 0) { + console.log( + mismatchingVersionsToFixedSummary(mismatchingVersionsFixable) + ); + } - // Show output for dependencies that still have mismatches. - if (mismatchingVersions.length > 0) { - console.log(mismatchingVersionsToOutput(mismatchingVersions)); + // Show output for dependencies that still have mismatches. + if (mismatchingVersionsNotFixable.length > 0) { + console.log( + mismatchingVersionsToDetailedSummary(mismatchingVersionsNotFixable) + ); + process.exitCode = 1; + } + } else if (mismatchingVersions.length > 0) { + // Show output for dependencies that have mismatches. + console.log(mismatchingVersionsToDetailedSummary(mismatchingVersions)); process.exitCode = 1; } }) diff --git a/lib/dependency-versions.ts b/lib/dependency-versions.ts index bc1576c0..4d298136 100644 --- a/lib/dependency-versions.ts +++ b/lib/dependency-versions.ts @@ -14,6 +14,7 @@ export type DependenciesToVersionsSeen = Map< { package: Package; version: string; isLocalPackageVersion: boolean }[] >; +/** A dependency that was found to have mismatching versions, the versions present of it, and the packages each of those versions are seen in. */ export type MismatchingDependencyVersions = Array<{ dependency: string; versions: { @@ -287,15 +288,17 @@ function writeDependencyVersion( ); } +// eslint-disable-next-line complexity export function fixMismatchingVersions( packages: Package[], - mismatchingVersions: MismatchingDependencyVersions + mismatchingVersions: MismatchingDependencyVersions, + dryrun = false ): { - fixed: MismatchingDependencyVersions; - notFixed: MismatchingDependencyVersions; + fixable: MismatchingDependencyVersions; + notFixable: MismatchingDependencyVersions; } { - const fixed = []; - const notFixed = []; + const fixable: MismatchingDependencyVersions = []; + const notFixable: MismatchingDependencyVersions = []; // Loop through each dependency that has a mismatching versions. for (const mismatchingVersion of mismatchingVersions) { // Decide what version we should fix to. @@ -307,7 +310,7 @@ export function fixMismatchingVersions( fixedVersion = getIncreasedLatestVersion(versions); } catch { // Skip this dependency. - notFixed.push(mismatchingVersion); + notFixable.push(mismatchingVersion); continue; } @@ -321,7 +324,7 @@ export function fixMismatchingVersions( compareVersionRanges(fixedVersion, localPackage.packageJson.version) > 0 ) { // Skip this dependency. - notFixed.push(mismatchingVersion); + notFixable.push(mismatchingVersion); continue; } @@ -342,13 +345,15 @@ export function fixMismatchingVersions( package_.packageJson.devDependencies[mismatchingVersion.dependency] !== fixedVersion ) { - writeDependencyVersion( - package_.pathPackageJson, - package_.packageJsonEndsInNewline, - 'devDependencies', - mismatchingVersion.dependency, - fixedVersion - ); + if (!dryrun) { + writeDependencyVersion( + package_.pathPackageJson, + package_.packageJsonEndsInNewline, + 'devDependencies', + mismatchingVersion.dependency, + fixedVersion + ); + } isFixed = true; } @@ -358,13 +363,15 @@ export function fixMismatchingVersions( package_.packageJson.dependencies[mismatchingVersion.dependency] !== fixedVersion ) { - writeDependencyVersion( - package_.pathPackageJson, - package_.packageJsonEndsInNewline, - 'dependencies', - mismatchingVersion.dependency, - fixedVersion - ); + if (!dryrun) { + writeDependencyVersion( + package_.pathPackageJson, + package_.packageJsonEndsInNewline, + 'dependencies', + mismatchingVersion.dependency, + fixedVersion + ); + } isFixed = true; } @@ -374,24 +381,26 @@ export function fixMismatchingVersions( package_.packageJson.resolutions[mismatchingVersion.dependency] !== fixedVersion ) { - writeDependencyVersion( - package_.pathPackageJson, - package_.packageJsonEndsInNewline, - 'resolutions', - mismatchingVersion.dependency, - fixedVersion - ); + if (!dryrun) { + writeDependencyVersion( + package_.pathPackageJson, + package_.packageJsonEndsInNewline, + 'resolutions', + mismatchingVersion.dependency, + fixedVersion + ); + } isFixed = true; } } if (isFixed) { - fixed.push(mismatchingVersion); + fixable.push(mismatchingVersion); } } return { - fixed, - notFixed, + fixable, + notFixable, }; } diff --git a/lib/index.ts b/lib/index.ts new file mode 100644 index 00000000..15f98456 --- /dev/null +++ b/lib/index.ts @@ -0,0 +1,7 @@ +// Public Node API. + +export { check } from './check.js'; +export { + mismatchingVersionsToDetailedSummary, + mismatchingVersionsToFixedSummary, +} from './output.js'; diff --git a/lib/output.ts b/lib/output.ts index faee7a92..c83fe6f4 100644 --- a/lib/output.ts +++ b/lib/output.ts @@ -6,7 +6,10 @@ import { } from './semver.js'; import { table } from 'table'; -export function mismatchingVersionsToOutput( +/** + * Returns human-readable tables describing mismatching dependency versions. + */ +export function mismatchingVersionsToDetailedSummary( mismatchingDependencyVersions: MismatchingDependencyVersions ): string { if (mismatchingDependencyVersions.length === 0) { @@ -59,7 +62,10 @@ export function mismatchingVersionsToOutput( ].join('\n'); } -export function mismatchingVersionsFixedToOutput( +/** + * Returns a summary of the mismatching dependency versions that were fixed. + */ +export function mismatchingVersionsToFixedSummary( mismatchingDependencyVersions: MismatchingDependencyVersions ): string { if (mismatchingDependencyVersions.length === 0) { diff --git a/lib/package.ts b/lib/package.ts index 1ee8a2ab..c6807e50 100644 --- a/lib/package.ts +++ b/lib/package.ts @@ -3,11 +3,16 @@ import { join, relative } from 'node:path'; import { PackageJson } from 'type-fest'; import { load } from 'js-yaml'; -// Class to represent all of the information we need to know about a package in a workspace. +/* + * Class to represent all of the information we need to know about a package in a workspace. + */ export class Package { - path: string; // Absolute path to package. - pathWorkspace: string; // Absolute path to workspace. - pathPackageJson: string; // Absolute path to package.json. + /** Absolute path to package */ + path: string; + /** Absolute path to workspace.*/ + pathWorkspace: string; + /** Absolute path to package.json. */ + pathPackageJson: string; packageJson: PackageJson; packageJsonEndsInNewline: boolean; pnpmWorkspacePackages?: string[]; @@ -33,7 +38,7 @@ export class Package { } } - get name() { + get name(): string { if (this.workspacePatterns.length > 0 && !this.packageJson.name) { return '(Root)'; } @@ -43,8 +48,8 @@ export class Package { return this.packageJson.name; } - // Relative to workspace root. - get pathRelative() { + /** Relative to workspace root. */ + get pathRelative(): string { return relative(this.pathWorkspace, this.path); } @@ -76,12 +81,15 @@ export class Package { return []; } - static exists(path: string) { + static exists(path: string): boolean { const packageJsonPath = join(path, 'package.json'); return existsSync(packageJsonPath); } - static some(packages: Package[], callback: (package_: Package) => boolean) { + static some( + packages: Package[], + callback: (package_: Package) => boolean + ): boolean { return packages.some((package_) => callback(package_)); } diff --git a/package.json b/package.json index ac61f9b7..3d64ffba 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,9 @@ }, "license": "MIT", "type": "module", + "exports": "./dist/lib/index.js", + "main": "./dist/lib/index.js", + "types": "./dist/lib/index.d.ts", "bin": { "check-dependency-version-consistency": "dist/bin/check-dependency-version-consistency.js" }, diff --git a/test/lib/check-test.ts b/test/lib/check-test.ts new file mode 100644 index 00000000..977ddd5f --- /dev/null +++ b/test/lib/check-test.ts @@ -0,0 +1,359 @@ +import { check } from '../../lib/check.js'; +import { + FIXTURE_PATH_VALID, + FIXTURE_PATH_INCONSISTENT_VERSIONS, +} from '../fixtures/index.js'; +import mockFs from 'mock-fs'; +import { readFileSync } from 'node:fs'; +import type { PackageJson } from 'type-fest'; +import { join } from 'node:path'; + +describe('Utils | check', function () { + describe('#check', function () { + it('behaves correctly with valid fixture', function () { + expect(check(FIXTURE_PATH_VALID)).toStrictEqual({ + mismatchingVersions: [], + mismatchingVersionsFixable: [], + mismatchingVersionsNotFixable: [], + }); + }); + + it('behaves correctly with invalid fixture', function () { + expect(check(FIXTURE_PATH_INCONSISTENT_VERSIONS)).toStrictEqual({ + mismatchingVersions: [ + { + dependency: 'baz', + versions: [ + { + version: '^7.8.9', + packages: [ + expect.objectContaining({ + path: join( + FIXTURE_PATH_INCONSISTENT_VERSIONS, + '@scope1', + 'package1' + ), + }), + ], + }, + { + version: '^8.0.0', + packages: [ + expect.objectContaining({ + path: join( + FIXTURE_PATH_INCONSISTENT_VERSIONS, + '@scope1', + 'package2' + ), + }), + ], + }, + ], + }, + { + dependency: 'foo', + versions: [ + { + version: '1.2.0', + packages: [ + expect.objectContaining({ + path: join(FIXTURE_PATH_INCONSISTENT_VERSIONS), + }), + expect.objectContaining({ + path: join( + FIXTURE_PATH_INCONSISTENT_VERSIONS, + '@scope1', + 'package2' + ), + }), + expect.objectContaining({ + path: join( + FIXTURE_PATH_INCONSISTENT_VERSIONS, + '@scope1', + 'package3' + ), + }), + ], + }, + { + version: '1.3.0', + packages: [ + expect.objectContaining({ + path: join( + FIXTURE_PATH_INCONSISTENT_VERSIONS, + '@scope1', + 'package1' + ), + }), + ], + }, + ], + }, + ], + mismatchingVersionsFixable: [ + { + dependency: 'baz', + versions: [ + { + version: '^7.8.9', + packages: [ + expect.objectContaining({ + path: join( + FIXTURE_PATH_INCONSISTENT_VERSIONS, + '@scope1', + 'package1' + ), + }), + ], + }, + { + version: '^8.0.0', + packages: [ + expect.objectContaining({ + path: join( + FIXTURE_PATH_INCONSISTENT_VERSIONS, + '@scope1', + 'package2' + ), + }), + ], + }, + ], + }, + { + dependency: 'foo', + versions: [ + { + version: '1.2.0', + packages: [ + expect.objectContaining({ + path: join(FIXTURE_PATH_INCONSISTENT_VERSIONS), + }), + expect.objectContaining({ + path: join( + FIXTURE_PATH_INCONSISTENT_VERSIONS, + '@scope1', + 'package2' + ), + }), + expect.objectContaining({ + path: join( + FIXTURE_PATH_INCONSISTENT_VERSIONS, + '@scope1', + 'package3' + ), + }), + ], + }, + { + version: '1.3.0', + packages: [ + expect.objectContaining({ + path: join( + FIXTURE_PATH_INCONSISTENT_VERSIONS, + '@scope1', + 'package1' + ), + }), + ], + }, + ], + }, + ], + mismatchingVersionsNotFixable: [], + }); + }); + + it('behaves correctly with invalid fixture and ignore patterns', function () { + expect( + check(FIXTURE_PATH_INCONSISTENT_VERSIONS, { + ignorePackagePattern: ['package3'], + ignorePathPattern: ['package3'], + ignoreDepPattern: ['foo'], + }) + ).toStrictEqual({ + mismatchingVersions: [expect.objectContaining({ dependency: 'baz' })], + mismatchingVersionsFixable: [ + expect.objectContaining({ dependency: 'baz' }), + ], + mismatchingVersionsNotFixable: [], + }); + }); + + describe('mocking the filesystem, with an unfixable version', function () { + let expectedPackage1: PackageJson; + let expectedPackage2: PackageJson; + beforeEach(function () { + // Create a mock workspace filesystem for temporary usage in this test because changes will be written to some files. + expectedPackage1 = { + name: 'package1', + dependencies: { + foo: '^1.0.0', + }, + }; + expectedPackage2 = { + name: 'package2', + dependencies: { + foo: '*', + }, + }; + mockFs({ + 'package.json': JSON.stringify({ + workspaces: ['*'], + }), + package1: { + 'package.json': JSON.stringify(expectedPackage1), + }, + package2: { + 'package.json': JSON.stringify(expectedPackage2), + }, + }); + }); + + afterEach(function () { + mockFs.restore(); + }); + + it('does not fix because of unfixable version and returns the unfixable list', function () { + expect(check('.', { fix: true })).toStrictEqual({ + mismatchingVersions: [expect.objectContaining({ dependency: 'foo' })], + mismatchingVersionsFixable: [], + mismatchingVersionsNotFixable: [ + expect.objectContaining({ + dependency: 'foo', + }), + ], + }); + + // Read in package.json files. + const packageJson1Contents = readFileSync( + 'package1/package.json', + 'utf8' + ); + const packageJson2Contents = readFileSync( + 'package2/package.json', + 'utf8' + ); + const actualPackageJson1: PackageJson = + JSON.parse(packageJson1Contents); + const actualPackageJson2: PackageJson = + JSON.parse(packageJson2Contents); + + expect(actualPackageJson1).toStrictEqual(expectedPackage1); + expect(actualPackageJson2).toStrictEqual(expectedPackage2); + }); + }); + + describe('mocking the filesystem, without fixing', function () { + let expectedPackage1: PackageJson; + let expectedPackage2: PackageJson; + beforeEach(function () { + // Create a mock workspace filesystem for temporary usage in this test because changes will be written to some files. + expectedPackage1 = { + name: 'package1', + dependencies: { + foo: '^1.0.0', + }, + }; + expectedPackage2 = { + name: 'package2', + dependencies: { + foo: '1.5.0', + }, + }; + mockFs({ + 'package.json': JSON.stringify({ + workspaces: ['*'], + }), + package1: { + 'package.json': JSON.stringify(expectedPackage1), + }, + package2: { + 'package.json': JSON.stringify(expectedPackage2), + }, + }); + }); + + afterEach(function () { + mockFs.restore(); + }); + + it('does not fix by default', function () { + check('.'); + + // Read in package.json files. + const packageJson1Contents = readFileSync( + 'package1/package.json', + 'utf8' + ); + const packageJson2Contents = readFileSync( + 'package2/package.json', + 'utf8' + ); + const actualPackageJson1: PackageJson = + JSON.parse(packageJson1Contents); + const actualPackageJson2: PackageJson = + JSON.parse(packageJson2Contents); + + expect(actualPackageJson1).toStrictEqual(expectedPackage1); + expect(actualPackageJson2).toStrictEqual(expectedPackage2); + }); + }); + + describe('mocking the filesystem, with fixing', function () { + beforeEach(function () { + // Create a mock workspace filesystem for temporary usage in this test because changes will be written to some files. + mockFs({ + 'package.json': JSON.stringify({ + workspaces: ['*'], + }), + package1: { + 'package.json': JSON.stringify({ + name: 'package1', + dependencies: { + foo: '^1.0.0', + }, + }), + }, + package2: { + 'package.json': JSON.stringify({ + name: 'package2', + dependencies: { + foo: '1.5.0', + }, + }), + }, + }); + }); + + afterEach(function () { + mockFs.restore(); + }); + + it('fixes when option provided', function () { + check('.', { fix: true }); + + // Read in package.json files. + const packageJson1Contents = readFileSync( + 'package1/package.json', + 'utf8' + ); + const packageJson2Contents = readFileSync( + 'package2/package.json', + 'utf8' + ); + const actualPackageJson1: PackageJson = + JSON.parse(packageJson1Contents); + const actualPackageJson2: PackageJson = + JSON.parse(packageJson2Contents); + + expect( + actualPackageJson1.dependencies && actualPackageJson1.dependencies.foo + ).toStrictEqual('^1.5.0'); + expect( + actualPackageJson2.dependencies && actualPackageJson2.dependencies.foo + ).toStrictEqual('^1.5.0'); + }); + }); + }); +}); diff --git a/test/lib/dependency-versions-test.ts b/test/lib/dependency-versions-test.ts index 5a023530..51315e2b 100644 --- a/test/lib/dependency-versions-test.ts +++ b/test/lib/dependency-versions-test.ts @@ -460,7 +460,7 @@ describe('Utils | dependency-versions', function () { const mismatchingVersions = calculateMismatchingVersions( calculateVersionsForEachDependency(packages) ); - const { fixed, notFixed } = fixMismatchingVersions( + const { fixable, notFixable } = fixMismatchingVersions( packages, mismatchingVersions ); @@ -540,9 +540,9 @@ describe('Utils | dependency-versions', function () { ).toStrictEqual('1.0.1'); // Check return value. - expect(notFixed).toStrictEqual([ + expect(notFixable).toStrictEqual([ { - // Should not be fixed due to the abnormal version present. + // Should not be fixable due to the abnormal version present. dependency: 'bar', versions: [ { @@ -565,7 +565,7 @@ describe('Utils | dependency-versions', function () { }, ]); - expect(fixed).toStrictEqual([ + expect(fixable).toStrictEqual([ { dependency: '@types/one', versions: [ @@ -706,7 +706,7 @@ describe('Utils | dependency-versions', function () { const mismatchingVersions = calculateMismatchingVersions( calculateVersionsForEachDependency(packages) ); - const { fixed, notFixed } = fixMismatchingVersions( + const { fixable, notFixable } = fixMismatchingVersions( packages, mismatchingVersions ); @@ -740,9 +740,9 @@ describe('Utils | dependency-versions', function () { packageJson3.dependencies && packageJson3.dependencies['package1'] ).toStrictEqual('^0.0.0'); - expect(notFixed).toStrictEqual([ + expect(notFixable).toStrictEqual([ { - // Not fixed since found version higher than actual version of this local package. + // Not fixable since found version higher than actual version of this local package. dependency: 'package1', versions: [ { @@ -761,9 +761,9 @@ describe('Utils | dependency-versions', function () { }, ]); - expect(fixed).toStrictEqual([ + expect(fixable).toStrictEqual([ { - // Fixed since updated to actual version of this local package. + // Fixable since updated to actual version of this local package. dependency: 'package2', versions: [ { @@ -823,7 +823,7 @@ describe('Utils | dependency-versions', function () { const mismatchingVersions = calculateMismatchingVersions( calculateVersionsForEachDependency(packages) ); - const { fixed, notFixed } = fixMismatchingVersions( + const { fixable, notFixable } = fixMismatchingVersions( packages, mismatchingVersions ); @@ -852,9 +852,9 @@ describe('Utils | dependency-versions', function () { packageJson1.dependencies && packageJson1.dependencies['foo'] ).toStrictEqual('^2.0.0'); - expect(notFixed).toStrictEqual([]); + expect(notFixable).toStrictEqual([]); - expect(fixed).toStrictEqual([ + expect(fixable).toStrictEqual([ { dependency: 'foo', versions: [ diff --git a/test/lib/output-test.ts b/test/lib/output-test.ts index d248ac2f..7891d47a 100644 --- a/test/lib/output-test.ts +++ b/test/lib/output-test.ts @@ -3,8 +3,8 @@ import { calculateMismatchingVersions, } from '../../lib/dependency-versions.js'; import { - mismatchingVersionsToOutput, - mismatchingVersionsFixedToOutput, + mismatchingVersionsToDetailedSummary, + mismatchingVersionsToFixedSummary, } from '../../lib/output.js'; import { getPackages } from '../../lib/workspace.js'; import { @@ -14,10 +14,10 @@ import { } from '../fixtures/index.js'; describe('Utils | output', function () { - describe('#mismatchingVersionsToOutputLines', function () { + describe('#mismatchingVersionsToDetailedSummary', function () { it('behaves correctly', function () { expect( - mismatchingVersionsToOutput( + mismatchingVersionsToDetailedSummary( calculateMismatchingVersions( calculateVersionsForEachDependency( getPackages(FIXTURE_PATH_TESTING_OUTPUT, [], [], [], []) @@ -62,7 +62,7 @@ describe('Utils | output', function () { it('behaves correctly when package names do not match locations', function () { expect( - mismatchingVersionsToOutput( + mismatchingVersionsToDetailedSummary( calculateMismatchingVersions( calculateVersionsForEachDependency( getPackages( @@ -90,17 +90,17 @@ describe('Utils | output', function () { it('behaves correctly with empty input', function () { expect(() => - mismatchingVersionsToOutput([]) + mismatchingVersionsToDetailedSummary([]) ).toThrowErrorMatchingInlineSnapshot( '"No mismatching versions to output."' ); }); }); - describe('#mismatchingVersionsFixedToOutputLines', function () { + describe('#mismatchingVersionsToFixedSummary', function () { it('behaves correctly', function () { expect( - mismatchingVersionsFixedToOutput( + mismatchingVersionsToFixedSummary( calculateMismatchingVersions( calculateVersionsForEachDependency( getPackages(FIXTURE_PATH_TESTING_OUTPUT, [], [], [], []) @@ -114,7 +114,7 @@ describe('Utils | output', function () { it('behaves correctly with a single fix', function () { expect( - mismatchingVersionsFixedToOutput( + mismatchingVersionsToFixedSummary( calculateMismatchingVersions( calculateVersionsForEachDependency( getPackages(FIXTURE_PATH_TESTING_OUTPUT, [], [], [], []) @@ -126,7 +126,7 @@ describe('Utils | output', function () { it('behaves correctly with an increasable range', function () { expect( - mismatchingVersionsFixedToOutput( + mismatchingVersionsToFixedSummary( calculateMismatchingVersions( calculateVersionsForEachDependency( getPackages(FIXTURE_PATH_INCREASABLE_RANGE, [], [], [], []) @@ -138,7 +138,7 @@ describe('Utils | output', function () { it('behaves correctly with empty input', function () { expect(() => - mismatchingVersionsFixedToOutput([]) + mismatchingVersionsToFixedSummary([]) ).toThrowErrorMatchingInlineSnapshot('"No fixes to output."'); }); });