Created
December 12, 2016 01:04
-
-
Save ctaggart/7c8b7e11a37c1f1ebd7be9ceec588d7d to your computer and use it in GitHub Desktop.
TypeScript Testing of jQuery Ajax from Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface FreeGeoIP { | |
ip: string; | |
country_code: string; | |
country_name: string; | |
region_code: string; | |
region_name: string; | |
city: string; | |
zip_code: string; | |
time_zone: string; | |
latitude: number; | |
longitude: number; | |
metro_code: number; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh -e | |
npm install --save-dev @types/node | |
# unit testing | |
npm install --save-dev chai | |
npm install --save-dev mocha | |
npm install --save-dev @types/chai | |
npm install --save-dev @types/mocha | |
# jquery | |
npm install --save jquery | |
npm install --save-dev jsdom | |
npm install --save-dev @types/jquery | |
npm install --save-dev @types/jsdom |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as mocha from "mocha"; | |
import { assert } from "chai"; | |
import * as jsdom from "jsdom"; | |
import * as jquery from "jquery"; | |
describe("jquery tests", () => { | |
it("get ip", done => { | |
// window is required for jQuery to work in Node.js | |
// http://stackoverflow.com/a/36238302/23059 | |
jsdom.env("", (err, window) => { | |
const $ = require("jquery")(window) as JQueryStatic; | |
$.ajax({url: "http://freegeoip.net/json/"}).done(data => { | |
const geoip = data as FreeGeoIP; | |
console.log(geoip); | |
console.log("ip is " + geoip.ip); | |
assert.isString(geoip.ip); | |
assert.isString(geoip.country_code); | |
done(); | |
}); | |
}); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS C:\Users\c\ts\fetch> tsc | |
PS C:\Users\c\ts\fetch> mocha .\jquery-tests.js | |
jquery tests | |
{ ip: '23.16.226.33', | |
country_code: 'CA', | |
country_name: 'Canada', | |
region_code: 'BC', | |
region_name: 'British Columbia', | |
city: 'Revelstoke', | |
zip_code: '', | |
time_zone: 'America/Vancouver', | |
latitude: 51, | |
longitude: -118.1833, | |
metro_code: 0 } | |
ip is 23.16.226.33 | |
√ get ip (624ms) | |
1 passing (624ms) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"compilerOptions": { | |
"module": "commonjs", | |
"sourceMap": true | |
}, | |
"files": [ | |
"jquery-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