2022-04-24 22:48:54 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */
|
2023-03-28 21:05:36 +00:00
|
|
|
import fs from 'node:fs';
|
2022-04-24 22:48:54 +00:00
|
|
|
import is from '@sindresorhus/is';
|
2021-08-15 05:25:30 +00:00
|
|
|
import { getOptions } from '../lib/config/options';
|
2019-09-25 09:40:58 +00:00
|
|
|
|
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;
|
2022-09-12 14:58:52 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2022-09-12 14:58:52 +00:00
|
|
|
toContainOption(arg: ContainsOption<R>): void;
|
2020-03-07 10:27:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const options = getOptions();
|
2017-12-26 04:40:14 +00:00
|
|
|
|
2021-08-18 05:46:56 +00:00
|
|
|
describe('website-docs', () => {
|
2019-09-09 02:28:25 +00:00
|
|
|
const doc = fs.readFileSync('docs/usage/configuration-options.md', 'utf8');
|
2018-05-07 10:57:31 +00:00
|
|
|
const selfHostDoc = fs.readFileSync(
|
2019-09-09 02:28:25 +00:00
|
|
|
'docs/usage/self-hosted-configuration.md',
|
2018-05-07 10:57:31 +00:00
|
|
|
'utf8'
|
|
|
|
);
|
2017-12-26 04:40:14 +00:00
|
|
|
const headers = doc
|
|
|
|
.match(/\n## (.*?)\n/g)
|
2022-04-24 22:48:54 +00:00
|
|
|
?.map((match) => match.substring(4, match.length - 1));
|
2018-05-07 10:57:31 +00:00
|
|
|
const selfHostHeaders = selfHostDoc
|
|
|
|
.match(/\n## (.*?)\n/g)
|
2022-04-24 22:48:54 +00:00
|
|
|
?.map((match) => match.substring(4, match.length - 1));
|
2017-12-26 04:40:14 +00:00
|
|
|
const expectedOptions = options
|
2021-08-15 06:34:54 +00:00
|
|
|
.filter((option) => !option.globalOnly)
|
2020-04-12 16:09:36 +00:00
|
|
|
.filter((option) => !option.parent)
|
|
|
|
.filter((option) => !option.autogenerated)
|
|
|
|
.map((option) => option.name)
|
2017-12-26 04:40:14 +00:00
|
|
|
.sort();
|
2018-05-07 10:57:31 +00:00
|
|
|
|
|
|
|
const selfHostExpectedOptions = options
|
2022-06-24 09:32:41 +00:00
|
|
|
.filter((option) => !!option.globalOnly)
|
2020-04-12 16:09:36 +00:00
|
|
|
.map((option) => option.name)
|
2018-05-07 10:57:31 +00:00
|
|
|
.sort();
|
|
|
|
|
2017-12-26 04:40:14 +00:00
|
|
|
it('has doc headers sorted alphabetically', () => {
|
2022-04-24 22:48:54 +00:00
|
|
|
expect(headers).toEqual([...headers!].sort());
|
2017-12-26 04:40:14 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2017-12-26 04:40:14 +00:00
|
|
|
it('has headers for every required option', () => {
|
|
|
|
expect(headers).toEqual(expectedOptions);
|
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2018-05-07 10:57:31 +00:00
|
|
|
it('has self hosted doc headers sorted alphabetically', () => {
|
2022-04-24 22:48:54 +00:00
|
|
|
expect(selfHostHeaders).toEqual([...selfHostHeaders!].sort());
|
2018-05-07 10:57:31 +00:00
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2018-05-07 10:57:31 +00:00
|
|
|
it('has headers (self hosted) for every required option', () => {
|
|
|
|
expect(selfHostHeaders).toEqual(selfHostExpectedOptions);
|
|
|
|
});
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2018-05-06 07:34:19 +00:00
|
|
|
const headers3 = doc
|
|
|
|
.match(/\n### (.*?)\n/g)
|
2022-04-24 22:48:54 +00:00
|
|
|
?.map((match) => match.substring(5, match.length - 1));
|
|
|
|
headers3!.sort();
|
2018-05-06 07:34:19 +00:00
|
|
|
const expectedOptions3 = options
|
2020-04-12 16:09:36 +00:00
|
|
|
.filter((option) => option.stage !== 'global')
|
2021-08-15 06:34:54 +00:00
|
|
|
.filter((option) => !option.globalOnly)
|
2020-04-12 16:09:36 +00:00
|
|
|
.filter((option) => option.parent)
|
|
|
|
.map((option) => option.name)
|
2018-05-06 07:34:19 +00:00
|
|
|
.sort();
|
|
|
|
expectedOptions3.sort();
|
2022-04-12 14:49:49 +00:00
|
|
|
|
2018-05-06 07:34:19 +00:00
|
|
|
it('has headers for every required sub-option', () => {
|
|
|
|
expect(headers3).toEqual(expectedOptions3);
|
|
|
|
});
|
2018-05-11 15:36:34 +00:00
|
|
|
|
2021-08-15 05:25:30 +00:00
|
|
|
// Checking relatedOptions field in options
|
2020-03-07 10:27:10 +00:00
|
|
|
const relatedOptionsMatrix = options
|
2020-04-12 16:09:36 +00:00
|
|
|
.map((option) => option.relatedOptions)
|
2022-04-24 22:48:54 +00:00
|
|
|
.filter(is.truthy)
|
2018-05-11 15:36:34 +00:00
|
|
|
.sort();
|
|
|
|
|
2022-04-24 22:48:54 +00:00
|
|
|
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({
|
2020-08-19 04:46:00 +00:00
|
|
|
toContainOption<T extends string>(received: T[], argument: T) {
|
2018-05-11 15:36:34 +00:00
|
|
|
if (received.includes(argument)) {
|
|
|
|
return {
|
2019-11-24 07:43:24 +00:00
|
|
|
message: (): string =>
|
2021-08-15 05:25:30 +00:00
|
|
|
`Option "${argument}" should be within options`,
|
2018-05-11 15:36:34 +00:00
|
|
|
pass: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
2019-11-24 07:43:24 +00:00
|
|
|
message: (): string =>
|
2021-08-15 05:25:30 +00:00
|
|
|
`Option "${argument}" doesn't exist within options`,
|
2018-05-11 15:36:34 +00:00
|
|
|
pass: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-04-12 16:09:36 +00:00
|
|
|
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', () => {
|
2020-04-12 16:09:36 +00:00
|
|
|
relatedOptions.forEach((relOption) => {
|
2018-05-11 15:36:34 +00:00
|
|
|
expect(allOptionNames).toContainOption(relOption);
|
|
|
|
});
|
|
|
|
});
|
2017-12-26 04:40:14 +00:00
|
|
|
});
|