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

Test Service Worker installation using Lighthouse #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions .travis.yml
@@ -1,16 +1,17 @@
---
language: node_js
node_js:
- "4"
- "6"
- "stable"

sudo: false
sudo: required
dist: trusty

cache:
directories:
- node_modules
- node-tests/fixtures/simple-app/node_modules
- chrome-linux

env:
- TEST_SCENARIO=test
Expand All @@ -30,5 +31,11 @@ install:
- cd node-tests/fixtures/simple-app && npm install && bower install && cd -
- npm install

before_script:
- export DISPLAY=:99.0
- export LIGHTHOUSE_CHROMIUM_PATH="$(pwd)/chrome-linux/chrome"
- sh -e /etc/init.d/xvfb start
- ./scripts/download-chrome.sh

script:
- npm run $TEST_SCENARIO
67 changes: 65 additions & 2 deletions node-tests/acceptance-test.js
Expand Up @@ -2,15 +2,40 @@ var assert = require('chai').assert;
var fs = require('fs');
var path = require('path');
var rimraf = require('rimraf').sync;
var spawnSync = require('child_process').spawnSync;
var childProcess = require('child_process');

var spawn = childProcess.spawn;
var spawnSync = childProcess.spawnSync;

var emberCLIPath = path.resolve(__dirname, './fixtures/simple-app/node_modules/ember-cli/bin/ember');

var Lighthouse = require('lighthouse');
var ChromeLauncher = require('lighthouse/lighthouse-cli/chrome-launcher').ChromeLauncher;

function runLighthouse(url, flags, config) {
var launcher = new ChromeLauncher({port: 9222, autoSelectChrome: true});
return launcher.isDebuggerReady()
.catch(() => {
if (flags.skipAutolaunch) {
return;
}
return launcher.run(); // Launch Chrome.
})
.then(() => Lighthouse(url, flags, config)) // Run Lighthouse.
.then(results => launcher.kill().then(() => results)) // Kill Chrome and return results.
.catch(err => {
// Kill Chrome if there's an error.
return launcher.kill().then(() => {
throw err;
}, console.error);
});
}

describe('Acceptance Tests', function() {
this.timeout(120000);
var fixturePath = path.resolve(__dirname, './fixtures/simple-app');

context('A Simple App', function() {
var fixturePath = path.resolve(__dirname, './fixtures/simple-app');

function dist(file) {
return path.join(fixturePath, 'dist', file);
Expand Down Expand Up @@ -41,6 +66,38 @@ describe('Acceptance Tests', function() {
contains(dist('sw-registration.js'), "self.hello = 'Hello from Ember Service Worker Test';");
});
});

context('SW', function() {
let results;
let emberServer;

before(function(done) {
emberServer = runEmberCommandAsync(fixturePath, ['server', '--port', '4242']);

emberServer.stdout.on('data', (data) => {
if (data.indexOf('Build successful') !== -1) {

runLighthouse('http://localhost:4242', {
output: 'json'
}, require('./fixtures/lighthouse-config.js'))
.then((lhResults) => {
results = lhResults;
done();
})
.catch(err => console.error(err));
}
});
});

after(function() {
emberServer.kill();
cleanup(fixturePath);
});

it('is registered', function() {
assert.ok(results.audits['service-worker'].score);
});
});
});

function runEmberCommand(packagePath, command) {
Expand All @@ -53,6 +110,12 @@ function runEmberCommand(packagePath, command) {
}
}

function runEmberCommandAsync(packagePath, command) {
return spawn(emberCLIPath, command, {
cwd: packagePath
});
}

function cleanup(packagePath) {
rimraf(path.join(packagePath, 'dist'));
rimraf(path.join(packagePath, 'tmp'));
Expand Down
54 changes: 54 additions & 0 deletions node-tests/fixtures/lighthouse-config.js
@@ -0,0 +1,54 @@
module.exports = {
"settings": {},
"passes": [{
"passName": "defaultPass",
"recordNetwork": true,
"recordTrace": true,
"pauseBeforeTraceEndMs": 5000,
"useThrottling": true,
"gatherers": [
"url"
]
},
{
"passName": "offlinePass",
"recordNetwork": true,
"useThrottling": false,
"gatherers": [
"service-worker",
"offline"
]
}],

"audits": [
"service-worker",
],

"aggregations": [{
"name": "Progressive Web App",
"id": "pwa",
"description": "These audits validate the aspects of a Progressive Web App. They are a subset of the [PWA Checklist](https://developers.google.com/web/progressive-web-apps/checklist).",
"scored": true,
"categorizable": true,
"items": [{
"name": "App can load on offline/flaky connections",
"description": "Ensuring your web app can respond when the network connection is unavailable or flaky is critical to providing your users a good experience. This is achieved through use of a [Service Worker](https://developers.google.com/web/fundamentals/primers/service-worker/).",
"audits": {
"service-worker": {
"expectedValue": true,
"weight": 1
}
}
}]
}],
"categories": {
"pwa": {
"name": "Progressive Web App",
"weight": 1,
"description": "These audits validate the aspects of a Progressive Web App. They are a subset of the [PWA Checklist](https://developers.google.com/web/progressive-web-apps/checklist).",
"audits": [
{"id": "service-worker", "weight": 1},
]
}
}
}
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -22,6 +22,7 @@
"chai": "^3.5.0",
"ember-cli": "2.12.0",
"ember-fastboot-addon-tests": "^0.3.1",
"lighthouse": "1.6.5",
"mocha": "^2.5.3",
"rimraf": "^2.5.3"
},
Expand Down
15 changes: 15 additions & 0 deletions scripts/download-chrome.sh
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

# Download chrome inside of our CI env.
url="https://download-chromium.appspot.com/dl/Linux_x64?type=snapshots"

if [ x"$LIGHTHOUSE_CHROMIUM_PATH" == x ]; then
echo "Error: Environment variable LIGHTHOUSE_CHROMIUM_PATH not set"
exit 1
fi

if [ -e "$LIGHTHOUSE_CHROMIUM_PATH" ]; then
echo "cached chrome found"
else
wget "$url" --no-check-certificate -q -O chrome.zip && unzip chrome.zip
fi