Skip to content

Commit 1125b4a

Browse files
committed
chore: switch from Mocha to Jest, use TypeScript for tests
Move tests to "src". Add TypeScript configuration file.
1 parent e721e17 commit 1125b4a

File tree

9 files changed

+213
-160
lines changed

9 files changed

+213
-160
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
.DS_Store
2+
.vscode
23

34
node_modules
45
npm-debug.log
56

6-
.nyc_output
7-
coverage
7+
.coverage
88
*.lcov
99

1010
example/build

jest/config.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"collectCoverageFrom": [
3+
"!**/*.d.ts",
4+
"!**/node_modules/**",
5+
"src/*.{ts,js}"
6+
],
7+
"coverageDirectory": "<rootDir>/.coverage",
8+
"coverageReporters": [
9+
"lcov"
10+
],
11+
"moduleFileExtensions": [
12+
"js",
13+
"ts"
14+
],
15+
"modulePaths": [
16+
"<rootDir>/src"
17+
],
18+
"resetModules": true,
19+
"rootDir": "../",
20+
"testRegex": "/src/(.+\\.)?spec\\.ts$",
21+
"transform": {
22+
"^.+\\.(ts|js)$": "<rootDir>/jest/tsTransform.js"
23+
},
24+
"verbose": true
25+
}

jest/tsTransform.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const tsc = require('typescript');
2+
3+
const tsconfig = require('../tsconfig.json');
4+
5+
const compilerOptions = Object.assign({}, tsconfig.compilerOptions, {
6+
module: 'commonjs',
7+
});
8+
9+
module.exports = {
10+
process(source, path) {
11+
return (
12+
path.endsWith('.ts') || path.endsWith('.js')
13+
? tsc.transpile(source, compilerOptions, path, [])
14+
: source
15+
);
16+
},
17+
};

