renovate/test/website-docs.spec.ts

118 lines
3.6 KiB
TypeScript
Raw Normal View History

/* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */
import fs from 'node:fs';
import is from '@sindresorhus/is';
import { getOptions } from '../lib/config/options';
2020-03-07 10:27:10 +00:00
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace jest {
type ContainsOption<T> = T extends ArrayLike<unknown> ? T[number] : unknown;
interface Matchers<R> {
2020-03-07 10:27:10 +00:00
/**
* only available in `test/website-docs.spec.js`
* @param arg Value which current values should contain
*/
toContainOption(arg: ContainsOption<R>): void;
2020-03-07 10:27:10 +00:00
}
}
}
const options = getOptions();
describe('website-docs', () => {
2019-09-09 02:28:25 +00:00
const doc = fs.readFileSync('docs/usage/configuration-options.md', 'utf8');
const selfHostDoc = fs.readFileSync(
2019-09-09 02:28:25 +00:00
'docs/usage/self-hosted-configuration.md',
'utf8'
);
const headers = doc
.match(/\n## (.*?)\n/g)
?.map((match) => match.substring(4, match.length - 1));
const selfHostHeaders = selfHostDoc
.match(/\n## (.*?)\n/g)
?.map((match) => match.substring(4, match.length - 1));
const expectedOptions = options
2021-08-15 06:34:54 +00:00
.filter((option) => !option.globalOnly)
.filter((option) => !option.parent)
.filter((option) => !option.autogenerated)
.map((option) => option.name)
.sort();
const selfHostExpectedOptions = options
.filter((option) => !!option.globalOnly)
.map((option) => option.name)
.sort();
it('has doc headers sorted alphabetically', () => {
expect(headers).toEqual([...headers!].sort());
});
it('has headers for every required option', () => {
expect(headers).toEqual(expectedOptions);
});
it('has self hosted doc headers sorted alphabetically', () => {
expect(selfHostHeaders).toEqual([...selfHostHeaders!].sort());
});
it('has headers (self hosted) for every required option', () => {
expect(selfHostHeaders).toEqual(selfHostExpectedOptions);
});
const headers3 = doc
.match(/\n### (.*?)\n/g)
?.map((match) => match.substring(5, match.length - 1));
headers3!.sort();
const expectedOptions3 = options
.filter((option) => option.stage !== 'global')
2021-08-15 06:34:54 +00:00
.filter((option) => !option.globalOnly)
.filter((option) => option.parent)
.map((option) => option.name)
.sort();
expectedOptions3.sort();
it('has headers for every required sub-option', () => {
expect(headers3).toEqual(expectedOptions3);
});
2018-05-11 15:36:34 +00:00
// Checking relatedOptions field in options
2020-03-07 10:27:10 +00:00
const relatedOptionsMatrix = options
.map((option) => option.relatedOptions)
.filter(is.truthy)
2018-05-11 15:36:34 +00:00
.sort();
let relatedOptions = ([] as string[]).concat(...relatedOptionsMatrix!); // Converts the matrix to an 1D array
2018-05-11 15:36:34 +00:00
relatedOptions = [...new Set(relatedOptions)]; // Makes all options unique
/*
Matcher which checks if the argument is within the received array (or string)
on an error, it throws a custom message.
*/
expect.extend({
toContainOption<T extends string>(received: T[], argument: T) {
2018-05-11 15:36:34 +00:00
if (received.includes(argument)) {
return {
message: (): string =>
`Option "${argument}" should be within options`,
2018-05-11 15:36:34 +00:00
pass: true,
};
}
return {
message: (): string =>
`Option "${argument}" doesn't exist within options`,
2018-05-11 15:36:34 +00:00
pass: false,
};
},
});
const allOptionNames = options.map((option) => option.name).sort();
2018-05-11 15:36:34 +00:00
// Lists through each option in the relatedOptions array to be able to locate the exact element which causes error, in case of one
it('has valid relateOptions values', () => {
relatedOptions.forEach((relOption) => {
2018-05-11 15:36:34 +00:00
expect(allOptionNames).toContainOption(relOption);
});
});
});