Fix tests and rewrite to jest

This commit is contained in:
nndio 2019-01-08 18:35:12 +02:00
parent 7c3b78d312
commit b2adfb1238
19 changed files with 175 additions and 205 deletions

View File

@ -56,7 +56,7 @@
"eslint-plugin-react": "^4.3.0",
"file-loader": "^1.1.5",
"git-url-parse": "^7.0.1",
"jest": "^21.2.1",
"jest": "^23.6.0",
"jsdom": "^11.3.0",
"node-sass": "^3.13.0",
"postcss-loader": "^2.0.8",

View File

@ -108,7 +108,7 @@ exports[`Select should select another option 1`] = `
autosize={true}
clearable={false}
menuMaxHeight={200}
onChange={[Function]}
onChange={[MockFunction]}
options={
Array [
Object {
@ -131,7 +131,7 @@ exports[`Select should select another option 1`] = `
autosize={true}
clearable={false}
menuMaxHeight={200}
onChange={[Function]}
onChange={[MockFunction]}
options={
Array [
Object {
@ -180,7 +180,7 @@ exports[`Select should select another option 1`] = `
multi={false}
noResultsText="No results found"
onBlurResetsInput={true}
onChange={[Function]}
onChange={[MockFunction]}
onCloseResetsInput={true}
onSelectResetsInput={true}
openOnClick={true}
@ -357,7 +357,7 @@ exports[`Select shouldn't find any results 1`] = `
autosize={true}
clearable={false}
menuMaxHeight={200}
onChange={[Function]}
onChange={[MockFunction]}
options={
Array [
Object {
@ -380,7 +380,7 @@ exports[`Select shouldn't find any results 1`] = `
autosize={true}
clearable={false}
menuMaxHeight={200}
onChange={[Function]}
onChange={[MockFunction]}
options={
Array [
Object {
@ -429,7 +429,7 @@ exports[`Select shouldn't find any results 1`] = `
multi={false}
noResultsText="No results found"
onBlurResetsInput={true}
onChange={[Function]}
onChange={[MockFunction]}
onCloseResetsInput={true}
onSelectResetsInput={true}
openOnClick={true}

View File

@ -3,8 +3,11 @@
"env": {
"browser": true,
"node": true,
"es6": true
"jest": true
},
"globals": {
"test": true,
"expect": true
},
"parser": "babel-eslint"
}

View File

@ -8,7 +8,7 @@
"build": "babel src --out-dir lib",
"build:umd": "webpack src/index.js dist/map2tree.js && NODE_ENV=production webpack src/index.js dist/map2tree.min.js",
"lint": "eslint src test",
"test": "babel-node test/map2tree.js | tap-diff",
"test": "jest",
"check": "npm run lint && npm run test",
"prepare": "npm run build && npm run build:umd",
"prepublishOnly": "npm run check && npm run clean && npm run build && npm run build:umd"
@ -40,10 +40,8 @@
"babel-preset-stage-0": "^6.3.13",
"eslint": "1.10.3",
"immutable": "3.7.6",
"jest": "^23.6.0",
"rimraf": "^2.3.4",
"tap-diff": "0.1.1",
"tap-spec": "4.1.1",
"tape": "4.4.0",
"webpack": "1.12.13"
},
"dependencies": {

View File

@ -1,17 +1,15 @@
import test from 'tape';
import map2tree from '../src';
import immutable from 'immutable';
test('# rootNodeKey', assert => {
test('# rootNodeKey', () => {
const map = {};
const options = {key: 'foo'};
assert.equal(map2tree(map, options).name, 'foo');
assert.end();
expect(map2tree(map, options).name).toBe('foo');
});
test('# shallow map', nest => {
nest.test('## null', assert => {
describe('# shallow map', () => {
test('## null', () => {
const map = {
a: null
};
@ -23,12 +21,11 @@ test('# shallow map', nest => {
]
};
assert.deepEqual(map2tree(map), expected);
assert.deepEqual(map2tree(immutable.fromJS(map)), expected, 'immutable');
assert.end();
expect(map2tree(map)).toEqual(expected);
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
});
nest.test('## value', assert => {
test('## value', () => {
const map = {
a: 'foo',
b: 'bar'
@ -42,12 +39,11 @@ test('# shallow map', nest => {
]
};
assert.deepEqual(map2tree(map), expected);
assert.deepEqual(map2tree(immutable.fromJS(map)), expected, 'immutable');
assert.end();
expect(map2tree(map)).toEqual(expected);
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
});
nest.test('## object', assert => {
test('## object', () => {
const map = {
a: {aa: 'foo'}
};
@ -59,12 +55,11 @@ test('# shallow map', nest => {
]
};
assert.deepEqual(map2tree(map), expected);
assert.deepEqual(map2tree(immutable.fromJS(map)), expected, 'immutable');
assert.end();
expect(map2tree(map)).toEqual(expected);
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
});
nest.test('## immutable Map', assert => {
test('## immutable Map', () => {
const map = {
a: immutable.fromJS({aa: 'foo', ab: 'bar'})
};
@ -76,13 +71,12 @@ test('# shallow map', nest => {
]
};
assert.deepEqual(map2tree(map), expected);
assert.end();
expect(map2tree(map)).toEqual(expected);
})
});
test('# deep map', nest => {
nest.test('## null', assert => {
describe('# deep map', () => {
test('## null', () => {
const map = {
a: {aa: null}
};
@ -102,12 +96,11 @@ test('# deep map', nest => {
]
};
assert.deepEqual(map2tree(map), expected);
assert.deepEqual(map2tree(immutable.fromJS(map)), expected, 'immutable');
assert.end();
expect(map2tree(map)).toEqual(expected);
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
});
nest.test('## object', assert => {
test('## object', () => {
const map = {
a: {aa: {aaa: 'foo'}}
};
@ -129,13 +122,12 @@ test('# deep map', nest => {
]
};
assert.deepEqual(map2tree(map), expected);
assert.deepEqual(map2tree(immutable.fromJS(map)), expected, 'immutable');
assert.end();
expect(map2tree(map)).toEqual(expected);
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
});
});
test('# array map', nest => {
describe('# array map', () => {
const map = {
a: [
1,
@ -143,7 +135,7 @@ test('# array map', nest => {
]
};
nest.test('## push', assert => {
test('## push', () => {
const expected = {
name: 'state',
children: [{
@ -154,12 +146,11 @@ test('# array map', nest => {
}]
};
assert.deepEqual(map2tree(map), expected);
assert.deepEqual(map2tree(immutable.fromJS(map)), expected, 'immutable');
assert.end();
expect(map2tree(map)).toEqual(expected);
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
});
nest.test('## unshift', assert => {
test('## unshift', () => {
const options = {pushMethod: 'unshift'};
const expected = {
name: 'state',
@ -172,12 +163,11 @@ test('# array map', nest => {
}]
};
assert.deepEqual(map2tree(map, options), expected);
assert.deepEqual(map2tree(immutable.fromJS(map), options), expected, 'immutable');
assert.end();
expect(map2tree(map, options)).toEqual(expected);
expect(map2tree(immutable.fromJS(map), options)).toEqual(expected);
});
nest.test('## null', assert => {
test('## null', () => {
const map = {
a: [
null
@ -194,14 +184,13 @@ test('# array map', nest => {
}]
};
assert.deepEqual(map2tree(map), expected);
assert.deepEqual(map2tree(immutable.fromJS(map)), expected, 'immutable');
assert.end();
expect(map2tree(map)).toEqual(expected);
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
})
});
test('# collection map', nest => {
nest.test('## value', assert => {
describe('# collection map', () => {
test('## value', () => {
const map = {
a: [
{aa: 1},
@ -222,12 +211,11 @@ test('# collection map', nest => {
]
};
assert.deepEqual(map2tree(map), expected);
assert.deepEqual(map2tree(immutable.fromJS(map)), expected, 'immutable');
assert.end();
expect(map2tree(map)).toEqual(expected);
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
});
nest.test('## object', assert => {
test('## object', () => {
const map = {
a: [
{aa: {aaa: 'foo'}}
@ -246,8 +234,7 @@ test('# collection map', nest => {
]
};
assert.deepEqual(map2tree(map), expected);
assert.deepEqual(map2tree(immutable.fromJS(map)), expected, 'immutable');
assert.end();
expect(map2tree(map)).toEqual(expected);
expect(map2tree(immutable.fromJS(map))).toEqual(expected);
})
});

View File

@ -8,7 +8,7 @@
],
"env": {
"browser": true,
"mocha": true,
"jest": true,
"node": true
},
"rules": {

View File

@ -9,14 +9,10 @@
"build:umd": "rimraf ./umd && webpack --progress --config webpack.config.umd.js",
"build:umd:min": "webpack --env.minimize --progress --config webpack.config.umd.js",
"lint": "eslint --max-warnings=0 src test examples/src",
"test":
"npm run lint && NODE_ENV=test mocha --compilers js:babel-core/register --recursive",
"test:watch":
"NODE_ENV=test mocha --compilers js:babel-core/register --recursive --watch",
"test:cov":
"babel-node ./node_modules/.bin/isparta cover ./node_modules/.bin/_mocha -- --recursive",
"test": "jest",
"prepare": "npm run build",
"prepublishOnly": "npm run test && npm run clean && npm run build && npm run build:umd && npm run build:umd:min",
"prepublishOnly":
"npm run lint && npm run test && npm run clean && npm run build && npm run build:umd && npm run build:umd:min",
"start": "cd examples && npm start",
"precommit": "lint-staged"
},
@ -64,11 +60,9 @@
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-react": "7.4.0",
"eslint-plugin-standard": "^3.0.1",
"expect": "^21.2.1",
"husky": "^0.14.3",
"isparta": "^4.0.0",
"jest": "^23.6.0",
"lint-staged": "^4.3.0",
"mocha": "^4.0.1",
"prettier": "^1.7.4",
"react": "^16.0.0",
"react-dom": "^16.0.0",

View File

@ -1,5 +1,4 @@
import React from 'react';
import expect from 'expect';
import { createRenderer } from 'react-test-renderer/shallow';
import JSONTree from '../src/index';

View File

@ -1,5 +1,3 @@
import expect from 'expect';
import objType from '../src/objType';
describe('objType', () => {

View File

@ -16,8 +16,7 @@
"scripts": {
"start": "node ./bin/redux-devtools.js",
"start:electron": "node ./bin/redux-devtools.js --open",
"test": "NODE_ENV=test mocha --recursive",
"test:watch": "NODE_ENV=test mocha --recursive --watch",
"test": "jest",
"prepublishOnly": "npm run test"
},
"repository": {
@ -58,8 +57,7 @@
"uuid": "^3.0.1"
},
"devDependencies": {
"expect": "^1.20.2",
"mocha": "^3.2.0",
"jest": "^23.6.0",
"socketcluster-client": "^14.0.0",
"supertest": "^3.0.0"
}

View File

@ -12,7 +12,7 @@ function serveUmdModule(name) {
app.use(express.static(require.resolve(name).match(/.*\/(node_modules|packages)\/[^/]+\//)[0] + 'umd'));
}
function routes(options, store) {
function routes(options, store, scServer) {
var limit = options.maxRequestBody;
var logHTTPRequests = options.logHTTPRequests;

View File

@ -13,7 +13,7 @@ class Worker extends SCWorker {
httpServer.on('request', app);
app.use(routes(options, store));
app.use(routes(options, store, scServer));
scServer.addMiddleware(scServer.MIDDLEWARE_EMIT, function (req, next) {
var channel = req.event;

View File

@ -1,31 +1,30 @@
var childProcess = require('child_process');
var request = require('supertest');
var expect = require('expect');
var scClient = require('socketcluster-client');
describe('Server', function() {
var scServer;
this.timeout(5000);
before(function(done) {
beforeAll(function(done) {
scServer = childProcess.fork(__dirname + '/../bin/redux-devtools.js');
setTimeout(done, 2000);
});
after(function() {
afterAll(function() {
if (scServer) {
scServer.kill();
}
});
describe('Express backend', function() {
it('loads main page', function() {
it('loads main page', function(done) {
request('http://localhost:8000')
.get('/')
.expect('Content-Type', /text\/html/)
.expect(200)
.then(function(res) {
expect(res.text).toMatch(/<title>Redux DevTools<\/title>/);
})
done();
});
});
it('resolves an inexistent url', function(done) {
@ -38,7 +37,7 @@ describe('Server', function() {
describe('Realtime monitoring', function() {
var socket, socket2, channel;
before(function() {
beforeAll(function() {
socket = scClient.connect({ hostname: 'localhost', port: 8000 });
socket.connect();
socket.on('error', function(error) {
@ -51,14 +50,14 @@ describe('Server', function() {
});
});
after(function() {
afterAll(function() {
socket.disconnect();
socket2.disconnect();
});
it('should connect', function(done) {
socket.on('connect', function(status) {
expect(status.id).toExist();
expect(status.id).toBeTruthy();
done();
});
});
@ -117,7 +116,7 @@ describe('Server', function() {
preloadedState: '{"todos":[{"text":"Use Redux","completed":false,"id":0}]}',
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36'
};
it('should add a report', function() {
it('should add a report', function(done) {
request('http://localhost:8000')
.post('/')
.send(report)
@ -126,11 +125,12 @@ describe('Server', function() {
.expect(200)
.then(function(res) {
id = res.body.id;
expect(id).toExist();
expect(id).toBeTruthy();
done();
});
});
it('should get the report', function() {
it('should get the report', function(done) {
request('http://localhost:8000')
.post('/')
.send({
@ -141,11 +141,12 @@ describe('Server', function() {
.expect('Content-Type', /application\/json/)
.expect(200)
.then(function(res) {
expect(res.body).toInclude(report);
expect.objectContaining(res.body, report);
done();
});
});
it('should list reports', function() {
it('should list reports', function(done) {
request('http://localhost:8000')
.post('/')
.send({
@ -158,13 +159,14 @@ describe('Server', function() {
expect(res.body.length).toBe(1);
expect(res.body[0].id).toBe(id);
expect(res.body[0].title).toBe('Test report');
expect(res.body[0].added).toExist();
expect(res.body[0].added).toBeTruthy();
done();
});
});
});
describe('GraphQL backend', function() {
it('should get the report', function() {
it('should get the report', function(done) {
request('http://localhost:8000')
.post('/graphql')
.send({
@ -176,9 +178,10 @@ describe('Server', function() {
.then(function(res) {
var reports = res.body.data.reports;
expect(reports.length).toBe(1);
expect(reports[0].id).toExist();
expect(reports[0].id).toBeTruthy();
expect(reports[0].title).toBe('Test report');
expect(reports[0].type).toBe('ACTIONS');
done();
});
});
});

View File

@ -11,7 +11,7 @@
"clean": "rimraf lib",
"lint": "eslint src test",
"lint:fix": "eslint src --fix",
"test": "NODE_ENV=test jest --no-cache",
"test": "jest --no-cache",
"prepare": "npm run build && npm run build:umd && npm run build:umd:min",
"prepublishOnly": "eslint ./src/app && npm run test && npm run build && npm run build:umd && npm run build:umd:min"
},
@ -63,7 +63,7 @@
"file-loader": "^3.0.0",
"html-loader": "^0.4.4",
"html-webpack-plugin": "^3.2.0",
"jest": "^21.2.1",
"jest": "^23.6.0",
"raw-loader": "^1.0.0",
"react": "^16.0.0",
"react-dom": "^16.0.0",

View File

@ -2,7 +2,7 @@
"extends": "eslint-config-airbnb",
"env": {
"browser": true,
"mocha": true,
"jest": true,
"node": true
},
"rules": {

View File

@ -7,9 +7,7 @@
"clean": "rimraf lib",
"build": "babel src --out-dir lib",
"lint": "eslint src test",
"test": "NODE_ENV=test mocha --compilers js:babel-core/register --recursive",
"test:watch": "NODE_ENV=test mocha --compilers js:babel-core/register --recursive --watch",
"test:cov": "babel-node ./node_modules/.bin/isparta cover ./node_modules/.bin/_mocha -- --recursive",
"test": "jest",
"prepare": "npm run build",
"prepublishOnly": "npm run lint && npm run test && npm run clean && npm run build"
},
@ -46,8 +44,7 @@
"eslint-config-airbnb": "0.0.6",
"eslint-plugin-react": "^2.3.0",
"expect": "^1.6.0",
"isparta": "^3.0.3",
"mocha": "^2.2.5",
"jest": "^23.6.0",
"redux": "^4.0.0",
"rimraf": "^2.3.4",
"rxjs": "^5.0.0-beta.6",

View File

@ -1,4 +1,3 @@
import expect, { spyOn } from 'expect';
import { createStore, compose } from 'redux';
import instrument, { ActionCreators } from '../src/instrument';
import { Observable } from 'rxjs';
@ -280,7 +279,7 @@ describe('instrument', () => {
});
it('should catch and record errors', () => {
let spy = spyOn(console, 'error');
let spy = jest.spyOn(console, 'error').mockImplementation(() => {});;
let storeWithBug = createStore(
counterWithBug,
instrument(undefined, { shouldCatchErrors: true })
@ -297,11 +296,11 @@ describe('instrument', () => {
expect(computedStates[3].error).toMatch(
/Interrupted by an error up the chain/
);
expect(spy.calls[0].arguments[0].toString()).toMatch(
expect(spy.mock.calls[0][0].toString()).toMatch(
/ReferenceError/
);
spy.restore();
spy.mockReset();
});
it('should catch invalid action type', () => {
@ -453,7 +452,7 @@ describe('instrument', () => {
expect(configuredStore.getState()).toBe(2);
expect(Object.keys(liftedStoreState.actionsById).length).toBe(3);
expect(liftedStoreState.committedState).toBe(undefined);
expect(liftedStoreState.stagedActionIds).toInclude(1);
expect(liftedStoreState.stagedActionIds).toContain(1);
// Trigger auto-commit.
configuredStore.dispatch({ type: 'INCREMENT' });
@ -461,7 +460,7 @@ describe('instrument', () => {
expect(configuredStore.getState()).toBe(3);
expect(Object.keys(liftedStoreState.actionsById).length).toBe(3);
expect(liftedStoreState.stagedActionIds).toExclude(1);
expect(liftedStoreState.stagedActionIds).not.toContain(1);
expect(liftedStoreState.computedStates[0].state).toBe(1);
expect(liftedStoreState.committedState).toBe(1);
expect(liftedStoreState.currentStateIndex).toBe(2);
@ -471,13 +470,13 @@ describe('instrument', () => {
configuredStore.dispatch({ type: 'INCREMENT' });
configuredLiftedStore.dispatch(ActionCreators.toggleAction(1));
configuredStore.dispatch({ type: 'INCREMENT' });
expect(configuredLiftedStore.getState().skippedActionIds).toInclude(1);
expect(configuredLiftedStore.getState().skippedActionIds).toContain(1);
configuredStore.dispatch({ type: 'INCREMENT' });
expect(configuredLiftedStore.getState().skippedActionIds).toExclude(1);
expect(configuredLiftedStore.getState().skippedActionIds).not.toContain(1);
});
it('should not auto-commit errors', () => {
let spy = spyOn(console, 'error');
let spy = jest.spyOn(console, 'error');
let storeWithBug = createStore(
counterWithBug,
@ -491,11 +490,11 @@ describe('instrument', () => {
storeWithBug.dispatch({ type: 'INCREMENT' });
expect(liftedStoreWithBug.getState().stagedActionIds.length).toBe(4);
spy.restore();
spy.mockReset();
});
it('should auto-commit actions after hot reload fixes error', () => {
let spy = spyOn(console, 'error');
let spy = jest.spyOn(console, 'error');
let storeWithBug = createStore(
counterWithBug,
@ -518,7 +517,7 @@ describe('instrument', () => {
storeWithBug.replaceReducer(counter);
expect(liftedStoreWithBug.getState().stagedActionIds.length).toBe(3);
spy.restore();
spy.mockReset();
});
it('should update currentStateIndex when auto-committing', () => {
@ -539,7 +538,7 @@ describe('instrument', () => {
});
it('should continue to increment currentStateIndex while error blocks commit', () => {
let spy = spyOn(console, 'error');
let spy = jest.spyOn(console, 'error');
let storeWithBug = createStore(
counterWithBug,
@ -556,13 +555,13 @@ describe('instrument', () => {
let currentComputedState = liftedStoreState.computedStates[liftedStoreState.currentStateIndex];
expect(liftedStoreState.currentStateIndex).toBe(4);
expect(currentComputedState.state).toBe(0);
expect(currentComputedState.error).toExist();
expect(currentComputedState.error).toBeTruthy();
spy.restore();
spy.mockReset();
});
it('should adjust currentStateIndex correctly when multiple actions are committed', () => {
let spy = spyOn(console, 'error');
let spy = jest.spyOn(console, 'error');
let storeWithBug = createStore(
counterWithBug,
@ -582,11 +581,11 @@ describe('instrument', () => {
expect(liftedStoreState.currentStateIndex).toBe(2);
expect(currentComputedState.state).toBe(-4);
spy.restore();
spy.mockReset();
});
it('should not allow currentStateIndex to drop below 0', () => {
let spy = spyOn(console, 'error');
let spy = jest.spyOn(console, 'error');
let storeWithBug = createStore(
counterWithBug,
@ -607,33 +606,33 @@ describe('instrument', () => {
expect(liftedStoreState.currentStateIndex).toBe(0);
expect(currentComputedState.state).toBe(-2);
spy.restore();
spy.mockReset();
});
it('should use dynamic maxAge', () => {
let max = 3;
const getMaxAge = expect.createSpy().andCall(() => max);
const getMaxAge = jest.fn().mockImplementation(() => max);
store = createStore(counter, instrument(undefined, { maxAge: getMaxAge }));
expect(getMaxAge.calls.length).toEqual(1);
expect(getMaxAge.mock.calls.length).toEqual(1);
store.dispatch({ type: 'INCREMENT' });
expect(getMaxAge.calls.length).toEqual(2);
expect(getMaxAge.mock.calls.length).toEqual(2);
store.dispatch({ type: 'INCREMENT' });
expect(getMaxAge.calls.length).toEqual(3);
expect(getMaxAge.mock.calls.length).toEqual(3);
let liftedStoreState = store.liftedStore.getState();
expect(getMaxAge.calls[0].arguments[0].type).toInclude('INIT');
expect(getMaxAge.calls[0].arguments[1]).toBe(undefined);
expect(getMaxAge.calls[1].arguments[0].type).toBe('PERFORM_ACTION');
expect(getMaxAge.calls[1].arguments[1].nextActionId).toBe(1);
expect(getMaxAge.calls[1].arguments[1].stagedActionIds).toEqual([0]);
expect(getMaxAge.calls[2].arguments[1].nextActionId).toBe(2);
expect(getMaxAge.calls[2].arguments[1].stagedActionIds).toEqual([0, 1]);
expect(getMaxAge.mock.calls[0][0].type).toContain('INIT');
expect(getMaxAge.mock.calls[0][1]).toBe(undefined);
expect(getMaxAge.mock.calls[1][0].type).toBe('PERFORM_ACTION');
expect(getMaxAge.mock.calls[1][1].nextActionId).toBe(1);
expect(getMaxAge.mock.calls[1][1].stagedActionIds).toEqual([0]);
expect(getMaxAge.mock.calls[2][1].nextActionId).toBe(2);
expect(getMaxAge.mock.calls[2][1].stagedActionIds).toEqual([0, 1]);
expect(store.getState()).toBe(2);
expect(Object.keys(liftedStoreState.actionsById).length).toBe(3);
expect(liftedStoreState.committedState).toBe(undefined);
expect(liftedStoreState.stagedActionIds).toInclude(1);
expect(liftedStoreState.stagedActionIds).toContain(1);
// Trigger auto-commit.
store.dispatch({ type: 'INCREMENT' });
@ -641,7 +640,7 @@ describe('instrument', () => {
expect(store.getState()).toBe(3);
expect(Object.keys(liftedStoreState.actionsById).length).toBe(3);
expect(liftedStoreState.stagedActionIds).toExclude(1);
expect(liftedStoreState.stagedActionIds).not.toContain(1);
expect(liftedStoreState.computedStates[0].state).toBe(1);
expect(liftedStoreState.committedState).toBe(1);
expect(liftedStoreState.currentStateIndex).toBe(2);
@ -652,7 +651,7 @@ describe('instrument', () => {
expect(store.getState()).toBe(4);
expect(Object.keys(liftedStoreState.actionsById).length).toBe(4);
expect(liftedStoreState.stagedActionIds).toExclude(1);
expect(liftedStoreState.stagedActionIds).not.toContain(1);
expect(liftedStoreState.computedStates[0].state).toBe(1);
expect(liftedStoreState.committedState).toBe(1);
expect(liftedStoreState.currentStateIndex).toBe(3);
@ -663,7 +662,7 @@ describe('instrument', () => {
expect(store.getState()).toBe(5);
expect(Object.keys(liftedStoreState.actionsById).length).toBe(3);
expect(liftedStoreState.stagedActionIds).toExclude(1);
expect(liftedStoreState.stagedActionIds).not.toContain(1);
expect(liftedStoreState.computedStates[0].state).toBe(3);
expect(liftedStoreState.committedState).toBe(3);
expect(liftedStoreState.currentStateIndex).toBe(2);
@ -673,7 +672,7 @@ describe('instrument', () => {
expect(store.getState()).toBe(6);
expect(Object.keys(liftedStoreState.actionsById).length).toBe(3);
expect(liftedStoreState.stagedActionIds).toExclude(1);
expect(liftedStoreState.stagedActionIds).not.toContain(1);
expect(liftedStoreState.computedStates[0].state).toBe(4);
expect(liftedStoreState.committedState).toBe(4);
expect(liftedStoreState.currentStateIndex).toBe(2);
@ -702,18 +701,27 @@ describe('instrument', () => {
});
it('should include stack trace', () => {
monitoredStore = createStore(counter, instrument(undefined, { trace: true }));
monitoredLiftedStore = monitoredStore.liftedStore;
monitoredStore.dispatch({ type: 'INCREMENT' });
function fn1() {
monitoredStore = createStore(counter, instrument(undefined, { trace: true }));
monitoredLiftedStore = monitoredStore.liftedStore;
monitoredStore.dispatch({ type: 'INCREMENT' });
exportedState = monitoredLiftedStore.getState();
expect(exportedState.actionsById[0].stack).toBe(undefined);
expect(exportedState.actionsById[1].stack).toBeA('string');
expect(exportedState.actionsById[1].stack).toMatch(/^Error/);
expect(exportedState.actionsById[1].stack).toNotMatch(/instrument.js/);
expect(exportedState.actionsById[1].stack).toContain('instrument.spec.js');
expect(exportedState.actionsById[1].stack).toContain('/mocha/');
expect(exportedState.actionsById[1].stack.split('\n').length).toBe(10 + 1); // +1 is for `Error\n`
exportedState = monitoredLiftedStore.getState();
expect(exportedState.actionsById[0].stack).toBe(undefined);
expect(typeof exportedState.actionsById[1].stack).toBe('string');
expect(exportedState.actionsById[1].stack).toMatch(/^Error/);
expect(exportedState.actionsById[1].stack).not.toMatch(/instrument.js/);
expect(exportedState.actionsById[1].stack).toMatch(/\bfn1\b/);
expect(exportedState.actionsById[1].stack).toMatch(/\bfn2\b/);
expect(exportedState.actionsById[1].stack).toMatch(/\bfn3\b/);
expect(exportedState.actionsById[1].stack).toMatch(/\bfn4\b/);
expect(exportedState.actionsById[1].stack).toContain('instrument.spec.js');
expect(exportedState.actionsById[1].stack.split('\n').length).toBe(10 + 1); // +1 is for `Error\n`
}
function fn2() { return fn1(); }
function fn3() { return fn2(); }
function fn4() { return fn3(); }
fn4();
});
it('should include only 3 frames for stack trace', () => {
@ -724,11 +732,12 @@ describe('instrument', () => {
exportedState = monitoredLiftedStore.getState();
expect(exportedState.actionsById[0].stack).toBe(undefined);
expect(exportedState.actionsById[1].stack).toBeA('string');
expect(exportedState.actionsById[1].stack).toMatch(/at fn1 /);
expect(exportedState.actionsById[1].stack).toMatch(/at fn2 /);
expect(exportedState.actionsById[1].stack).toMatch(/at fn3 /);
expect(exportedState.actionsById[1].stack).toNotMatch(/at fn4 /);
expect(typeof exportedState.actionsById[1].stack).toBe('string');
expect(exportedState.actionsById[1].stack).toMatch(/\bat dispatch\b/);
expect(exportedState.actionsById[1].stack).toMatch(/\bfn1\b/);
expect(exportedState.actionsById[1].stack).toMatch(/\bfn2\b/);
expect(exportedState.actionsById[1].stack).not.toMatch(/\bfn3\b/);
expect(exportedState.actionsById[1].stack).not.toMatch(/\bfn4\b/);
expect(exportedState.actionsById[1].stack).toContain('instrument.spec.js');
expect(exportedState.actionsById[1].stack.split('\n').length).toBe(3 + 1);
}
@ -748,11 +757,12 @@ describe('instrument', () => {
exportedState = monitoredLiftedStore.getState();
expect(exportedState.actionsById[0].stack).toBe(undefined);
expect(exportedState.actionsById[1].stack).toBeA('string');
expect(exportedState.actionsById[1].stack).toMatch(/at fn1 /);
expect(exportedState.actionsById[1].stack).toMatch(/at fn2 /);
expect(exportedState.actionsById[1].stack).toMatch(/at fn3 /);
expect(exportedState.actionsById[1].stack).toNotMatch(/at fn4 /);
expect(typeof exportedState.actionsById[1].stack).toBe('string');
expect(exportedState.actionsById[1].stack).toMatch(/\bat dispatch\b/);
expect(exportedState.actionsById[1].stack).toMatch(/\bfn1\b/);
expect(exportedState.actionsById[1].stack).toMatch(/\bfn2\b/);
expect(exportedState.actionsById[1].stack).not.toMatch(/\bfn3\b/);
expect(exportedState.actionsById[1].stack).not.toMatch(/\bfn4\b/);
expect(exportedState.actionsById[1].stack).toContain('instrument.spec.js');
expect(exportedState.actionsById[1].stack.split('\n').length).toBe(3 + 1);
}
@ -773,10 +783,9 @@ describe('instrument', () => {
exportedState = monitoredLiftedStore.getState();
expect(exportedState.actionsById[0].stack).toBe(undefined);
expect(exportedState.actionsById[1].stack).toBeA('string');
expect(typeof exportedState.actionsById[1].stack).toBe('string');
expect(exportedState.actionsById[1].stack).toMatch(/^Error/);
expect(exportedState.actionsById[1].stack).toContain('instrument.spec.js');
expect(exportedState.actionsById[1].stack).toContain('/mocha/');
expect(exportedState.actionsById[1].stack.split('\n').length).toBe(5 + 1);
});
@ -791,11 +800,11 @@ describe('instrument', () => {
exportedState = monitoredLiftedStore.getState();
expect(exportedState.actionsById[0].stack).toBe(undefined);
expect(exportedState.actionsById[1].stack).toBeA('string');
expect(exportedState.actionsById[1].stack).toMatch(/at fn1 /);
expect(exportedState.actionsById[1].stack).toMatch(/at fn2 /);
expect(exportedState.actionsById[1].stack).toMatch(/at fn3 /);
expect(exportedState.actionsById[1].stack).toMatch(/at fn4 /);
expect(typeof exportedState.actionsById[1].stack).toBe('string');
expect(exportedState.actionsById[1].stack).toMatch(/\bfn1\b/);
expect(exportedState.actionsById[1].stack).toMatch(/\bfn2\b/);
expect(exportedState.actionsById[1].stack).toMatch(/\bfn3\b/);
expect(exportedState.actionsById[1].stack).toMatch(/\bfn4\b/);
expect(exportedState.actionsById[1].stack).toContain('instrument.spec.js');
expect(exportedState.actionsById[1].stack.split('\n').length).toBe(10 + 1);
}
@ -815,11 +824,10 @@ describe('instrument', () => {
exportedState = monitoredLiftedStore.getState();
expect(exportedState.actionsById[0].stack).toBe(undefined);
expect(exportedState.actionsById[1].stack).toBeA('string');
expect(typeof exportedState.actionsById[1].stack).toBe('string');
expect(exportedState.actionsById[1].stack).toMatch(/^Error/);
expect(exportedState.actionsById[1].stack).toContain('instrument.js');
expect(exportedState.actionsById[1].stack).toContain('instrument.spec.js');
expect(exportedState.actionsById[1].stack).toContain('/mocha/');
expect(exportedState.actionsById[1].stack.split('\n').length).toBe(5 + 3 + 1);
});
@ -831,11 +839,10 @@ describe('instrument', () => {
exportedState = monitoredLiftedStore.getState();
expect(exportedState.actionsById[0].stack).toBe(undefined);
expect(exportedState.actionsById[1].stack).toBeA('string');
expect(exportedState.actionsById[1].stack).toContain('at Object.performAction');
expect(typeof exportedState.actionsById[1].stack).toBe('string');
expect(exportedState.actionsById[1].stack).toContain('at performAction');
expect(exportedState.actionsById[1].stack).toContain('instrument.js');
expect(exportedState.actionsById[1].stack).toContain('instrument.spec.js');
expect(exportedState.actionsById[1].stack).toContain('/mocha/');
});
it('should get stack trace inside setTimeout using a function', (done) => {
@ -848,11 +855,10 @@ describe('instrument', () => {
exportedState = monitoredLiftedStore.getState();
expect(exportedState.actionsById[0].stack).toBe(undefined);
expect(exportedState.actionsById[1].stack).toBeA('string');
expect(exportedState.actionsById[1].stack).toContain('at Object.performAction');
expect(typeof exportedState.actionsById[1].stack).toBe('string');
expect(exportedState.actionsById[1].stack).toContain('at performAction');
expect(exportedState.actionsById[1].stack).toContain('instrument.js');
expect(exportedState.actionsById[1].stack).toContain('instrument.spec.js');
expect(exportedState.actionsById[1].stack).toContain('/mocha/');
done();
});
});
@ -917,7 +923,7 @@ describe('instrument', () => {
const oldState = importMonitoredLiftedStore.getState();
expect(oldState.actionsById[0].stack).toBe(undefined);
expect(oldState.actionsById[1].stack).toBeA('string');
expect(typeof oldState.actionsById[1].stack).toBe('string');
importMonitoredLiftedStore.dispatch(ActionCreators.importState(oldState));
expect(importMonitoredLiftedStore.getState()).toEqual(oldState);
@ -982,7 +988,7 @@ describe('instrument', () => {
importMonitoredLiftedStore.dispatch(ActionCreators.importState(savedActions));
expect(importMonitoredLiftedStore.getState().actionsById[0].stack).toBe(undefined);
expect(importMonitoredLiftedStore.getState().actionsById[1].stack).toBeA('string');
expect(typeof importMonitoredLiftedStore.getState().actionsById[1].stack).toBe('string');
expect(filterStackAndTimestamps(importMonitoredLiftedStore.getState())).toEqual(exportedState);
});
});

View File

@ -7,9 +7,7 @@
"clean": "rimraf lib",
"build": "babel src --out-dir lib",
"lint": "eslint src test examples",
"test": "cross-env NODE_ENV=test mocha --compilers js:babel-core/register --recursive",
"test:watch": "cross-env NODE_ENV=test mocha --compilers js:babel-core/register --recursive --watch",
"test:cov": "babel-node ./node_modules/.bin/isparta cover ./node_modules/.bin/_mocha -- --recursive",
"test": "jest",
"prepare": "npm run build",
"prepublishOnly": "npm run lint && npm run test && npm run clean && npm run build"
},
@ -43,15 +41,10 @@
"babel-preset-es2015-loose": "^6.1.3",
"babel-preset-react": "6.3.13",
"babel-preset-stage-0": "^6.3.13",
"cross-env": "3.1.3",
"eslint": "^0.23",
"eslint-config-airbnb": "0.0.6",
"eslint-plugin-react": "^2.3.0",
"expect": "^1.6.0",
"isparta": "^3.0.3",
"jsdom": "^6.5.1",
"mocha": "^2.2.5",
"mocha-jsdom": "^1.0.0",
"jest": "^23.6.0",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"react-redux": "^6.0.0",

View File

@ -1,9 +1,9 @@
import expect from 'expect';
import { instrument, persistState } from '../src';
import { compose, createStore } from 'redux';
describe('persistState', () => {
let savedLocalStorage = global.localStorage;
delete global.localStorage;
beforeEach(() => {
global.localStorage = {
@ -23,7 +23,7 @@ describe('persistState', () => {
};
});
after(() => {
afterAll(() => {
global.localStorage = savedLocalStorage;
});
@ -89,29 +89,23 @@ describe('persistState', () => {
});
it('should warn if read from localStorage fails', () => {
const spy = expect.spyOn(console, 'warn');
const spy = jest.spyOn(console, 'warn').mockImplementation(() => {});
delete global.localStorage.getItem;
createStore(reducer, compose(instrument(), persistState('id')));
expect(spy.calls).toContain(
/Could not read debug session from localStorage/,
(call, errMsg) => call.arguments[0].match(errMsg)
);
expect(spy.mock.calls[0]).toContain('Could not read debug session from localStorage:');
spy.restore();
spy.mockReset();
});
it('should warn if write to localStorage fails', () => {
const spy = expect.spyOn(console, 'warn');
const spy = jest.spyOn(console, 'warn').mockImplementation(() => {});
delete global.localStorage.setItem;
const store = createStore(reducer, compose(instrument(), persistState('id')));
store.dispatch({ type: 'INCREMENT' });
expect(spy.calls).toContain(
/Could not write debug session to localStorage/,
(call, errMsg) => call.arguments[0].match(errMsg)
);
expect(spy.mock.calls[0]).toContain('Could not write debug session to localStorage:');
spy.restore();
spy.mockReset();
});
});