Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest spike #66

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
06908a7
spike(jest): Setup and switch from mocha/chai
Droogans Oct 24, 2017
0af6a1f
refact(visreg): Share one set of tests for both browsers
Droogans Oct 24, 2017
66fd923
fix(visreg): Remove debugging 'exit(0)' statement
Droogans Oct 24, 2017
ac2a24c
fix(visreg): Allow for informational messages again
Droogans Oct 24, 2017
be4955b
fix(visreg): Use geckodriver
Droogans Oct 26, 2017
2d19b81
refact(visreg): Move tests out of util
Droogans Oct 26, 2017
6aa9dc9
chore(jest): Don't load browser context for visreg
Droogans Oct 30, 2017
cfad2a8
test(jest): Outline 'functional' tests
Droogans Oct 30, 2017
8cf7382
refact(tests): Split each test type into dedicated directory
Droogans Oct 31, 2017
97fb831
docs(util): Better document 'page object' and its future
Droogans Oct 31, 2017
335bc09
style(testing): Avoid scientific notation for big numbers
Droogans Oct 31, 2017
274af5b
fix(script): Update reference away from deleted script
Droogans Nov 1, 2017
2b9c9f1
fix(tsconfig): Include custom type hints
Droogans Nov 1, 2017
0ab7eda
fix(visreg): Honor the actual screenshot directory
Droogans Nov 1, 2017
b527452
fix(visreg): Wrong path to visreg config file
Droogans Nov 1, 2017
e98239a
fix(visreg): Refer to new npm run script
Droogans Nov 1, 2017
27c2b95
fix(jest): Don't spin up jsdom environment
Droogans Nov 1, 2017
bdc0a8e
fix(jest): Optimize for selenium
Droogans Nov 1, 2017
3f9b68a
fix(visreg): Avoid large png files
Droogans Nov 1, 2017
3a7ed64
chore(yarn): Switch to ava
Droogans Nov 2, 2017
59af4f8
test(ava): Convert to ava
Droogans Nov 2, 2017
adba06e
style(visreg): Remove missing comment
Droogans Nov 2, 2017
47e2666
style(visreg): Remove unused template literals
Droogans Nov 2, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.github-token
bin/*.js
bin/visreg.config.ts
built/
index.js
screenshots
!built/functional/*.js.snap
!built/functional/*.js.md
screenshots
6 changes: 3 additions & 3 deletions test/bin/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export function createBaseline(
"git commit --allow-empty -m \"Baseline.\"",
"cd -"
].join('; '));
cmd(`git checkout ${targetBranch}; npm test`);
cmd(`git checkout ${targetBranch}; npm run test:visreg`);

const commit = commitScreenshots(token, screenshotsDirectory, "before").toString();
const baseCommitMatch = commit.match(hasCommitRegex);
Expand All @@ -118,7 +118,7 @@ export function createBaseline(
}

export function createDiff(token: string, currentBranch: string, screenshotsDirectory: string) {
cmd(`git checkout ${currentBranch}; npm test`);
cmd(`git checkout ${currentBranch}; npm run test:visreg`);
const afterCommitData = commitScreenshots(token, screenshotsDirectory, "after").toString();
const afterCommitMatch = afterCommitData.match(hasCommitRegex);
const afterCommit = afterCommitMatch && afterCommitMatch[1];
Expand Down Expand Up @@ -251,7 +251,7 @@ export function resetRepository(screenshotsDirectory: string) {
export function cloneRepo(token: string, screenshotsDirectory: string, repoUrl: url.Url) {
let cloneUrl = `https://${token}@${repoUrl.host}${repoUrl.path}.git`;
console.log(`Cloning a screenshots project into "${path.resolve(screenshotsDirectory)}"`);
safeExecSync(token, `git clone ${cloneUrl} screenshots/ > /dev/null`)
safeExecSync(token, `git clone ${cloneUrl} ${screenshotsDirectory}/ > /dev/null`)

console.log(`Cloned a screenshots project into "${path.resolve(screenshotsDirectory)}"`);
};
Expand Down
Empty file removed test/bin/visreg.d.ts
Empty file.
4 changes: 1 addition & 3 deletions test/bin/visreg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as opn from "opn"
import * as util from "./util";

import {config, IConfig} from "./visreg.config";
const screenshotsDirectory = "screenshots";
const screenshotsDirectory = "visreg/screenshots";

async function visreg(
currentBranch: string,
Expand All @@ -29,8 +29,6 @@ async function visreg(
const token = await util.validateToken(await util.getGithubToken());
const repoUrl = url.parse(`https://${config.githubHostname}/${config.githubName}/${config.repo}`);

process.exit(0);

util.resetRepository(screenshotsDirectory);
if (!util.repositoryExists(token, repoUrl)) {
console.log(`Creating a new screenshots repository at ${repoUrl.href}`);
Expand Down
23 changes: 23 additions & 0 deletions test/built/functional/hx-reveal.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Snapshot report for `built/functional/hx-reveal.js`

The actual snapshot is saved in `hx-reveal.js.snap`.

Generated by [AVA](https://ava.li).

## should not be open

> Snapshot 1

`<hx-reveal>␊
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we configure the newline characters in AVA?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nm, I just realized that's on purpose to visualize new lines in the documentation

<span slot="summary">Click me to show content</span>␊
<p>You can't see me.</p>␊
</hx-reveal>`

## should open

> Snapshot 1

`<hx-reveal open="">␊
<span slot="summary">Click me to show content</span>␊
<p>You can't see me.</p>␊
</hx-reveal>`
Binary file added test/built/functional/hx-reveal.js.snap
Binary file not shown.
10 changes: 8 additions & 2 deletions test/util.ts → test/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Helper functions for the tests in `index.ts`.
*/
import {By, ISize, ThenableWebDriver, WebDriver, WebElementPromise} from "selenium-webdriver";

