Skip to content

Commit

Permalink
Convert historian test to jest
Browse files Browse the repository at this point in the history
  • Loading branch information
anmic committed Jul 19, 2019
1 parent 8b57080 commit 0ba8331
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 63 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"babel-plugin-transform-define": "^1.3.1",
"chai": "4.2.0",
"imports-loader": "0.8.0",
"isomorphic-fetch": "^2.2.1",
"jest": "^24.8.0",
"karma": "4.2.0",
"karma-chrome-launcher": "3.0.0",
Expand Down
137 changes: 74 additions & 63 deletions test/unit/historian_test.ts → tests/historian.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import * as sinon from 'sinon';
import Client from '../../src/client';
import { expect } from './sinon_chai';
import fetch from 'isomorphic-fetch';
import Client from '../src/client';

class Location {
private s: string;
constructor(s) {
this.s = s;
}

constructor(s: string) {
this.s = s;
}

public toString(): string {
return this.s;
}
toString() {
return this.s;
}
}

describe('instrumentation', () => {
Expand All @@ -20,10 +17,10 @@ describe('instrumentation', () => {
let client;

beforeEach(() => {
processor = sinon.spy((data) => {
processor = jest.fn((data) => {
return data;
});
reporter = sinon.spy(() => {
reporter = jest.fn(() => {
return Promise.resolve({ id: 1 });
});
client = new Client({
Expand All @@ -39,7 +36,7 @@ describe('instrumentation', () => {
let locations = ['', 'http://hello/world', 'foo', new Location('/')];
for (let loc of locations) {
try {
window.history.pushState(null, '', loc as string);
window.history.pushState(null, '', loc);
} catch (_) {
// ignore
}
Expand All @@ -48,29 +45,29 @@ describe('instrumentation', () => {
});

it('records browser history', () => {
expect(reporter).to.have.been.called;
let notice = reporter.lastCall.args[0];
expect(reporter.mock.calls.length).toBe(1);
let notice = reporter.mock.calls[0][0];
let history = notice.context.history;

let state = history[history.length - 3];
delete state.date;
expect(state).to.deep.equal({
expect(state).toStrictEqual({
type: 'location',
from: '/context.html',
from: '/',
to: '/world',
});

state = history[history.length - 2];
delete state.date;
expect(state).to.deep.equal({
expect(state).toStrictEqual({
type: 'location',
from: '/world',
to: '/foo',
});

state = history[history.length - 1];
delete state.date;
expect(state).to.deep.equal({
expect(state).toStrictEqual({
type: 'location',
from: '/foo',
to: '/',
Expand All @@ -80,70 +77,84 @@ describe('instrumentation', () => {

describe('XHR', () => {
beforeEach(() => {
let req = new XMLHttpRequest();
req.open('GET', 'http://ip2c.org/self', false);
req.send();
client.notify(new Error('test'));
let promise = new Promise((resolve, reject) => {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://ip2c.org/self');
xhr.onreadystatechange = () => {
if (xhr.readyState != 4) return;
if (xhr.status == 200) {
resolve(xhr.response);
} else {
reject();
}
};
xhr.send();
});

promise.then(() => {
client.notify(new Error('test'));
});
return promise;
});

it('records request', () => {
expect(reporter).to.have.been.called;
let notice = reporter.lastCall.args[0];
it.only('records request', () => {
expect(reporter.mock.calls.length).toBe(1);
let notice = reporter.mock.calls[0][0];
let history = notice.context.history;

let state = history[history.length - 1];
expect(state.type).to.equal('xhr');
expect(state.method).to.equal('GET');
expect(state.url).to.equal('http://ip2c.org/self');
expect(state.statusCode).to.equal(200);
expect(state.duration).to.be.a('number');
expect(state.type).toBe('xhr');
expect(state.method).toBe('GET');
expect(state.url).toBe('http://ip2c.org/self');
expect(state.statusCode).toBe(200);
expect(state.duration).toEqual(expect.any(Number));
});
});

describe('fetch', () => {
describe('simple fetch', () => {
beforeEach(() => {
let promise = fetch('http://ip2c.org/4.4.4.4');
let promise = global.fetch('http://ip2c.org/4.4.4.4');
promise.then(() => {
client.notify(new Error('test'));
});
return promise;
});

it('records request', () => {
expect(reporter).to.have.been.called;
let notice = reporter.lastCall.args[0];
expect(reporter.mock.calls.length).toBe(1);
let notice = reporter.mock.calls[0][0];
let history = notice.context.history;

let state = history[history.length - 1];
expect(state.type).to.equal('xhr');
expect(state.method).to.equal('GET');
expect(state.url).to.equal('http://ip2c.org/4.4.4.4');
expect(state.statusCode).to.equal(200);
expect(state.duration).to.be.a('number');
expect(state.type).toBe('xhr');
expect(state.method).toBe('GET');
expect(state.url).toBe('http://ip2c.org/4.4.4.4');
expect(state.statusCode).toBe(200);
expect(state.duration).toEqual(expect.any(Number));
});
});

describe('fetch with options', () => {
beforeEach(() => {
let promise = fetch('http://ip2c.org/4.4.4.4', { method: 'POST' });
let promise = global.fetch('http://ip2c.org/4.4.4.4', { method: 'POST' });
promise.then(() => {
client.notify(new Error('test'));
});
return promise;
});

it('records request', () => {
expect(reporter).to.have.been.called;
let notice = reporter.lastCall.args[0];
expect(reporter.mock.calls.length).toBe(1);
let notice = reporter.mock.calls[0][0];
let history = notice.context.history;

let state = history[history.length - 1];
expect(state.type).to.equal('xhr');
expect(state.method).to.equal('POST');
expect(state.url).to.equal('http://ip2c.org/4.4.4.4');
expect(state.statusCode).to.equal(200);
expect(state.duration).to.be.a('number');
expect(state.type).toBe('xhr');
expect(state.method).toBe('POST');
expect(state.url).toBe('http://ip2c.org/4.4.4.4');
expect(state.statusCode).toBe(200);
expect(state.duration).toEqual(expect.any(Number));
});
});

Expand All @@ -153,24 +164,24 @@ describe('instrumentation', () => {
method: 'POST',
body: '{"foo": "bar"}',
});
let promise = fetch(req);
let promise = global.fetch(req);
promise.then(() => {
client.notify(new Error('test'));
});
return promise;
});

it('records request', () => {
expect(reporter).to.have.been.called;
let notice = reporter.lastCall.args[0];
expect(reporter.mock.calls.length).toBe(1);
let notice = reporter.mock.calls[0][0];
let history = notice.context.history;

let state = history[history.length - 1];
expect(state.type).to.equal('xhr');
expect(state.method).to.equal('POST');
expect(state.url).to.equal('http://ip2c.org/4.4.4.4');
expect(state.statusCode).to.equal(200);
expect(state.duration).to.be.a('number');
expect(state.type).toBe('xhr');
expect(state.method).toBe('POST');
expect(state.url).toBe('http://ip2c.org/4.4.4.4');
expect(state.statusCode).toBe(200);
expect(state.duration).toEqual(expect.any(Number));
});
});
});
Expand All @@ -185,20 +196,20 @@ describe('instrumentation', () => {
});

it('records log message', () => {
expect(reporter).to.have.been.called;
let notice = reporter.lastCall.args[0];
expect(reporter.mock.calls.length).toBe(1);
let notice = reporter.mock.calls[0][0];
let history = notice.context.history;
expect(history).to.have.length(20);
expect(history).toHaveLength(20);

for (let i in history) {
if (!history.hasOwnProperty(i)) {
continue;
}
let state = history[i];
expect(state.type).to.equal('log');
expect(state.severity).to.equal('log');
expect(state.arguments).to.deep.equal([+i + 5]);
expect(state.date).to.exist;
expect(state.type).toBe('log');
expect(state.severity).toBe('log');
expect(state.arguments).toStrictEqual([+i + 5]);
expect(state.date).not.toBeNull();
}
});
});
Expand Down

0 comments on commit 0ba8331

Please sign in to comment.