Skip to content

Commit

Permalink
Update tests to run in a SW env
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed Apr 2, 2019
1 parent d478486 commit 10d3568
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 96 deletions.
21 changes: 1 addition & 20 deletions gulp-tasks/test-integration.js
Expand Up @@ -66,27 +66,8 @@ const runIntegrationTestSuite = async (testPath, nodeEnv, seleniumBrowser,
webdriver,
};

// Use a whitelist while we're migrating node tests to browser tests.
let testMatcher;
if (testPath.includes('workbox-background-sync') ||
testPath.includes('workbox-broadcast-update') ||
testPath.includes('workbox-cacheable-response') ||
testPath.includes('workbox-core') ||
testPath.includes('workbox-expiration') ||
testPath.includes('workbox-google-analytics') ||
testPath.includes('workbox-navigation-preload') ||
testPath.includes('workbox-precaching') ||
testPath.includes('workbox-range-requests') ||
testPath.includes('workbox-routing') ||
testPath.includes('workbox-strategies') ||
testPath.includes('workbox-window')) {
testMatcher = 'test-*.js';
} else {
testMatcher = '*.js';
}

const testFiles = glob.sync(
path.posix.join(__dirname, '..', testPath, testMatcher));
path.posix.join(__dirname, '..', testPath, 'test-*.js'));

await runFiles(testFiles);
} catch (err) {
Expand Down
18 changes: 14 additions & 4 deletions test/workbox-streams/integration/test-all.js
Expand Up @@ -7,22 +7,32 @@
*/

const {expect} = require('chai');

const activateAndControlSW = require('../../../infra/testing/activate-and-control');
const {runUnitTests} = require('../../../infra/testing/webdriver/runUnitTests');


// Store local references of these globals.
const {webdriver, server} = global.__workbox;

describe(`[workbox-streams]`, function() {
it(`passes all SW unit tests`, async function() {
await runUnitTests('/test/workbox-streams/sw/');
});
});

describe(`[workbox-streams] Integration Tests`, function() {
const testServerAddress = global.__workbox.server.getAddress();
const testServerAddress = server.getAddress();
const testingURL = `${testServerAddress}/test/workbox-streams/static/`;
const swURL = `${testingURL}sw.js`;

before(async function() {
await global.__workbox.webdriver.get(testingURL);
await webdriver.get(testingURL);
await activateAndControlSW(swURL);
});

for (const testCase of ['concatenate', 'concatenateToResponse', 'strategy']) {
it(`should return the expected response for the '${testCase}' approach`, async function() {
const {text, headers} = await global.__workbox.webdriver.executeAsyncScript(async (testCase, cb) => {
const {text, headers} = await webdriver.executeAsyncScript(async (testCase, cb) => {
try {
const response = await fetch(new URL(testCase, location));
const headers = [...response.headers].sort((a, b) => {
Expand Down
25 changes: 0 additions & 25 deletions test/workbox-streams/sw/test-exports.mjs

This file was deleted.

43 changes: 7 additions & 36 deletions test/workbox-streams/sw/test-isSupported.mjs
Expand Up @@ -6,46 +6,17 @@
https://opensource.org/licenses/MIT.
*/

import {expect} from 'chai';
import clearRequire from 'clear-require';
import {isSupported} from 'workbox-streams/isSupported.mjs';

describe(`[workbox-streams] isSupported`, function() {
const originalReadableStream = global.ReadableStream;
const MODULE_ID = '../../../packages/workbox-streams/isSupported.mjs';

afterEach(function() {
global.ReadableStream = originalReadableStream;
clearRequire(MODULE_ID);
});

describe(`isSupported`, function() {
it(`should return true when ReadableStream is available`, async function() {
class ReadableStream {
constructor() {
// no-op
}
}
global.ReadableStream = ReadableStream;

const {isSupported} = await import(MODULE_ID);
expect(isSupported()).to.be.true;
});
try {
new ReadableStream({start() {}});

it(`should return false when ReadableStream is not available`, async function() {
global.ReadableStream = undefined;

const {isSupported} = await import(MODULE_ID);
expect(isSupported()).to.be.false;
});

it(`should return false when ReadableStream throws during construction`, async function() {
class ReadableStream {
constructor() {
throw new Error();
}
expect(isSupported()).to.be.true;
} catch (error) {
expect(isSupported()).to.be.false;
}
global.ReadableStream = ReadableStream;

const {isSupported} = await import(MODULE_ID);
expect(isSupported()).to.be.false;
});
});
23 changes: 12 additions & 11 deletions test/workbox-streams/sw/utils/test-createHeaders.mjs
Expand Up @@ -6,26 +6,26 @@
https://opensource.org/licenses/MIT.
*/

import {expect} from 'chai';
import sinon from 'sinon';
import {createHeaders} from 'workbox-streams/utils/createHeaders.mjs';

import {createHeaders} from '../../../../packages/workbox-streams/utils/createHeaders.mjs';

describe(`[workbox-streams] utils/createHeaders`, function() {
describe(`createHeaders`, function() {
const sandbox = sinon.createSandbox();

beforeEach(function() {
sandbox.restore();
});

afterEach(function() {
sandbox.restore();
});

const DEFAULT_CONTENT_TYPE = ['content-type', 'text/html'];

it(`should use the default Content-Type, and construct with an empty object, when headersInit is undefined`, function() {
const headersSpy = sandbox.spy(global, 'Headers');
const headersSpy = sandbox.spy(self, 'Headers');
const headers = createHeaders();
expect(headers.entries()).to.eql([
DEFAULT_CONTENT_TYPE,
]);
expect([...headers]).to.eql([DEFAULT_CONTENT_TYPE]);
expect(headersSpy.calledOnce).to.be.true;
// See https://github.com/GoogleChrome/workbox/issues/1461
expect(headersSpy.args[0][0]).to.eql({});
Expand All @@ -37,9 +37,10 @@ describe(`[workbox-streams] utils/createHeaders`, function() {
'x-two': '2',
};
const headers = createHeaders(headersInit);
expect(headers.entries()).to.eql([
...Object.entries(headersInit),

expect([...headers]).to.eql([
DEFAULT_CONTENT_TYPE,
...Object.entries(headersInit),
]);
});

Expand All @@ -48,6 +49,6 @@ describe(`[workbox-streams] utils/createHeaders`, function() {
'content-type': 'text/plain',
};
const headers = createHeaders(headersInit);
expect(headers.entries()).to.eql(Object.entries(headersInit));
expect([...headers]).to.eql(Object.entries(headersInit));
});
});

0 comments on commit 10d3568

Please sign in to comment.