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", "eslint-plugin-react": "^4.3.0",
"file-loader": "^1.1.5", "file-loader": "^1.1.5",
"git-url-parse": "^7.0.1", "git-url-parse": "^7.0.1",
"jest": "^21.2.1", "jest": "^23.6.0",
"jsdom": "^11.3.0", "jsdom": "^11.3.0",
"node-sass": "^3.13.0", "node-sass": "^3.13.0",
"postcss-loader": "^2.0.8", "postcss-loader": "^2.0.8",

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -9,14 +9,10 @@
"build:umd": "rimraf ./umd && webpack --progress --config webpack.config.umd.js", "build:umd": "rimraf ./umd && webpack --progress --config webpack.config.umd.js",
"build:umd:min": "webpack --env.minimize --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", "lint": "eslint --max-warnings=0 src test examples/src",
"test": "test": "jest",
"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",
"prepare": "npm run build", "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", "start": "cd examples && npm start",
"precommit": "lint-staged" "precommit": "lint-staged"
}, },
@ -64,11 +60,9 @@
"eslint-plugin-promise": "^3.6.0", "eslint-plugin-promise": "^3.6.0",
"eslint-plugin-react": "7.4.0", "eslint-plugin-react": "7.4.0",
"eslint-plugin-standard": "^3.0.1", "eslint-plugin-standard": "^3.0.1",
"expect": "^21.2.1",
"husky": "^0.14.3", "husky": "^0.14.3",
"isparta": "^4.0.0", "jest": "^23.6.0",
"lint-staged": "^4.3.0", "lint-staged": "^4.3.0",
"mocha": "^4.0.1",
"prettier": "^1.7.4", "prettier": "^1.7.4",
"react": "^16.0.0", "react": "^16.0.0",
"react-dom": "^16.0.0", "react-dom": "^16.0.0",

View File

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

View File

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

View File

@ -16,8 +16,7 @@
"scripts": { "scripts": {
"start": "node ./bin/redux-devtools.js", "start": "node ./bin/redux-devtools.js",
"start:electron": "node ./bin/redux-devtools.js --open", "start:electron": "node ./bin/redux-devtools.js --open",
"test": "NODE_ENV=test mocha --recursive", "test": "jest",
"test:watch": "NODE_ENV=test mocha --recursive --watch",
"prepublishOnly": "npm run test" "prepublishOnly": "npm run test"
}, },
"repository": { "repository": {
@ -58,8 +57,7 @@
"uuid": "^3.0.1" "uuid": "^3.0.1"
}, },
"devDependencies": { "devDependencies": {
"expect": "^1.20.2", "jest": "^23.6.0",
"mocha": "^3.2.0",
"socketcluster-client": "^14.0.0", "socketcluster-client": "^14.0.0",
"supertest": "^3.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')); 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 limit = options.maxRequestBody;
var logHTTPRequests = options.logHTTPRequests; var logHTTPRequests = options.logHTTPRequests;

View File

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

View File

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

View File

@ -11,7 +11,7 @@
"clean": "rimraf lib", "clean": "rimraf lib",
"lint": "eslint src test", "lint": "eslint src test",
"lint:fix": "eslint src --fix", "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", "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" "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", "file-loader": "^3.0.0",
"html-loader": "^0.4.4", "html-loader": "^0.4.4",
"html-webpack-plugin": "^3.2.0", "html-webpack-plugin": "^3.2.0",
"jest": "^21.2.1", "jest": "^23.6.0",
"raw-loader": "^1.0.0", "raw-loader": "^1.0.0",
"react": "^16.0.0", "react": "^16.0.0",
"react-dom": "^16.0.0", "react-dom": "^16.0.0",

View File

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

View File

@ -7,9 +7,7 @@
"clean": "rimraf lib", "clean": "rimraf lib",
"build": "babel src --out-dir lib", "build": "babel src --out-dir lib",
"lint": "eslint src test", "lint": "eslint src test",
"test": "NODE_ENV=test mocha --compilers js:babel-core/register --recursive", "test": "jest",
"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",
"prepare": "npm run build", "prepare": "npm run build",
"prepublishOnly": "npm run lint && npm run test && npm run clean && 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-config-airbnb": "0.0.6",
"eslint-plugin-react": "^2.3.0", "eslint-plugin-react": "^2.3.0",
"expect": "^1.6.0", "expect": "^1.6.0",
"isparta": "^3.0.3", "jest": "^23.6.0",
"mocha": "^2.2.5",
"redux": "^4.0.0", "redux": "^4.0.0",
"rimraf": "^2.3.4", "rimraf": "^2.3.4",
"rxjs": "^5.0.0-beta.6", "rxjs": "^5.0.0-beta.6",

View File

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

View File

@ -7,9 +7,7 @@
"clean": "rimraf lib", "clean": "rimraf lib",
"build": "babel src --out-dir lib", "build": "babel src --out-dir lib",
"lint": "eslint src test examples", "lint": "eslint src test examples",
"test": "cross-env NODE_ENV=test mocha --compilers js:babel-core/register --recursive", "test": "jest",
"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",
"prepare": "npm run build", "prepare": "npm run build",
"prepublishOnly": "npm run lint && npm run test && npm run clean && 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-es2015-loose": "^6.1.3",
"babel-preset-react": "6.3.13", "babel-preset-react": "6.3.13",
"babel-preset-stage-0": "^6.3.13", "babel-preset-stage-0": "^6.3.13",
"cross-env": "3.1.3",
"eslint": "^0.23", "eslint": "^0.23",
"eslint-config-airbnb": "0.0.6", "eslint-config-airbnb": "0.0.6",
"eslint-plugin-react": "^2.3.0", "eslint-plugin-react": "^2.3.0",
"expect": "^1.6.0", "jest": "^23.6.0",
"isparta": "^3.0.3",
"jsdom": "^6.5.1",
"mocha": "^2.2.5",
"mocha-jsdom": "^1.0.0",
"react": "^16.0.0", "react": "^16.0.0",
"react-dom": "^16.0.0", "react-dom": "^16.0.0",
"react-redux": "^6.0.0", "react-redux": "^6.0.0",

View File

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