Skip to content

Commit

Permalink
fix: Adapt tests to new tsconfig options
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomtec331 committed Feb 10, 2024
1 parent d947bac commit fc3fee8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
16 changes: 10 additions & 6 deletions backend/test/integration/MongoDBPersister.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import { MongoClient } from "mongodb";
import MongoDBPersister from "../../src/database/MongoDBPersister";
import { beforeAll, it, expect, afterAll } from "@jest/globals";

let instance: MongoDBPersister = null;
let connection: MongoClient = null;
let instance: MongoDBPersister;
let connection: MongoClient;

beforeAll(async () => {
connection = await MongoClient.connect(process.env.MONGO_URL);
instance = new MongoDBPersister(process.env.MONGO_URL);
const ENV_MONGO_URL = process.env.MONGO_URL;

if (!ENV_MONGO_URL) throw new Error("MongoDB url not provided (MONGO_URL).");

connection = await MongoClient.connect(ENV_MONGO_URL);
instance = new MongoDBPersister(ENV_MONGO_URL);
try {
await connection.db().dropCollection("users");
} catch (err) {}
Expand Down Expand Up @@ -39,13 +43,13 @@ it("successfully adds a new environment to a user's existing list of environment
"testuser",
"some-uuid",
"some-description",
"some-instance"
"some-instance",
);
const result = await connection
.db()
.collection("users")
.findOne({ username: "testuser" });
expect(result.environments).toEqual([
expect(result?.environments).toEqual([
{ identifier: "environmentXYZ" },
{ identifier: "some-uuid", description: "some-description" },
]);
Expand Down
16 changes: 10 additions & 6 deletions backend/test/integration/User.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import LocalVMProvider from "../../src/providers/LocalVMProvider";
import { beforeAll, describe, it, expect, afterAll } from "@jest/globals";

const app = express();
let connection: MongoClient = null;
let instance: MongoDBPersister = null;
let connection: MongoClient;
let instance: MongoDBPersister;

beforeAll(async () => {
connection = await MongoClient.connect(process.env.MONGO_URL);
const ENV_MONGO_URL = process.env.MONGO_URL;

if (!ENV_MONGO_URL) throw new Error("MongoDB url not provided (MONGO_URL).");

connection = await MongoClient.connect(ENV_MONGO_URL);
try {
await connection.db().dropCollection("users");
} catch (err) {}
Expand All @@ -22,13 +26,13 @@ beforeAll(async () => {
password: "testpassword",
environments: [],
});
instance = new MongoDBPersister(process.env.MONGO_URL);
instance = new MongoDBPersister(ENV_MONGO_URL);
app.use(
APIRoutes(
instance,
[new MongoDBAuthenticationProvider(instance)],
new LocalVMProvider()
)
new LocalVMProvider(),
),
);
});

Expand Down
12 changes: 7 additions & 5 deletions backend/test/unit/AuthenticationMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const mockRequest: Partial<Request> = {
};
const mockResponse: Partial<Response> = {
status: jest.fn().mockReturnThis() as (
code: number
code: number,
) => Response<unknown, Record<string, unknown>>,
json: jest.fn().mockReturnThis() as Send,
};
Expand All @@ -17,7 +17,7 @@ test("returns 401 if authentication token is not present in authorization header
AuthenticationMiddleware(
mockRequest as Request,
mockResponse as Response,
nexthandler
nexthandler,
);
expect(nexthandler).not.toHaveBeenCalled();
expect(mockResponse.status).toHaveBeenCalledWith(401);
Expand All @@ -26,18 +26,20 @@ test("returns 401 if authentication token is not present in authorization header
test("calls next() if a proper token was passed", () => {
const nexthandler = jest.fn();

if (!mockRequest.headers) mockRequest.headers = {};

mockRequest.headers.authorization = jwt.sign(
{
username: "testuser",
id: "testid",
},
/* replace secret */
"some-secret"
/* TODO: replace secret */
"some-secret",
);
AuthenticationMiddleware(
mockRequest as Request,
mockResponse as Response,
nexthandler
nexthandler,
);
expect(nexthandler).toHaveBeenCalled();
expect(mockRequest).toMatchObject({
Expand Down
4 changes: 2 additions & 2 deletions backend/test/unit/CompilationResultExtractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ test("it extracts errors successfully", () => {
extractCompilationResult(
`/tmp/test-wHXUrU/test.p4(13):syntax error, unexpected TYPEDEF, expecting ;
typedef
error: 1 errors encountered, aborting compilation\n`
)
error: 1 errors encountered, aborting compilation\n`,
),
).toEqual([
{
line: 13,
Expand Down

0 comments on commit fc3fee8

Please sign in to comment.