feat: setup jest

This commit is contained in:
Joe Previte 2021-01-08 20:55:47 +00:00
parent c52198f30d
commit cef7d42652
No known key found for this signature in database
GPG Key ID: 2C91590C6B742C24
8 changed files with 1918 additions and 186 deletions

5
jest.config.js Normal file
View File

@ -0,0 +1,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ["/node_modules/", "lib/vscode/"]
};

View File

@ -35,6 +35,7 @@
"@types/express": "^4.17.8", "@types/express": "^4.17.8",
"@types/fs-extra": "^8.0.1", "@types/fs-extra": "^8.0.1",
"@types/http-proxy": "^1.17.4", "@types/http-proxy": "^1.17.4",
"@types/jest": "^26.0.20",
"@types/js-yaml": "^3.12.3", "@types/js-yaml": "^3.12.3",
"@types/mocha": "^8.0.3", "@types/mocha": "^8.0.3",
"@types/node": "^12.12.7", "@types/node": "^12.12.7",
@ -55,6 +56,7 @@
"eslint-config-prettier": "^6.0.0", "eslint-config-prettier": "^6.0.0",
"eslint-plugin-import": "^2.18.2", "eslint-plugin-import": "^2.18.2",
"eslint-plugin-prettier": "^3.1.0", "eslint-plugin-prettier": "^3.1.0",
"jest": "^26.6.3",
"leaked-handles": "^5.2.0", "leaked-handles": "^5.2.0",
"mocha": "^8.1.2", "mocha": "^8.1.2",
"parcel-bundler": "^1.12.4", "parcel-bundler": "^1.12.4",

View File