export async function setViewportSize (
driver: ThenableWebDriver,
size: ISize,
Expand Down Expand Up @@ -30,7 +31,12 @@ export function $x(
return driver.findElement(By.xpath(xpath));
}

// "page object"
export var $ = {
/* A "starter page object" until there's a greater need for something more robust.
* For now this only contains common CSS selectors used throughout tests in the
* visreg/functional directories, but may also contain functions in the future.
*
* Once that happens, pull this out of `util.ts` and move it someplace more page-object-y.
*/
export var selectors = {
nav: ".hxApp__nav",
}
40 changes: 40 additions & 0 deletions test/functional/hx-reveal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {$, snap, Snappit, IConfig} from "snappit-visual-regression";
import {WebDriver, WebElementPromise} from "selenium-webdriver";

import {test, TestContext} from "ava";

const baseUrl = "http://localhost:3000/helix-ui";
const snapshot = async (t: TestContext, element: WebElementPromise) => {
t.snapshot(await element.getAttribute("outerHTML"));
};

let snappit: Snappit;
let driver: WebDriver;
let reveal: WebElementPromise;

test.before(async () => {
const config: IConfig = {
browser: "chrome",
serverUrl: "http://localhost:4444/wd/hub",
};

snappit = new Snappit(config);
driver = await snappit.start();
await driver.get(`${baseUrl}/components/reveal`);
reveal = $(".demo hx-reveal");
});

test("should not be open", async t => {
t.is(await reveal.getAttribute("open"), null);
await snapshot(t, reveal);
});

test("should open", async t => {
await reveal.click();
t.is(await reveal.getAttribute("open"), "true");
await snapshot(t, reveal);
});

test.after.always(async () => {
await snappit.stop();
});
Empty file removed test/index.d.ts
Empty file.
99 changes: 0 additions & 99 deletions test/index.ts

This file was deleted.

29 changes: 16 additions & 13 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
{
"dependencies": {
"@types/chai": "^4.0.4",
"@types/lodash": "^4.14.74",
"@types/mocha": "^2.2.43",
"@types/node": "^8.0.28",
"@types/opn": "^3.0.28",
"@types/reflect-metadata": "^0.0.5",
"@types/selenium-webdriver": "^3.0.7",
"chai": "^4.1.2",
"ava": "^0.23.0",
"inquirer-promise": "^1.0.0",
"inquirer-shortcuts": "^1.1.0",
"lodash": "^4.17.4",
"mocha": "^3.5.3",
"opn": "^5.1.0",
"selenium-webdriver": "^3.5.0",
"snappit-visual-regression": "^0.6.4",
Expand All @@ -20,15 +17,21 @@
},
"scripts": {
"build": "tsc",
"clean": "npm run clean:js && npm run clean:screenshots",
"clean:screenshots": "rm -rf screenshots",
"clean:js": "rm -rf built/",
"lint": "tslint index.ts",
"test": "npm run build && mocha --timeout 15000 built/index.js",
"previsreg": "[ -f ./bin/visreg.config.ts ] || cp bin/visreg{.example,}.config.ts",
"visreg": "npm run clean:js && npm run build && node ./built/bin/util.js && node ./built/bin/visreg.js",
"clean": "npm run clean:visreg && npm run clean:func",
"clean:func": "find built/functional -mindepth 1 \\( ! -path 'built/functional/*.js.snap' ! -path 'built/functional/*.js.md' \\) -delete",
"clean:visreg": "rm -rf visreg/screenshots built/visreg",
"lint": "tslint -c tslint.json visreg/ functional/",
"test:func": "npm run build && ava built/functional/*.js",
"test:visreg": "npm run build && ava built/visreg/{chrome,firefox}.js --verbose",
"postinstall": "[ -f bin/visreg.config.ts ] || cp bin/visreg{.example,}.config.ts",
"previsreg": "npm run clean:visreg",
"visreg": "npm run build && node built/bin/util.js && node built/bin/visreg.js",
"webdriver": "npm run webdriver:update && npm run webdriver:start",
"webdriver:update": "scripts/webdriver-update",
"webdriver:start": "scripts/webdriver-start"
"webdriver:start": "scripts/webdriver-start",
"webdriver:update": "scripts/webdriver-update"
},
"ava": {
"snapshotLocation": "~/code/js/helix-ui/test/snapshots",
"failWithoutAssertions": false
}
}
17 changes: 7 additions & 10 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,19 @@
"types": [
"reflect-metadata",
"node",
"mocha"
"ava"
],
"baseUrl": ".",
"paths": {
"*": ["types/*"]
},
"outDir": "./built"
"outDir": "built"
},
"exclude": ["node_modules"],
"include": [
"index.ts",
"bin/util.ts",
"bin/visreg.ts",
"bin/util.ts",
"bin/visreg.config.ts"
],
"exclude": [
"node_modules"
"common/*.ts",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be common/**/*.ts to match files in common/util/?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a single file in there right now. util.ts.

"functional/*.ts",
"visreg/*.ts",
"bin/*.ts"
]
}
3 changes: 3 additions & 0 deletions test/visreg/chrome.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as visreg from "./visreg";

