Skip to content

Commit

Permalink
Merge pull request #561 from wheresrhys/test-clean
Browse files Browse the repository at this point in the history
Test clean
  • Loading branch information
wheresrhys committed May 20, 2020
2 parents 14cc153 + 99eaa5e commit 17f940a
Show file tree
Hide file tree
Showing 39 changed files with 2,604 additions and 2,588 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ TEST_BROWSER := $(shell [ -z $(TEST_BROWSER) ] && echo "Chrome" || echo ${TEST_B

# intended for local dev
test:
mocha --file test/server-setup.js test/{server-specs,specs}/*.test.js
mocha --file test/server-setup.js test/{server-specs,specs}/*.test.js test/specs/**/*.test.js

test-browser:
@if [ -z $(CI) ]; \
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"babel-preset-env": "^1.7.0",
"bluebird": "^3.4.6",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"coveralls": "^3.0.0",
"dtslint": "^1.0.2",
"eslint": "^4.14.0",
Expand All @@ -94,7 +95,7 @@
"karma-mocha-reporter": "^2.2.5",
"karma-webpack": "^3.0.0",
"mocha": "^7.1.2",
"node-fetch": "^2.0.0",
"node-fetch": "^2.6.0",
"nyc": "^11.7.3",
"prettier": "^2.0.4",
"rollup": "^1.25.2",
Expand Down
3 changes: 2 additions & 1 deletion src/lib/response-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ e.g. {"body": {"status: "registered"}}`);
this.options.statusText = this.fetchMock.statusTextMap[
String(this.options.status)
];

// Set up response headers. The empty object is to cope with
// new Headers(undefined) throwing in Chrome
// https://code.google.com/p/chromium/issues/detail?id=335871
Expand Down Expand Up @@ -150,7 +151,7 @@ e.g. {"body": {"status: "registered"}}`);
// Using a proxy means we can set properties that may not be writable on
// the original Response. It also means we can track the resolution of
// promises returned by res.json(), res.text() etc
this.debug('Wrappipng Response in ES proxy for observability');
this.debug('Wrapping Response in ES proxy for observability');
return new Proxy(response, {
get: (originalResponse, name) => {
if (this.responseConfig.redirectUrl) {
Expand Down
135 changes: 61 additions & 74 deletions test/client-specs/client-only.test.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,76 @@
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const expect = chai.expect;
chai.use(chaiAsPromised);
const { fetchMock } = testGlobals;

describe('client-side only tests', () => {
describe('native fetch behaviour', function () {
it('should not throw when passing unmatched calls through to native fetch', function () {
fetchMock.mock(/a/, 200);
expect(function () {
fetch('http://www.example.com');
}).not.to.throw();
fetchMock.restore();
});
afterEach(() => fetchMock.restore());
it('not throw when passing unmatched calls through to native fetch', () => {
fetchMock.config.fallbackToNetwork = true;
fetchMock.mock();
expect(() => fetch('http://a.com')).not.to.throw();
fetchMock.config.fallbackToNetwork = false;
});

// this is because we read the body once when normalising the request and
// want to make sure fetch can still use the sullied request
it('can still POST a body successfully when spying', async () => {
fetchMock.spy();
const req = new fetchMock.config.Request(
'http://localhost:9876/dummy-file.txt',
{ method: 'post', body: JSON.stringify({ prop: 'val' }) }
);
expect(() => fetch(req)).not.to.throw();
fetchMock.restore();
});
// this is because we read the body once when normalising the request and
// want to make sure fetch can still use the sullied request
it('can send a body on a Request instance when spying ', async () => {
fetchMock.spy();
const req = new fetchMock.config.Request(
'http://localhost:9876/dummy-file.txt',
{ method: 'post', body: JSON.stringify({ prop: 'val' }) }
);
let response;
expect(() => {
response = fetch(req);
}).not.to.throw();
await expect(response).to.not.be.rejected;
});

describe('request types that only work in the browser', function () {
it('respond with blob', function (done) {
const blob = new Blob();
fetchMock.mock('http://it.at.there/', blob, { sendAsJson: false });
fetch('http://it.at.there/').then(function (res) {
expect(res.status).to.equal(200);
res.blob().then(function (blobData) {
expect(blobData).to.eql(blob);
fetchMock.restore();
done();
});
});
});
it('respond with blob', async () => {
const blob = new Blob();
fetchMock.mock('*', blob, { sendAsJson: false });
const res = await fetch('http://a.com');
expect(res.status).to.equal(200);
const blobData = await res.blob();
expect(blobData).to.eql(blob);
});

describe('no real fetch', function () {
it('should cope when there is no global fetch defined', function () {
const fetchCache = window.fetch;
delete window.fetch;
const realFetchCache = fetchMock.realFetch;
delete fetchMock.realFetch;
fetchMock.mock(/a/, 200);
expect(function () {
fetch('http://www.example.com');
}).not.to.throw();
it('should cope when there is no global fetch defined', () => {
const originalFetch = window.fetch;
delete window.fetch;
const originalRealFetch = fetchMock.realFetch;
delete fetchMock.realFetch;
fetchMock.mock('*', 200);
expect(() => {
fetch('http://a.com');
}).not.to.throw();

expect(function () {
fetchMock.calls();
}).not.to.throw();
fetchMock.restore();
fetchMock.realFetch = realFetchCache;
window.fetch = fetchCache;
});
expect(() => {
fetchMock.calls();
}).not.to.throw();
fetchMock.restore();
fetchMock.realFetch = originalRealFetch;
window.fetch = originalFetch;
});

describe('service worker', () => {
it('should work within a service worker', () => {
return (
navigator.serviceWorker &&
navigator.serviceWorker.register('__sw.js').then((registration) => {
return new Promise((resolve, reject) => {
if (registration.installing) {
registration.installing.onstatechange = function () {
if (this.state === 'activated') {
resolve();
}
};
} else {
reject('No idea what happened');
if (navigator.serviceWorker) {
it('should work within a service worker', async () => {
const registration = await navigator.serviceWorker.register('__sw.js');
await new Promise((resolve, reject) => {
if (registration.installing) {
registration.installing.onstatechange = function () {
if (this.state === 'activated') {
resolve();
}
}).then(() => {
expect(true).to.be.true;
return navigator.serviceWorker
.getRegistration()
.then((registration) =>
registration ? registration.unregister() : false
);
});
})
);
};
} else {
reject('No idea what happened');
}
});

await registration.unregister();
});
});
}
});
14 changes: 0 additions & 14 deletions test/server-setup.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
const fetchMock = require(process.env.FETCH_MOCK_SRC || '../src/server.js');
const fetch = require('node-fetch');
const AbortController = require('abort-controller');
const http = require('http');
const { promisify } = require('util');

global.testGlobals = {
fetchMock,
theGlobal: global,
fetch,
AbortController,
};

let server;
before(() => {
server = http.createServer((req, res) => {
res.writeHead(200);
res.end();
});
return promisify(server.listen.bind(server))(9876);
});
after(() => {
server.close();
});
14 changes: 0 additions & 14 deletions test/server-setup.mjs
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
import fetchMock from '../esm/server.js';
import fetch from 'node-fetch';
import AbortController from 'abort-controller';
import http from 'http';
import { promisify } from 'util';

global.testGlobals = {
fetchMock,
theGlobal: global,
fetch,
AbortController,
};

let server;
before(() => {
server = http.createServer((req, res) => {
res.writeHead(200);
res.end();
});
return promisify(server.listen.bind(server))(9876);
});
after(() => {
server.close();
});
Loading

0 comments on commit 17f940a

Please sign in to comment.