Skip to content

Commit ca56440

Browse files
authored
Merge pull request #2852 from cdr/jsjoeio-2646-separate-testing
dev(testing): separate unit and e2e tests
2 parents 0506875 + 7e23575 commit ca56440

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+136
-76
lines changed

.github/workflows/ci.yaml

+13-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ jobs:
2727
with:
2828
args: ./ci/steps/lint.sh
2929

30-
test:
30+
test-unit:
31+
runs-on: ubuntu-latest
32+
steps:
33+
- uses: actions/checkout@v1
34+
- name: Run unit tests
35+
uses: ./ci/images/debian10
36+
with:
37+
args: ./ci/steps/test-unit.sh
38+
test-e2e:
3139
needs: linux-amd64
3240
runs-on: ubuntu-latest
3341
env:
@@ -44,19 +52,19 @@ jobs:
4452
run: |
4553
cd release-packages && tar -xzf code-server*-linux-amd64.tar.gz
4654
- uses: microsoft/playwright-github-action@v1
47-
- name: Install dependencies and run tests
55+
- name: Install dependencies and run end-to-end tests
4856
run: |
4957
./release-packages/code-server*-linux-amd64/bin/code-server --home $CODE_SERVER_ADDRESS/healthz &
5058
yarn --frozen-lockfile
51-
yarn test
59+
yarn test:e2e
5260
- name: Upload test artifacts
5361
if: always()
5462
uses: actions/upload-artifact@v2
5563
with:
5664
name: test-videos
57-
path: ./test/videos
65+
path: ./test/e2e/videos
5866
- name: Remove release packages and test artifacts
59-
run: rm -rf ./release-packages ./test/videos
67+
run: rm -rf ./release-packages ./test/e2e/videos
6068

6169
release:
6270
runs-on: ubuntu-latest

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ node-*
1616
.home
1717
coverage
1818
**/.DS_Store
19-
test/videos
20-
test/screenshots
19+
test/e2e/videos
20+
test/e2e/screenshots

ci/README.md

