From e098df076666248f48b1da5a7c5a79505cb45357 Mon Sep 17 00:00:00 2001 From: Asher Date: Tue, 9 Feb 2021 15:23:08 -0600 Subject: [PATCH] Fix code-server module not being provided in Jest --- src/node/plugin.ts | 30 ++++++++++++++++-------------- test/plugin.test.ts | 5 ++++- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/node/plugin.ts b/src/node/plugin.ts index d0531610..2ba1bf1e 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -21,20 +21,22 @@ type Module = any */ const originalLoad = require("module")._load require("module")._load = function (request: string, parent: object, isMain: boolean): Module { - if (request === "code-server") { - return { - express, - field, - HttpCode, - HttpError, - Level, - proxy, - replaceTemplates, - WsRouter, - wss, - } - } - return originalLoad.apply(this, [request, parent, isMain]) + return request === "code-server" ? codeServer : originalLoad.apply(this, [request, parent, isMain]) +} + +/** + * The module you get when importing "code-server". + */ +export const codeServer = { + express, + field, + HttpCode, + HttpError, + Level, + proxy, + replaceTemplates, + WsRouter, + wss, } interface Plugin extends pluginapi.Plugin { diff --git a/test/plugin.test.ts b/test/plugin.test.ts index b5bfb763..e9248eed 100644 --- a/test/plugin.test.ts +++ b/test/plugin.test.ts @@ -3,11 +3,14 @@ import * as express from "express" import * as fs from "fs" import * as path from "path" import { HttpCode } from "../src/common/http" -import { PluginAPI } from "../src/node/plugin" +import { codeServer, PluginAPI } from "../src/node/plugin" import * as apps from "../src/node/routes/apps" import * as httpserver from "./httpserver" const fsp = fs.promises +// Jest overrides `require` so our usual override doesn't work. +jest.mock("code-server", () => codeServer) + /** * Use $LOG_LEVEL=debug to see debug logs. */