Skip to content

Commit

Permalink
feat(tests refactoring): add mocha, chai tests with puppeteer
Browse files Browse the repository at this point in the history
  • Loading branch information
addyosmani committed Nov 25, 2018
1 parent fd81b71 commit d0b8911
Show file tree
Hide file tree
Showing 7 changed files with 372 additions and 1,995 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
module.exports = {
env: {
browser: true,
es6: true
es6: true,
node: true
},
parserOptions: {
sourceType: 'module',
ecmaVersion: 8
},
extends: 'google',
rules: {
Expand Down
29 changes: 12 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
"jsnext:main": "dist/quicklink.mjs",
"umd:main": "dist/quicklink.umd.js",
"scripts": {
"test": "eslint src/*.mjs && jest",
"lint": "eslint src/*.mjs test/*.js",
"lint-fix": "eslint src/*.mjs test/*.js --fix",
"server": "http-server demo",
"test": "mocha test/bootstrap.js --recursive test",
"build": "microbundle src/index.mjs --no-sourcemap",
"prepare": "npm run -s build",
"release": "cross-var npm run build -s && cross-var git commit -am $npm_package_version && cross-var git tag $npm_package_version && git push && git push --tags && npm publish --access public"
Expand All @@ -23,26 +26,18 @@
"background",
"speed"
],
"jest": {
"testURL": "http://localhost/",
"testMatch": [
"<rootDir>/test/**/*.?(m)js?(x)"
],
"moduleFileExtensions": [
"mjs",
"js"
],
"transform": {
"^.+\\.m?jsx?$": "babel-jest"
}
},
"devDependencies": {
"babel-preset-env": "^1.7.0",
"chai": "^4.2.0",
"cross-var": "^1.1.0",
"eslint": "^5.9.0",
"eslint-config-google": "^0.11.0",
"jest": "^23.6.0",
"microbundle": "^0.7.0"
"http-server": "^0.11.1",
"lodash": "^4.17.11",
"microbundle": "^0.7.0",
"mocha": "^5.2.0"
},
"dependencies": {}
"dependencies": {
"puppeteer": "^1.10.0"
}
}
25 changes: 25 additions & 0 deletions test/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const puppeteer = require('puppeteer');
const {expect} = require('chai');
const _ = require('lodash');
const globalVariables = _.pick(global, ['browser', 'expect']);

// puppeteer options
const opts = {
headless: false,
slowMo: 100,
timeout: 10000,
};

// expose variables
before(async function () {
global.expect = expect;
global.browser = await puppeteer.launch(opts);
});

// close browser and reset global variables
after(function () {
browser.close();

global.browser = globalVariables.browser;
global.expect = globalVariables.expect;
});
10 changes: 10 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>Puppeteer Mocha</title>
</head>
<body>
<h1>Page Title</h1>
<p class='main-content'>Some paragraph text</p>
</body>
</html>
22 changes: 0 additions & 22 deletions test/index.js

This file was deleted.

29 changes: 29 additions & 0 deletions test/sample.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
describe('quicklink tests', function () {
let page;

before(async function () {
page = await browser.newPage();
await page.goto('http://127.0.0.1:8080/demo/index.html');
});

after(async function () {
await page.close();
});

it('should have the correct page title', async function () {
expect(await page.title()).to.eql('Prefetch experiments');
});

it('should prefetch pages correctly', async function () {
const responseURLs = [];
page.on('response', resp => {
responseURLs.push(resp.url());
});
await page.goto('http://127.0.0.1:8080/demo/index.html');
await page.waitFor(1000);
expect(responseURLs).to.be.an('array');
expect(responseURLs).to.include('http://127.0.0.1:8080/demo/2.html');
expect(responseURLs).to.include('http://127.0.0.1:8080/demo/2.html');
expect(responseURLs).to.include('http://127.0.0.1:8080/demo/3.html');
});
});
Loading

0 comments on commit d0b8911

Please sign in to comment.