+11-7
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Make sure you have `$GITHUB_TOKEN` set and [hub](https://github.com/github/hub)
5252

5353
Currently, we run a command to manually generate the code coverage shield. Follow these steps:
5454

55-
1. Run `yarn test` and make sure all the tests are passing
55+
1. Run `yarn test:unit` and make sure all the tests are passing
5656
2. Run `yarn badges`
5757
3. Go into the README and change the color from `red` to `green` in this line:
5858

@@ -72,8 +72,10 @@ This directory contains scripts used for the development of code-server.
7272
- Runs formatters.
7373
- [./ci/dev/lint.sh](./dev/lint.sh) (`yarn lint`)
7474
- Runs linters.
75-
- [./ci/dev/test.sh](./dev/test.sh) (`yarn test`)
76-
- Runs tests.
75+
- [./ci/dev/test-unit.sh](./dev/test-unit.sh) (`yarn test:unit`)
76+
- Runs unit tests.
77+
- [./ci/dev/test-e2e.sh](./dev/test-e2e.sh) (`yarn test:e2e`)
78+
- Runs end-to-end tests.
7779
- [./ci/dev/ci.sh](./dev/ci.sh) (`yarn ci`)
7880
- Runs `yarn fmt`, `yarn lint` and `yarn test`.
7981
- [./ci/dev/watch.ts](./dev/watch.ts) (`yarn watch`)
@@ -142,11 +144,13 @@ This directory contains the scripts used in CI.
142144
Helps avoid clobbering the CI configuration.
143145

144146
- [./steps/fmt.sh](./steps/fmt.sh)
145-
- Runs `yarn fmt` after ensuring VS Code is patched.
147+
- Runs `yarn fmt`.
146148
- [./steps/lint.sh](./steps/lint.sh)
147-
- Runs `yarn lint` after ensuring VS Code is patched.
148-
- [./steps/test.sh](./steps/test.sh)
149-
- Runs `yarn test` after ensuring VS Code is patched.
149+
- Runs `yarn lint`.
150+
- [./steps/test-unit.sh](./steps/test-unit.sh)
151+
- Runs `yarn test:unit`.
152+
- [./steps/test-e2e.sh](./steps/test-e2e.sh)
153+
- Runs `yarn test:e2e`.
150154
- [./steps/release.sh](./steps/release.sh)
151155
- Runs the release process.
152156
- Generates the npm package at `./release`.

ci/dev/ci.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ main() {
66

77
yarn fmt
88
yarn lint
9-
yarn test
9+
yarn test:unit
1010
}
1111

1212
main "$@"

ci/dev/test.sh renamed to ci/dev/test-e2e.sh

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ set -euo pipefail
33

44
main() {
55
cd "$(dirname "$0")/../.."
6-
cd test/test-plugin
7-
make -s out/index.js
86
# We must keep jest in a sub-directory. See ../../test/package.json for more
97
# information. We must also run it from the root otherwise coverage will not
108
# include our source files.
11-
cd "$OLDPWD"
129
if [[ -z ${PASSWORD-} ]] || [[ -z ${CODE_SERVER_ADDRESS-} ]]; then
1310
echo "The end-to-end testing suites rely on your local environment"
1411
echo -e "\n"
@@ -21,7 +18,7 @@ main() {
2118
echo -e "\n"
2219
exit 1
2320
fi
24-
CS_DISABLE_PLUGINS=true ./test/node_modules/.bin/jest "$@"
21+
CS_DISABLE_PLUGINS=true ./test/node_modules/.bin/jest "$@" --config ./test/jest.e2e.config.ts
2522
}
2623

2724
main "$@"

ci/dev/test-unit.sh

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
main() {
5+
cd "$(dirname "$0")/../.."
6+
cd test/unit/test-plugin
7+
make -s out/index.js
8+
# We must keep jest in a sub-directory. See ../../test/package.json for more
9+
# information. We must also run it from the root otherwise coverage will not
10+
# include our source files.
11+
cd "$OLDPWD"
12+
CS_DISABLE_PLUGINS=true ./test/node_modules/.bin/jest "$@"
13+
}
14+
15+
main "$@"

ci/steps/test-e2e.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
main() {
5+
cd "$(dirname "$0")/../.."
6+
7+
"./release-packages/code-server*-linux-amd64/bin/code-server" --home "$CODE_SERVER_ADDRESS"/healthz &
8+
yarn --frozen-lockfile
9+
yarn test:e2e
10+
}
11+
12+
main "$@"

ci/steps/test.sh renamed to ci/steps/test-unit.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ main() {
66

77
yarn --frozen-lockfile
88

9-
yarn test
9+
yarn test:unit
1010
}
1111

1212
main "$@"

package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
"release:standalone": "./ci/build/build-standalone-release.sh",
1717
"release:github-draft": "./ci/build/release-github-draft.sh",
1818
"release:github-assets": "./ci/build/release-github-assets.sh",
19+
"test:e2e": "./ci/dev/test-e2e.sh",
1920
"test:standalone-release": "./ci/build/test-standalone-release.sh",
21+
"test:unit": "./ci/dev/test-unit.sh",
2022
"package": "./ci/build/build-packages.sh",
2123
"postinstall": "./ci/dev/postinstall.sh",
2224
"update:vscode": "./ci/dev/update-vscode.sh",
@@ -124,7 +126,8 @@
124126
"testPathIgnorePatterns": [
125127
"node_modules",
126128
"lib",
127-
"out"
129+
"out",
130+
"test/e2e"
128131
],
129132
"collectCoverage": true,
130133
"collectCoverageFrom": [
@@ -144,8 +147,6 @@
144147
"lines": 40
145148
}
146149
},
147-
"testTimeout": 30000,
148-
"globalSetup": "<rootDir>/test/globalSetup.ts",
149150
"modulePathIgnorePatterns": [
150151
"<rootDir>/lib/vscode",
151152
"<rootDir>/release-packages",
@@ -156,7 +157,7 @@
156157
"<rootDir>/release-images"
157158
],
158159
"moduleNameMapper": {
159-
"^.+\\.(css|less)$": "<rootDir>/test/cssStub.ts"
160+
"^.+\\.(css|less)$": "<rootDir>/test/utils/cssStub.ts"
160161
}
161162
}
162163
}

