Files
renovate/lib/workers/global/config/parse/util.spec.ts
Jamie Tanna ac013be4e2 fix(workers/global): parse .jsonc files (#44034)
* fix(workers/global): parse `.jsonc` files

As noted in #44033, this leads to cases where
`renovate-config-validator` can't parse a repo's `renovate.jsonc`.

This doesn't currently affect when Renovate itself runs fully.

* test(workers/global): add coverage for `getParsedContent`

This test also validates that the fix introduced in the previous commit
correctly fixes the issue.

This also makes sure that if we change the default JSON-like filenames,
we'll validate that it can be parsed, based on the filename.

Co-authored-by: Claude Sonnet 4.6 <jamie.tanna+claude-code@mend.io>

---------

Co-authored-by: Claude Sonnet 4.6 <jamie.tanna+claude-code@mend.io>
2026-06-16 13:45:14 +00:00

55 lines
1.7 KiB
TypeScript

import { getConfigFileNames } from '../../../../config/app-strings.ts';
import { logger } from '../../../../logger/index.ts';
import { readSystemFile } from '../../../../util/fs/index.ts';
import { getParsedContent, migrateAndValidateConfig } from './util.ts';
vi.mock('../../../../util/fs/index.ts');
describe('workers/global/config/parse/util', () => {
describe('getParsedContent()', () => {
it.each(getConfigFileNames())('parses %s', async (file) => {
vi.mocked(readSystemFile).mockResolvedValueOnce('{"token":"abc"}');
const result = await getParsedContent(file);
expect(result).toMatchObject({ token: 'abc' });
});
it.each`
file | content
${'config.jsonc'} | ${'{"token":"abc"} // comment'}
${'config.json5'} | ${'{"token":"abc",}'}
${'config.yaml'} | ${'token: abc'}
${'config.yml'} | ${'token: abc'}
`('parses $file', async ({ file, content }) => {
vi.mocked(readSystemFile).mockResolvedValueOnce(content);
const result = await getParsedContent(file);
expect(result).toMatchObject({ token: 'abc' });
});
it('throws for unsupported file type', async () => {
await expect(getParsedContent('config.txt')).rejects.toThrow(
'Unsupported file type',
);
});
});
it('massages config', async () => {
const config = {
packageRules: [
{
description: 'haha',
matchPackageNames: ['name'],
enabled: false,
},
],
};
const migratedConfig = await migrateAndValidateConfig(config, 'global');
expect(migratedConfig?.packageRules?.[0].description).toBeArray();
expect(logger.warn).toHaveBeenCalledTimes(0);
});
});