Skip to content

Commit

Permalink
add workaround to make tests happy
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Aug 23, 2024
1 parent 1402352 commit c12cbfa
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
Binary file modified packages/docusaurus-plugin-content-docs/src/__tests__/cli.test.ts
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import pluginContentDocs from '../index';
import {toSidebarsProp} from '../props';
import {DefaultSidebarItemsGenerator} from '../sidebars/generator';
import {DisabledSidebars} from '../sidebars';
import * as cliDocs from '../cli';
import cliDocs from '../cli';
import {validateOptions} from '../options';

import type {RouteConfig, Validate, Plugin} from '@docusaurus/types';
Expand Down
16 changes: 15 additions & 1 deletion packages/docusaurus-plugin-content-docs/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async function createVersionedSidebarFile({
}

// Tests depend on non-default export for mocking.
export async function cliDocsVersionCommand(
async function cliDocsVersionCommand(
version: unknown,
{id: pluginId, path: docsPath, sidebarPath}: PluginOptions,
{siteDir, i18n}: LoadContext,
Expand Down Expand Up @@ -142,3 +142,17 @@ export async function cliDocsVersionCommand(

logger.success`name=${pluginIdLogPrefix}: version name=${version} created!`;
}

// TODO try to remove this workaround
// Why use a default export instead of named exports here?
// This is only to make Jest mocking happy
// After upgrading Jest/SWC we got this weird mocking error in extendCli tests
// "spyOn: Cannot redefine property cliDocsVersionCommand"
// I tried various workarounds, and it's the only one that worked :/
// See also:
// - https://pyk.sh/fixing-typeerror-cannot-redefine-property-x-error-in-jest-tests#heading-solution-2-using-barrel-imports
// - https://github.com/aelbore/esbuild-jest/issues/26
// - https://stackoverflow.com/questions/67872622/jest-spyon-not-working-on-index-file-cannot-redefine-property/69951703#69951703
export default {
cliDocsVersionCommand,
};
4 changes: 2 additions & 2 deletions packages/docusaurus-plugin-content-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
readVersionsMetadata,
toFullVersion,
} from './versions';
import {cliDocsVersionCommand} from './cli';
import cliDocs from './cli';
import {VERSIONS_JSON_FILE} from './constants';
import {toGlobalDataVersion} from './globalData';
import {
Expand Down Expand Up @@ -134,7 +134,7 @@ export default async function pluginContentDocs(
.arguments('<version>')
.description(commandDescription)
.action((version: unknown) =>
cliDocsVersionCommand(version, options, context),
cliDocs.cliDocsVersionCommand(version, options, context),
);
},

Expand Down

0 comments on commit c12cbfa

Please sign in to comment.