test: enable a few jest eslint rules (#6900)

* test: enable a few jest eslint rules

* more
This commit is contained in:
Joshua Chen 2022-03-12 08:43:09 +08:00 committed by GitHub
parent 1efc6c6091
commit aa5a2d4c04
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
155 changed files with 3644 additions and 3478 deletions

View file

@ -0,0 +1,78 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`postProcess corrects collapsed state inconsistencies 1`] = `
Object {
"sidebar": Array [
Object {
"collapsed": false,
"collapsible": false,
"items": Array [
Object {
"id": "foo",
"type": "doc",
},
],
"label": "Category",
"link": undefined,
"type": "category",
},
],
}
`;
exports[`postProcess corrects collapsed state inconsistencies 2`] = `
Object {
"sidebar": Array [
Object {
"collapsed": false,
"collapsible": false,
"items": Array [
Object {
"id": "foo",
"type": "doc",
},
],
"label": "Category",
"link": undefined,
"type": "category",
},
],
}
`;
exports[`postProcess corrects collapsed state inconsistencies 3`] = `
Object {
"sidebar": Array [
Object {
"collapsed": false,
"collapsible": false,
"items": Array [
Object {
"id": "foo",
"type": "doc",
},
],
"label": "Category",
"link": undefined,
"type": "category",
},
],
}
`;
exports[`postProcess transforms category without subitems 1`] = `
Object {
"sidebar": Array [
Object {
"href": "version/generated/permalink",
"label": "Category",
"type": "link",
},
Object {
"id": "doc ID",
"label": "Category 2",
"type": "doc",
},
],
}
`;

View file

