Skip to content
Merged
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bower_components
node_modules
coverage*
coverage*
dist
src/pretender.es.js
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
src/index.ts
test
rollup.config.js
karma.conf.js
*.md
yarn.lock
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ const server = new Pretender(function() {
$.get('/photos/12', {success() => { ... }})
```

## Usage

```
yard add -D pretender
# or
npm install --save-dev pretender
```

You can load Pretender directly in the browser.

```javascript
<script src="pretender.js"></script>
```

Or as a module.

```javascript
import Pretender from 'pretender';
const server = new Pretender(function() {});
```

## The Server DSL
The server DSL is inspired by express/sinatra. Pass a function to the Pretender constructor
that will be invoked with the Pretender instance as its context. Available methods are
Expand Down
4 changes: 2 additions & 2 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = function(config) {
'node_modules/es6-promise/dist/es6-promise.auto.js',
'node_modules/abortcontroller-polyfill/dist/abortcontroller-polyfill-only.js',
'node_modules/whatwg-fetch/dist/fetch.umd.js',
'pretender.js',
'dist/pretender.js',
'test/**/*.js'
],

Expand All @@ -35,7 +35,7 @@ module.exports = function(config) {
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'pretender.js': ['coverage']
'dist/pretender.js': ['coverage']
},

coverageReporter: {
Expand Down
21 changes: 16 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
{
"name": "pretender",
"version": "2.1.0",
"main": "./pretender.js",
"main": "./dist/pretender.js",
"module": "./src/pretender.es.js",
"types": "index.d.ts",
"description": "Pretender is a mock server library for XMLHttpRequest and Fetch, that comes with an express/sinatra style syntax for defining routes and their handlers.",
"license": "MIT",
"engines": {
"node": "6.* || 8.* || 10.* || >= 11.*"
},
"scripts": {
"prepublishOnly": "npm run build && npm run tests-only",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making sure that the build step is ran before publishing a new version, in case a PR bypassed a red CI somehow.

"pretest": "bower install",
"build": "rollup --config",
"test": "npm run lint && npm run jscs && npm run tests-only",
"test-ci": "npm run pretest && npm run lint && npm run jscs && npm run tests-only-ci",
"test-ci": "npm run pretest && npm run build && npm run lint && npm run jscs && npm run tests-only-ci",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making sure that the build step is ran before running the tests, in case there is a PR that didn't include the build outputs.

"tests-only": "karma start --single-run",
"tests-only-ci": "karma start --single-run --browsers PhantomJS",
"lint": "jshint pretender.js test",
"jscs": "jscs pretender.js test",
"lint": "jshint test",
"jscs": "jscs test",
"test:server": "karma start --no-single-run"
},
"repository": {
Expand All @@ -36,7 +40,14 @@
"karma-sinon": "^1.0.5",
"phantomjs": "^2.1.7",
"qunit": "^2.6.1",
"sinon": "^3.2.1"
"rollup": "0.68.2",
"rollup-plugin-commonjs": "^9.2.0",
"rollup-plugin-multi-entry": "^2.1.0",
"rollup-plugin-node-resolve": "^4.0.0",
"rollup-plugin-typescript": "^1.0.0",
"sinon": "^3.2.1",
"tslib": "^1.9.3",
"typescript": "~3.1.1"
},
"dependencies": {
"whatwg-fetch": "^3.0.0",
Expand Down
42 changes: 42 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const commonjs = require('rollup-plugin-commonjs');
const path = require('path');
const resolve = require('rollup-plugin-node-resolve');
const typescript = require('rollup-plugin-typescript');
const pkg = require('./package.json');

const selfId = path.resolve(__dirname, 'src/iife-self-placeholder.js');

module.exports = {
input: 'src/index.ts',
external: [
selfId,
'whatwg-fetch',
'fake-xml-http-request',
'route-recognizer',
],
output: [
{
name: 'Pretender',
file: pkg.main,
format: 'iife',
globals: {
[selfId]: 'self',
'whatwg-fetch': 'FakeFetch',
'fake-xml-http-request': 'FakeXMLHttpRequest',
'route-recognizer': 'RouteRecognizer',
},
banner: 'var FakeFetch = self.WHATWGFetch;\n' +
'var FakeXMLHttpRequest = self.FakeXMLHttpRequest;\n' +
'var RouteRecognizer = self.RouteRecognizer;\n',
},
{
file: pkg.module,
format: 'es'
},
],
plugins: [
commonjs(),
resolve(),
typescript()
],
};
3 changes: 3 additions & 0 deletions src/iife-self-placeholder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This is just a placeholder for the build step
// See the IIFE output in the Rollup config
export default window;
32 changes: 5 additions & 27 deletions pretender.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
(function(self) {
'use strict';

function getModuleDefault(module) {
return module.default || module;
}

var appearsBrowserified = typeof self !== 'undefined' &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a breaking change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't break anyones app. The build step produces two files with unique consumption mechanisms that immediately inform whether you are browserifying or not depending on which one you choose to use. So, Pretender doesn't need to figure that out at runtime anymore.

typeof process !== 'undefined' &&
(Object.prototype.toString.call(process) === '[object Object]' ||
Object.prototype.toString.call(process) === '[object process]');

var RouteRecognizer = appearsBrowserified ? getModuleDefault(require('route-recognizer')) : self.RouteRecognizer;
var FakeXMLHttpRequest = appearsBrowserified ? getModuleDefault(require('fake-xml-http-request')) :
self.FakeXMLHttpRequest;

// fetch related ponyfills
var FakeFetch = appearsBrowserified ? getModuleDefault(require('whatwg-fetch')) : self.WHATWGFetch;
import self from './iife-self-placeholder';
import RouteRecognizer from 'route-recognizer';
import FakeXMLHttpRequest from 'fake-xml-http-request';
import * as FakeFetch from 'whatwg-fetch';

/**
* parseURL - decompose a URL into its parts
Expand Down Expand Up @@ -495,12 +481,4 @@ Pretender.parseURL = parseURL;
Pretender.Hosts = Hosts;
Pretender.Registry = Registry;

if (typeof module === 'object') {
module.exports = Pretender;
} else if (typeof define !== 'undefined') {
define('pretender', [], function() {
return Pretender;
});
}
self.Pretender = Pretender;
}(self));
export default Pretender;
60 changes: 60 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"compilerOptions": {
/* Basic Options */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
"declaration": false, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "removeComments": true, /* Do not emit comments to output. */
"noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

"newLine": "LF",

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
"noUnusedLocals": true, /* Report errors on unused locals. */
"noUnusedParameters": true, /* Report errors on unused parameters. */
"noImplicitReturns": false, /* Report error when not all code paths in function return a value. */
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": false, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
}
}
Loading