Fix code-server module not being provided in Jest

This commit is contained in:
Asher 2021-02-09 15:23:08 -06:00
parent b881117762
commit e098df0766
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
2 changed files with 20 additions and 15 deletions

View File

@ -21,8 +21,13 @@ type Module = any
*/
const originalLoad = require("module")._load
require("module")._load = function (request: string, parent: object, isMain: boolean): Module {
if (request === "code-server") {
return {
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,
@ -32,9 +37,6 @@ require("module")._load = function (request: string, parent: object, isMain: boo
replaceTemplates,
WsRouter,
wss,
}
}
return originalLoad.apply(this, [request, parent, isMain])
}
interface Plugin extends pluginapi.Plugin {

View File

@ -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.
*/