@ -36,7 +36,7 @@ describe('DefaultSidebarItemsGenerator', () => {
});
}
test('generates empty sidebar slice when no docs and emit a warning', async () => {
it('generates empty sidebar slice when no docs and emit a warning', async () => {
const consoleWarn = jest.spyOn(console, 'warn');
const sidebarSlice = await testDefaultSidebarItemsGenerator({
docs: [],
@ -49,7 +49,7 @@ describe('DefaultSidebarItemsGenerator', () => {
);
});
test('generates simple flat sidebar', async () => {
it('generates simple flat sidebar', async () => {
const sidebarSlice = await DefaultSidebarItemsGenerator({
numberPrefixParser: DefaultNumberPrefixParser,
item: {
@ -108,7 +108,7 @@ describe('DefaultSidebarItemsGenerator', () => {
expect(sidebarSlice).toMatchSnapshot();
});
test('generates complex nested sidebar', async () => {
it('generates complex nested sidebar', async () => {
const sidebarSlice = await DefaultSidebarItemsGenerator({
numberPrefixParser: DefaultNumberPrefixParser,
isCategoryIndex,
@ -212,7 +212,7 @@ describe('DefaultSidebarItemsGenerator', () => {
expect(sidebarSlice).toMatchSnapshot();
});
test('generates subfolder sidebar', async () => {
it('generates subfolder sidebar', async () => {
// Ensure that category metadata file is correctly read
// fix edge case found in https://github.com/facebook/docusaurus/issues/4638
const sidebarSlice = await DefaultSidebarItemsGenerator({
@ -308,7 +308,7 @@ describe('DefaultSidebarItemsGenerator', () => {
expect(sidebarSlice).toMatchSnapshot();
});
test('uses explicit link over the index/readme.{md,mdx} naming convention', async () => {
it('uses explicit link over the index/readme.{md,mdx} naming convention', async () => {
const sidebarSlice = await DefaultSidebarItemsGenerator({
numberPrefixParser: DefaultNumberPrefixParser,
item: {
@ -379,7 +379,7 @@ describe('DefaultSidebarItemsGenerator', () => {
expect(sidebarSlice).toMatchSnapshot();
});
test('respects custom isCategoryIndex', async () => {
it('respects custom isCategoryIndex', async () => {
const sidebarSlice = await DefaultSidebarItemsGenerator({
numberPrefixParser: DefaultNumberPrefixParser,
isCategoryIndex({fileName, directories}) {
@ -462,7 +462,7 @@ describe('DefaultSidebarItemsGenerator', () => {
expect(sidebarSlice).toMatchSnapshot();
});
test('throws for unknown index link', async () => {
it('throws for unknown index link', async () => {
const generateSidebar = () =>
DefaultSidebarItemsGenerator({
numberPrefixParser: DefaultNumberPrefixParser,

View file

@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/
import {jest} from '@jest/globals';
import path from 'path';
import {loadSidebars, DisabledSidebars} from '../index';
import type {SidebarProcessorParams} from '../types';
@ -30,19 +31,19 @@ describe('loadSidebars', () => {
categoryLabelSlugger: null,
sidebarOptions: {sidebarCollapsed: true, sidebarCollapsible: true},
};
test('sidebars with known sidebar item type', async () => {
it('sidebars with known sidebar item type', async () => {
const sidebarPath = path.join(fixtureDir, 'sidebars.json');
const result = await loadSidebars(sidebarPath, params);
expect(result).toMatchSnapshot();
});
test('sidebars with deep level of category', async () => {
it('sidebars with deep level of category', async () => {
const sidebarPath = path.join(fixtureDir, 'sidebars-category.js');
const result = await loadSidebars(sidebarPath, params);
expect(result).toMatchSnapshot();
});
test('sidebars shorthand and longform lead to exact same sidebar', async () => {
it('sidebars shorthand and longform lead to exact same sidebar', async () => {
const sidebarPath1 = path.join(fixtureDir, 'sidebars-category.js');
const sidebarPath2 = path.join(
fixtureDir,
@ -53,7 +54,7 @@ describe('loadSidebars', () => {
expect(sidebar1).toEqual(sidebar2);
});
test('sidebars with category but category.items is not an array', async () => {
it('sidebars with category but category.items is not an array', async () => {
const sidebarPath = path.join(
fixtureDir,
'sidebars-category-wrong-items.json',
@ -65,7 +66,7 @@ describe('loadSidebars', () => {
);
});
test('sidebars with first level not a category', async () => {
it('sidebars with first level not a category', async () => {
const sidebarPath = path.join(
fixtureDir,
'sidebars-first-level-not-category.js',
@ -74,35 +75,35 @@ describe('loadSidebars', () => {
expect(result).toMatchSnapshot();
});
test('sidebars link', async () => {
it('sidebars link', async () => {
const sidebarPath = path.join(fixtureDir, 'sidebars-link.json');
const result = await loadSidebars(sidebarPath, params);
expect(result).toMatchSnapshot();
});
test('unexisting path', async () => {
it('unexisting path', async () => {
await expect(loadSidebars('badpath', params)).resolves.toEqual(
DisabledSidebars,
);
});
test('undefined path', async () => {
it('undefined path', async () => {
await expect(loadSidebars(undefined, params)).resolves.toMatchSnapshot();
});
test('literal false path', async () => {
it('literal false path', async () => {
await expect(loadSidebars(false, params)).resolves.toEqual(
DisabledSidebars,
);
});
test('sidebars with category.collapsed property', async () => {
it('sidebars with category.collapsed property', async () => {
const sidebarPath = path.join(fixtureDir, 'sidebars-collapsed.json');
const result = await loadSidebars(sidebarPath, params);
expect(result).toMatchSnapshot();
});
test('sidebars with category.collapsed property at first level', async () => {
it('sidebars with category.collapsed property at first level', async () => {
const sidebarPath = path.join(
fixtureDir,
'sidebars-collapsed-first-level.json',
@ -111,7 +112,7 @@ describe('loadSidebars', () => {
expect(result).toMatchSnapshot();
});
test('duplicate category metadata files', async () => {
it('duplicate category metadata files', async () => {
const sidebarPath = path.join(
fixtureDir,
'sidebars-collapsed-first-level.json',

View file

@ -8,7 +8,7 @@
import {normalizeSidebars} from '../normalization';
describe('normalization', () => {
test('normalizes shorthands', () => {
it('normalizes shorthands', () => {
expect(
normalizeSidebars({
sidebar: {
@ -37,7 +37,7 @@ describe('normalization', () => {
}),
).toMatchSnapshot();
});
test('rejects some invalid cases', () => {
it('rejects some invalid cases', () => {
expect(() =>
normalizeSidebars({
sidebar: {

View file

@ -8,7 +8,7 @@
import {postProcessSidebars} from '../postProcessor';
describe('postProcess', () => {
test('transforms category without subitems', () => {
it('transforms category without subitems', () => {
const processedSidebar = postProcessSidebars(
{
sidebar: [
@ -38,22 +38,7 @@ describe('postProcess', () => {
},
);
expect(processedSidebar).toMatchInlineSnapshot(`
Object {
"sidebar": Array [
Object {
"href": "version/generated/permalink",
"label": "Category",
"type": "link",
},
Object {
"id": "doc ID",
"label": "Category 2",
"type": "doc",
},
],
}
`);
expect(processedSidebar).toMatchSnapshot();
expect(() => {
postProcessSidebars(
@ -76,7 +61,7 @@ describe('postProcess', () => {
);
});
test('corrects collapsed state inconsistencies', () => {
it('corrects collapsed state inconsistencies', () => {
expect(
postProcessSidebars(
{
@ -96,25 +81,7 @@ describe('postProcess', () => {
version: {versionPath: 'version'},
},
),
).toMatchInlineSnapshot(`
Object {
"sidebar": Array [
Object {
"collapsed": false,
"collapsible": false,
"items": Array [
Object {
"id": "foo",
"type": "doc",
},
],
"label": "Category",
"link": undefined,
"type": "category",
},
],
}
`);
).toMatchSnapshot();
expect(
postProcessSidebars(
@ -134,25 +101,7 @@ describe('postProcess', () => {
version: {versionPath: 'version'},
},
),
).toMatchInlineSnapshot(`
Object {
"sidebar": Array [
Object {
"collapsed": false,
"collapsible": false,
"items": Array [
Object {
"id": "foo",
"type": "doc",
},
],
"label": "Category",
"link": undefined,
"type": "category",
},
],
}
`);
).toMatchSnapshot();
expect(
postProcessSidebars(
@ -171,24 +120,6 @@ describe('postProcess', () => {
version: {versionPath: 'version'},
},
),
).toMatchInlineSnapshot(`
Object {
"sidebar": Array [
Object {
"collapsed": false,
"collapsible": false,
"items": Array [
Object {
"id": "foo",
"type": "doc",
},
],
"label": "Category",
"link": undefined,
"type": "category",
},
],
}
`);
).toMatchSnapshot();
});
});

View file

@ -66,7 +66,7 @@ describe('processSidebars', () => {
});
}
test('let sidebars without autogenerated items untouched', async () => {
it('leaves sidebars without autogenerated items untouched', async () => {
const unprocessedSidebars: NormalizedSidebars = {
someSidebar: [
{type: 'doc', id: 'doc1'},
@ -96,7 +96,7 @@ describe('processSidebars', () => {
expect(processedSidebar).toEqual(unprocessedSidebars);
});
test('replace autogenerated items by generated sidebars slices', async () => {
it('replaces autogenerated items by generated sidebars slices', async () => {
const unprocessedSidebars: NormalizedSidebars = {
someSidebar: [
{type: 'doc', id: 'doc1'},
@ -199,7 +199,7 @@ describe('processSidebars', () => {
} as ProcessedSidebars);
});
test('ensure generated items are normalized', async () => {
it('ensures generated items are normalized', async () => {
const sidebarSliceContainingCategoryGeneratedIndex: NormalizedSidebar = [
{
type: 'category',

View file

@ -131,11 +131,11 @@ describe('createSidebarsUtils', () => {
getFirstLink,
} = createSidebarsUtils(sidebars);
test('getFirstDocIdOfFirstSidebar', async () => {
it('getFirstDocIdOfFirstSidebar', async () => {
expect(getFirstDocIdOfFirstSidebar()).toEqual('doc1');
});
test('getSidebarNameByDocId', async () => {
it('getSidebarNameByDocId', async () => {
expect(getSidebarNameByDocId('doc1')).toEqual('sidebar1');
expect(getSidebarNameByDocId('doc2')).toEqual('sidebar1');
expect(getSidebarNameByDocId('doc3')).toEqual('sidebar2');
@ -146,7 +146,7 @@ describe('createSidebarsUtils', () => {
expect(getSidebarNameByDocId('unknown_id')).toEqual(undefined);
});
test('getDocNavigation', async () => {
it('getDocNavigation', async () => {
expect(getDocNavigation('doc1', 'doc1', undefined)).toEqual({
sidebarName: 'sidebar1',
previous: undefined,
@ -226,7 +226,7 @@ describe('createSidebarsUtils', () => {
});
});
test('getCategoryGeneratedIndexNavigation', async () => {
it('getCategoryGeneratedIndexNavigation', async () => {
expect(
getCategoryGeneratedIndexNavigation('/s3-subcategory-index-permalink'),
).toMatchObject({
@ -256,7 +256,7 @@ describe('createSidebarsUtils', () => {
});
});
test('getCategoryGeneratedIndexList', async () => {
it('getCategoryGeneratedIndexList', async () => {
expect(getCategoryGeneratedIndexList()).toMatchObject([
{
type: 'category',
@ -273,7 +273,7 @@ describe('createSidebarsUtils', () => {
]);
});
test('getFirstLink', () => {
it('getFirstLink', () => {
expect(getFirstLink('sidebar1')).toEqual({
id: 'doc1',
type: 'doc',
@ -298,7 +298,7 @@ describe('createSidebarsUtils', () => {
});
describe('collectSidebarDocItems', () => {
test('can collect docs', async () => {
it('can collect docs', async () => {
const sidebar: Sidebar = [
{
type: 'category',
@ -354,7 +354,7 @@ describe('collectSidebarDocItems', () => {
});
describe('collectSidebarCategories', () => {
test('can collect categories', async () => {
it('can collect categories', async () => {
const sidebar: Sidebar = [
{
type: 'category',
@ -412,7 +412,7 @@ describe('collectSidebarCategories', () => {
});
describe('collectSidebarLinks', () => {
test('can collect links', async () => {
it('can collect links', async () => {
const sidebar: Sidebar = [
{
type: 'category',
@ -450,7 +450,7 @@ describe('collectSidebarLinks', () => {
});
describe('collectSidebarsDocIds', () => {
test('can collect sidebars doc items', async () => {
it('can collect sidebars doc items', async () => {
const sidebar1: Sidebar = [
{
type: 'category',
@ -496,7 +496,7 @@ describe('collectSidebarsDocIds', () => {
});
describe('transformSidebarItems', () => {
test('can transform sidebar items', async () => {
it('can transform sidebar items', async () => {
const sidebar: Sidebar = [
{
type: 'category',
@ -606,7 +606,7 @@ describe('toDocNavigationLink', () => {
return data as DocMetadataBase;
}
test('with no front matter', () => {
it('with no front matter', () => {
expect(
toDocNavigationLink(
testDoc({
@ -621,7 +621,7 @@ describe('toDocNavigationLink', () => {
} as DocNavLink);
});
test('with pagination_label front matter', () => {
it('with pagination_label front matter', () => {
expect(
toDocNavigationLink(
testDoc({
@ -638,7 +638,7 @@ describe('toDocNavigationLink', () => {
} as DocNavLink);
});
test('with sidebar_label front matter', () => {
it('with sidebar_label front matter', () => {
expect(
toDocNavigationLink(
testDoc({
@ -655,7 +655,7 @@ describe('toDocNavigationLink', () => {
} as DocNavLink);
});
test('with pagination_label + sidebar_label front matter', () => {
it('with pagination_label + sidebar_label front matter', () => {
expect(
toDocNavigationLink(
testDoc({
@ -691,7 +691,7 @@ describe('toNavigationLink', () => {
}),
};
test('with doc items', () => {
it('with doc items', () => {
expect(toNavigationLink({type: 'doc', id: 'doc1'}, docsById)).toEqual(
toDocNavigationLink(docsById.doc1),
);
@ -705,7 +705,7 @@ describe('toNavigationLink', () => {
);
});
test('with category item and doc link', () => {
it('with category item and doc link', () => {
expect(
toNavigationLink(
{
@ -742,7 +742,7 @@ describe('toNavigationLink', () => {
);
});
test('with category item and generated-index link', () => {
it('with category item and generated-index link', () => {
expect(
toNavigationLink(
{

View file

@ -9,7 +9,7 @@ import {validateSidebars, validateCategoryMetadataFile} from '../validation';
import type {SidebarsConfig, CategoryMetadataFile} from '../types';
describe('validateSidebars', () => {
test('throw for bad value', async () => {
it('throw for bad value', async () => {
expect(() => validateSidebars({sidebar: [{type: 42}]}))
.toThrowErrorMatchingInlineSnapshot(`
"{
@ -21,12 +21,12 @@ describe('validateSidebars', () => {
`);
});
test('accept empty object', async () => {
it('accept empty object', async () => {
const sidebars: SidebarsConfig = {};
validateSidebars(sidebars);
});
test('accept valid values', async () => {
it('accept valid values', async () => {
const sidebars: SidebarsConfig = {
sidebar1: [
{type: 'doc', id: 'doc1'},
@ -41,17 +41,19 @@ describe('validateSidebars', () => {
validateSidebars(sidebars);
});
test('sidebar category wrong label', () => {
expect(() =>
validateSidebars({
docs: [
{
type: 'category',
label: true,
items: [{type: 'doc', id: 'doc1'}],
},
],
}),
it('sidebar category wrong label', () => {
expect(
() =>
validateSidebars({
docs: [
{
type: 'category',
label: true,
items: [{type: 'doc', id: 'doc1'}],
},
],
}),
// eslint-disable-next-line jest/no-large-snapshots
).toThrowErrorMatchingInlineSnapshot(`
"{
\\"type\\": \\"category\\",
@ -68,7 +70,7 @@ describe('validateSidebars', () => {
`);
});
test('sidebars link wrong label', () => {
it('sidebars link wrong label', () => {
expect(() =>
validateSidebars({
docs: [
@ -90,7 +92,7 @@ describe('validateSidebars', () => {
`);
});
test('sidebars link wrong href', () => {
it('sidebars link wrong href', () => {
expect(() =>
validateSidebars({
docs: [
@ -114,7 +116,7 @@ describe('validateSidebars', () => {
`);
});
test('sidebars with unknown sidebar item type', () => {
it('sidebars with unknown sidebar item type', () => {
expect(() =>
validateSidebars({
docs: [
@ -133,7 +135,7 @@ describe('validateSidebars', () => {
`);
});
test('sidebars category missing items', () => {
it('sidebars category missing items', () => {
expect(() =>
validateSidebars({
docs: [
@ -159,7 +161,7 @@ describe('validateSidebars', () => {
`);
});
test('sidebars category wrong field', () => {
it('sidebars category wrong field', () => {
expect(() =>
validateSidebars({
docs: [
@ -188,7 +190,7 @@ describe('validateSidebars', () => {
`);
});
test('sidebar category wrong items', () => {
it('sidebar category wrong items', () => {
expect(() =>
validateSidebars({
docs: {
@ -204,7 +206,7 @@ describe('validateSidebars', () => {
).toThrowErrorMatchingInlineSnapshot(`"sidebar.forEach is not a function"`);
});
test('sidebars item doc but id is not a string', async () => {
it('sidebars item doc but id is not a string', async () => {
expect(() =>
validateSidebars({
docs: [
@ -226,7 +228,7 @@ describe('validateSidebars', () => {
`);
});
test('HTML type requires a value', () => {
it('hTML type requires a value', () => {
const sidebars: SidebarsConfig = {
sidebar1: [
{
@ -246,7 +248,7 @@ describe('validateSidebars', () => {
`);
});
test('HTML type accepts valid values', () => {
it('hTML type accepts valid values', () => {
const sidebars: SidebarsConfig = {
sidebar1: [
{
@ -264,7 +266,7 @@ describe('validateSidebars', () => {
describe('validateCategoryMetadataFile', () => {
// TODO add more tests
test('throw for bad value', async () => {
it('throw for bad value', async () => {
expect(() =>
validateCategoryMetadataFile(42),
).toThrowErrorMatchingInlineSnapshot(
@ -272,12 +274,12 @@ describe('validateCategoryMetadataFile', () => {
);
});
test('accept empty object', async () => {
it('accept empty object', async () => {
const content: CategoryMetadataFile = {};
expect(validateCategoryMetadataFile(content)).toEqual(content);
});
test('accept valid values', async () => {
it('accept valid values', async () => {
const content: CategoryMetadataFile = {
className: 'className',
label: 'Category Label',
@ -294,7 +296,7 @@ describe('validateCategoryMetadataFile', () => {
expect(validateCategoryMetadataFile(content)).toEqual(content);
});
test('rejects permalink', async () => {
it('rejects permalink', async () => {
const content: CategoryMetadataFile = {
className: 'className',
label: 'Category Label',