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

Updated framework so it doesn't have to be modified to be used #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
49 changes: 14 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,19 @@ You'll probably need to adjust the `files` entry. As Karma does not support SW n
}

### Adding tests
For the service worker to know which test files should load, you need to add them to a special file `sw-tests.js` in the root of your project:
For the service worker to know which test files should load, you need to add them to the client configuration as well as the files array in the config. This is because the framework loads these files within the service worker directly, and does not have access to the main files list:

```js
// sw-tests.js
var SW_TESTS = [
'/base/path/to/your/tests/myTest.sw-spec.js'
];
```

Karma will serve all your files under the `/base/` path. So if your tests are in `test/test1.sw-spec.js` and `test/test2.sw-spec.js` they should be added as:
**Please remember to include the service worker that you want to test as the first script to be loaded in here.**

```js
// sw-tests.js
var SW_TESTS = [
'/base/test/test1.sw-spec.js',
'/base/test/test2.sw-spec.js'
];
client: {
'sw-mocha': {
SW_TESTS: [
'path/to/your/worker/sw.js',
'samples/mocha-sinon-chai-bdd/sw-tests.js'
]
}
},
```

### Loading other libraries
Expand All @@ -60,7 +56,6 @@ To include them in your service worker setup, edit `sw-tests.js` and import the

```js
// sw-tests.js
var SW_TESTS = [ /* your test files... */ ];
importScripts('/base/node_modules/chai/chai.js');
importScripts('/base/node_modules/sinon/pkg/sinon.js');
```
Expand All @@ -78,10 +73,8 @@ The file `sw-tests.js` will be load before executing any test so you can add the

```js
// sw-tests.js
var SW_TESTS = [ /* your test files... */ ];

importScripts('/base/node_modules/chai/chai.js');
// your other imports...
// any other imports, not your tests...

mocha.setup({ ui: 'bdd' });
self.expect = chai.expect;
Expand All @@ -91,21 +84,7 @@ self.expect = chai.expect;

In [`samples/mocha-sinon-chai-bdd`](https://github.com/delapuente/karma-sw-mocha/tree/master/samples/mocha-sinon-chai-bdd) you will find sample files for the Karma configuration file and `sw-tests.js` to set and Mocha BDD + Chai + Sinon environment up.

## Enabling tests of Firefox
Currently only Firefox Nightly has support for Service Workers and only after turning on certain flags. You will need a custom launcher to enable SW on Nightly. Do it by replacing the `Firefox` launcher in your config file with some similar to this:
## Browser support
At the time of writing, Chrome, Firefox, Opera and Samsung Internet support service workers. See this [compatibility table](https://jakearchibald.github.io/isserviceworkerready/) for the most up to date information. **PhantomJS** is not supported and is highly unlikely to ever be supported.

```js
{
browsers: ['NightlySW'],

customLaunchers: {
'NightlySW': {
base: 'FirefoxNightly',
prefs: {
'devtools.serviceWorkers.testing.enabled': true,
'dom.serviceWorkers.enabled': true
}
}
}
}
```
For headless support, try [SlimerJS](https://github.com/laurentj/slimerjs) ([runner](https://github.com/karma-runner/karma-slimerjs-launcher)) or, if you're feeling lucky, [Raw Chromium for Linux](https://download-chromium.appspot.com/) with --headless and --disable-gpu flags set. Track the Chrome Headless project [here](https://bugs.chromium.org/p/chromium/issues/detail?id=546953)
10 changes: 10 additions & 0 deletions lib/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@

navigator.serviceWorker.addEventListener('message', execKarmaMethod);

function handleMessage(evt) {
if (evt.data.command === 'waitForTests') {
var specsToTest = window.parent.__karma__.config['serviceworker-jasmine'].SW_TESTS;
navigator.serviceWorker.controller.postMessage(specsToTest);
} else {
execKarmaMethod(evt);
}
}

function execKarmaMethod(evt) {
var msg = evt.data;
console.log(msg);
var tc = window.parent.__karma__;
tc[msg.command].apply(tc, msg.args);
if (msg.command === 'complete') {
Expand Down
1 change: 0 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var createPattern = function(path, included) {

var initMocha = function(files, mochaConfig) {
var mochaPath = path.dirname(require.resolve('mocha'));
files.unshift(createPattern(__dirname + '/../../../sw-tests.js'));
files.unshift(createPattern(__dirname + '/testrunner.sw.js'));
files.unshift(createPattern(__dirname + '/index.html'));
files.unshift(createPattern(mochaPath + '/mocha.js'));
Expand Down
18 changes: 11 additions & 7 deletions lib/testrunner.sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ self.window = self;
importScripts('/base/node_modules/mocha/mocha.js');
mocha.setup({ reporter: createSWMochaReporter() });

importScripts('/base/sw-tests.js');
SW_TESTS.forEach(function (testFile) {
importScripts(testFile);
});

self.onactivate = function (evt) {
evt.waitUntil(
self.clients.claim().then(function () {
mocha.run();
return mocha.complete;
send('waitForTests');
})
);
};

// Handles messages between the client and worker. This one is listening for tests
self.addEventListener('message', function (evt) {
var testList = evt.data;
testList.forEach(function (testFile) {
importScripts(testFile);
});
mocha.run();
return mocha.complete;
});

function createSWMochaReporter() {
return function (runner) {
runner.on('start', function() {
Expand Down
20 changes: 9 additions & 11 deletions samples/mocha-sinon-chai-bdd/karma-sw.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ module.exports = function(config) {
config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
// This must be the root of the project
basePath: '../',


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['sw-mocha', 'sinon', 'chai'],

client: {
'sw-mocha': {
SW_TESTS: [
'samples/mocha-sinon-chai-bdd/sw-tests.js'
]
}
},

// list of files / patterns to load in the browser
files: [
Expand Down Expand Up @@ -54,17 +62,7 @@ module.exports = function(config) {

// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome', 'NightlySW'],

customLaunchers: {
'NightlySW': {
base: 'FirefoxNightly',
prefs: {
'devtools.serviceWorkers.testing.enabled': true,
'dom.serviceWorkers.enabled': true
}
}
},
browsers: ['Chrome', 'Firefox'],


// Continuous Integration mode
Expand Down
7 changes: 0 additions & 7 deletions samples/mocha-sinon-chai-bdd/sw-tests.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@

// This is the set of the SW tests. They will be run inside a SW environment.
// Karma publishes the static content from /base/ path.
var SW_TESTS = [
'/base/path/to/tests/test.sw-spec.js',
// more tests...
];

// Import chai and sinon into the ServiceWorkerGlobalScope
importScripts('/base/node_modules/chai/chai.js');
importScripts('/base/node_modules/sinon/pkg/sinon.js');
Expand Down