From 29ac9af81d94f7de298d98d9597c73ddd1c93bb0 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 12 Jul 2021 15:27:24 +0200 Subject: Improved the unit tests --- test/unit/commit.unit.test.ts | 70 ++++++++++++------- test/unit/repository.unit.test.ts | 137 ++++++++++++++++++++++++++------------ test/util.ts | 20 ++++++ 3 files changed, 161 insertions(+), 66 deletions(-) create mode 100644 test/util.ts diff --git a/test/unit/commit.unit.test.ts b/test/unit/commit.unit.test.ts index f197583..b37e9cf 100644 --- a/test/unit/commit.unit.test.ts +++ b/test/unit/commit.unit.test.ts @@ -1,5 +1,10 @@ import { Repository } from "server/src/git/repository"; import { Commit } from "server/src/git/commit"; +import { EnvironmentVariables, expectCommitProperties } from "../util"; +import { Diff } from "server/src/git/diff"; +import { Tree } from "server/src/git/tree"; + +const env = process.env as EnvironmentVariables; jest.setTimeout(10000); @@ -7,58 +12,73 @@ describe("Commit", () => { let repository: Repository; beforeAll(async () => { - repository = await Repository.open(process.env.BASE_DIR, process.env.AVAIL_REPO); + repository = await Repository.open(env.BASE_DIR, env.AVAIL_REPO); }); - test("Lookup a existing commit by id", async () => { - const lookupCommit = jest.fn(() => Commit.lookup(repository, process.env.AVAIL_COMMIT)); + it("Looks up a commit", async () => { + expect.assertions(8); + + const commit = await Commit.lookup(repository, env.AVAIL_COMMIT); - const commit = await lookupCommit(); + expect(commit).toBeDefined(); + expect(commit).toBeInstanceOf(Commit); - expect(lookupCommit).toReturn(); - expect(commit.id).toBe(process.env.AVAIL_COMMIT); + expectCommitProperties(commit); }); - test("Lookup a nonexistant commit by id throws", async () => { - expect(Commit.lookup(repository, process.env.UNAVAIL_COMMIT)).rejects.toThrow(); + it("Looks up a nonexistant commit and throws", async () => { + expect.assertions(1); + + await expect(Commit.lookup(repository, env.UNAVAIL_COMMIT)).rejects.toThrow(); }); - test("Lookup if an existing commit exists by id", async () => { - expect(Commit.lookupExists(repository, process.env.AVAIL_COMMIT)).resolves.toBeTruthy(); + it("Looks up if an commit that exists exist", async () => { + expect.assertions(1); + + await expect(Commit.lookupExists(repository, env.AVAIL_COMMIT)).resolves.toBeTruthy(); }); - test("Lookup if an nonexistant commit exists by id", async () => { - expect(Commit.lookupExists(repository, process.env.UNAVAIL_COMMIT)).resolves.toBeFalsy(); + it("Looks up if an nonexistant commit exists", async () => { + expect.assertions(1); + + await expect(Commit.lookupExists(repository, env.UNAVAIL_COMMIT)).resolves.toBeFalsy(); }); - describe("Functions", () => { + describe("Methods", () => { let commit: Commit; beforeAll(async () => { commit = await repository.latestCommit(); }); - test("Get stats", async () => { - const getStats = jest.fn(() => commit.stats()); - await getStats(); + it("Gets the stats", async () => { + expect.assertions(4); + + const stats = await commit.stats(); + + expect(stats).toBeDefined(); - expect(getStats).toReturn(); + expect(stats).toHaveProperty("insertions"); + expect(stats).toHaveProperty("deletions"); + expect(stats).toHaveProperty("files_changed"); }); - test("Get diff", async () => { - const getDiff = jest.fn(() => commit.diff()); + it("Gets the diff", async () => { + expect.assertions(2); - await getDiff(); + const diff = await commit.diff(); - expect(getDiff).toReturn(); + expect(diff).toBeDefined(); + expect(diff).toBeInstanceOf(Diff); }); - test("Get tree", async () => { - const getTree = jest.fn(() => commit.tree()); + it("Gets the tree", async () => { + expect.assertions(2); - await getTree(); + const tree = await commit.tree(); - expect(getTree).toReturn(); + expect(tree).toBeDefined(); + expect(tree).toBeInstanceOf(Tree); }); }); }); \ No newline at end of file diff --git a/test/unit/repository.unit.test.ts b/test/unit/repository.unit.test.ts index 2a68a29..86ac55e 100644 --- a/test/unit/repository.unit.test.ts +++ b/test/unit/repository.unit.test.ts @@ -1,83 +1,138 @@ import { Repository } from "server/src/git/repository"; +import { Commit } from "server/src/git/commit"; +import { Tree } from "server/src/git/tree"; +import { Branch } from "server/src/git/branch"; +import { Tag } from "server/src/git/tag"; +import { EnvironmentVariables, expectCommitProperties } from "../util"; + +const env = process.env as EnvironmentVariables; + +function expectRepositoryProperties(repository: Repository) { + expect(repository).toHaveProperty("base_dir"); + expect(repository).toHaveProperty("description"); + expect(repository).toHaveProperty("name"); + expect(repository).toHaveProperty("name.full"); + expect(repository).toHaveProperty("name.short"); + expect(repository).toHaveProperty("owner"); +} describe("Repository", () => { - test("Open existing repository", async () => { - const openRepository = jest.fn(() => Repository.open(process.env.BASE_DIR, process.env.AVAIL_REPO)); + it("Opens a repository successfully", async () => { + expect.assertions(8); - await openRepository(); - - expect(openRepository).toReturn(); + const repository = await Repository.open(process.env.BASE_DIR, env.AVAIL_REPO); + + expect(repository).toBeDefined(); + expect(repository).toBeInstanceOf(Repository); + + expectRepositoryProperties(repository); }); - test("Open nonexistant repository throws", async () => { - expect(Repository.open(process.env.BASE_DIR, process.env.UNAVAIL_REPO)).rejects.toThrow(); + it("Fails to open a nonexistant repository", async () => { + expect.assertions(1); + + await expect(Repository.open(process.env.BASE_DIR, env.UNAVAIL_REPO)).rejects.toThrow(); }); - test("Open all repositories", async () => { - const openAllRepositories = jest.fn(() => Repository.openAll(process.env.BASE_DIR)); + it("Opens all repositories", async () => { + expect.hasAssertions(); + + const all_repositories = await Repository.openAll(env.BASE_DIR); - await openAllRepositories(); + expect(all_repositories).toBeDefined(); - expect(openAllRepositories).toReturn(); + for(const repository of all_repositories) { + expect(repository).toBeDefined(); + expect(repository).toBeInstanceOf(Repository) + + expectRepositoryProperties(repository); + } }); - describe("Functions", () => { + describe("Methods", () => { let repository: Repository; beforeAll(async () => { - repository = await Repository.open(process.env.BASE_DIR, process.env.AVAIL_REPO); + repository = await Repository.open(process.env.BASE_DIR, env.AVAIL_REPO); }); - test("Lookup if an existing object exists", async () => { - const exists = await repository.lookupExists(process.env.AVAIL_OBJECT); + it("Looks up if an object that exists exist", async () => { + expect.assertions(1); - expect(exists).toBeTruthy(); + await expect(repository.lookupExists(env.AVAIL_OBJECT)).resolves.toBeTruthy(); }); - test("Lookup if an nonexistant object exists", async () => { - const exists = await repository.lookupExists(process.env.UNAVAIL_OBJECT); + it("Looks up if an nonexistant object exists", async () => { + expect.assertions(1); - expect(exists).toBeFalsy(); + await expect(repository.lookupExists(env.UNAVAIL_OBJECT)).resolves.toBeFalsy(); }); - test("Get latest commit", async () => { - const getLatestCommit = jest.fn(() => repository.latestCommit()); + it("Gets the latest commit", async () => { + expect.assertions(8); + + const latest_commit = await repository.latestCommit(); + + expect(latest_commit).toBeDefined(); + expect(latest_commit).toBeInstanceOf(Commit); - await getLatestCommit(); - - expect(getLatestCommit).toReturn(); + expectCommitProperties(latest_commit); }); - test("Get commits", async () => { - const getCommits = jest.fn(() => repository.commits()); + it("Gets the commits", async () => { + expect.hasAssertions(); - await getCommits(); + const commits = await repository.commits(); - expect(getCommits).toReturn(); + expect(commits).toBeDefined(); + + for(const commit of commits) { + expect(commit).toBeDefined(); + expect(commit).toBeInstanceOf(Commit); + + expectCommitProperties(commit); + } }); - test("Get tree", async () => { - const getTree = jest.fn(() => repository.tree()); + it("Gets the tree", async () => { + expect.assertions(2); - await getTree(); + const tree = await repository.tree(); - expect(getTree).toReturn(); + expect(tree).toBeDefined(); + expect(tree).toBeInstanceOf(Tree); }); - test("Get branches", async () => { - const getBranches = jest.fn(() => repository.branches()); + it("Gets the branches", async () => { + expect.hasAssertions(); + + const branches = await repository.branches(); + + expect(branches).toBeDefined(); - await getBranches(); + for(const branch of branches) { + expect(branch).toBeDefined(); + expect(branch).toBeInstanceOf(Branch); - expect(getBranches).toReturn(); + expect(branch).toHaveProperty("id"); + expect(branch).toHaveProperty("name"); + } }); - test("Get tags", async () => { - const getTags = jest.fn(() => repository.tags()); + it("Gets the tags", async () => { + expect.hasAssertions(); + + const tags = await repository.tags(); + + expect(tags).toBeDefined(); + + for(const tag of tags) { + expect(tag).toBeDefined(); + expect(tag).toBeInstanceOf(Tag); - await getTags(); - - expect(getTags).toReturn(); + expect(tag).toHaveProperty("id"); + expect(tag).toHaveProperty("name"); + } }); }); }); \ No newline at end of file diff --git a/test/util.ts b/test/util.ts new file mode 100644 index 0000000..50611d6 --- /dev/null +++ b/test/util.ts @@ -0,0 +1,20 @@ +import { Commit } from "server/src/git/commit"; + +export type EnvironmentVariables = { + BASE_DIR: string, + AVAIL_REPO: string, + UNAVAIL_REPO: string, + AVAIL_OBJECT: string, + UNAVAIL_OBJECT: string, + AVAIL_COMMIT: string, + UNAVAIL_COMMIT: string +} + +export function expectCommitProperties(commit: Commit) { + expect(commit).toHaveProperty("id"); + expect(commit).toHaveProperty("author"); + expect(commit).toHaveProperty("author.name"); + expect(commit).toHaveProperty("author.email"); + expect(commit).toHaveProperty("date"); + expect(commit).toHaveProperty("message"); +} \ No newline at end of file -- cgit v1.2.3-18-g5258