Skip to content

Instantly share code, notes, and snippets.

@ctaggart
Last active December 16, 2016 03:03
Show Gist options
  • Save ctaggart/001de11269981c1d4eb3d3e5d38fa4b7 to your computer and use it in GitHub Desktop.
Save ctaggart/001de11269981c1d4eb3d3e5d38fa4b7 to your computer and use it in GitHub Desktop.
Fetch from Browsers using Mocha & System.js
#!/bin/sh -e
npm install --save-dev typescript
npm install --save-dev http-server
# browser
npm install --save whatwg-fetch
npm install --save-dev @types/whatwg-fetch
npm install --save es6-promise
npm install --save systemjs
npm install --save-dev @types/systemjs
# node
npm install --save node-fetch
npm install --save-dev @types/node
# testing
npm install --save-dev mocha
npm install --save-dev @types/mocha
npm install --save-dev chai
npm install --save-dev @types/chai
import Mocha = require("mocha");
import fetch = require("node-fetch");
(<any>global).fetch = fetch;
var runner = new Mocha({ui: "bdd"});
runner.addFile("node/build/fetch-tests.js");
runner.run();
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"target": "es6",
"outDir": "build"
},
"files": [
"tests.ts",
"../fetch-tests.ts",
"../FreeGeoIP.d.ts"
],
"typeRoots": [
"../../node_modules/@types"
]
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tests</title>
<link href="node_modules/mocha/mocha.css" rel="stylesheet" />
</head>
<body>
<div id="mocha"></div>
<script src="node_modules/es6-promise/dist/es6-promise.js"></script>
<script src="node_modules/whatwg-fetch/fetch.js"></script>
<script src="node_modules/systemjs/dist/system.js"></script>
<script src="build/tests.js"></script>
</body>
</html>
SystemJS.config({
meta: {
'node_modules/mocha/mocha.js': { format: 'global' }
},
map: {
chai: "node_modules/chai/chai.js",
mocha: "node_modules/mocha/mocha.js"
}
});
SystemJS.import("mocha").then(m => {
mocha.setup("bdd"); // https://mochajs.org/#bdd
SystemJS.import("build/fetch-tests.js").then(m => {
mocha.run();
});
});
{
"compilerOptions": {
"module": "system",
"sourceMap": true,
"target": "es5",
"lib": [ "dom", "es6" ],
"outDir": "build"
},
"files": [
"tests.ts",
"fetch-tests.ts",
"FreeGeoIP.d.ts"
],
"typeRoots": [
"../node_modules/@types"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment