Skip to content

dev(testing): separate unit and e2e tests #2852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: move unit tests to test/unit
  • Loading branch information
jsjoeio committed Mar 12, 2021
commit 3f7104bb4ee06f27e42b74967e928b873fb9c956
4 changes: 2 additions & 2 deletions test/cli.test.ts → test/unit/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import * as fs from "fs-extra"
import * as net from "net"
import * as os from "os"
import * as path from "path"
import { Args, parse, setDefaults, shouldOpenInExistingInstance } from "../src/node/cli"
import { paths, tmpdir } from "../src/node/util"
import { Args, parse, setDefaults, shouldOpenInExistingInstance } from "../../src/node/cli"
import { paths, tmpdir } from "../../src/node/util"

type Mutable<T> = {
-readonly [P in keyof T]: T[P]
Expand Down
6 changes: 3 additions & 3 deletions test/constants.test.ts → test/unit/constants.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { commit, getPackageJson, version } from "../src/node/constants"
import { loggerModule } from "./helpers"
import { commit, getPackageJson, version } from "../../src/node/constants"
import { loggerModule } from "../utils/helpers"

// jest.mock is hoisted above the imports so we must use `require` here.
jest.mock("@coder/logger", () => require("./helpers").loggerModule)
jest.mock("@coder/logger", () => require("../utils/helpers").loggerModule)

describe("constants", () => {
describe("getPackageJson", () => {
Expand Down
4 changes: 2 additions & 2 deletions test/emitter.test.ts → test/unit/emitter.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Note: we need to import logger from the root
// because this is the logger used in logError in ../src/common/util
import { logger } from "../node_modules/@coder/logger"
import { logger } from "../../node_modules/@coder/logger"

import { Emitter } from "../src/common/emitter"
import { Emitter } from "../../src/common/emitter"

describe("emitter", () => {
let spy: jest.SpyInstance
Expand Down
4 changes: 2 additions & 2 deletions test/health.test.ts → test/unit/health.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as httpserver from "./httpserver"
import * as integration from "./integration"
import * as httpserver from "../utils/httpserver"
import * as integration from "../utils/integration"

describe("health", () => {
let codeServer: httpserver.HttpServer | undefined
Expand Down
2 changes: 1 addition & 1 deletion test/http.test.ts → test/unit/http.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpCode, HttpError } from "../src/common/http"
import { HttpCode, HttpError } from "../../src/common/http"

describe("http", () => {
describe("HttpCode", () => {
Expand Down
10 changes: 5 additions & 5 deletions test/plugin.test.ts → test/unit/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { logger } from "@coder/logger"
import * as express from "express"
import * as fs from "fs"
import * as path from "path"
import { HttpCode } from "../src/common/http"
import { AuthType } from "../src/node/cli"
import { codeServer, PluginAPI } from "../src/node/plugin"
import * as apps from "../src/node/routes/apps"
import * as httpserver from "./httpserver"
import { HttpCode } from "../../src/common/http"
import { AuthType } from "../../src/node/cli"
import { codeServer, PluginAPI } from "../../src/node/plugin"
import * as apps from "../../src/node/routes/apps"
import * as httpserver from "../utils/httpserver"
const fsp = fs.promises

// Jest overrides `require` so our usual override doesn't work.
Expand Down
4 changes: 2 additions & 2 deletions test/proxy.test.ts → test/unit/proxy.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import bodyParser from "body-parser"
import * as express from "express"
import * as httpserver from "./httpserver"
import * as integration from "./integration"
import * as httpserver from "../utils/httpserver"
import * as integration from "../utils/integration"

describe("proxy", () => {
const nhooyrDevServer = new httpserver.HttpServer()
Expand Down
8 changes: 4 additions & 4 deletions test/register.test.ts → test/unit/register.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JSDOM } from "jsdom"
import { loggerModule } from "./helpers"
import { loggerModule } from "../utils/helpers"

describe("register", () => {
describe("when navigator and serviceWorker are defined", () => {
Expand Down Expand Up @@ -40,7 +40,7 @@ describe("register", () => {

it("should register a ServiceWorker", () => {
// Load service worker like you would in the browser
require("../src/browser/register")
require("../../src/browser/register")
expect(mockRegisterFn).toHaveBeenCalled()
expect(mockRegisterFn).toHaveBeenCalledTimes(1)
})
Expand All @@ -54,7 +54,7 @@ describe("register", () => {
})

// Load service worker like you would in the browser
require("../src/browser/register")
require("../../src/browser/register")

expect(mockRegisterFn).toHaveBeenCalled()
expect(loggerModule.logger.error).toHaveBeenCalled()
Expand All @@ -78,7 +78,7 @@ describe("register", () => {

it("should log an error to the console", () => {
// Load service worker like you would in the browser
require("../src/browser/register")
require("../../src/browser/register")
expect(spy).toHaveBeenCalled()
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith("[Service Worker] navigator is undefined")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe("serviceWorker", () => {
})

it("should add 3 listeners: install, activate and fetch", () => {
require("../src/browser/serviceWorker.ts")
require("../../src/browser/serviceWorker.ts")
const listenerEventNames = listeners.map((listener) => listener.event)

expect(listeners).toHaveLength(3)
Expand All @@ -68,20 +68,20 @@ describe("serviceWorker", () => {
})

it("should call the proper callbacks for 'install'", async () => {
require("../src/browser/serviceWorker.ts")
require("../../src/browser/serviceWorker.ts")
emit("install")
expect(spy).toHaveBeenCalledWith("[Service Worker] installed")
expect(spy).toHaveBeenCalledTimes(1)
})

it("should do nothing when 'fetch' is called", async () => {
require("../src/browser/serviceWorker.ts")
require("../../src/browser/serviceWorker.ts")
emit("fetch")
expect(spy).not.toHaveBeenCalled()
})

it("should call the proper callbacks for 'activate'", async () => {
require("../src/browser/serviceWorker.ts")
require("../../src/browser/serviceWorker.ts")
emit("activate")

// Activate serviceWorker
Expand Down
6 changes: 3 additions & 3 deletions test/socket.test.ts → test/unit/socket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import * as fs from "fs-extra"
import * as net from "net"
import * as path from "path"
import * as tls from "tls"
import { Emitter } from "../src/common/emitter"
import { SocketProxyProvider } from "../src/node/socket"
import { generateCertificate, tmpdir } from "../src/node/util"
import { Emitter } from "../../src/common/emitter"
import { SocketProxyProvider } from "../../src/node/socket"
import { generateCertificate, tmpdir } from "../../src/node/util"

describe("SocketProxyProvider", () => {
const provider = new SocketProxyProvider()
Expand Down
5 changes: 5 additions & 0 deletions test/unit/test-plugin/.eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
settings:
import/resolver:
alias:
map:
- [code-server, ./typings/pluginapi.d.ts]
1 change: 1 addition & 0 deletions test/unit/test-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
out
6 changes: 6 additions & 0 deletions test/unit/test-plugin/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
out/index.js: src/index.ts
# Typescript always emits, even on errors.
yarn build || rm out/index.js

node_modules: package.json yarn.lock
yarn
16 changes: 16 additions & 0 deletions test/unit/test-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"private": true,
"name": "test-plugin",
"version": "1.0.0",
"engines": {
"code-server": "^3.7.0"
},
"main": "out/index.js",
"devDependencies": {
"@types/express": "^4.17.8",
"typescript": "^4.0.5"
},
"scripts": {
"build": "tsc"
}
}
1 change: 1 addition & 0 deletions test/unit/test-plugin/public/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions test/unit/test-plugin/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Test Plugin</title>
</head>
<body>
<p>Welcome to the test plugin!</p>
</body>
</html>
52 changes: 52 additions & 0 deletions test/unit/test-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as cs from "code-server"
import * as fspath from "path"

export const plugin: cs.Plugin = {
displayName: "Test Plugin",
routerPath: "/test-plugin",
homepageURL: "https://example.com",
description: "Plugin used in code-server tests.",

init(config) {
config.logger.debug("test-plugin loaded!")
},

router() {
const r = cs.express.Router()
r.get("/test-app", (_, res) => {
res.sendFile(fspath.resolve(__dirname, "../public/index.html"))
})
r.get("/goland/icon.svg", (_, res) => {
res.sendFile(fspath.resolve(__dirname, "../public/icon.svg"))
})
r.get("/error", () => {
throw new cs.HttpError("error", cs.HttpCode.LargePayload)
})
return r
},

wsRouter() {
const wr = cs.WsRouter()
wr.ws("/test-app", (req) => {
cs.wss.handleUpgrade(req, req.ws, req.head, (ws) => {
req.ws.resume()
ws.send("hello")
})
})
return wr
},

applications() {
return [
{
name: "Test App",
version: "4.0.0",
iconPath: "/icon.svg",
path: "/test-app",

description: "This app does XYZ.",
homepageURL: "https://example.com",
},
]
},
}
71 changes: 71 additions & 0 deletions test/unit/test-plugin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./out" /* Redirect output structure to the directory. */,
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true /* Enable all strict type-checking options. */,
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": "./" /* Base directory to resolve non-absolute module names. */,
"paths": {
"code-server": ["../../typings/pluginapi"]
} /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */,
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

/* Advanced Options */
"skipLibCheck": true /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}
Loading