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

Esm 4 realz #529

Open
wants to merge 2 commits into
base: main
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
9 changes: 7 additions & 2 deletions Makefile
Expand Up @@ -14,6 +14,9 @@ test-firefox:
test-unit:
mocha test/server.js

test-esm:
node --experimental-modules -r esm node_modules/mocha/bin/_mocha test/server.js

test-node6: transpile
node test/node6.js

Expand All @@ -38,9 +41,11 @@ transpile:
babel src --out-dir es5

build: transpile
if [ ! -d "cjs" ]; then mkdir cjs; fi
cp -r src/* cjs
if [ ! -d "esm" ]; then mkdir esm; fi
cp -r src/* esm
rollup -c rollup.config.js
touch cjs/package.json
echo '{"type": "commonjs"}' > cjs/package.json

docs:
cd docs; jekyll serve build --watch
6 changes: 6 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -83,6 +83,7 @@
"eslint-config-origami-component": "^1.0.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-prettier": "^2.6.1",
"esm": "^3.2.25",
"karma": "^3.1.4",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^2.2.0",
Expand Down
8 changes: 4 additions & 4 deletions rollup.config.js
Expand Up @@ -8,16 +8,16 @@ export default [
{
input: 'src/client.js',
output: {
file: 'esm/client.mjs',
format: 'esm'
file: 'cjs/client.js',
format: 'cjs'
},
plugins: [resolve({preferBuiltins: false}), commonjs()]
},
{
input: 'src/server.js',
output: {
file: 'esm/server.mjs',
format: 'esm'
file: 'cjs/server.js',
format: 'cjs'
},
plugins: [resolve({preferBuiltins: true}), commonjs(), builtins(), globals()]
},
Expand Down
8 changes: 5 additions & 3 deletions src/client-legacy.js
@@ -1,5 +1,7 @@
require('babel-core').transform('code', {
import babel from 'babel-core'

babel.transform('code', {
plugins: ['transform-runtime']
});

module.exports = require('./client');
import fetchMock from './client'
export default fetchMock;
8 changes: 4 additions & 4 deletions src/client.js
@@ -1,7 +1,7 @@
const FetchMock = require('./lib/index');
const statusTextMap = require('./lib/status-text');
import FetchMock from './lib/index';
import statusTextMap from './lib/status-text';
const theGlobal = typeof window !== 'undefined' ? window : self;
const { setUrlImplementation } = require('./lib/request-utils');
import { setUrlImplementation } from './lib/request-utils';
setUrlImplementation(theGlobal.URL);

FetchMock.global = theGlobal;
Expand All @@ -14,4 +14,4 @@ FetchMock.config = Object.assign(FetchMock.config, {
Headers: theGlobal.Headers
});

module.exports = FetchMock.createInstance();
export default FetchMock.createInstance();
8 changes: 3 additions & 5 deletions src/lib/compile-route.js
@@ -1,6 +1,6 @@
const generateMatcher = require('./generate-matcher');
import generateMatcher from './generate-matcher';

const sanitizeRoute = route => {
export const sanitizeRoute = route => {
route = Object.assign({}, route);

if (route.method) {
Expand Down Expand Up @@ -49,13 +49,11 @@ const delayResponse = route => {
}
};

module.exports = route => {
export default route => {
validateRoute(route);
route = sanitizeRoute(route);
route.matcher = generateMatcher(route);
limitMatcher(route);
delayResponse(route);
return route;
};

module.exports.sanitizeRoute = sanitizeRoute;
6 changes: 3 additions & 3 deletions src/lib/fetch-handler.js
@@ -1,5 +1,5 @@
const responseBuilder = require('./response-builder');
const requestUtils = require('./request-utils');
import responseBuilder from './response-builder';
import * as requestUtils from './request-utils';
const FetchMock = {};

// see https://heycam.github.io/webidl/#aborterror for the standardised interface
Expand Down Expand Up @@ -168,4 +168,4 @@ FetchMock.push = function({ url, options, request, isUnmatched, identifier }) {
this._calls.push(args);
};

module.exports = FetchMock;
export default FetchMock;
16 changes: 8 additions & 8 deletions src/lib/generate-matcher.js
@@ -1,13 +1,13 @@
const glob = require('glob-to-regexp');
const pathToRegexp = require('path-to-regexp');
const querystring = require('querystring');
const {
headers: headerUtils,
import glob from 'glob-to-regexp';
import pathToRegexp from 'path-to-regexp';
import querystring from 'querystring';
import {
headers as headerUtils,
getPath,
getQuery,
normalizeUrl
} = require('./request-utils');
const isEqual = require('lodash.isequal');
} from './request-utils';
import isEqual from 'lodash.isequal';

const stringMatchers = {
begin: targetString => url => url.indexOf(targetString) === 0,
Expand Down Expand Up @@ -129,7 +129,7 @@ const getUrlMatcher = route => {
};
};

module.exports = route => {
export default route => {
const matchers = [
route.query && getQueryStringMatcher(route),
route.method && getMethodMatcher(route),
Expand Down
8 changes: 4 additions & 4 deletions src/lib/index.js
@@ -1,6 +1,6 @@
const setUpAndTearDown = require('./set-up-and-tear-down');
const fetchHandler = require('./fetch-handler');
const inspecting = require('./inspecting');
import setUpAndTearDown from './set-up-and-tear-down';
import fetchHandler from './fetch-handler';
import inspecting from './inspecting';

const FetchMock = Object.assign({}, fetchHandler, setUpAndTearDown, inspecting);

Expand Down Expand Up @@ -50,4 +50,4 @@ FetchMock.sandbox = function() {
return sandbox;
};

module.exports = FetchMock;
export default FetchMock;
8 changes: 4 additions & 4 deletions src/lib/inspecting.js
@@ -1,7 +1,7 @@
const { normalizeUrl } = require('./request-utils');
import { normalizeUrl } from './request-utils';
const FetchMock = {};
const { sanitizeRoute } = require('./compile-route');
const generateMatcher = require('./generate-matcher');
import { sanitizeRoute } from './compile-route';
import generateMatcher from './generate-matcher';
const isName = nameOrMatcher =>
typeof nameOrMatcher === 'string' && /^[\da-zA-Z\-]+$/.test(nameOrMatcher);

Expand Down Expand Up @@ -104,4 +104,4 @@ FetchMock.done = function(nameOrMatcher) {
.every(isDone => isDone);
};

module.exports = FetchMock;
export default FetchMock;
29 changes: 16 additions & 13 deletions src/lib/request-utils.js
Expand Up @@ -16,7 +16,7 @@ const headersToArray = headers => {
const zipObject = entries =>
entries.reduce((obj, [key, val]) => Object.assign(obj, { [key]: val }), {});

const normalizeUrl = url => {
export const normalizeUrl = url => {
if (
typeof url === 'function' ||
url instanceof RegExp ||
Expand All @@ -33,11 +33,11 @@ const normalizeUrl = url => {
}
};

module.exports = {
setUrlImplementation: it => {
export const setUrlImplementation = it => {
URL = it;
},
normalizeRequest: (url, options, Request) => {
}

export const normalizeRequest = (url, options, Request) => {
if (Request.prototype.isPrototypeOf(url)) {
const obj = {
url: normalizeUrl(url.url),
Expand Down Expand Up @@ -72,22 +72,26 @@ module.exports = {
} else {
throw new TypeError('fetch-mock: Invalid arguments passed to fetch');
}
},
normalizeUrl,
getPath: url => {
}




export const getPath = url => {
const u = absoluteUrlRX.test(url)
? new URL(url)
: new URL(url, 'http://dummy');
return u.pathname;
},
}

getQuery: url => {
export const getQuery = url => {
const u = absoluteUrlRX.test(url)
? new URL(url)
: new URL(url, 'http://dummy');
return u.search ? u.search.substr(1) : '';
},
headers: {
}

export const headers = {
normalize: headers => zipObject(headersToArray(headers)),
toLowerCase: headers =>
Object.keys(headers).reduce((obj, k) => {
Expand All @@ -109,4 +113,3 @@ module.exports = {
return actualHeader.every((val, i) => val === expectedHeader[i]);
}
}
};
2 changes: 1 addition & 1 deletion src/lib/response-builder.js
Expand Up @@ -169,4 +169,4 @@ e.g. {"body": {"status: "registered"}}`);
}
}

module.exports = options => new ResponseBuilder(options).exec();
export default options => new ResponseBuilder(options).exec();
4 changes: 2 additions & 2 deletions src/lib/set-up-and-tear-down.js
@@ -1,4 +1,4 @@
const compileRoute = require('./compile-route');
import compileRoute from './compile-route';
const FetchMock = {};

FetchMock.mock = function(...args) {
Expand Down Expand Up @@ -135,4 +135,4 @@ FetchMock.restore = FetchMock.reset = function() {
return this;
};

module.exports = FetchMock;
export default FetchMock;
2 changes: 1 addition & 1 deletion src/lib/status-text.js
Expand Up @@ -63,4 +63,4 @@ const statusTextMap = {
'511': 'Network Authentication Required'
};

module.exports = statusTextMap;
export default statusTextMap;
1 change: 0 additions & 1 deletion src/package.json

This file was deleted.

15 changes: 8 additions & 7 deletions src/server.js
@@ -1,12 +1,13 @@
const fetch = require('node-fetch');
import fetch from 'node-fetch';
const Request = fetch.Request;
const Response = fetch.Response;
const Headers = fetch.Headers;
const Stream = require('stream');
const FetchMock = require('./lib/index');
const http = require('http');
const { setUrlImplementation } = require('./lib/request-utils');
setUrlImplementation(require('whatwg-url').URL);
import Stream from 'stream';
import FetchMock from './lib/index';
import http from 'http';
import { setUrlImplementation } from './lib/request-utils';
import {URL} from 'whatwg-url'
setUrlImplementation(URL);

FetchMock.global = global;
FetchMock.statusTextMap = http.STATUS_CODES;
Expand All @@ -19,4 +20,4 @@ FetchMock.config = Object.assign(FetchMock.config, {
Headers: Headers
});

module.exports = FetchMock.createInstance();
export default FetchMock.createInstance();
7 changes: 4 additions & 3 deletions test/client.js
@@ -1,5 +1,6 @@
const fetchMock = require('../src/client.js');
const expect = require('chai').expect;
import fetchMock from '../src/client';
import {expect} from 'chai';
import runner from './runner'

describe('native fetch behaviour', function() {
it('should not throw when passing unmatched calls through to native fetch', function() {
Expand All @@ -26,7 +27,7 @@ describe('request types that only work in the browser', function() {
});
});

require('./runner')(fetchMock, window, window.fetch, window.AbortController);
runner(fetchMock, window, window.fetch, window.AbortController);

describe('no real fetch', function() {
it('should cope when there is no global fetch defined', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/fetch-proxy.js
@@ -1 +1 @@
module.exports = require('node-fetch');
export default require('node-fetch');
2 changes: 1 addition & 1 deletion test/fixtures/sw.js
@@ -1,4 +1,4 @@
const fetchMock = require('../../src/client');
import fetchMock from '../../src/client';

self.addEventListener('install', ev => {
fetchMock.mock(/.*/, 203);
Expand Down
7 changes: 5 additions & 2 deletions test/node6.js
Expand Up @@ -2,10 +2,13 @@
// transpiling all the tests, which muddies the waters a bit
// So instead just making sure there are no syntax errors or issues with
// missing globals or methods
require('babel-core').transform('code', {
import babel from 'babel-core'

babel.transform('code', {
plugins: ['transform-runtime']
});
const fetchMock = require('../es5/server');

import fetchMock from '../es5/server';

fetchMock.mock('http://it.at.there/', 200);
fetchMock.fetchHandler('http://it.at.there/').catch(() => process.exit(2));
34 changes: 23 additions & 11 deletions test/runner.js
@@ -1,14 +1,26 @@
module.exports = (fetchMock, theGlobal, fetch, AbortController) => {
import setUpAndTearDown from './specs/set-up-and-tear-down.test';
import globalFetch from './specs/global-fetch.test';
import sandbox from './specs/sandbox.test';
import routing from './specs/routing.test';
import responses from './specs/responses.test';
import inspecting from './specs/inspecting.test';
import repeat from './specs/repeat.test';
import customImplementations from './specs/custom-implementations.test';
import options from './specs/options.test';
import abortable from './specs/abortable.test';


export default (fetchMock, theGlobal, fetch, AbortController) => {
describe('fetch-mock', () => {
require('./specs/set-up-and-tear-down.test')(fetchMock);
require('./specs/global-fetch.test')(fetchMock, theGlobal);
require('./specs/sandbox.test')(fetchMock, theGlobal);
require('./specs/routing.test')(fetchMock);
require('./specs/responses.test')(fetchMock);
require('./specs/inspecting.test')(fetchMock);
require('./specs/repeat.test')(fetchMock);
require('./specs/custom-implementations.test')(fetchMock);
require('./specs/options.test')(fetchMock, theGlobal, fetch);
require('./specs/abortable.test')(fetchMock, AbortController);
setUpAndTearDown(fetchMock);
globalFetch(fetchMock, theGlobal);
sandbox(fetchMock, theGlobal);
routing(fetchMock);
responses(fetchMock);
inspecting(fetchMock);
repeat(fetchMock);
customImplementations(fetchMock);
options(fetchMock, theGlobal, fetch);
abortable(fetchMock, AbortController);
});
};