Skip to content

Commit 9633b7d

Browse files
committed
fix(tests): move to jest as mocha was getting difficult with ts
1 parent 4f93dfe commit 9633b7d

File tree

7 files changed

+1394
-77
lines changed

7 files changed

+1394
-77
lines changed

.mocharc.jsonc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
// Specify "require" for CommonJS
3+
"require": "ts-node/register",
4+
// Specify "loader" for native ESM
5+
"loader": "ts-node/esm",
6+
"extensions": ["ts", "tsx"],
7+
"spec": ["test/**/*.spec.*"],
8+
"watch-files": ["src"]
9+
}

jest.config.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module.exports = {
2+
// The root of your source code, typically /src
3+
// `<rootDir>` is a token Jest substitutes
4+
roots: ['<rootDir>/test'],
5+
6+
// Jest transformations -- this adds support for TypeScript
7+
// using ts-jest
8+
transform: {
9+
'^.+\\.tsx?$': 'ts-jest',
10+
'^.+\\.scss$': 'jest-scss-transform'
11+
},
12+
13+
// Runs special logic, such as cleaning up components
14+
// when using React Testing Library and adds special
15+
// extended assertions to Jest
16+
setupFilesAfterEnv: [
17+
// '@testing-library/react/cleanup-after-each',
18+
'@testing-library/jest-dom/extend-expect'
19+
],
20+
21+
// Test spec file resolution pattern
22+
// Matches parent folder `__tests__` and filename
23+
// should contain `test` or `spec`.
24+
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
25+
26+
// Module file extensions for importing
27+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
28+
};

package.json

+14-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"eslint:fix": "npx eslint ./src/** --ignore-path ./.eslintignore --fix",
1515
"pretty:quick": "./node_modules/.bin/pretty-quick --staged",
1616
"lint:staged": "./node_modules/.bin/lint-staged",
17-
"test:unit": "mocha --require @babel/register --require ignore-styles test/*.spec.js",
17+
"test:unit": "jest",
1818
"clean": "rimraf dist",
1919
"copyfiles": "copyfiles ./src/**/*.scss out-tsc/",
2020
"build:rollup": "tsc && yarn copyfiles && yarn rollup -c",
@@ -59,17 +59,23 @@
5959
"dependencies": {
6060
"@open-wc/building-rollup": "^2.0.1",
6161
"@rollup/plugin-typescript": "^8.3.1",
62+
"@testing-library/jest-dom": "^5.16.3",
63+
"@types/jest": "^27.4.1",
64+
"@types/node": "^17.0.23",
6265
"@types/prop-types": "^15.7.4",
6366
"@types/react": "^17.0.43",
6467
"@typescript-eslint/eslint-plugin": "^5.17.0",
6568
"@typescript-eslint/parser": "^5.17.0",
6669
"copyfiles": "^2.4.1",
6770
"deepmerge": "^4.2.2",
6871
"eslint-plugin-react-hooks": "^4.4.0",
72+
"jest": "^27.5.1",
6973
"postcss": "^8.4.12",
7074
"prop-types": "^15.7.2",
7175
"rollup-plugin-css": "^1.0.10",
7276
"rollup-plugin-terser": "^7.0.2",
77+
"ts-jest": "^27.1.4",
78+
"ts-node": "^10.7.0",
7379
"typescript": "^4.6.3",
7480
"uuid": "^8.3.2"
7581
},
@@ -98,7 +104,9 @@
98104
"@semantic-release/npm": "^7.0.5",
99105
"@semantic-release/release-notes-generator": "^9.0.1",
100106
"@svgr/rollup": "6.2.1",
101-
"@testing-library/react": "12.1.4",
107+
"@testing-library/react": "12",
108+
"@types/expect": "^24.3.0",
109+
"@types/mocha": "^9.1.0",
102110
"babel-loader": "8.2.4",
103111
"babelify": "10.0.0",
104112
"browserify": "17.0.0",
@@ -124,7 +132,9 @@
124132
"github-changes": "^1.1.2",
125133
"http-server": "14.1.0",
126134
"husky": "^3.1.0",
135+
"identity-obj-proxy": "^3.0.0",
127136
"ignore-styles": "5.0.1",
137+
"jest-scss-transform": "^1.0.1",
128138
"jsdom": "19.0.0",
129139
"lint-staged": "12.3.7",
130140
"mocha": "9.2.2",
@@ -138,6 +148,7 @@
138148
"react": "17",
139149
"react-addons-test-utils": "^15.1.0",
140150
"react-dom": "17",
151+
"react-test-renderer": "17",
141152
"rimraf": "^3.0.0",
142153
"rollup": "2.70.1",
143154
"rollup-plugin-babel": "^4.3.3",
@@ -153,6 +164,7 @@
153164
"stylelint": "^14.6.1",
154165
"stylelint-config-standard": "25.0.0",
155166
"stylelint-config-standard-scss": "^3.0.0",
167+
"ts-mocha": "^9.0.2",
156168
"uglifyjs": "^2.4.10",
157169
"watchify": "4.0.0"
158170
},