@ -1,5 +1,4 @@
import { Level, logger } from "@coder/logger" import { Level, logger } from "@coder/logger"
import * as assert from "assert"
import * as fs from "fs-extra" import * as fs from "fs-extra"
import * as net from "net" import * as net from "net"
import * as os from "os" import * as os from "os"
@ -32,11 +31,11 @@ describe("parser", () => {
} }
it("should parse nothing", () => { it("should parse nothing", () => {
assert.deepEqual(parse([]), { _: [] }) expect(parse([])).toBe({ _: [] })
}) })
it("should parse all available options", () => { it("should parse all available options", () => {
assert.deepEqual( expect(
parse([ parse([
"--bind-addr=192.169.0.1:8080", "--bind-addr=192.169.0.1:8080",
"--auth", "--auth",
@ -73,7 +72,7 @@ describe("parser", () => {
"--", "--",
"-5", "-5",
"--6", "--6",
]), ])).toEqual(
{ {
_: ["1", "2", "3", "4", "-5", "--6"], _: ["1", "2", "3", "4", "-5", "--6"],
auth: "none", auth: "none",
@ -102,7 +101,7 @@ describe("parser", () => {
}) })
it("should work with short options", () => { it("should work with short options", () => {
assert.deepEqual(parse(["-vvv", "-v"]), { expect(parse(["-vvv", "-v"])).toEqual({
_: [], _: [],
verbose: true, verbose: true,
version: true, version: true,
@ -111,102 +110,108 @@ describe("parser", () => {
it("should use log level env var", async () => { it("should use log level env var", async () => {
const args = parse([]) const args = parse([])
assert.deepEqual(args, { _: [] }) expect(args).toEqual({ _: [] })
process.env.LOG_LEVEL = "debug" process.env.LOG_LEVEL = "debug"
assert.deepEqual(await setDefaults(args), { const defaults = await setDefaults(args)
expect(defaults).toEqual({
...defaults, ...defaults,
_: [], _: [],
log: "debug", log: "debug",
verbose: false, verbose: false,
}) })
assert.equal(process.env.LOG_LEVEL, "debug") expect(process.env.LOG_LEVEL).toEqual("debug")
assert.equal(logger.level, Level.Debug) expect(logger.level).toEqual(Level.Debug)
process.env.LOG_LEVEL = "trace" process.env.LOG_LEVEL = "trace"
assert.deepEqual(await setDefaults(args), { const updated = await setDefaults(args)
...defaults, expect(updated).toBe( {
...updated,
_: [], _: [],
log: "trace", log: "trace",
verbose: true, verbose: true,
}) })
assert.equal(process.env.LOG_LEVEL, "trace") expect(process.env.LOG_LEVEL).toEqual("trace")
assert.equal(logger.level, Level.Trace) expect(logger.level).toEqual(Level.Trace)
}) })
it("should prefer --log to env var and --verbose to --log", async () => { it("should prefer --log to env var and --verbose to --log", async () => {
let args = parse(["--log", "info"]) let args = parse(["--log", "info"])
assert.deepEqual(args, { expect(args).toEqual({
_: [], _: [],
log: "info", log: "info",
}) })
process.env.LOG_LEVEL = "debug" process.env.LOG_LEVEL = "debug"
assert.deepEqual(await setDefaults(args), { const defaults = await setDefaults(args)
expect(defaults).toEqual({
...defaults, ...defaults,
_: [], _: [],
log: "info", log: "info",
verbose: false, verbose: false,
}) })
assert.equal(process.env.LOG_LEVEL, "info") expect(process.env.LOG_LEVEL).toEqual("info")
assert.equal(logger.level, Level.Info) expect(logger.level).toEqual(Level.Info)
process.env.LOG_LEVEL = "trace" process.env.LOG_LEVEL = "trace"
assert.deepEqual(await setDefaults(args), { const updated = await setDefaults(args)
expect(updated).toEqual( {
...defaults, ...defaults,
_: [], _: [],
log: "info", log: "info",
verbose: false, verbose: false,
}) })
assert.equal(process.env.LOG_LEVEL, "info") expect(process.env.LOG_LEVEL).toEqual("info")
assert.equal(logger.level, Level.Info) expect(logger.level).toEqual(Level.Info)
args = parse(["--log", "info", "--verbose"]) args = parse(["--log", "info", "--verbose"])
assert.deepEqual(args, { expect(args).toEqual({
_: [], _: [],
log: "info", log: "info",
verbose: true, verbose: true,
}) })
process.env.LOG_LEVEL = "warn" process.env.LOG_LEVEL = "warn"
assert.deepEqual(await setDefaults(args), { const updatedAgain = await setDefaults(args)
expect(updatedAgain).toEqual( {
...defaults, ...defaults,
_: [], _: [],
log: "trace", log: "trace",
verbose: true, verbose: true,
}) })
assert.equal(process.env.LOG_LEVEL, "trace") expect(process.env.LOG_LEVEL).toEqual("trace")
assert.equal(logger.level, Level.Trace) expect(logger.level).toEqual(Level.Trace)
}) })
it("should ignore invalid log level env var", async () => { it("should ignore invalid log level env var", async () => {
process.env.LOG_LEVEL = "bogus" process.env.LOG_LEVEL = "bogus"
assert.deepEqual(await setDefaults(parse([])), { const defaults = await setDefaults(parse([]))
_: [], expect(defaults).toEqual({
...defaults, ...defaults,
_: [],
}) })
}) })
it("should error if value isn't provided", () => { it("should error if value isn't provided", () => {
assert.throws(() => parse(["--auth"]), /--auth requires a value/) expect(() => parse(["--auth"])).toThrowError(/--auth requires a value/)
assert.throws(() => parse(["--auth=", "--log=debug"]), /--auth requires a value/) expect(() => parse(["--auth=", "--log=debug"])).toThrowError(/--auth requires a value/)
assert.throws(() => parse(["--auth", "--log"]), /--auth requires a value/) expect(() => parse(["--auth", "--log"])).toThrowError(/--auth requires a value/)
assert.throws(() => parse(["--auth", "--invalid"]), /--auth requires a value/) expect(() => parse(["--auth", "--invalid"])).toThrowError(/--auth requires a value/)
assert.throws(() => parse(["--bind-addr"]), /--bind-addr requires a value/) expect(() => parse(["--bind-addr"])).toThrowError(/--bind-addr requires a value/)
}) })
it("should error if value is invalid", () => { it("should error if value is invalid", () => {
assert.throws(() => parse(["--port", "foo"]), /--port must be a number/) expect(() => parse(["--port", "foo"])).toThrowError(/--port must be a number/)
assert.throws(() => parse(["--auth", "invalid"]), /--auth valid values: \[password, none\]/) expect(() => parse(["--auth", "invalid"])).toThrowError(/--auth valid values: \[password, none\]/)
assert.throws(() => parse(["--log", "invalid"]), /--log valid values: \[trace, debug, info, warn, error\]/) expect(() => parse(["--log", "invalid"])).toThrowError(/--log valid values: \[trace, debug, info, warn, error\]/)
}) })
it("should error if the option doesn't exist", () => { it("should error if the option doesn't exist", () => {
assert.throws(() => parse(["--foo"]), /Unknown option --foo/) expect(()=> parse(["--foo"])).toThrowError(/Unknown option --foo/)
}) })
it("should not error if the value is optional", () => { it("should not error if the value is optional", () => {
assert.deepEqual(parse(["--cert"]), { expect(parse(["--cert"])).toEqual({
_: [], _: [],
cert: { cert: {
value: undefined, value: undefined,
@ -215,55 +220,60 @@ describe("parser", () => {
}) })
it("should not allow option-like values", () => { it("should not allow option-like values", () => {
assert.throws(() => parse(["--socket", "--socket-path-value"]), /--socket requires a value/) expect(() => parse(["--socket", "--socket-path-value"])).toThrowError(/--socket requires a value/)
// If you actually had a path like this you would do this instead: // If you actually had a path like this you would do this instead:
assert.deepEqual(parse(["--socket", "./--socket-path-value"]), { expect(parse(["--socket", "./--socket-path-value"])).toEqual({
_: [], _: [],
socket: path.resolve("--socket-path-value"), socket: path.resolve("--socket-path-value"),
}) })
assert.throws(() => parse(["--cert", "--socket-path-value"]), /Unknown option --socket-path-value/) expect(() => parse(["--cert", "--socket-path-value"])).toThrowError(/Unknown option --socket-path-value/)
}) })
it("should allow positional arguments before options", () => { it("should allow positional arguments before options", () => {
assert.deepEqual(parse(["foo", "test", "--auth", "none"]), { expect(parse(["foo", "test", "--auth", "none"])).toEqual({
_: ["foo", "test"], _: ["foo", "test"],
auth: "none", auth: "none",
}) })
}) })
it("should support repeatable flags", () => { it("should support repeatable flags", () => {
assert.deepEqual(parse(["--proxy-domain", "*.coder.com"]), { expect(parse(["--proxy-domain", "*.coder.com"])).toEqual({
_: [], _: [],
"proxy-domain": ["*.coder.com"], "proxy-domain": ["*.coder.com"],
}) })
assert.deepEqual(parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "test.com"]), { expect(parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "test.com"])).toEqual({
_: [], _: [],
"proxy-domain": ["*.coder.com", "test.com"], "proxy-domain": ["*.coder.com", "test.com"],
}) })
}) })
it("should enforce cert-key with cert value or otherwise generate one", async () => { it(
const args = parse(["--cert"]) "should enforce cert-key with cert value or otherwise generate one",
assert.deepEqual(args, { async () => {
_: [], const args = parse(["--cert"])
cert: { expect(args).toEqual( {
value: undefined, _: [],
}, cert: {
}) value: undefined,
assert.throws(() => parse(["--cert", "test"]), /--cert-key is missing/) },
assert.deepEqual(await setDefaults(args), { })
_: [], expect(() => parse(["--cert", "test"])).toThrowError(/--cert-key is missing/)
...defaults, const defaultArgs = await setDefaults(args)
cert: { expect(defaultArgs).toEqual({
value: path.join(paths.data, "localhost.crt"), _: [],
}, ...defaults,
"cert-key": path.join(paths.data, "localhost.key"), cert: {
}) value: path.join(paths.data, "localhost.crt"),
}) },
"cert-key": path.join(paths.data, "localhost.key"),
})
}
)
it("should override with --link", async () => { it("should override with --link", async () => {
const args = parse("--cert test --cert-key test --socket test --host 0.0.0.0 --port 8888 --link test".split(" ")) const args = parse("--cert test --cert-key test --socket test --host 0.0.0.0 --port 8888 --link test".split(" "))
assert.deepEqual(await setDefaults(args), { const defaultArgs = await setDefaults(args)
expect(defaultArgs).toEqual({
_: [], _: [],
...defaults, ...defaults,
auth: "none", auth: "none",
@ -281,11 +291,12 @@ describe("parser", () => {
it("should use env var password", async () => { it("should use env var password", async () => {
process.env.PASSWORD = "test" process.env.PASSWORD = "test"
const args = parse([]) const args = parse([])
assert.deepEqual(args, { expect(args).toEqual({
_: [], _: [],
}) })
assert.deepEqual(await setDefaults(args), { const defaultArgs = await setDefaults(args)
expect(defaultArgs).toEqual({
...defaults, ...defaults,
_: [], _: [],
password: "test", password: "test",
@ -296,11 +307,12 @@ describe("parser", () => {
it("should use env var hashed password", async () => { it("should use env var hashed password", async () => {
process.env.HASHED_PASSWORD = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" // test process.env.HASHED_PASSWORD = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" // test
const args = parse([]) const args = parse([])
assert.deepEqual(args, { expect(args).toEqual({
_: [], _: [],
}) })
assert.deepEqual(await setDefaults(args), { const defaultArgs = await setDefaults(args)
expect(defaultArgs).toEqual({
...defaults, ...defaults,
_: [], _: [],
"hashed-password": "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", "hashed-password": "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
@ -310,12 +322,13 @@ describe("parser", () => {
it("should filter proxy domains", async () => { it("should filter proxy domains", async () => {
const args = parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "coder.com", "--proxy-domain", "coder.org"]) const args = parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "coder.com", "--proxy-domain", "coder.org"])
assert.deepEqual(args, { expect(args).toEqual({
_: [], _: [],
"proxy-domain": ["*.coder.com", "coder.com", "coder.org"], "proxy-domain": ["*.coder.com", "coder.com", "coder.org"],
}) })
assert.deepEqual(await setDefaults(args), { const defaultArgs = await setDefaults(args)
expect(defaultArgs).toEqual({
...defaults, ...defaults,
_: [], _: [],
"proxy-domain": ["coder.com", "coder.org"], "proxy-domain": ["coder.com", "coder.org"],
@ -328,7 +341,7 @@ describe("cli", () => {
const testDir = path.join(tmpdir, "tests/cli") const testDir = path.join(tmpdir, "tests/cli")
const vscodeIpcPath = path.join(os.tmpdir(), "vscode-ipc") const vscodeIpcPath = path.join(os.tmpdir(), "vscode-ipc")
before(async () => { beforeAll(async () => {
await fs.remove(testDir) await fs.remove(testDir)
await fs.mkdirp(testDir) await fs.mkdirp(testDir)
}) })
@ -341,57 +354,66 @@ describe("cli", () => {
it("should use existing if inside code-server", async () => { it("should use existing if inside code-server", async () => {
process.env.VSCODE_IPC_HOOK_CLI = "test" process.env.VSCODE_IPC_HOOK_CLI = "test"
assert.strictEqual(await shouldOpenInExistingInstance(args), "test") const shouldOpen = await shouldOpenInExistingInstance(args)
expect(shouldOpen).toStrictEqual("test")
args.port = 8081 args.port = 8081
args._.push("./file") args._.push("./file")
assert.strictEqual(await shouldOpenInExistingInstance(args), "test") const _shouldOpen = await shouldOpenInExistingInstance(args)
expect(_shouldOpen).toStrictEqual("test")
}) })
it("should use existing if --reuse-window is set", async () => { it("should use existing if --reuse-window is set", async () => {
args["reuse-window"] = true args["reuse-window"] = true
assert.strictEqual(await shouldOpenInExistingInstance(args), undefined) const shouldOpen = await shouldOpenInExistingInstance(args)
await expect(shouldOpen).toStrictEqual(undefined)
await fs.writeFile(vscodeIpcPath, "test") await fs.writeFile(vscodeIpcPath, "test")
assert.strictEqual(await shouldOpenInExistingInstance(args), "test") await expect(shouldOpenInExistingInstance(args)).resolves.toStrictEqual("test")
args.port = 8081 args.port = 8081
assert.strictEqual(await shouldOpenInExistingInstance(args), "test") await expect(shouldOpenInExistingInstance(args)).resolves.toStrictEqual("test")
}) })
// TODO
// fix red squiggles
// and don't use should Open on all these
it("should use existing if --new-window is set", async () => { it("should use existing if --new-window is set", async () => {
args["new-window"] = true args["new-window"] = true
assert.strictEqual(await shouldOpenInExistingInstance(args), undefined) expect(await shouldOpenInExistingInstance(args).toStrictEqual(undefined)
await fs.writeFile(vscodeIpcPath, "test") await fs.writeFile(vscodeIpcPath, "test")
assert.strictEqual(await shouldOpenInExistingInstance(args), "test") expect(await shouldOpenInExistingInstance(args).toStrictEqual("test")
args.port = 8081 args.port = 8081
assert.strictEqual(await shouldOpenInExistingInstance(args), "test") expect(await shouldOpenInExistingInstance(args).toStrictEqual("test")
}) })
it("should use existing if no unrelated flags are set, has positional, and socket is active", async () => { it(
assert.strictEqual(await shouldOpenInExistingInstance(args), undefined) "should use existing if no unrelated flags are set, has positional, and socket is active",
async () => {
expect(await shouldOpenInExistingInstance(args).toStrictEqual(undefined)
args._.push("./file") args._.push("./file")
assert.strictEqual(await shouldOpenInExistingInstance(args), undefined) expect(await shouldOpenInExistingInstance(args).toStrictEqual(undefined)
const socketPath = path.join(testDir, "socket") const socketPath = path.join(testDir, "socket")
await fs.writeFile(vscodeIpcPath, socketPath) await fs.writeFile(vscodeIpcPath, socketPath)
assert.strictEqual(await shouldOpenInExistingInstance(args), undefined) expect(await shouldOpenInExistingInstance(args).toStrictEqual(undefined)
await new Promise((resolve) => { await new Promise((resolve) => {
const server = net.createServer(() => { const server = net.createServer(() => {
// Close after getting the first connection. // Close after getting the first connection.
server.close() server.close()
})
server.once("listening", () => resolve(server))
server.listen(socketPath)
}) })
server.once("listening", () => resolve(server))
server.listen(socketPath)
})
assert.strictEqual(await shouldOpenInExistingInstance(args), socketPath) expect(await shouldOpenInExistingInstance(args).toStrictEqual(socketPath)
args.port = 8081 args.port = 8081
assert.strictEqual(await shouldOpenInExistingInstance(args), undefined) expect(await shouldOpenInExistingInstance(args).toStrictEqual(undefined)
}) }
)
}) })

View File

@ -2,7 +2,6 @@ import { logger } from "@coder/logger"
import * as assert from "assert" import * as assert from "assert"
import * as express from "express" import * as express from "express"
import * as fs from "fs" import * as fs from "fs"
import { describe } from "mocha"
import * as path from "path" import * as path from "path"
import { PluginAPI } from "../src/node/plugin" import { PluginAPI } from "../src/node/plugin"
import * as apps from "../src/node/routes/apps" import * as apps from "../src/node/routes/apps"

View File

@ -1,5 +1,4 @@
import { field, logger } from "@coder/logger" import { field, logger } from "@coder/logger"
import * as assert from "assert"
import * as fs from "fs-extra" import * as fs from "fs-extra"
import "leaked-handles" import "leaked-handles"
import * as net from "net" import * as net from "net"
@ -44,7 +43,7 @@ describe("SocketProxyProvider", () => {
}) })
} }
before(async () => { beforeAll(async () => {
const cert = await generateCertificate("localhost") const cert = await generateCertificate("localhost")
const options = { const options = {
cert: fs.readFileSync(cert.cert), cert: fs.readFileSync(cert.cert),
@ -93,14 +92,16 @@ describe("SocketProxyProvider", () => {
it("should work without a proxy", async () => { it("should work without a proxy", async () => {
server.write("server->client") server.write("server->client")
assert.equal(await getData(fromServerToClient), "server->client") const dataFromServerToClient = await (await getData(fromServerToClient)).toString()
expect(dataFromServerToClient).toBe("server->client")
client.write("client->server") client.write("client->server")
assert.equal(await getData(fromClientToServer), "client->server") const dataFromClientToServer = await (await getData(fromClientToServer)).toString()
assert.equal(errors, 0) expect(dataFromClientToServer).toBe("client->server")
expect(errors).toEqual(0)
}) })
it("should work with a proxy", async () => { it("should work with a proxy", async () => {
assert.equal(server instanceof tls.TLSSocket, true) expect(server instanceof tls.TLSSocket).toBe(true)
proxy = (await provider.createProxy(server)) proxy = (await provider.createProxy(server))
.on("data", (d) => fromClientToProxy.emit(d)) .on("data", (d) => fromClientToProxy.emit(d))
.on("error", (error) => onProxyError.emit({ event: "error", error })) .on("error", (error) => onProxyError.emit({ event: "error", error }))
@ -110,10 +111,12 @@ describe("SocketProxyProvider", () => {
provider.stop() // We don't need more proxies. provider.stop() // We don't need more proxies.
proxy.write("server proxy->client") proxy.write("server proxy->client")
assert.equal(await getData(fromServerToClient), "server proxy->client") const dataFromServerToClient = await (await getData(fromServerToClient)).toString()
expect(dataFromServerToClient).toBe("server proxy->client")
client.write("client->server proxy") client.write("client->server proxy")
assert.equal(await getData(fromClientToProxy), "client->server proxy") const dataFromClientToProxy = await (await getData(fromClientToProxy)).toString()
assert.equal(errors, 0) expect(dataFromClientToProxy).toBe("client->server proxy")
expect(errors).toEqual(0)
}) })
it("should close", async () => { it("should close", async () => {
@ -122,3 +125,4 @@ describe("SocketProxyProvider", () => {
proxy.end() proxy.end()
}) })
}) })

View File

@ -1,4 +1,3 @@
import * as assert from "assert"
import * as fs from "fs-extra" import * as fs from "fs-extra"
import * as http from "http" import * as http from "http"
import * as path from "path" import * as path from "path"
@ -45,7 +44,7 @@ describe.skip("update", () => {
return _provider return _provider
} }
before(async () => { beforeAll(async () => {
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
server.on("error", reject) server.on("error", reject)
server.on("listening", resolve) server.on("listening", resolve)
@ -58,7 +57,7 @@ describe.skip("update", () => {
await fs.mkdirp(path.join(tmpdir, "tests/updates")) await fs.mkdirp(path.join(tmpdir, "tests/updates"))
}) })
after(() => { afterAll(() => {
server.close() server.close()
}) })
@ -73,11 +72,11 @@ describe.skip("update", () => {
const now = Date.now() const now = Date.now()
const update = await p.getUpdate() const update = await p.getUpdate()
assert.deepEqual({ update }, await settings.read()) await expect(settings.read()).resolves.toEqual({ update })
assert.equal(isNaN(update.checked), false) expect(isNaN(update.checked)).toEqual(false)
assert.equal(update.checked < Date.now() && update.checked >= now, true) expect(update.checked < Date.now() && update.checked >= now).toEqual(true)
assert.equal(update.version, "2.1.0") expect(update.version).toBe("2.1.0")
assert.deepEqual(spy, ["/latest"]) expect(spy).toEqual(["/latest"])
}) })
it("should keep existing information", async () => { it("should keep existing information", async () => {
@ -87,11 +86,11 @@ describe.skip("update", () => {
const now = Date.now() const now = Date.now()
const update = await p.getUpdate() const update = await p.getUpdate()
assert.deepEqual({ update }, await settings.read()) await expect(settings.read()).resolves.toEqual({ update })
assert.equal(isNaN(update.checked), false) expect(isNaN(update.checked)).toBe(false)
assert.equal(update.checked < now, true) expect(update.checked < now).toBe(true)
assert.equal(update.version, "2.1.0") expect(update.version).toBe("2.1.0")
assert.deepEqual(spy, []) expect(spy).toEqual([])
}) })
it("should force getting the latest", async () => { it("should force getting the latest", async () => {
@ -101,29 +100,29 @@ describe.skip("update", () => {
const now = Date.now() const now = Date.now()
const update = await p.getUpdate(true) const update = await p.getUpdate(true)
assert.deepEqual({ update }, await settings.read()) await expect(settings.read()).resolves.toEqual({ update })
assert.equal(isNaN(update.checked), false) expect(isNaN(update.checked)).toBe(false)
assert.equal(update.checked < Date.now() && update.checked >= now, true) expect(update.checked < Date.now() && update.checked >= now).toBe(true)
assert.equal(update.version, "4.1.1") expect(update.version).toBe("4.1.1")
assert.deepEqual(spy, ["/latest"]) expect(spy).toBe(["/latest"])
}) })
it("should get latest after interval passes", async () => { it("should get latest after interval passes", async () => {
const p = provider() const p = provider()
await p.getUpdate() await p.getUpdate()
assert.deepEqual(spy, []) expect(spy).toEqual([])
let checked = Date.now() - 1000 * 60 * 60 * 23 let checked = Date.now() - 1000 * 60 * 60 * 23
await settings.write({ update: { checked, version } }) await settings.write({ update: { checked, version } })
await p.getUpdate() await p.getUpdate()
assert.deepEqual(spy, []) expect(spy).toEqual([])
checked = Date.now() - 1000 * 60 * 60 * 25 checked = Date.now() - 1000 * 60 * 60 * 25
await settings.write({ update: { checked, version } }) await settings.write({ update: { checked, version } })
const update = await p.getUpdate() const update = await p.getUpdate()
assert.notEqual(update.checked, checked) expect(update.checked).not.toBe(checked)
assert.deepEqual(spy, ["/latest"]) expect(spy).toBe(["/latest"])
}) })
it("should check if it's the current version", async () => { it("should check if it's the current version", async () => {
@ -131,23 +130,24 @@ describe.skip("update", () => {
const p = provider() const p = provider()
let update = await p.getUpdate(true) let update = await p.getUpdate(true)
assert.equal(p.isLatestVersion(update), false) expect(p.isLatestVersion(update)).toBe(false)
version = "0.0.0" version = "0.0.0"
update = await p.getUpdate(true) update = await p.getUpdate(true)
assert.equal(p.isLatestVersion(update), true) expect(p.isLatestVersion(update)).toBe(true)
// Old version format; make sure it doesn't report as being later. // Old version format; make sure it doesn't report as being later.
version = "999999.9999-invalid999.99.9" version = "999999.9999-invalid999.99.9"
update = await p.getUpdate(true) update = await p.getUpdate(true)
assert.equal(p.isLatestVersion(update), true) expect(p.isLatestVersion(update)).toBe(true)
}) })
it("should not reject if unable to fetch", async () => { it("should not reject if unable to fetch", async () => {
expect.assertions(2)
let provider = new UpdateProvider("invalid", settings) let provider = new UpdateProvider("invalid", settings)
await assert.doesNotReject(() => provider.getUpdate(true)) await expect(() => provider.getUpdate(true)).resolves.toBe(undefined)
provider = new UpdateProvider("http://probably.invalid.dev.localhost/latest", settings) provider = new UpdateProvider("http://probably.invalid.dev.localhost/latest", settings)
await assert.doesNotReject(() => provider.getUpdate(true)) await expect(() => provider.getUpdate(true)).resolves.toBe(undefined)
}) })
}) })

View File

@ -1,19 +1,18 @@
import * as assert from "assert"
import { normalize } from "../src/common/util" import { normalize } from "../src/common/util"
describe("util", () => { describe("util", () => {
describe("normalize", () => { describe("normalize", () => {
it("should remove multiple slashes", () => { it("should remove multiple slashes", () => {
assert.equal(normalize("//foo//bar//baz///mumble"), "/foo/bar/baz/mumble") expect(normalize("//foo//bar//baz///mumble")).toBe("/foo/bar/baz/mumble")
}) })
it("should remove trailing slashes", () => { it("should remove trailing slashes", () => {
assert.equal(normalize("qux///"), "qux") expect(normalize("qux///")).toBe("qux")
}) })
it("should preserve trailing slash if it exists", () => { it("should preserve trailing slash if it exists", () => {
assert.equal(normalize("qux///", true), "qux/") expect(normalize("qux///", true)).toBe("qux/")
assert.equal(normalize("qux", true), "qux") expect(normalize("qux", true)).toBe("qux")
}) })
}) })
}) })

1799
yarn.lock

File diff suppressed because it is too large Load Diff