Skip to content

Commit

Permalink
Fix Jest to work with TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Sep 2, 2019
1 parent 6a2e2af commit b6436e4
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 276 deletions.
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,5 @@ language: node_js
node_js:
- node

install:
- cd packages/browser
- yarn install

script:
- yarn test
- ./scripts/test.sh
1 change: 1 addition & 0 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"imports-loader": "0.8.0",
"jest": "^24.8.0",
"promise-polyfill": "^8.1.3",
"rollup": "^1.20.3",
"rollup-plugin-commonjs": "^10.0.1",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-replace": "^2.2.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/browser/tests/processor/stacktracejs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('stacktracejs processor', () => {

it('provides backtrace', () => {
let backtrace = error.backtrace;
expect(backtrace.length).toBe(5);
expect(backtrace.length).toBeGreaterThanOrEqual(5);

let frame = backtrace[0];
expect(frame.file).toContain('tests/processor/stacktracejs.test');
Expand All @@ -45,7 +45,7 @@ describe('stacktracejs processor', () => {

it('provides backtrace', () => {
let backtrace = error.backtrace;
expect(backtrace.length).toBe(4);
expect(backtrace.length).toBeGreaterThanOrEqual(4);
});
});
});
3 changes: 3 additions & 0 deletions packages/node/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: [['@babel/preset-env', { targets: { node: 'current' } }]],
};
10 changes: 10 additions & 0 deletions packages/node/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
transform: {
'^.+\\.jsx?$': 'babel-jest',
'^.+\\.tsx?$': 'ts-jest',
},
testEnvironment: 'node',
moduleNameMapper: {
'^@browser/(.*)$': '<rootDir>/../browser/src/$1',
},
};
11 changes: 11 additions & 0 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@
"airbrake",
"notifier"
],
"dependencies": {
"cross-fetch": "^3.0.4",
"tdigest": "^0.1.1"
},
"devDependencies": {
"@babel/core": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"babel-jest": "^24.9.0",
"jest": "^24.8.0",
"rollup": "^1.20.3",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-typescript2": "^0.22.1",
"ts-jest": "^24.0.2",
"typescript": "^3.5.3"
},
"main": "dist/airbrake.common.js",
Expand Down
15 changes: 4 additions & 11 deletions packages/node/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import typescript from 'rollup-plugin-typescript2';
import replace from 'rollup-plugin-replace';

const nodePlugins = [typescript()];
const pkg = require('./package.json');

const nodePlugins = [typescript(), replace({ VERSION: `${pkg.version}` })];