package.json

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
"main": "dist/redux-logger.js",
66
"scripts": {
77
"lint": "eslint src",
8-
"test": "npm run lint && npm run spec",
9-
"spec": "nyc --all --silent --require babel-core/register mocha --plugins transform-inline-environment-variables --recursive spec/*.spec.js",
10-
"spec:watch": "npm run spec -- --watch",
11-
"coverage": "nyc report",
12-
"coverage:html": "nyc report --reporter=html && http-server -p 8077 ./coverage -o",
13-
"coverage:production": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
8+
"test": "jest --config jest/config.json",
9+
"test:coverage": "npm test -- --coverage",
10+
"test:coverage:show": "http-server -o -p 1335 .coverage/lcov-report",
11+
"test:production": "npm test:coverage -- --ci",
12+
"test:watch": "npm test -- --watch",
1413
"clean": "rimraf dist",
1514
"build": "rollup -c",
1615
"precommit": "npm test && npm run lint",
@@ -22,21 +21,9 @@
2221
"no-console": "off"
2322
},
2423
"env": {
25-
"browser": true,
26-
"mocha": true
24+
"browser": true
2725
}
2826
},
29-
"nyc": {
30-
"exclude": [
31-
"node_modules",
32-
"spec",
33-
"example",
34-
"lib",
35-
"dist",
36-
"coverage",
37-
"rollup.config.js"
38-
]
39-
},
4027
"files": [
4128
"dist",
4229
"src"
@@ -58,11 +45,11 @@
5845
},
5946
"homepage": "https://github.com/theaqua/redux-logger#readme",
6047
"devDependencies": {
48+
"@types/jest": "^22.2.0",
6149
"babel-core": "^6.24.0",
6250
"babel-plugin-external-helpers": "^6.22.0",
6351
"babel-plugin-transform-inline-environment-variables": "6.8.0",
6452
"babel-preset-es2015": "^6.24.0",
65-
"chai": "3.5.0",
6653
"codecov": "1.0.1",
6754
"eslint": "^3.19.0",
6855
"eslint-config-airbnb": "^14.1.0",
@@ -71,16 +58,16 @@
7158
"eslint-plugin-react": "^6.10.3",
7259
"http-server": "0.9.0",
7360
"husky": "^0.13.2",
74-
"mocha": "3.1.2",
75-
"nyc": "9.0.1",
61+
"jest": "^22.4.2",
7662
"redux": "^3.6.0",
7763
"rimraf": "^2.6.1",
7864
"rollup": "^0.41.6",
7965
"rollup-plugin-babel": "^2.7.1",
8066
"rollup-plugin-commonjs": "^8.0.2",
8167
"rollup-plugin-node-resolve": "^3.0.0",
8268
"rollup-plugin-uglify": "^1.0.2",
83-
"sinon": "^1.17.7"
69+
"tslib": "^1.9.0",
70+
"typescript": "^2.8.0-rc"
8471
},
8572
"dependencies": {
8673
"deep-diff": "^0.3.5"

spec/diff.spec.js

Lines changed: 0 additions & 115 deletions
This file was deleted.

src/diff.spec.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import diffLogger, { style, render } from './diff';
2+
3+
describe('Diff', () => {
4+
describe('style', () => {
5+
it('return css rules for the given kind of diff changes', () => {
6+
expect(style('E')).toEqual('color: #2196F3; font-weight: bold');
7+
expect(style('N')).toEqual('color: #4CAF50; font-weight: bold');
8+
expect(style('D')).toEqual('color: #F44336; font-weight: bold');
9+
expect(style('A')).toEqual('color: #2196F3; font-weight: bold');
10+
});
11+
});
12+
13+
describe('render', () => {
14+
it('should return an array indicating the changes', () => {
15+
expect(render({
16+
kind: 'E',
17+
path: ['capitain', 'name'],
18+
lhs: 'kirk',
19+
rhs: 'picard',
20+
})).toEqual(['capitain.name', 'kirk', '→', 'picard']);
21+
});
22+
23+
it('should return an array indicating an added property/element', () => {
24+
expect(render({
25+
kind: 'N',
26+
path: ['crew', 'engineer'],
27+
rhs: 'geordi',
28+
})).toEqual(['crew.engineer', 'geordi']);
29+
});
30+
31+
it('should return an array indicating a removed property/element', () => {
32+
expect(render({
33+
kind: 'D',
34+
path: ['crew', 'security'],
35+
})).toEqual(['crew.security']);
36+
});
37+
38+
it('should return an array indicating a changed index', () => {
39+
expect(render({
40+
kind: 'A',
41+
path: ['crew'],
42+
index: 2,
43+
item: {
44+
kind: 'N',
45+
rhs: 'after',
46+
},
47+
})).toEqual(['crew[2]', {
48+
kind: 'N',
49+
rhs: 'after',
50+
}]);
51+
});
52+
53+
it('should return an empty array', () => {
54+
expect(render({})).toEqual([]);
55+
});
56+
});
57+
58+
describe('diffLogger', () => {
59+
let logger: any;
60+
61+
beforeEach(() => {
62+
logger = {
63+
log: jest.fn(),
64+
groupCollapsed: jest.fn(),
65+
groupEnd: jest.fn(),
66+
group: jest.fn(),
67+
};
68+
});
69+
70+
it('should show no diff with group collapsed', () => {
71+
diffLogger({}, {}, logger, true);
72+
73+
expect(logger.group).not.toHaveBeenCalled();
74+
expect(logger.groupCollapsed).toHaveBeenCalled();
75+
expect(logger.groupEnd).toHaveBeenCalled();
76+
expect(logger.log).toHaveBeenCalledWith('—— no diff ——');
77+
});
78+
79+
it('should show no diff with group not collapsed', () => {
80+
diffLogger({}, {}, logger, false);
81+
82+
expect(logger.group).toHaveBeenCalled();
83+
expect(logger.groupCollapsed).not.toHaveBeenCalled();
84+
expect(logger.groupEnd).toHaveBeenCalled();
85+
expect(logger.log).toHaveBeenCalledWith('—— no diff ——');
86+
});
87+
88+
it('should log no diff without group', () => {
89+
const loggerWithNoGroupCollapsed = Object.assign({}, logger, {
90+
groupCollapsed: () => {
91+
throw new Error()
92+
},
93+
groupEnd: () => {
94+
throw new Error()
95+
},
96+
});
97+
98+
diffLogger({}, {}, loggerWithNoGroupCollapsed, true);
99+
100+
expect(loggerWithNoGroupCollapsed.log).toHaveBeenCalledWith('diff');
101+
expect(loggerWithNoGroupCollapsed.log).toHaveBeenCalledWith('—— no diff ——');
102+
expect(loggerWithNoGroupCollapsed.log).toHaveBeenCalledWith('—— diff end —— ');
103+
});
104+
105+
it('should log the diffs', () => {
106+
diffLogger({ name: 'kirk' }, { name: 'picard' }, logger, false);
107+
108+
expect(logger.log).toHaveBeenCalledWith('%c CHANGED:', 'color: #2196F3; font-weight: bold', 'name', 'kirk', '→', 'picard');
109+
});
110+
});
111+
});
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
import { expect } from 'chai';
2-
import { repeat, pad, formatTime } from '../src/helpers';
1+
import { repeat, pad, formatTime } from './helpers';
32

4-
context('Helpers', () => {
3+
describe('Helpers', () => {
54
describe('repeat', () => {
65
it('should repeat a string the number of indicated times', () => {
7-
expect(repeat('teacher', 3)).to.equal('teacherteacherteacher');
6+
expect(repeat('teacher', 3)).toEqual('teacherteacherteacher');
87
});
98
});
109

1110
describe('pad', () => {
1211
it('should add leading zeros to a number given a maximun length', () => {
13-
expect(pad(56, 4)).to.equal('0056');
12+
expect(pad(56, 4)).toEqual('0056');
1413
});
1514
});
1615

1716
describe('formatTime', () => {
1817
it('should format a time given a Date object', () => {
1918
const time = new Date('December 25, 1995 23:15:30');
20-
expect(formatTime(time)).to.equal('23:15:30.000');
19+
expect(formatTime(time)).toEqual('23:15:30.000');
2120
});
2221
});
2322
});

0 commit comments

Comments
 (0)