test(npm): add tests for pnpm catalog updates

This commit is contained in:
Fotis Papadogeorgopoulos 2025-01-02 10:59:52 +02:00
parent c9657a0040
commit ea5c14f003
No known key found for this signature in database
GPG key ID: 9E2557DCF51E95F8
2 changed files with 216 additions and 3 deletions

View file

@ -0,0 +1,204 @@
import { codeBlock } from 'common-tags';
import * as npmUpdater from '../..';
/**
* Per the YAML spec, a document ends with a newline. The 'yaml' library always
* uses that when serialising, but `codeBlock` strips the last indentation. This
* helper makes assertions simpler.
*/
function yamlCodeBlock(
literals: TemplateStringsArray,
...placeholders: any[]
): string {
return codeBlock(literals, placeholders) + '\n';
}
describe('modules/manager/npm/update/dependency/pnpm', () => {
it('handles implicit default catalog dependency', () => {
const upgrade = {
depType: 'pnpm.catalog',
depName: 'react',
newValue: '19.0.0',
managerData: {
catalogName: 'default',
},
};
const pnpmWorkspaceYaml = yamlCodeBlock`
packages:
- pkg-a
catalog:
react: 18.3.1
`;
const expected = yamlCodeBlock`
packages:
- pkg-a
catalog:
react: 19.0.0
`;
const testContent = npmUpdater.updateDependency({
fileContent: pnpmWorkspaceYaml,
upgrade,
});
expect(testContent).toEqual(expected);
});
it('handles explicit default catalog dependency', () => {
const upgrade = {
depType: 'pnpm.catalog',
depName: 'react',
newValue: '19.0.0',
managerData: {
catalogName: 'default',
},
};
const pnpmWorkspaceYaml = yamlCodeBlock`
packages:
- pkg-a
catalogs:
default:
react: 18.3.1
`;
const expected = yamlCodeBlock`
packages:
- pkg-a
catalogs:
default:
react: 19.0.0
`;
const testContent = npmUpdater.updateDependency({
fileContent: pnpmWorkspaceYaml,
upgrade,
});
expect(testContent).toEqual(expected);
});
it('handles explicit named catalog dependency', () => {
const upgrade = {
depType: 'pnpm.catalog',
depName: 'react',
newValue: '19.0.0',
managerData: {
catalogName: 'react17',
},
};
const pnpmWorkspaceYaml = yamlCodeBlock`
packages:
- pkg-a
catalog:
react: 18.3.1
catalogs:
react17:
react: 17.0.0
`;
const expected = yamlCodeBlock`
packages:
- pkg-a
catalog:
react: 18.3.1
catalogs:
react17:
react: 19.0.0
`;
const testContent = npmUpdater.updateDependency({
fileContent: pnpmWorkspaceYaml,
upgrade,
});
expect(testContent).toEqual(expected);
});
it.failing('replaces package', () => {
const upgrade = {
depType: 'pnpm.catalog',
depName: 'config',
newName: 'abc',
newValue: '2.0.0',
};
const pnpmWorkspaceYaml = yamlCodeBlock`
packages:
- pkg-a
catalog:
config: 1.21.0
`;
const expected = yamlCodeBlock`
packages:
- pkg-a
catalog:
abc: 2.0.0
`;
const testContent = npmUpdater.updateDependency({
fileContent: pnpmWorkspaceYaml,
upgrade,
});
expect(testContent).toEqual(expected);
});
it('returns null if the dependency is not present in the target catalog', () => {
const upgrade = {
depType: 'pnpm.catalog',
depName: 'react-not',
newValue: '19.0.0',
managerData: {
catalogName: 'default',
},
};
const pnpmWorkspaceYaml = yamlCodeBlock`
packages:
- pkg-a
catalog:
react: 18.3.1
`;
const testContent = npmUpdater.updateDependency({
fileContent: pnpmWorkspaceYaml,
upgrade,
});
expect(testContent).toBeNull();
});
it('returns null if catalogs are missing', () => {
const upgrade = {
depType: 'pnpm.catalog',
depName: 'react',
newValue: '19.0.0',
managerData: {
catalogName: 'default',
},
};
const pnpmWorkspaceYaml = yamlCodeBlock`
packages:
- pkg-a
`;
const testContent = npmUpdater.updateDependency({
fileContent: pnpmWorkspaceYaml,
upgrade,
});
expect(testContent).toBeNull();
});
it('returns null if empty file', () => {
const upgrade = {
depType: 'pnpm.catalog',
depName: 'react',
newValue: '19.0.0',
managerData: {
catalogName: 'default',
},
};
const testContent = npmUpdater.updateDependency({
fileContent: null as never,
upgrade,
});
expect(testContent).toBeNull();
});
});

View file

@ -1,4 +1,5 @@
import is from '@sindresorhus/is';
import { stringify } from 'yaml';
import { logger } from '../../../../../logger';
import { parseSingleYamlDocument } from '../../../../../util/yaml';
import type { UpdateDependencyConfig } from '../../../types';
@ -15,7 +16,7 @@ export function updatePnpmCatalogDependency({
if (!is.string(catalogName)) {
logger.error(
'No catalogName was found; this is likely an extractoin error.',
'No catalogName was found; this is likely an extraction error.',
);
return null;
}
@ -25,7 +26,7 @@ export function updatePnpmCatalogDependency({
newValue = getNewGitValue(upgrade) ?? newValue;
newValue = getNewNpmAliasValue(newValue, upgrade) ?? newValue;
logger.info(
logger.debug(
`npm.updatePnpmCatalogDependency(): ${depType}:${managerData?.catalogName}.${depName} = ${newValue}`,
);
@ -57,11 +58,19 @@ export function updatePnpmCatalogDependency({
return fileContent;
}
// TODO: handle depName === oldValue
// The old value is the name of the dependency itself
if (oldVersion === undefined) {
// There is some subtlety here
return null;
}
if (catalogName === 'default' && usesImplicitDefaultCatalog) {
document.setIn(['catalog', depName], newValue);
} else {
document.setIn(['catalogs', catalogName, depName], newValue);
}
return document.toString();
return stringify(document);
}