Skip to content

Commit

Permalink
tests for utils
Browse files Browse the repository at this point in the history
  • Loading branch information
krzkaczor committed Aug 16, 2018
1 parent 4093ef6 commit c4b515a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/utils.ts
Expand Up @@ -5,6 +5,6 @@ export function getFilenameWithoutAnyExtensions(filePath: string): string {
return filePath.slice(0, endPosition !== -1 ? endPosition : filePath.length);
}

export function getRelativeModulePath(from: string, to: string): string {
return ("./" + relative(from, to)).replace(".ts", ""); // @note: this is probably not the best way to find relative path for modules
export function getRelativeModulePath(cwd: string, to: string): string {
return ("./" + relative(cwd, to)).replace(".ts", ""); // @note(kk): any better way to find it?
}
30 changes: 30 additions & 0 deletions test/utils.spec.ts
@@ -0,0 +1,30 @@
import { getFilenameWithoutAnyExtensions, getRelativeModulePath } from "../src/utils";
import { expect } from "chai";

describe("utils", () => {
describe("getFilenameWithoutAnyExtensions", () => {
it("should work with simple path", () => {
expect(getFilenameWithoutAnyExtensions("/test/file.txt")).to.be.eq("/test/file");
});

it("should work with double extension", () => {
expect(getFilenameWithoutAnyExtensions("/test/file.txt.ts")).to.be.eq("/test/file");
});
});

describe("getRelativeModulePath", () => {
it("should work in the same directory", () => {
const cwd = "/app/";
const to = "/app/module2.ts";

expect(getRelativeModulePath(cwd, to)).to.be.eq("./module2");
});

it("should work in the same directory", () => {
const cwd = "/app2";
const to = "/app/module2.ts";

expect(getRelativeModulePath(cwd, to)).to.be.eq("./../app/module2");
});
});
});

0 comments on commit c4b515a

Please sign in to comment.