Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Sep 20, 2024
1 parent 7fc4c7c commit 4940034
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/docusaurus-bundler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
"webpack": "^5.88.1",
"webpackbar": "^6.0.1"
},
"devDependencies": {
"@total-typescript/shoehorn": "^0.1.2"
},
"engines": {
"node": ">=18.0"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus/src/commands/start/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import logger from '@docusaurus/logger';
import WebpackDevServer from 'webpack-dev-server';
import evalSourceMapMiddleware from 'react-dev-utils/evalSourceMapMiddleware';
import {createPollingOptions} from './watcher';
import {getHttpsConfig} from '../../webpack/utils';
import getHttpsConfig from '../../webpack/utils/getHttpsConfig';
import {
createConfigureWebpackUtils,
executePluginsConfigureWebpack,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import {jest} from '@jest/globals';
import path from 'path';
import fs from 'fs-extra';
import tmp from 'tmp-promise';
import {getBabelOptions} from '@docusaurus/bundler';
import {SRC_DIR_NAME} from '@docusaurus/utils';
import {
extractSourceCodeFileTranslations,
extractSiteSourceCodeTranslations,
} from '../translationsExtractor';
import {getBabelOptions} from '../../../webpack/utils';
import type {InitializedPlugin, LoadedPlugin} from '@docusaurus/types';

const TestBabelOptions = getBabelOptions({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import path from 'path';
import getHttpsConfig from '../getHttpsConfig';

describe('getHttpsConfig', () => {
const originalEnv = process.env;

function getFixture(name: string) {
return path.join(__dirname, '__fixtures__/getHttpsConfig', name);
}

beforeEach(() => {
jest.resetModules();
process.env = {...originalEnv};
});

afterAll(() => {
process.env = originalEnv;
});

it('returns true for HTTPS not env', async () => {
await expect(getHttpsConfig()).resolves.toBe(false);
});

it('returns true for HTTPS in env', async () => {
process.env.HTTPS = 'true';
await expect(getHttpsConfig()).resolves.toBe(true);
});

it('returns custom certs if they are in env', async () => {
process.env.HTTPS = 'true';
process.env.SSL_CRT_FILE = getFixture('host.crt');
process.env.SSL_KEY_FILE = getFixture('host.key');
await expect(getHttpsConfig()).resolves.toEqual({
key: expect.any(Buffer),
cert: expect.any(Buffer),
});
});

it("throws if file doesn't exist", async () => {
process.env.HTTPS = 'true';
process.env.SSL_CRT_FILE = getFixture('nonexistent.crt');
process.env.SSL_KEY_FILE = getFixture('host.key');
await expect(getHttpsConfig()).rejects.toThrowErrorMatchingInlineSnapshot(
`"You specified SSL_CRT_FILE in your env, but the file "<PROJECT_ROOT>/packages/docusaurus/src/webpack/utils/__tests__/__fixtures__/getHttpsConfig/nonexistent.crt" can't be found."`,
);
});

it('throws for invalid key', async () => {
process.env.HTTPS = 'true';
process.env.SSL_CRT_FILE = getFixture('host.crt');
process.env.SSL_KEY_FILE = getFixture('invalid.key');
await expect(getHttpsConfig()).rejects.toThrow();
});

it('throws for invalid cert', async () => {
process.env.HTTPS = 'true';
process.env.SSL_CRT_FILE = getFixture('invalid.crt');
process.env.SSL_KEY_FILE = getFixture('host.key');
await expect(getHttpsConfig()).rejects.toThrow();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async function readEnvFile(file: string, type: string) {

// Get the https config
// Return cert files if provided in env, otherwise just true or false
export async function getHttpsConfig(): Promise<
export default async function getHttpsConfig(): Promise<
boolean | {cert: Buffer; key: Buffer}
> {
const appDirectory = await fs.realpath(process.cwd());
Expand Down

0 comments on commit 4940034

Please sign in to comment.