test/e2e.test.ts renamed to test/e2e/e2e.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { chromium, Page, Browser } from "playwright"
2-
import { CODE_SERVER_ADDRESS } from "./constants"
2+
import { CODE_SERVER_ADDRESS } from "../utils/constants"
33

44
let browser: Browser
55
let page: Page

test/goHome.test.ts renamed to test/e2e/goHome.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { chromium, Page, Browser, BrowserContext, Cookie } from "playwright"
2-
import { hash } from "../src/node/util"
3-
import { CODE_SERVER_ADDRESS, PASSWORD, STORAGE } from "./constants"
4-
import { createCookieIfDoesntExist } from "./helpers"
2+
import { hash } from "../../src/node/util"
3+
import { CODE_SERVER_ADDRESS, PASSWORD, STORAGE, E2E_VIDEO_DIR } from "../utils/constants"
4+
import { createCookieIfDoesntExist } from "../utils/helpers"
55

66
describe("go home", () => {
77
let browser: Browser
@@ -45,7 +45,7 @@ describe("go home", () => {
4545

4646
context = await browser.newContext({
4747
storageState: { cookies: maybeUpdatedCookies },
48-
recordVideo: { dir: "./test/videos/" },
48+
recordVideo: { dir: E2E_VIDEO_DIR },
4949
})
5050
})
5151

test/login.test.ts renamed to test/e2e/login.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { chromium, Page, Browser, BrowserContext } from "playwright"
2-
import { CODE_SERVER_ADDRESS, PASSWORD } from "./constants"
2+
import { CODE_SERVER_ADDRESS, PASSWORD } from "../utils/constants"
33

44
describe("login", () => {
55
let browser: Browser

test/jest.e2e.config.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// jest.config.ts
2+
import type { Config } from "@jest/types"
3+
4+
const config: Config.InitialOptions = {
5+
transform: {
6+
"^.+\\.ts$": "<rootDir>/node_modules/ts-jest",
7+
},
8+
globalSetup: "<rootDir>/utils/globalSetup.ts",
9+
testEnvironment: "node",
10+
testPathIgnorePatterns: ["node_modules", "lib", "out", "test/unit"],
11+
testTimeout: 30000,
12+
modulePathIgnorePatterns: [
13+
"<rootDir>/../lib/vscode",
14+
"<rootDir>/../release-packages",
15+
"<rootDir>/../release",
16+
"<rootDir>/../release-standalone",
17+
"<rootDir>/../release-npm-package",
18+
"<rootDir>/../release-gcp",
19+
"<rootDir>/../release-images",
20+
],
21+
}
22+
export default config

test/cli.test.ts renamed to test/unit/cli.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import * as fs from "fs-extra"
33
import * as net from "net"
44
import * as os from "os"
55
import * as path from "path"
6-
import { Args, parse, setDefaults, shouldOpenInExistingInstance } from "../src/node/cli"
7-
import { paths, tmpdir } from "../src/node/util"
6+
import { Args, parse, setDefaults, shouldOpenInExistingInstance } from "../../src/node/cli"
7+
import { paths, tmpdir } from "../../src/node/util"
88

99
type Mutable<T> = {
1010
-readonly [P in keyof T]: T[P]

test/constants.test.ts renamed to test/unit/constants.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { commit, getPackageJson, version } from "../src/node/constants"
2-
import { loggerModule } from "./helpers"
1+
import { commit, getPackageJson, version } from "../../src/node/constants"
2+
import { loggerModule } from "../utils/helpers"
33

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

77
describe("constants", () => {
88
describe("getPackageJson", () => {

test/emitter.test.ts renamed to test/unit/emitter.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Note: we need to import logger from the root
22
// because this is the logger used in logError in ../src/common/util
3-
import { logger } from "../node_modules/@coder/logger"
3+
import { logger } from "../../node_modules/@coder/logger"
44

5-
import { Emitter } from "../src/common/emitter"
5+
import { Emitter } from "../../src/common/emitter"
66

77
describe("emitter", () => {
88
let spy: jest.SpyInstance

test/health.test.ts renamed to test/unit/health.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as httpserver from "./httpserver"
2-
import * as integration from "./integration"
1+
import * as httpserver from "../utils/httpserver"
2+
import * as integration from "../utils/integration"
33

44
describe("health", () => {
55
let codeServer: httpserver.HttpServer | undefined

test/http.test.ts renamed to test/unit/http.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpCode, HttpError } from "../src/common/http"
1+
import { HttpCode, HttpError } from "../../src/common/http"
22

33
describe("http", () => {
44
describe("HttpCode", () => {

test/plugin.test.ts renamed to test/unit/plugin.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { logger } from "@coder/logger"
22
import * as express from "express"
33
import * as fs from "fs"
44
import * as path from "path"
5-
import { HttpCode } from "../src/common/http"
6-
import { AuthType } from "../src/node/cli"
7-
import { codeServer, PluginAPI } from "../src/node/plugin"
8-
import * as apps from "../src/node/routes/apps"
9-
import * as httpserver from "./httpserver"
5+
import { HttpCode } from "../../src/common/http"
6+
import { AuthType } from "../../src/node/cli"
7+
import { codeServer, PluginAPI } from "../../src/node/plugin"
8+
import * as apps from "../../src/node/routes/apps"
9+
import * as httpserver from "../utils/httpserver"
1010
const fsp = fs.promises
1111

1212
// Jest overrides `require` so our usual override doesn't work.

test/proxy.test.ts renamed to test/unit/proxy.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import bodyParser from "body-parser"
22
import * as express from "express"
3-
import * as httpserver from "./httpserver"
4-
import * as integration from "./integration"
3+
import * as httpserver from "../utils/httpserver"
4+
import * as integration from "../utils/integration"
55

66
describe("proxy", () => {
77
const nhooyrDevServer = new httpserver.HttpServer()

test/register.test.ts renamed to test/unit/register.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { JSDOM } from "jsdom"
2-
import { loggerModule } from "./helpers"
2+
import { loggerModule } from "../utils/helpers"
33

44
describe("register", () => {
55
describe("when navigator and serviceWorker are defined", () => {
@@ -40,7 +40,7 @@ describe("register", () => {
4040

4141
it("should register a ServiceWorker", () => {
4242
// Load service worker like you would in the browser
43-
require("../src/browser/register")
43+
require("../../src/browser/register")
4444
expect(mockRegisterFn).toHaveBeenCalled()
4545
expect(mockRegisterFn).toHaveBeenCalledTimes(1)
4646
})
@@ -54,7 +54,7 @@ describe("register", () => {
5454
})
5555

5656
// Load service worker like you would in the browser
57-
require("../src/browser/register")
57+
require("../../src/browser/register")
5858

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

7979
it("should log an error to the console", () => {
8080
// Load service worker like you would in the browser
81-
require("../src/browser/register")
81+
require("../../src/browser/register")
8282
expect(spy).toHaveBeenCalled()
8383
expect(spy).toHaveBeenCalledTimes(1)
8484
expect(spy).toHaveBeenCalledWith("[Service Worker] navigator is undefined")

test/serviceWorker.test.ts renamed to test/unit/serviceWorker.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe("serviceWorker", () => {
5858
})
5959

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

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

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

7777
it("should do nothing when 'fetch' is called", async () => {
78-
require("../src/browser/serviceWorker.ts")
78+
require("../../src/browser/serviceWorker.ts")
7979
emit("fetch")
8080
expect(spy).not.toHaveBeenCalled()
8181
})
8282

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

8787
// Activate serviceWorker

0 commit comments

Comments
 (0)