Skip to content
This repository was archived by the owner on Mar 30, 2022. It is now read-only.

Commit 330d6cd

Browse files
change _app
1 parent 8473eed commit 330d6cd

File tree

9 files changed

+125
-61
lines changed

9 files changed

+125
-61
lines changed

__tests__/pages/index.test.tsx

-21
This file was deleted.

babel.config.js

-15
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,4 @@ module.exports = {
99
},
1010
],
1111
],
12-
env: {
13-
test: {
14-
presets: [
15-
[
16-
'@babel/preset-env',
17-
{
18-
modules: false,
19-
targets: { node: 'current' },
20-
},
21-
],
22-
'@babel/react',
23-
'@babel/typescript',
24-
],
25-
},
26-
},
2712
};

jest.config.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// const esModules = ['next', 'enzyme'].join('|');
2-
31
module.exports = {
42
moduleFileExtensions: ['ts', 'tsx', 'js'],
53
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|js?|tsx?|ts?)$',

package-lock.json

-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"devDependencies": {
3030
"@babel/preset-env": "^7.5.4",
3131
"@babel/preset-react": "^7.0.0",
32-
"@babel/preset-typescript": "^7.3.3",
3332
"@types/enzyme": "^3.10.2",
3433
"@types/enzyme-adapter-react-16": "^1.0.5",
3534
"@types/jest": "^24.0.15",

pages/_app.tsx

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
import React from 'react';
2+
import { NextPage } from 'next';
23
import App, { Container } from 'next/app';
34

4-
class MyApp extends App {
5-
public render() {
6-
const { Component, pageProps } = this.props;
5+
export interface Props {
6+
Component: NextPage;
7+
pageProps: any;
8+
}
79

8-
return (
9-
<Container>
10-
<Component {...pageProps} />
11-
</Container>
12-
);
13-
}
10+
interface RootApp extends React.FC<Props> {
11+
getInitialProps: typeof App.getInitialProps;
1412
}
1513

14+
const MyApp: RootApp = ({ Component, pageProps }) => (
15+
<Container>
16+
<Component {...pageProps} />
17+
</Container>
18+
);
19+
20+
MyApp.getInitialProps = App.getInitialProps;
21+
1622
export default MyApp;

readme.md

+83-7
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,16 @@ This is an example project setup with NextJs, Typescript, Eslint, Prettier, Jest
6969
```
7070
7171
### [Create Pages](https://github.com/zeit/next.js#typescript)
72-
18. create `pages` folder
73-
19. create `pages.tsx` under `pages/` (i.e. `pages/index.tsx` for `/` route)
72+
10. create `pages` folder
73+
11. create `pages.tsx` under `pages/` (i.e. `pages/index.tsx` for `/` route)
7474
7575
7676
### [Eslint and Prettier](https://dev.to/robertcoopercode/using-eslint-and-prettier-in-a-typescript-project-53jb)
7777
78-
13. `npm i -D eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-react`
79-
14. `npm i -D eslint-config-airbnb eslint-plugin-jsx-a11y eslint-plugin-import eslint-plugin-react-hooks`
80-
15. `npm i -D prettier eslint-config-prettier eslint-plugin-prettier`
81-
16. create `.eslintrc.js`
78+
12. `npm i -D eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-react`
79+
13. `npm i -D eslint-config-airbnb eslint-plugin-jsx-a11y eslint-plugin-import eslint-plugin-react-hooks`
80+
14. `npm i -D prettier eslint-config-prettier eslint-plugin-prettier`
81+
15. create `.eslintrc.js`
8282
```
8383
module.exports = {
8484
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
@@ -154,7 +154,7 @@ This is an example project setup with NextJs, Typescript, Eslint, Prettier, Jest
154154
},
155155
}
156156
```
157-
17. create `.prettierrc.js`
157+
16. create `.prettierrc.js`
158158
```
159159
module.exports = {
160160
semi: true,
@@ -165,3 +165,79 @@ This is an example project setup with NextJs, Typescript, Eslint, Prettier, Jest
165165
};
166166
```
167167
168+
### [Jest and Enzyme](https://medium.com/@miiny/unit-test-next-js-with-jest-and-enzyme-5b305a8e29fe)
169+
17. `npm i -D jest babel-jest @babel/core @babel/preset-env @babel/preset-react`
170+
18. add scripts in `package.json`
171+
```
172+
"scripts": {
173+
"test": "jest",
174+
"test:watch": "jest --watch",
175+
"test:coverage": "jest --coverage"
176+
},
177+
```
178+
19. `npm i -D enzyme enzyme-adapter-react-16 enzyme-to-json`
179+
20. `npm i -D typescript @types/enzyme @types/enzyme-adapter-react-16 @types/jest`
180+
21. create `jest.config.js`
181+
```
182+
module.exports = {
183+
moduleFileExtensions: ['ts', 'tsx', 'js'],
184+
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|js?|tsx?|ts?)$',
185+
globals: {
186+
NODE_ENV: 'test',
187+
},
188+
snapshotSerializers: ['enzyme-to-json/serializer'],
189+
transform: {
190+
'^.+\\.(j|t)sx?$': 'babel-jest',
191+
},
192+
coveragePathIgnorePatterns: [
193+
'/node_modules/',
194+
'enzyme.js',
195+
'<rootDir>/configs/',
196+
'jest.config.js',
197+
'.json',
198+
'.snap',
199+
],
200+
setupFiles: ['<rootDir>/enzyme.js'],
201+
coverageReporters: ['json', 'lcov', 'text', 'text-summary'],
202+
moduleNameMapper: {
203+
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
204+
'<rootDir>/__mocks__/mocks.js',
205+
'\\.(css|less|scss)$': '<rootDir>/__mocks__/mocks.js',
206+
},
207+
};
208+
```
209+
22. create `babel.config.js`
210+
```
211+
module.exports = {
212+
presets: [
213+
[
214+
'next/babel',
215+
{
216+
'styled-jsx': {},
217+
'preset-env': {},
218+
'preset-react': {},
219+
},
220+
],
221+
],
222+
};
223+
```
224+
23. create `enzyme.js`
225+
```
226+
import Enzyme from 'enzyme';
227+
import Adapter from 'enzyme-adapter-react-16';
228+
// Make sure you can use "publicRuntimeConfig" within tests.
229+
230+
// import { setConfig } from 'next/config';
231+
// import config from './next.config';
232+
233+
// setConfig(config);
234+
Enzyme.configure({ adapter: new Adapter() });
235+
```
236+
26. change `env` in `.eslintrc.js`
237+
```
238+
env: {
239+
browser: true,
240+
node: true,
241+
jest: true
242+
},
243+
```

src/__tests__/pages/index.test.tsx

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ReactWrapper } from 'enzyme';
2+
import { act } from 'react-dom/test-utils';
3+
import AppWrapper from '../../utils/AppWrapper';
4+
import Index from '../../../pages/index';
5+
6+
describe('Pages', () => {
7+
describe('Index', () => {
8+
let TestIndexWrapper: ReactWrapper;
9+
it('should render without throwing an error', () => {
10+
act(() => {
11+
// TestIndexWrapper = mount(<Index userAgent="test" />);
12+
TestIndexWrapper = AppWrapper({
13+
Component: Index,
14+
pageProps: { userAgent: 'test' },
15+
});
16+
});
17+
expect(TestIndexWrapper.find('main').text()).toBe('Your user agent: test');
18+
});
19+
});
20+
});

src/utils/AppWrapper.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
import { mount } from 'enzyme';
3+
import App, { Props as AppProps } from '../../pages/_app';
4+
5+
const AppWrapper = (props: AppProps) => mount(<App {...props} />);
6+
7+
export default AppWrapper;

0 commit comments

Comments
 (0)