Skip to content

Commit

Permalink
feat(jest): Make links support hosted Github (#44)
Browse files Browse the repository at this point in the history
Previously, links generated by this plugin were hard-coded to github. This would lead to invalid
links when using a hosted version of GitHub. This changed mimics what the main danger file does to
read the URL from the GitHub repo and use that instead.
  • Loading branch information
ken-kenware authored and macklinu committed Jul 9, 2019
1 parent fcfce6b commit 97dc96a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
30 changes: 29 additions & 1 deletion src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ beforeEach(() => {
head: {
ref: "branch",
repo: {
full_name: "repo/slug"
full_name: "repo/slug",
html_url: "https://github.com/user",
owner: {
login: "user"
}
}
}
},
Expand Down Expand Up @@ -53,6 +57,30 @@ test("warns when test results JSON file cannot be read", () => {
expect(global["fail"]).toHaveBeenCalled();
});

test("can create links for github", () => {
jestResults({
testResultsJsonPath: fixture("failing-tests.json")
});
expect(global["fail"]).toHaveBeenCalledWith(
expect.stringMatching(
/https:\/\/github.com\/repo\/slug\/blob\/branch\/(.*)?\/file-utils.test.ts#L24/
)
);
});

test("can create links for hosted github", () => {
global["danger"].github.pr.head.repo.html_url =
"https://mydomain.github.com/user";
jestResults({
testResultsJsonPath: fixture("failing-tests.json")
});
expect(global["fail"]).toHaveBeenCalledWith(
expect.stringMatching(
/https:\/\/mydomain.github.com\/repo\/slug\/blob\/branch\/(.*)?\/file-utils.test.ts#L24/
)
);
});

test.skip("Fails 6", () => {
expect({ v: "asda" }).toContain("s");
});
Expand Down
14 changes: 6 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,13 @@ const presentErrorsForNewStyleResults = (jsonResults: IJestTestResults) => {
// e.g. https://github.com/orta/danger-plugin-jest/blob/master/src/__tests__/fails.test.ts
const linkToTest = (file: string, msg: string, title: string) => {
const line = lineOfError(msg, file);
const githubRoot = danger.github.pr.head.repo.html_url.split(
danger.github.pr.head.repo.owner.login
)[0];
const repo = danger.github.pr.head.repo;
const urlParts = [
"https://github.com",
repo.full_name,
"blob",
danger.github.pr.head.ref,
`${file}${"line" ? "#L" + line : ""}`
];
return `<a href='${urlParts.join("/")}'>${title}</a>`;
const url = `${githubRoot}${repo.full_name}/blob/${danger.github.pr.head
.ref}/${file}${"line" ? "#L" + line : ""}`;
return `<a href='${url}'>${title}</a>`;
};

const assertionFailString = (
Expand Down

0 comments on commit 97dc96a

Please sign in to comment.