visreg.suite("chrome");
3 changes: 3 additions & 0 deletions test/visreg/firefox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as visreg from "./visreg";

visreg.suite("firefox");
47 changes: 47 additions & 0 deletions test/visreg/visreg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {$, snap, Snappit, IConfig} from "snappit-visual-regression";

import * as util from "../common/util";
import {test} from "ava";

export function suite(browserName: string) {
let snappit: Snappit;
let driver: any;

test.before(async () => {
const config: IConfig = {
browser: browserName,
screenshotsDir: "visreg/screenshots",
logException: [
"MISMATCH",
"NO_BASELINE",
"SIZE_DIFFERENCE",
],
threshold: 0.1,
useDirect: true,
useGeckoDriver: (browserName === "firefox"),
};

snappit = new Snappit(config);
driver = await snappit.start();
await util.setViewportSize(driver, { width: 1366, height: 768 });
driver.get("http://localhost:3000/");
});

test("nav", async () => {
await snap("{browserName}/nav", $(util.selectors.nav));
});

test("guides", async () => {
await util.$x(driver, "//nav/hx-reveal//header", "Guides").click();
await snap("{browserName}/nav/guides", $(util.selectors.nav));
});

test("components", async () => {
await util.$x(driver, "//nav/hx-reveal//header", "Components").click();
await snap("{browserName}/nav/componenets", $(util.selectors.nav));
});

test.after.always(async () => {
await snappit.stop();
});
}
Loading