src/ReactTooltip.tsx

-2
Original file line numberDiff line numberDiff line change
@@ -746,8 +746,6 @@ class ReactTooltip extends React.Component<ReactTooltipProps, TooltipState> {
746746
this.props.delayUpdate
747747
: 0;
748748

749-
//const self = this;
750-
751749
const updateState = () => {
752750
this.setState(
753751
{

test/index.spec.tsx

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
14
import React from 'react';
25
import ReactTooltip from '../src/index';
36
import { render, cleanup } from '@testing-library/react';
4-
import { expect } from 'chai';
57
import forEach from 'mocha-each';
6-
import jsdom from 'mocha-jsdom';
78

89
afterEach(() => {
910
cleanup();
1011
});
1112

1213
describe('Tooltip', () => {
13-
jsdom({ url: 'http://localhost/' });
14-
1514
forEach([
1615
[
1716
{ textColor: 'green', backgroundColor: 'red', arrowColor: 'blue' },
@@ -184,7 +183,7 @@ describe('Tooltip', () => {
184183

185184
const tooltip = document.getElementById('colorSpec');
186185

187-
expect(tooltip.className).to.match(
186+
expect(tooltip.className).toMatch(
188187
new RegExp(
189188
'__react_component_tooltip [a-zA-Z0-9-]+ show' +
190189
(props.border ? ' border ' : ' ') +
@@ -193,27 +192,30 @@ describe('Tooltip', () => {
193192
'i'
194193
)
195194
);
196-
197195
const uuid = tooltip.className.split(' ')[1];
198-
const cssRules = tooltip.firstElementChild.sheet.cssRules;
196+
// TODO: incorrect type...
197+
const cssRules = (tooltip.firstElementChild as any).sheet.cssRules;
199198
const mainCssRule = cssRules.find(
200199
(rule) => rule.selectorText === `.${uuid}`
201200
).style;
202201

203-
expect(mainCssRule.color, 'Text color').to.equal(res.textColor);
204-
expect(mainCssRule.background, 'Background color').to.equal(res.background);
205-
expect(mainCssRule.border, 'Border color').to.equal(
202+
// 'Text color'
203+
expect(mainCssRule.color).toBe(res.textColor);
204+
// 'Background color'
205+
expect(mainCssRule.background).toBe(res.background);
206+
// 'Border color'
207+
expect(mainCssRule.border).toBe(
206208
'1px solid ' + (res.borderColor ? res.borderColor : 'transparent')
207209
);
208210

209211
const arrowPositions = ['top', 'bottom', 'left', 'right'];
210212
arrowPositions.forEach((pos) => {
213+
// pos + ' arrow color'
211214
expect(
212215
cssRules.find(
213216
(rule) => rule.selectorText === `.${uuid}.place-${pos}::after`
214-
).style[`border-${pos}-color`],
215-
pos + ' arrow color'
216-
).to.equal(res.arrowColor ? res.arrowColor : res.background);
217+
).style[`border-${pos}-color`]
218+
).toBe(res.arrowColor ? res.arrowColor : res.background);
217219
});
218220
});
219221
});

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"outDir": "./out-tsc",
66
"allowJs": true,
77
"target": "es2018",
8-
"module": "ESNext",
8+
"module": "ESNext", //ESNext breaks mocha
99
"moduleResolution": "node",
1010
"noImplicitAny": false,
1111
"noEmitOnError": true,

0 commit comments

Comments
 (0)