-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🧪 Add test suite using Jest and Puppeteer
- Loading branch information
1 parent
60d5e41
commit b6e83e6
Showing
6 changed files
with
2,617 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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 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,5 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
}; |
This file contains 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 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,25 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import { Runner, createRunner } from "./utils"; | ||
|
||
let runner: Runner; | ||
|
||
beforeAll(async () => { | ||
runner = await createRunner(); | ||
}); | ||
|
||
test("renders landing page", async () => { | ||
await runner.page.waitForSelector("#main-content"); | ||
|
||
const title = await runner.page.$eval( | ||
"#main-content > section:first-of-type .fdn-hero-body h1", | ||
(e) => e.innerText?.trim().replace(/\n/g, " ") | ||
); | ||
expect(title).toBe("A safer way to surf the web."); | ||
}); | ||
|
||
afterAll(async () => { | ||
await runner.browser.close(); | ||
}); |
This file contains 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,61 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import { locateApp, locateChrome, locateFirefox } from "locate-app"; | ||
import puppeteer, { Browser, Page, Product } from "puppeteer"; | ||
import { check } from "tcp-port-used"; | ||
|
||
export interface Runner { | ||
page: Page; | ||
browser: Browser; | ||
} | ||
|
||
const browsers: any = { | ||
// For Firefox, we want to see if Nightly is installed also | ||
firefox: async () => { | ||
try { | ||
return await locateFirefox(); | ||
} catch (e) { | ||
try { | ||
return await locateApp({ | ||
appName: "Firefox Nightly", | ||
linuxWhich: "firefox-nightly", | ||
windowsSuffix: "\\Mozilla Firefox\\firefox.exe", | ||
macOsName: "Firefox Nightly" | ||
}); | ||
} catch (e) { | ||
throw e; | ||
} | ||
} | ||
}, | ||
chrome: locateChrome | ||
}; | ||
|
||
export const createRunner = async () => { | ||
if (!((process.env.BROWSER as string) in browsers)) { | ||
throw new Error(`Unknown browser '${process.env.BROWSER}'`); | ||
} | ||
|
||
if (!(await check(3000))) { | ||
throw new Error(`Server needs to be running first!`); | ||
} | ||
|
||
const executablePath = await browsers[ | ||
process.env.BROWSER as string | ||
](); | ||
|
||
const browser = await puppeteer.launch({ | ||
headless: process.env.BROWSER == "chrome" ? "new" : true, | ||
product: process.env.BROWSER as Product, | ||
executablePath | ||
}); | ||
|
||
const page = await browser.newPage(); | ||
await page.goto("http://localhost:3000/"); | ||
|
||
return { | ||
page, | ||
browser | ||
}; | ||
}; |
Oops, something went wrong.