Skip to content

Commit e01e975

Browse files
author
Kylie Stewart
authored
Update types + add TS tests (#77)
* Update contributing guidelines * Use tsc's project reference to isolate tests * Update our timeout call * Update docs surrounding new scripts * Remove compiled JS, do not generate on test * Add eslint-plugin-react * Run tslint in CI
1 parent bf6d0c2 commit e01e975

11 files changed

Lines changed: 321 additions & 19 deletions

File tree

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test/typescript/index.js

.eslintrc.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
env:
22
node: true
3-
extends: 'eslint:recommended'
3+
extends: [ 'eslint:recommended', 'plugin:react/recommended' ]
4+
parser: '@typescript-eslint/parser'
5+
parserOptions:
6+
sourceType: module
7+
settings:
8+
react:
9+
version: detect
410
rules:
511
indent: [ 2, 2, { SwitchCase: 1 } ]
612
no-trailing-spaces: 2

.github/pull_request_template.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Please include a summary of the change and which issue is fixed. Please also inc
1212
## Checklist:
1313

1414
- [ ] All tests are passing
15+
- [ ] Type definitions, if updated, pass both `test-ts-defs` and `test-ts-usage`
1516
- [ ] Benchmark performance has not significantly decreased
1617
- [ ] Bundle size has not been significantly impacted
1718
- [ ] The bundle size badge has been updated to reflect the new size

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ jspm_packages/
3939
# Typescript v1 declaration files
4040
typings/
4141

42+
# Compiled output from Typescript usage test
43+
test/typescript/*.js
44+
4245
# Optional npm cache directory
4346
.npm
4447

CONTRIBUTING.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,14 @@ $ yarn run test-browser-ie
8484

8585
### Types
8686

87-
We validate our TypeScript `index.d.ts` with:
87+
We validate our TypeScript `index.d.ts` with two steps:
8888

8989
```sh
90-
$ yarn run test-ts
90+
# Runs the TypeScript compiler over our types
91+
$ yarn run test-ts-defs
92+
93+
# Runs our types through a sample TypeScript file
94+
$ yarn run test-ts-usage
9195
```
9296

9397
### Style
@@ -135,7 +139,13 @@ please flag to your reviewers and have a discussion about whether or not the siz
135139

136140
_Only for project administrators_
137141

138-
1. Run `npm version patch` (or `minor|major|VERSION`) to run tests and lint,
139-
build published directories, then update `package.json` + add a git tag.
140-
2. Run `npm publish` and publish to NPM if all is well.
141-
3. Run `git push && git push --tags`
142+
```sh
143+
# (1) Run tests, lint, build published dir, update package.json
144+
$ npm version [patch|minor|major|<version>]
145+
146+
# (2) If all is well, publish the new version to the npm registry
147+
$ npm publish
148+
149+
# (3) Then, update github with the associated tag
150+
$ git push && git push --tags
151+
```

package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
"version": "3.1.1",
44
"description": "Fastest deep equal comparison for React. Great for React.memo & shouldComponentUpdate. Also really fast general-purpose deep comparison.",
55
"main": "index.js",
6+
"typings": "index.d.ts",
67
"scripts": {
78
"preversion": "yarn test",
89
"benchmark": "node benchmark",
910
"eslint": "eslint \"*.js\" benchmark test",
11+
"tslint": "eslint test/typescript/sample-usage.tsx",
1012
"test-browser": "karma start test/browser/karma.conf.js",
1113
"test-browser-ie": "karma start test/browser/karma.conf.ie.js",
1214
"test-node": "mocha \"test/node/*.spec.js\"",
1315
"test-node-cov": "nyc mocha \"test/node/*.spec.js\"",
14-
"test-ts": "tsc --target ES5 --noImplicitAny index.d.ts",
15-
"test": "builder concurrent --buffer eslint test-ts test-node-cov test-browser",
16-
"test-ie": "builder concurrent --buffer eslint test-ts test-node-cov test-browser-ie",
16+
"test-ts-usage": "tsc --esModuleInterop --jsx react --noEmit test/typescript/sample-usage.tsx",
17+
"test-ts-defs": "tsc --target ES5 index.d.ts",
18+
"test": "builder concurrent --buffer eslint tslint test-ts-usage test-ts-defs test-node-cov test-browser",
19+
"test-ie": "builder concurrent --buffer eslint tslint test-ts-usage test-ts-defs test-node-cov test-browser-ie",
1720
"compress": "terser --compress --mangle=\"toplevel:true\" -- index.js",
1821
"size-min-gz": "yarn -s compress | gzip -9 | wc -c"
1922
},
@@ -40,12 +43,16 @@
4043
"@babel/preset-env": "^7.7.6",
4144
"@testing-library/dom": "^7.5.1",
4245
"@testing-library/preact": "^1.0.2",
46+
"@types/node": "^14.0.1",
47+
"@types/react": "^16.9.35",
48+
"@typescript-eslint/parser": "^2.34.0",
4349
"babel-loader": "^8.0.6",
4450
"benchmark": "^2.1.4",
4551
"builder": "^5.0.0",
4652
"codecov": "^3.6.5",
4753
"core-js": "^3.5.0",
4854
"eslint": "^6.7.2",
55+
"eslint-plugin-react": "^7.20.0",
4956
"fast-deep-equal": "3.1.1",
5057
"fast-deep-equal-git": "epoberezkin/fast-deep-equal#v3.1.1",
5158
"jsdom": "^16.2.2",

test/node/advanced.spec.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
22

3+
/* eslint-disable react/prop-types */
4+
/* eslint-disable react/no-children-prop */
5+
36
const assert = require('assert');
47
const jsdom = require('jsdom-global');
58
const sinon = require('sinon');
@@ -13,7 +16,10 @@ const PreactTestRenderer = require('@testing-library/preact');
1316
const equal = require('../..');
1417
const tests = require('./tests');
1518

16-
const TIMEOUT = '5000';
19+
// `jsdom-global` does a lazy require under the hood to `jsdom` which is
20+
// super expensive. We deliberately call + cleanup to take the require hit
21+
// early and seed the require cache so subsequent calls are fast.
22+
jsdom()();
1723

1824
class ReactChild extends React.Component {
1925
shouldComponentUpdate(nextProps) {
@@ -61,9 +67,6 @@ describe('advanced', () => {
6167
let sandbox;
6268
let warnStub;
6369

64-
// eslint-disable-next-line no-invalid-this
65-
beforeEach(function () { this.timeout(TIMEOUT); });
66-
6770
beforeEach(() => {
6871
sandbox = sinon.createSandbox();
6972
warnStub = sandbox.stub(console, 'warn');

test/typescript/sample-usage.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// This file exists to test our types against sample user code
2+
// This is compiled using `tsc` in our `test-ts-usage` script
3+
import React from 'react';
4+
import equal from '../../index.js';
5+
6+
type ITodo = {
7+
text: string;
8+
id: string;
9+
};
10+
11+
const testArr: ITodo[] = [
12+
{ text: 'green', id: '23' },
13+
{ text: 'sunshine', id: '1' },
14+
{ text: 'mountain', id: '11' },
15+
{ text: 'air', id: '8' },
16+
{ text: 'plants', id: '9' },
17+
];
18+
19+
type IProps = {
20+
todo: ITodo;
21+
};
22+
23+
class TestChild extends React.Component<IProps> {
24+
shouldComponentUpdate(nextProps: IProps) {
25+
return !equal(this.props, nextProps);
26+
}
27+
28+
render() {
29+
const todo = this.props.todo;
30+
return <div>{todo.text}</div>;
31+
}
32+
}
33+
34+
class TestContainer extends React.Component {
35+
render() {
36+
return testArr.map((item) => <TestChild key={item.id} todo={item} />);
37+
}
38+
}
39+
40+
export default TestContainer;

test/typescript/tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"composite": true,
4+
"jsx": "react",
5+
"target": "ES6",
6+
"allowSyntheticDefaultImports": true
7+
}
8+
}

tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"noImplicitAny": true
4+
},
5+
"env": {
6+
"browser": true,
7+
"node": true,
8+
"es6": true
9+
},
10+
"references": [{ "path": "test/typescript" }],
11+
"parserOptions": {
12+
"sourceType": "module"
13+
}
14+
}

0 commit comments

Comments
 (0)