code-server/test/plugin.test.ts
Anmol Sethi 197a09f0c1
plugin: Test endpoints via supertest
Unfortunately we can't use node-mocks-http to test a express.Router
that has async routes. See https://github.com/howardabrams/node-mocks-http/issues/225

router will just return undefined if the executing handler is async and
so the test will have no way to wait for it to complete. Thus, we have
to use supertest which starts an actual HTTP server in the background
and uses a HTTP client to send requests.
2020-11-06 10:13:01 -05:00

58 lines
1.5 KiB
TypeScript

import { logger } from "@coder/logger"
import { describe } from "mocha"
import * as path from "path"
import { PluginAPI } from "../src/node/plugin"
import * as supertest from "supertest"
import * as express from "express"
import * as apps from "../src/node/routes/apps"
/**
* Use $LOG_LEVEL=debug to see debug logs.
*/
describe("plugin", () => {
let papi: PluginAPI
let app: express.Application
let agent: supertest.SuperAgentTest
before(async () => {
papi = new PluginAPI(logger, path.resolve(__dirname, "test-plugin") + ":meow")
await papi.loadPlugins()
app = express.default()
papi.mount(app)
app.use("/api/applications", apps.router(papi))
agent = supertest.agent(app)
})
it("/api/applications", async () => {
await agent.get("/api/applications").expect(200, [
{
name: "Test App",
version: "4.0.0",
description: "This app does XYZ.",
iconPath: "/test-plugin/test-app/icon.svg",
homepageURL: "https://example.com",
path: "/test-plugin/test-app",
plugin: {
name: "test-plugin",
version: "1.0.0",
modulePath: path.join(__dirname, "test-plugin"),
displayName: "Test Plugin",
description: "Plugin used in code-server tests.",
routerPath: "/test-plugin",
homepageURL: "https://example.com",
},
},
])
})
it("/test-plugin/test-app", async () => {
await agent.get("/test-plugin/test-app").expect(200, { date: "2000-02-05T05:00:00.000Z" })
})
})