function cjs(cfg) {
return Object.assign(
Expand All @@ -12,16 +15,6 @@ function cjs(cfg) {
);
}

function esm(cfg) {
return Object.assign(
{
format: 'esm',
sourcemap: true,
},
cfg,
);
}

export default [
{
input: 'src/node.entry.ts',
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/filter/node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { INotice } from '../notice';
import { INotice } from '@browser/notice';

const os = require('os');

Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/instrumentation/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function makeMiddleware(airbrake: Notifier) {
next();
let end = Date.now();
let route = req.route ? req.route.path : 'UNKNOWN';
airbrake.routes.notifyRequest({
airbrake.routes.notify({
method: req.method,
route,
statusCode: res.statusCode,
Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/notifier.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IOptions } from './options';
import { BaseNotifier } from './base_notifier';
import { IOptions } from '@browser/options';
import { BaseNotifier } from '@browser/base_notifier';
import { nodeFilter } from './filter/node';
import { Routes } from './routes';

Expand Down
98 changes: 48 additions & 50 deletions packages/node/src/routes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { makeRequester, Requester } from './http_req';
import { IOptions } from './options';
import { IOptions } from '@browser/options';
import { makeRequester, Requester } from '@browser/http_req';

const TDigest = require('tdigest').TDigest;

Expand Down Expand Up @@ -33,12 +33,27 @@ interface IRouteKey {
time: Date;
}

interface IRouteStat {
count: number;
sum: number;
sumsq: number;
tdigest?: ITDigest;
tdigestCentroids?: ITDigestCentroids;
class TDigestStat {
protected count = 0;
protected sum = 0;
protected sumsq = 0;
protected td = new TDigest();

add(ms: number) {
this.count += 1;
this.sum += ms;
this.sumsq += ms * ms;
this.td.push(ms);
}

toJSON() {
return {
count: this.count,
sum: this.sum,
sumsq: this.sumsq,
tdigestCentroids: tdigestCentroids(this.td),
};
}
}

type time = Date | number;
Expand All @@ -47,26 +62,27 @@ export interface IRequestInfo {
method: string;
route: string;
statusCode: number;
contentType?: string;
start: time;
end: time;
}

export class Routes {
private opts: IOptions;
private url: string;
// TODO: use RouteKey as map key
private m: { [key: string]: IRouteStat } = {};
private timer;
protected opts: IOptions;
protected url: string;
protected requester: Requester;

private requester: Requester;
// TODO: use RouteKey as map key
protected m: { [key: string]: TDigestStat } = {};
protected timer;

constructor(opts: IOptions) {
this.opts = opts;
this.url = `${opts.host}/api/v5/projects/${opts.projectId}/routes-stats?key=${opts.projectKey}`;
this.requester = makeRequester(opts);
}

public notifyRequest(req: IRequestInfo): void {
public notify(req: IRequestInfo): void {
let ms = durationMs(req.start, req.end);
if (ms === 0) {
ms = 0.00001;
Expand All @@ -83,24 +99,15 @@ export class Routes {
};
let keyStr = JSON.stringify(key);

let stat: IRouteStat;
let stat: TDigestStat;
if (keyStr in this.m) {
stat = this.m[keyStr];
} else {
stat = {
count: 0,
sum: 0,
sumsq: 0,
tdigest: new TDigest(),
};

stat = new TDigestStat();
this.m[keyStr] = stat;
}

stat.count++;
stat.sum += ms;
stat.sumsq += ms * ms;
stat.tdigest.push(ms);
stat.add(ms);

if (this.timer) {
return;
Expand All @@ -110,22 +117,13 @@ export class Routes {
}, FLUSH_INTERVAL);
}

private flush(): void {
protected flush(): void {
let routes = [];
for (let keyStr in this.m) {
if (!this.m.hasOwnProperty(keyStr)) {
continue;
}
let key: IRouteKey = JSON.parse(keyStr);
let v = {
...key,
...this.m[keyStr],
};
if (v.tdigest) {
v.tdigestCentroids = this.tdigestCentroids(v.tdigest);
delete v.tdigest;
}
routes.push(v);
routes.push(this.m[keyStr].toJSON());
}

this.m = {};
Expand All @@ -150,19 +148,19 @@ export class Routes {
}
});
}
}

private tdigestCentroids(td: ITDigest): ITDigestCentroids {
let means: number[] = [];
let counts: number[] = [];
td.centroids.each((c: ICentroid) => {
means.push(c.mean);
counts.push(c.n);
});
return {
mean: means,
count: counts,
};
}
function tdigestCentroids(td: ITDigest): ITDigestCentroids {
let means: number[] = [];
let counts: number[] = [];
td.centroids.each((c: ICentroid) => {
means.push(c.mean);
counts.push(c.n);
});
return {
mean: means,
count: counts,
};
}

function toTime(tm: time): number {
Expand Down
30 changes: 30 additions & 0 deletions packages/node/tests/routes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Routes } from '../src/routes';

describe('Routes', () => {
it('works', () => {
let routes = new Routes({
projectId: 1,
projectKey: 'test',
});

routes.notify({
method: 'GET',
route: '/projects/:id',
statusCode: 200,
contentType: 'application/json',
start: new Date(1),
end: new Date(1000),
});
clearTimeout(routes.timer);

let m = JSON.parse(JSON.stringify(routes.m));
expect(m).toStrictEqual({
'{"method":"GET","route":"/projects/:id","statusCode":200,"time":"1970-01-01T00:00:00.000Z"}': {
count: 1,
sum: 999,
sumsq: 998001,
tdigestCentroids: { count: [1], mean: [999] },
},
});
});
});
4 changes: 4 additions & 0 deletions packages/node/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@browser/*": ["../browser/src/*"]
},
"rootDirs": ["src", "../browser/src"],
"lib": ["ES2015", "DOM"],
"target": "ES5",
Expand Down
19 changes: 19 additions & 0 deletions packages/node/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": [
"tslint:latest",
"tslint-config-prettier",
"tslint-no-unused-expression-chai"
],
"rules": {
"prefer-const": false,
"object-literal-sort-keys": false,
"variable-name": [true, "allow-leading-underscore"],
"no-console": [true, "log"],
"no-bitwise": false,
"no-implicit-dependencies": false,
"no-submodule-imports": [true, "promise-polyfill/src/polyfill"],
"member-ordering": false,
"no-shadowed-variable": [true, { "temporalDeadZone": false }],
"no-unused-expression-chai": [true, "allow-new"]
}
}
Loading

0 comments on commit b6436e4

Please sign in to comment.