-
Notifications
You must be signed in to change notification settings - Fork 5.9k
feat(testing): add unit tests for constants #2701
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
164d11e
chore: clean up comment in util.test
jsjoeio 719481e
refactor: add getPackageJson fn in constants
jsjoeio a2a6122
feat: add tests for constants
jsjoeio c7c851d
feat: add tests for src/common/http
jsjoeio f1337d5
Merge pull request #2702 from cdr/add-unit-tests-http
jsjoeio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
import { logger } from "@coder/logger" | ||
import { JSONSchemaForNPMPackageJsonFiles } from "@schemastore/package" | ||
import * as path from "path" | ||
|
||
let pkg: { version?: string; commit?: string } = {} | ||
try { | ||
pkg = require("../../package.json") | ||
} catch (error) { | ||
logger.warn(error.message) | ||
export function getPackageJson(relativePath: string): JSONSchemaForNPMPackageJsonFiles { | ||
let pkg = {} | ||
try { | ||
pkg = require(relativePath) | ||
} catch (error) { | ||
logger.warn(error.message) | ||
} | ||
|
||
return pkg | ||
} | ||
|
||
const pkg = getPackageJson("../../package.json") | ||
|
||
export const version = pkg.version || "development" | ||
export const commit = pkg.commit || "development" | ||
export const rootPath = path.resolve(__dirname, "../..") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// 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 { commit, getPackageJson, version } from "../src/node/constants" | ||
|
||
describe("constants", () => { | ||
describe("getPackageJson", () => { | ||
let spy: jest.SpyInstance | ||
|
||
beforeEach(() => { | ||
spy = jest.spyOn(logger, "warn") | ||
}) | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
afterAll(() => { | ||
jest.restoreAllMocks() | ||
}) | ||
|
||
it("should log a warning if package.json not found", () => { | ||
const expectedErrorMessage = "Cannot find module './package.json' from 'src/node/constants.ts'" | ||
|
||
getPackageJson("./package.json") | ||
|
||
expect(spy).toHaveBeenCalled() | ||
expect(spy).toHaveBeenCalledWith(expectedErrorMessage) | ||
}) | ||
|
||
it("should find the package.json", () => { | ||
// the function calls require from src/node/constants | ||
// so to get the root package.json we need to use ../../ | ||
const packageJson = getPackageJson("../../package.json") | ||
expect(Object.keys(packageJson).length).toBeGreaterThan(0) | ||
expect(packageJson.name).toBe("code-server") | ||
expect(packageJson.description).toBe("Run VS Code on a remote server.") | ||
expect(packageJson.repository).toBe("https://github.com/cdr/code-server") | ||
}) | ||
}) | ||
describe("version", () => { | ||
it("should return the package.json version", () => { | ||
// Source: https://gist.github.com/jhorsman/62eeea161a13b80e39f5249281e17c39#gistcomment-2896416 | ||
const validSemVar = new RegExp("^(0|[1-9]d*).(0|[1-9]d*).(0|[1-9]d*)") | ||
const isValidSemVar = validSemVar.test(version) | ||
expect(version).not.toBe(null) | ||
expect(isValidSemVar).toBe(true) | ||
}) | ||
}) | ||
|
||
describe("commit", () => { | ||
it("should return 'development' if commit is undefined", () => { | ||
// In development, the commit is not stored in our package.json | ||
// But when we build code-server and release it, it is | ||
expect(commit).toBe("development") | ||
}) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { HttpCode, HttpError } from "../src/common/http" | ||
|
||
describe("http", () => { | ||
describe("HttpCode", () => { | ||
it("should return the correct HTTP codes", () => { | ||
expect(HttpCode.Ok).toBe(200) | ||
expect(HttpCode.Redirect).toBe(302) | ||
expect(HttpCode.NotFound).toBe(404) | ||
expect(HttpCode.BadRequest).toBe(400) | ||
expect(HttpCode.Unauthorized).toBe(401) | ||
expect(HttpCode.LargePayload).toBe(413) | ||
expect(HttpCode.ServerError).toBe(500) | ||
}) | ||
}) | ||
|
||
describe("HttpError", () => { | ||
it("should work as expected", () => { | ||
const message = "Bad request from client" | ||
const httpError = new HttpError(message, HttpCode.BadRequest) | ||
|
||
expect(httpError.message).toBe(message) | ||
expect(httpError.status).toBe(400) | ||
expect(httpError.details).toBeUndefined() | ||
}) | ||
it("should have details if provided", () => { | ||
const details = { | ||
message: "User needs to be signed-in in order to perform action", | ||
} | ||
const message = "Unauthorized" | ||
const httpError = new HttpError(message, HttpCode.BadRequest, details) | ||
|
||
expect(httpError.details).toStrictEqual(details) | ||
}) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,10 +125,6 @@ describe("util", () => { | |
}) | ||
|
||
describe("getOptions", () => { | ||
// Things to mock | ||
// logger | ||
// location | ||
// document | ||
Comment on lines
-128
to
-131
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment disappeared! no other changes in this file; curious about the change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was intentional! See commit: 164d11e |
||
beforeEach(() => { | ||
const location: LocationLike = { | ||
pathname: "/healthz", | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this was a manual change or something done automatically? Not particularly an issue, just curious 👀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that happens when you add a new dependency to the top? Not sure, I did see it in one of Asher's PRs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah
yarn
resorts the dependencies. 😄There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The real question is how it got out of order in the first place lol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LOL I have no idea - could have been me 😂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha idk I say we just blame
yarn
.