Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 3bcc2de

Browse files
mjc1283jordangarcia
authored andcommitted
Update datafile loader to use XMLHttpRequest, and run tests in karma (optimizely#6)
- ChangeFetchUrlDatafileLoader class implementation to use XMLHttpRequest instead of fetch - Use karma to run tests - Built using karma-webpack. TS compiled via ts-loader - Use karma-chrome-launcher and puppeteer to run in headless Chrome - Remove old dependencies that are no longer used - Update tests to not spam log messages or send real events to logx
1 parent 84590e1 commit 3bcc2de

File tree

7 files changed

+3348
-208
lines changed

7 files changed

+3348
-208
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Karma configuration
2+
// Generated on Thu Jan 17 2019 18:12:32 GMT-0800 (Pacific Standard Time)
3+
4+
5+
// Run tests headlessly with Puppeteer
6+
// See: https://github.com/karma-runner/karma-chrome-launcher#headless-chromium-with-puppeteer
7+
process.env.CHROME_BIN = require('puppeteer').executablePath()
8+
9+
module.exports = function(config) {
10+
config.set({
11+
// base path that will be used to resolve all patterns (eg. files, exclude)
12+
basePath: '',
13+
14+
15+
// frameworks to use
16+
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
17+
frameworks: ['mocha'],
18+
19+
20+
// list of files / patterns to load in the browser
21+
files: [
22+
{ pattern: 'test/*.spec.ts', watched: false },
23+
],
24+
25+
26+
// list of files / patterns to exclude
27+
exclude: [
28+
],
29+
30+
31+
// preprocess matching files before serving them to the browser
32+
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
33+
preprocessors: {
34+
'test/*.spec.ts': ['webpack'],
35+
},
36+
37+
webpack: {
38+
mode: 'development',
39+
module: {
40+
rules: [
41+
{
42+
test: /\.tsx?$/,
43+
use: 'ts-loader',
44+
exclude: /node_modules/,
45+
},
46+
],
47+
},
48+
resolve: {
49+
extensions: ['.tsx', '.ts', '.js'],
50+
},
51+
},
52+
53+
// test results reporter to use
54+
// possible values: 'dots', 'progress'
55+
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
56+
// reporters: ['progress'],
57+
reporters: ['dots'],
58+
59+
60+
// web server port
61+
port: 9876,
62+
63+
64+
// enable / disable colors in the output (reporters and logs)
65+
colors: true,
66+
67+
68+
// level of logging
69+
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
70+
logLevel: config.LOG_INFO,
71+
72+
73+
// enable / disable watching file and executing tests whenever any file changes
74+
autoWatch: true,
75+
76+
77+
// start these browsers
78+
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
79+
// Note: you can run in regular (head-ful) Chrome by changing the line below to browsers: ['Chrome']
80+
// ChromeHeadless runs it via Puppeteer
81+
browsers: ['ChromeHeadless'],
82+
83+
84+
// Continuous Integration mode
85+
// if true, Karma captures browsers, runs the tests and exits
86+
singleRun: false,
87+
88+
// Concurrency level
89+
// how many browser should be started simultaneous
90+
concurrency: Infinity
91+
})
92+
}

packages/js-web-sdk/packages/js-web-sdk/package.json

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,35 @@
1313
},
1414
"scripts": {
1515
"tsc": "tsc",
16-
"test": "./node_modules/.bin/ts-mocha --exit test/**",
16+
"test": "karma start karma.conf.js --single-run",
17+
"test:watch": "karma start karma.conf.js",
1718
"build-browser-umd": "rm -rf dist && webpack",
1819
"cover": "nyc mocha test/**",
1920
"coveralls": "npm run cover -- --report lcovonly && cat ./coverage/lcov.info | coveralls",
2021
"prepublishOnly": "npm run build-browser-umd && npm test && npm run test-xbrowser"
2122
},
2223
"dependencies": {
23-
"@optimizely/optimizely-sdk": "^3.0.0-rc2",
24-
"node-fetch": "^2.3.0"
24+
"@optimizely/optimizely-sdk": "^3.0.0-rc2"
2525
},
2626
"devDependencies": {
2727
"@types/chai": "^4.1.7",
2828
"@types/mocha": "^5.2.5",
29-
"@types/nock": "^9.3.0",
30-
"@types/sinon": "^5.0.6",
29+
"@types/sinon": "^7.0.4",
3130
"chai": "^4.2.0",
3231
"coveralls": "^3.0.2",
3332
"istanbul": "^0.4.5",
33+
"karma": "^3.1.4",
34+
"karma-chrome-launcher": "^2.2.0",
35+
"karma-mocha": "^1.3.0",
36+
"karma-webpack": "^3.0.5",
3437
"mocha": "^5.2.0",
3538
"mocha-lcov-reporter": "^1.3.0",
36-
"nock": "^10.0.2",
3739
"nyc": "^13.1.0",
38-
"sinon": "^2.3.1",
39-
"ts-mocha": "^2.0.0",
40-
"typescript": "^3.1.6"
40+
"puppeteer": "^1.11.0",
41+
"sinon": "^7.2.3",
42+
"ts-loader": "^5.3.3",
43+
"typescript": "^3.1.6",
44+
"webpack": "^4.29.0"
4145
},
4246
"nyc": {
4347
"include": [

packages/js-web-sdk/packages/js-web-sdk/src/DatafileLoaders.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { OptimizelyDatafile } from './Datafile'
22
import { ResourceLoader } from './ResourceManager'
3-
const fetch = require('node-fetch')
43

54
export class ProvidedDatafileLoader implements ResourceLoader<OptimizelyDatafile> {
65
private datafile: OptimizelyDatafile
@@ -70,17 +69,35 @@ export class FetchUrlDatafileLoader implements ResourceLoader<OptimizelyDatafile
7069
return (Date.now() - cacheResult.metadata.timestampCached) <= FetchUrlDatafileLoader.MAX_CACHE_AGE_MS
7170
}
7271

72+
private static GET_METHOD = 'GET'
73+
74+
private static READY_STATE_COMPLETE = 4
75+
7376
async fetchDatafile(): Promise<OptimizelyDatafile> {
7477
const datafileUrl = `https://cdn.optimizely.com/datafiles/${this.sdkKey}.json`
75-
const resp = await fetch(datafileUrl, { mode: 'cors' })
76-
if (resp.status !== 200) {
77-
return Promise.reject(resp)
78-
}
78+
return new Promise((resolve, reject) => {
79+
const req = new XMLHttpRequest()
80+
req.open(FetchUrlDatafileLoader.GET_METHOD, datafileUrl, true);
81+
req.onreadystatechange = () => {
82+
if (req.readyState === FetchUrlDatafileLoader.READY_STATE_COMPLETE) {
83+
// TODO: Improve & add more error handling
84+
if (req.status >= 400) {
85+
reject('Error response fetching datafile')
86+
return
87+
}
7988

80-
let datafile: any = await resp.json()
81-
// TODO handle errors
82-
83-
return datafile
89+
let datafile
90+
try {
91+
datafile = JSON.parse(req.response)
92+
} catch (e) {
93+
reject(`Datafile was not valid JSON. Got: ${req.response}`)
94+
return;
95+
}
96+
resolve(datafile)
97+
}
98+
}
99+
req.send()
100+
})
84101
}
85102

86103
getFromCache(): FetchUrlCacheEntry | null {

packages/js-web-sdk/packages/js-web-sdk/src/UserIdLoaders.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ResourceLoader } from './ResourceManager'
2-
import { emitter } from 'nock';
32
import * as utils from './utils'
43

54
export type UserId = string | null

0 commit comments

Comments
 (0)