Skip to content

Commit c763f73

Browse files
committed
set up testing with tsconfig.test.json
1 parent bfda97f commit c763f73

File tree

6 files changed

+259
-30
lines changed

6 files changed

+259
-30
lines changed

package.json

+19-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"scripts": {
4040
"start": "tsdx watch",
4141
"build": "tsdx build",
42-
"test": "tsdx test --env=jsdom"
42+
"test": "tsdx --tsconfig ./tsconfig.test.json test --env=jsdom"
4343
},
4444
"peerDependencies": {
4545
"react": ">=16.8"
@@ -55,15 +55,29 @@
5555
"singleQuote": true,
5656
"trailingComma": "es5"
5757
},
58+
"jest": {
59+
"globals": {
60+
"ts-jest": {
61+
"tsConfig": "tsconfig.test.json"
62+
}
63+
},
64+
"setupFilesAfterEnv": [
65+
"<rootDir>/test/setupTests.ts"
66+
]
67+
},
5868
"devDependencies": {
69+
"@testing-library/react": "^9.3.0",
70+
"@testing-library/react-hooks": "^3.1.0",
5971
"@types/jest": "^24.0.12",
60-
"@types/react": "^16.8.16",
61-
"@types/react-dom": "^16.8.4",
72+
"@types/react": "^16.9.9",
73+
"@types/react-dom": "^16.9.2",
6274
"husky": "^2.2.0",
75+
"jest-fetch-mock": "^2.1.2",
6376
"prettier": "^1.17.0",
6477
"pretty-quick": "^1.10.0",
65-
"react": "^16.8.6",
66-
"react-dom": "^16.8.6",
78+
"react": "^16.10.2",
79+
"react-dom": "^16.10.2",
80+
"react-test-renderer": "^16.10.2",
6781
"tsdx": "^0.7.2",
6882
"tslib": "^1.9.3",
6983
"typescript": "^3.4.5"

test/setupTests.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { GlobalWithFetchMock } from 'jest-fetch-mock';
2+
3+
const customGlobal: GlobalWithFetchMock = global as GlobalWithFetchMock;
4+
customGlobal.fetch = require('jest-fetch-mock');
5+
customGlobal.fetchMock = customGlobal.fetch;

test/test.tsx

-6
This file was deleted.

test/useAsync.test.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { useAsync } from '../src';
2+
import { cleanup } from '@testing-library/react';
3+
import { FetchMock } from 'jest-fetch-mock/types';
4+
5+
const fetch: FetchMock = global.fetch;
6+
7+
interface StarwarsHero {
8+
name: string;
9+
}
10+
11+
export const generateMockResponseData = (amount: number = 5): StarwarsHero[] =>
12+
[...Array(amount).keys()].map(n => ({
13+
id: n + 1,
14+
name: `Starwars Hero ${n + 1}`,
15+
}));
16+
17+
describe('useAync', () => {
18+
afterEach(cleanup);
19+
20+
beforeEach(() => {
21+
fetch.resetMocks();
22+
});
23+
24+
it('should have a useAsync hook', () => {
25+
expect(useAsync).toBeDefined();
26+
});
27+
28+
// it('should set loading flag when request is initially made', () => {
29+
30+
// });
31+
});

tsconfig.test.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"downlevelIteration": true
5+
},
6+
"include": ["src/**/*.ts", "test/**/*.ts"]
7+
}

0 commit comments

Comments
 (0)