Skip to content

Commit a404e59

Browse files
committed
refactor: tmpdir and add to test utils
1 parent b0ecff3 commit a404e59

File tree

4 files changed

+39
-31
lines changed

4 files changed

+39
-31
lines changed

test/e2e/models/CodeServer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class CodeServer {
4747
*/
4848
async isEditorVisible() {
4949
// Make sure the editor actually loaded
50-
// If it's not visible after 2 seconds, something is wrong
50+
// If it's not visible after 5 seconds, something is wrong
5151
await this.page.waitForLoadState("networkidle")
5252
return await this.page.isVisible("div.monaco-workbench", { timeout: 5000 })
5353
}

test/e2e/terminal.test.ts

+12-30
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { test, expect } from "@playwright/test"
2+
import * as cp from "child_process"
23
import * as fs from "fs"
3-
import { tmpdir } from "os"
4+
// import { tmpdir } from "os"
45
import * as path from "path"
56
import util from "util"
6-
import * as cp from "child_process"
7-
import { STORAGE } from "../utils/constants"
7+
import { STORAGE, tmpdir } from "../utils/constants"
88
import { CodeServer } from "./models/CodeServer"
99

1010
test.describe("Integrated Terminal", () => {
@@ -14,8 +14,8 @@ test.describe("Integrated Terminal", () => {
1414
const testFileName = "pipe"
1515
const testString = "new string test from e2e test"
1616
let codeServer: CodeServer
17-
let tmpFolderPath: string = ""
18-
let tmpFile: string = ""
17+
let tmpFolderPath = ""
18+
let tmpFile = ""
1919

2020
// TODO@jsjoeio
2121
// Fix this once https://github.com/microsoft/playwright-test/issues/240
@@ -26,20 +26,19 @@ test.describe("Integrated Terminal", () => {
2626
storageState,
2727
}
2828
}
29+
test.beforeAll(async () => {
30+
tmpFolderPath = await tmpdir("integrated-terminal")
31+
tmpFile = path.join(tmpFolderPath, testFileName)
32+
})
33+
2934
test.beforeEach(async ({ page }) => {
3035
codeServer = new CodeServer(page)
3136
await codeServer.setup()
32-
// NOTE@jsjoeio
33-
// We're not using tmpdir from src/node/constants
34-
// because Playwright doesn't fully support ES modules from
35-
// the erorrs I'm seeing
36-
tmpFolderPath = fs.mkdtempSync(path.join(tmpdir(), "code-server-test"))
37-
tmpFile = path.join(tmpFolderPath, testFileName)
3837
})
3938

40-
test.afterEach(async () => {
39+
test.afterAll(async () => {
4140
// Ensure directory was removed
42-
fs.rmdirSync(tmpFolderPath, { recursive: true })
41+
await fs.promises.rmdir(tmpFolderPath, { recursive: true })
4342
})
4443

4544
test("should echo a string to a file", options, async ({ page }) => {
@@ -56,22 +55,5 @@ test.describe("Integrated Terminal", () => {
5655

5756
const { stdout } = await output
5857
expect(stdout).toMatch(testString)
59-
60-
// .access checks if the file exists without opening it
61-
// it doesn't return anything hence why we expect it to
62-
// resolve to undefined
63-
// If the promise rejects (i.e. the file doesn't exist)
64-
// then the assertion will fail
65-
await expect(fs.promises.access(tmpFile)).resolves.toBeUndefined()
66-
67-
await fs.promises.rmdir(tmpFolderPath, { recursive: true })
68-
// Make sure neither file nor folder exist
69-
// Note: We have to use ts-ignore because of an upstream typing error
70-
// See: https://github.com/microsoft/folio/issues/230#event-4621948411
71-
/* eslint-disable @typescript-eslint/ban-ts-comment */
72-
// @ts-ignore
73-
expect(fs.promises.access(tmpFile)).rejects.toThrowError(/no such file or directory/)
74-
// @ts-ignore
75-
expect(fs.promises.access(tmpFolderPath)).rejects.toThrowError(/no such file or directory/)
7658
})
7759
})

test/unit/constants.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import * as fs from "fs"
12
import { commit, getPackageJson, version } from "../../src/node/constants"
3+
import { tmpdir } from "../../test/utils/constants"
24
import { loggerModule } from "../utils/helpers"
35

46
// jest.mock is hoisted above the imports so we must use `require` here.
@@ -51,3 +53,16 @@ describe("constants", () => {
5153
})
5254
})
5355
})
56+
57+
describe("test constants", () => {
58+
describe("tmpdir", () => {
59+
it("should return a temp directory", async () => {
60+
const testName = "temp-dir"
61+
const pathToTempDir = await tmpdir(testName)
62+
63+
expect(pathToTempDir).toContain(testName)
64+
65+
await fs.rmdir(pathToTempDir, () => {})
66+
})
67+
})
68+
})

test/utils/constants.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
import * as fs from "fs"
2+
import * as os from "os"
3+
import * as path from "path"
4+
15
export const CODE_SERVER_ADDRESS = process.env.CODE_SERVER_ADDRESS || "http://localhost:8080"
26
export const PASSWORD = process.env.PASSWORD || "e45432jklfdsab"
37
export const STORAGE = process.env.STORAGE || ""
8+
9+
export async function tmpdir(testName: string): Promise<string> {
10+
const dir = path.join(os.tmpdir(), "code-server")
11+
await fs.promises.mkdir(dir, { recursive: true })
12+
13+
return await fs.promises.mkdtemp(path.join(dir, `test-${testName}-`), { encoding: "utf8" })
14+
}

0 commit comments

Comments
 (0)