Skip to content

Commit 438c904

Browse files
authored
chore: cleanup all test files (#293)
## Changes made - Removed all unused imports, and made sure type imports were labeled correctly - Updated all comparisons to be more strict - Simplified loops to remove unneeded closure functions - Removed all explicit `any` types - Updated how strings were defined to follow general TypeScript best practices ## Notes - We definitely want some kind of linting setup for this repo. I'm going to bring this up when Blueberry has its next team meeting next week
1 parent bd6747f commit 438c904

File tree

16 files changed

+52
-55
lines changed

16 files changed

+52
-55
lines changed

aws-region/main.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, expect, it } from "bun:test";
22
import {
3-
executeScriptInContainer,
43
runTerraformApply,
54
runTerraformInit,
65
testRequiredVariables,

azure-region/main.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, expect, it } from "bun:test";
22
import {
3-
executeScriptInContainer,
43
runTerraformApply,
54
runTerraformInit,
65
testRequiredVariables,

coder-login/main.test.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import { describe, expect, it } from "bun:test";
2-
import {
3-
executeScriptInContainer,
4-
runTerraformApply,
5-
runTerraformInit,
6-
testRequiredVariables,
7-
} from "../test";
1+
import { describe } from "bun:test";
2+
import { runTerraformInit, testRequiredVariables } from "../test";
83

94
describe("coder-login", async () => {
105
await runTerraformInit(import.meta.dir);

cursor/main.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, expect, it } from "bun:test";
22
import {
3-
executeScriptInContainer,
43
runTerraformApply,
54
runTerraformInit,
65
testRequiredVariables,

exoscale-zone/main.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, expect, it } from "bun:test";
22
import {
3-
executeScriptInContainer,
43
runTerraformApply,
54
runTerraformInit,
65
testRequiredVariables,

github-upload-public-key/main.test.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { type Server, serve } from "bun";
12
import { describe, expect, it } from "bun:test";
23
import {
34
createJSONResponse,
@@ -9,7 +10,6 @@ import {
910
testRequiredVariables,
1011
writeCoder,
1112
} from "../test";
12-
import { Server, serve } from "bun";
1313

1414
describe("github-upload-public-key", async () => {
1515
await runTerraformInit(import.meta.dir);
@@ -21,10 +21,12 @@ describe("github-upload-public-key", async () => {
2121
it("creates new key if one does not exist", async () => {
2222
const { instance, id, server } = await setupContainer();
2323
await writeCoder(id, "echo foo");
24-
let exec = await execContainer(id, [
24+
25+
const url = server.url.toString().slice(0, -1);
26+
const exec = await execContainer(id, [
2527
"env",
26-
"CODER_ACCESS_URL=" + server.url.toString().slice(0, -1),
27-
"GITHUB_API_URL=" + server.url.toString().slice(0, -1),
28+
`CODER_ACCESS_URL=${url}`,
29+
`GITHUB_API_URL=${url}`,
2830
"CODER_OWNER_SESSION_TOKEN=foo",
2931
"CODER_EXTERNAL_AUTH_ID=github",
3032
"bash",
@@ -42,10 +44,12 @@ describe("github-upload-public-key", async () => {
4244
const { instance, id, server } = await setupContainer();
4345
// use keyword to make server return a existing key
4446
await writeCoder(id, "echo findkey");
45-
let exec = await execContainer(id, [
47+
48+
const url = server.url.toString().slice(0, -1);
49+
const exec = await execContainer(id, [
4650
"env",
47-
"CODER_ACCESS_URL=" + server.url.toString().slice(0, -1),
48-
"GITHUB_API_URL=" + server.url.toString().slice(0, -1),
51+
`CODER_ACCESS_URL=${url}`,
52+
`GITHUB_API_URL=${url}`,
4953
"CODER_OWNER_SESSION_TOKEN=foo",
5054
"CODER_EXTERNAL_AUTH_ID=github",
5155
"bash",
@@ -95,7 +99,7 @@ const setupServer = async (): Promise<Server> => {
9599
}
96100

97101
// case: key already exists
98-
if (req.headers.get("Authorization") == "Bearer findkey") {
102+
if (req.headers.get("Authorization") === "Bearer findkey") {
99103
return createJSONResponse([
100104
{
101105
key: "foo",

jfrog-oauth/main.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ EOF`;
116116
const proxies = ["foo", "bar", "baz"]
117117
.map((r) => `https://${user}:@${fakeFrogApi}/go/${r}`)
118118
.join(",");
119-
expect(proxyEnv["value"]).toEqual(proxies);
119+
expect(proxyEnv.value).toEqual(proxies);
120120

121121
const coderScript = findResourceInstance(state, "coder_script");
122122
expect(coderScript.script).toContain(

jfrog-token/main.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ EOF`;
151151
const proxies = ["foo", "bar", "baz"]
152152
.map((r) => `https://${user}:${token}@${fakeFrogApi}/go/${r}`)
153153
.join(",");
154-
expect(proxyEnv["value"]).toEqual(proxies);
154+
expect(proxyEnv.value).toEqual(proxies);
155155

156156
const coderScript = findResourceInstance(state, "coder_script");
157157
expect(coderScript.script).toContain(

jupyterlab/main.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import { describe, expect, it } from "bun:test";
22
import {
3+
execContainer,
34
executeScriptInContainer,
5+
findResourceInstance,
6+
runContainer,
47
runTerraformApply,
58
runTerraformInit,
69
testRequiredVariables,
7-
findResourceInstance,
8-
runContainer,
9-
TerraformState,
10-
execContainer,
10+
type TerraformState,
1111
} from "../test";
1212

1313
// executes the coder script after installing pip
1414
const executeScriptInContainerWithPip = async (
1515
state: TerraformState,
1616
image: string,
17-
shell: string = "sh",
17+
shell = "sh",
1818
): Promise<{
1919
exitCode: number;
2020
stdout: string[];

nodejs/main.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it } from "bun:test";
1+
import { describe } from "bun:test";
22
import { runTerraformInit, testRequiredVariables } from "../test";
33

44
describe("nodejs", async () => {

personalize/main.test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import { readableStreamToText, spawn } from "bun";
21
import { describe, expect, it } from "bun:test";
32
import {
43
executeScriptInContainer,
54
runTerraformApply,
65
runTerraformInit,
76
testRequiredVariables,
8-
runContainer,
9-
execContainer,
10-
findResourceInstance,
117
} from "../test";
128

139
describe("personalize", async () => {

slackme/main.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ executed`,
7272
it("formats execution with milliseconds", async () => {
7373
await assertSlackMessage({
7474
command: "echo test",
75-
format: `$COMMAND took $DURATION`,
75+
format: "$COMMAND took $DURATION",
7676
durationMS: 150,
7777
output: "echo test took 150ms",
7878
});
@@ -81,7 +81,7 @@ executed`,
8181
it("formats execution with seconds", async () => {
8282
await assertSlackMessage({
8383
command: "echo test",
84-
format: `$COMMAND took $DURATION`,
84+
format: "$COMMAND took $DURATION",
8585
durationMS: 15000,
8686
output: "echo test took 15.0s",
8787
});
@@ -90,7 +90,7 @@ executed`,
9090
it("formats execution with minutes", async () => {
9191
await assertSlackMessage({
9292
command: "echo test",
93-
format: `$COMMAND took $DURATION`,
93+
format: "$COMMAND took $DURATION",
9494
durationMS: 120000,
9595
output: "echo test took 2m 0.0s",
9696
});
@@ -99,7 +99,7 @@ executed`,
9999
it("formats execution with hours", async () => {
100100
await assertSlackMessage({
101101
command: "echo test",
102-
format: `$COMMAND took $DURATION`,
102+
format: "$COMMAND took $DURATION",
103103
durationMS: 60000 * 60,
104104
output: "echo test took 1hr 0m 0.0s",
105105
});

test.ts

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readableStreamToText, spawn } from "bun";
2-
import { afterEach, expect, it } from "bun:test";
3-
import { readFile, unlink } from "fs/promises";
2+
import { expect, it } from "bun:test";
3+
import { readFile, unlink } from "node:fs/promises";
44

55
export const runContainer = async (
66
image: string,
@@ -21,7 +21,8 @@ export const runContainer = async (
2121
"-c",
2222
init,
2323
]);
24-
let containerID = await readableStreamToText(proc.stdout);
24+
25+
const containerID = await readableStreamToText(proc.stdout);
2526
const exitCode = await proc.exited;
2627
if (exitCode !== 0) {
2728
throw new Error(containerID);
@@ -36,7 +37,7 @@ export const runContainer = async (
3637
export const executeScriptInContainer = async (
3738
state: TerraformState,
3839
image: string,
39-
shell: string = "sh",
40+
shell = "sh",
4041
): Promise<{
4142
exitCode: number;
4243
stdout: string[];
@@ -116,6 +117,9 @@ export interface CoderScriptAttributes {
116117
url: string;
117118
}
118119

120+
export type ResourceInstance<T extends string = string> =
121+
T extends "coder_script" ? CoderScriptAttributes : Record<string, string>;
122+
119123
/**
120124
* finds the first instance of the given resource type in the given state. If
121125
* name is specified, it will only find the instance with the given name.
@@ -124,10 +128,7 @@ export const findResourceInstance = <T extends string>(
124128
state: TerraformState,
125129
type: T,
126130
name?: string,
127-
// if type is "coder_script" return CoderScriptAttributes
128-
): T extends "coder_script"
129-
? CoderScriptAttributes
130-
: Record<string, string> => {
131+
): ResourceInstance<T> => {
131132
const resource = state.resources.find(
132133
(resource) =>
133134
resource.type === type && (name ? resource.name === name : true),
@@ -140,7 +141,8 @@ export const findResourceInstance = <T extends string>(
140141
`Resource ${type} has ${resource.instances.length} instances`,
141142
);
142143
}
143-
return resource.instances[0].attributes as any;
144+
145+
return resource.instances[0].attributes as ResourceInstance<T>;
144146
};
145147

146148
/**
@@ -157,15 +159,15 @@ export const testRequiredVariables = <TVars extends TerraformVariables>(
157159
});
158160

159161
const varNames = Object.keys(vars);
160-
varNames.forEach((varName) => {
162+
for (const varName of varNames) {
161163
// Ensures that every variable provided is required!
162-
it("missing variable " + varName, async () => {
164+
it(`missing variable: ${varName}`, async () => {
163165
const localVars: TerraformVariables = {};
164-
varNames.forEach((otherVarName) => {
166+
for (const otherVarName of varNames) {
165167
if (otherVarName !== varName) {
166168
localVars[otherVarName] = vars[otherVarName];
167169
}
168-
});
170+
}
169171

170172
try {
171173
await runTerraformApply(dir, localVars);
@@ -181,7 +183,7 @@ export const testRequiredVariables = <TVars extends TerraformVariables>(
181183
}
182184
throw new Error(`${varName} is not a required variable!`);
183185
});
184-
});
186+
}
185187
};
186188

187189
/**

tsconfig.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
{
22
"compilerOptions": {
3-
"target": "esnext",
4-
"module": "nodenext",
3+
// If we were just compiling for the tests, we could safely target ESNext at
4+
// all times, but just because we've been starting to add more runtime logic
5+
// files to some of the modules, erring on the side of caution by having a
6+
// older compilation target
7+
"target": "ES6",
8+
"module": "ESNext",
59
"strict": true,
610
"allowSyntheticDefaultImports": true,
7-
"moduleResolution": "nodenext",
11+
"moduleResolution": "node",
812
"types": ["bun-types"]
913
}
1014
}

vscode-desktop/main.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe("vscode-desktop", async () => {
2222
);
2323

2424
const coder_app = state.resources.find(
25-
(res) => res.type == "coder_app" && res.name == "vscode",
25+
(res) => res.type === "coder_app" && res.name === "vscode",
2626
);
2727

2828
expect(coder_app).not.toBeNull();
@@ -79,7 +79,7 @@ describe("vscode-desktop", async () => {
7979
});
8080

8181
const coder_app = state.resources.find(
82-
(res) => res.type == "coder_app" && res.name == "vscode",
82+
(res) => res.type === "coder_app" && res.name === "vscode",
8383
);
8484

8585
expect(coder_app).not.toBeNull();

windows-rdp/main.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from "bun:test";
22
import {
3-
TerraformState,
3+
type TerraformState,
44
runTerraformApply,
55
runTerraformInit,
66
testRequiredVariables,

0 commit comments

Comments
 (0)