Skip to content

Commit f6e8efe

Browse files
Karina/wmdp 33 linter typechecker (#6)
* yarn create next-app --typescript * clean up auto-generated pages, public and styles * next.config.js: setup i18n locale for internationalization * next.config.js: add code comment re: internationalization * add html tags for VO * Update app/styles/Home.module.css update stickcy nav Co-authored-by: Rocket <[email protected]> * Update app/README.md Co-authored-by: Rocket <[email protected]> * Update app/README.md Co-authored-by: Rocket <[email protected]> * tsconfig: target es6 * add babelec * add __mocks__ with mock styles for testing * -test: add first jest test and snapshot test for Home page - add jest.setup.js to require jest-dom * - add jest.config.js - package.json: add react testing library, babel and jest dependencies - add first test - update jsx - yarn.lock updated with new packages * .eslintrc.json: extend nava eslint config * package.json: add eslint-config-nava * add ts check to all js files * add dev note regarding incremental type checking * add TS-specific eslint * update read me with development typecheck and linting tips * update read me with development typecheck and linting tips * add and setup prettier * Update app/.babelrc Co-authored-by: Rocket <[email protected]> * Update app/package.json Co-authored-by: Rocket <[email protected]> * Update package.json add scripts to package.json * run prettier * add checkJs to tsconfig, remove duplicate code from js files * add typescript parser and implement type information with linting * eslintrc: add prettier recommended plugin * preettierrc: add trailing comma check, remove redundant code * README: add auto-generated modified note * Update app/README.md Co-authored-by: Rocket <[email protected]> * add testRegex and testPathIgnorePatterns * re-add localesubpaths to next.config.js * tsconfig.json: remove inline comment * README: add docimentation for eslint and tsconfig * setup jest-axe * add new lines add end of files * jest.config.js: add inline comments * README - add documentation for test * README: add tsconfig documentation * README: add test script documentation * README: add documentation for dependencies * yarn.lock * Update app/__mocks__/styleMock.js Co-authored-by: Rocket <[email protected]> Co-authored-by: Rocket <[email protected]>
1 parent 74bd68e commit f6e8efe

13 files changed

+370
-140
lines changed

app/.babelrc

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* `next/babel` is required for our React tests to work
44
* https://nextjs.org/docs/advanced-features/customizing-babel-config
55
*/
6-
"presets": ["next/babel"],
6+
"presets": [
7+
"next/babel",
8+
"@babel/preset-typescript"
9+
],
710
"plugins": []
811
}

app/.eslintrc.json

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
{
2-
"extends": "next/core-web-vitals"
2+
"parser": "@typescript-eslint/parser",
3+
"parserOptions": {
4+
"tsconfigRootDir": "__dirname",
5+
"project": ["./tsconfig.json"]
6+
},
7+
"plugins": ["@typescript-eslint"],
8+
"extends": [
9+
"plugin:@typescript-eslint/recommended",
10+
"plugin:@typescript-eslint/recommended-requiring-type-checking",
11+
"plugin:prettier/recommended",
12+
"prettier",
13+
"next",
14+
"nava"
15+
],
16+
"rules": {
17+
"@typescript-eslint/no-unused-vars": "error",
18+
"@typescript-eslint/no-explicit-any": "error"
19+
}
320
}

app/.prettierrc.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all"
4+
}

app/README.md

+55-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,39 @@ A starter [API route](https://nextjs.org/docs/api-routes/introduction) can be ac
2222

2323
The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead of React pages.
2424

25+
## Local Linter and Typechecker Setup
26+
27+
### Setting up up linting and typechecking for local development
28+
29+
For linting, this application is leveraging `eslint`, `prettier` and Nava's [eslint-config-nava](https://github.com/navapbc/eslint-config-nava). Although, these will be run on CI and prior to commit, it is still recommended that we tell our code editor to auto-fix eslint and prettier errors on save.
30+
31+
In VSCode, do so by creating a `.vscode/settings.json` file with:
32+
33+
```
34+
{
35+
"editor.codeActionsOnSave": {
36+
"source.fixAll.eslint": true
37+
},
38+
"editor.formatOnSave": true,
39+
"editor.defaultFormatter": "esbenp.prettier-vscode"
40+
}
41+
```
42+
43+
For typechecking, this application is leveraging Next.js' [incremental typechecking](https://nextjs.org/docs/basic-features/typescript#incremental-type-checking). NextJS will run type checking as a part of `next build`-- Although it is still recommended that we set up type checking using our code editor for development. In VSCode, do so by adding the following to your `.vscode/settings.json` file
44+
45+
```
46+
"typescript.validate.enable": true
47+
```
48+
49+
Note: make sure TypeScript and Javascript Language Features are enabled in VS Code Extensions.
50+
51+
### Eslint rules explained
52+
53+
- "@typescript-eslint/no-unused-vars": "error"
54+
- Disallows unused variables-- prevents dead code accumulation.
55+
- "@typescript-eslint/no-explicit-any": "error"
56+
- Disallows usage of `any` type. The usage of `any` defeats the purpose of typescript. Consider using `unknown` type instead instead.
57+
2558
### Tsconfig additions to auto-generated file
2659

2760
- `target: es6` -- nextJS set this valule to `es5` by default.
@@ -34,4 +67,25 @@ The `pages/api` directory is mapped to `/api/*`. Files in this directory are tre
3467
### Package.json
3568

3669
#### Scripts
37-
- `test`: runs `jest --ci --coverage`. [--ci option](https://jestjs.io/docs/cli#--ci) is provided so that snapshots don't get created automatically, it will require Jest to be run with `--updateSnapshot`. [--coverage option](https://jestjs.io/docs/cli#--coverageboolean) is provided to instruct jest to collect and report test coverage in output.
70+
- `format`: instructs prettier to rewrite files with fixes for formatting violations.
71+
- `format-check`: instructs prettier to only check files for violations without fixing them.
72+
- `test`: runs `jest --ci --coverage`. [--ci option](https://jestjs.io/docs/cli#--ci) is provided to prevent automatic creation of snapshots. This requires Jest to be run with `--updateSnapshot`. [--coverage option](https://jestjs.io/docs/cli#--coverageboolean) is provided to instruct jest to collect and report test coverage in output.
73+
- `ts:check`: runs `tsc --noEmit`. [--noEmit option](https://www.typescriptlang.org/tsconfig#noEmit) is provided to prevent type checker compiler from outputting files.
74+
75+
#### Dependencies
76+
- `@typescript-eslint/parser`: implemented in .eslint.json via `"parser": "@typescript-eslint/parser"`. This [plugin](https://www.npmjs.com/package/@typescript-eslint/parser) works in tandem with other plugins to help facilitate the usage of ESlint with TypeScript.
77+
- `@types/jest-axe`: This package contains type definitions for [jest-axe](https://www.npmjs.com/package/jest-axe).
78+
79+
#### Dev Dependencies
80+
- `@babel/preset-typescript`: This [module](https://babeljs.io/docs/en/babel-preset-typescript) allows us to use babel to transpile TypeScript into Javascript, specifically for [jest testing](https://jestjs.io/docs/getting-started#using-typescript) in our case. Implemented in .babelrc > presets.
81+
- `@testing-library/dom`: This [module](https://github.com/testing-library/dom-testing-library) provides querying fo.
82+
- `@testing-library/jest-dom`: This [module](https://testing-library.com/docs/ecosystem-jest-dom/) is a companion for testing-library/dom-- it provides DOM element matchers for jest.
83+
- `@testing-library/react`: This [module](https://testing-library.com/docs/react-testing-library/intro/) works in tandem with testing-library/dom to add APIs for testing React components.
84+
- `babel-jest`: This [module](https://www.npmjs.com/package/babel-jest) works to compile JavaScript/Typescript code using babel for testing. Automatically implemented if `jest-cli` is used.
85+
- `eslint`: This [module](https://www.npmjs.com/package/eslint) provides linting. Configuration implemented in .eslintrc.
86+
- `eslint-config-nava`: This [module](https://github.com/navapbc/eslint-config-nava) contains nava's preferred configurations for eslint. It is implemented in .eslintrc > extensions > nava.
87+
- `eslint-config-next`: This [module](https://nextjs.org/docs/basic-features/eslint) is automatically installed by nextJS, it includes out of the eslint configuration. Implemented in .eslintrc.json > extends > next.
88+
- `eslint-config-prettier`: This [module](https://github.com/prettier/eslint-config-prettier) turns off eslint rules that may conflict with prettier. Implemented in .eslintrc > extends > prettier.
89+
- `jest-environment-jsdom`: This [module](https://www.npmjs.com/package/jest-environment-jsdom) simulates the DOM for testing. Implemented in jest.config.js > testEnvironment.
90+
- `prettier`: This [module](https://prettier.io/) is used for code formatting. Implemented in .prettierrc.json.
91+
- `ts-jest`: This [module](https://www.npmjs.com/package/ts-jest) lets ys yse hest to test our project written in TypeScript. Implemented in jest.config.js > preset > ts-jest

app/__mocks__/styleMock.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = {}
1+
module.exports = {};

app/next.config.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/** @type {import('next').NextConfig} */
2+
23
const nextConfig = {
34
i18n: {
45
// TODO: implement i18n internationalization-- look into nextJS subpath routing
56
locales: ['en-US', 'es-ES'],
67
defaultLocale: 'en-US',
78
localeSubpaths: {
89
es: "es"
9-
},
10+
}
1011
},
11-
reactStrictMode: true
12-
}
12+
reactStrictMode: true,
13+
};
1314

14-
module.exports = nextConfig
15+
module.exports = nextConfig;

app/package.json

+11-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
"scripts": {
66
"build": "next build",
77
"dev": "next dev",
8+
"format": "prettier --write '**/*.{js,json,md,mdx,ts,tsx,scss,yaml,yml}'",
9+
"format-check": "prettier --check '**/*.{js,json,md,mdx,ts,tsx,scss,yaml,yml}'",
810
"lint": "next lint",
911
"start": "next start",
10-
"test": "jest --ci --coverage"
12+
"test": "jest --ci --coverage",
13+
"ts:check": "tsc --noEmit"
1114
},
1215
"dependencies": {
1316
"@types/jest-axe": "^3.5.3",
17+
"@typescript-eslint/parser": "^5.26.0",
1418
"jest-axe": "^6.0.0",
1519
"next": "12.1.6",
1620
"react": "18.1.0",
@@ -27,11 +31,16 @@
2731
"@types/react-dom": "18.0.4",
2832
"@types/testing-library__jest-dom": "^5.14.3",
2933
"@types/testing-library__react": "^10.2.0",
34+
"@typescript-eslint/eslint-plugin": "^5.25.0",
3035
"babel-jest": "^28.1.0",
31-
"eslint": "8.15.0",
36+
"eslint": "^8.15.0",
37+
"eslint-config-nava": "^11.0.0",
3238
"eslint-config-next": "12.1.6",
39+
"eslint-config-prettier": "^8.5.0",
3340
"jest": "^28.1.0",
41+
"jest-cli": "^28.1.0",
3442
"jest-environment-jsdom": "^28.1.0",
43+
"prettier": "^2.6.2",
3544
"ts-jest": "^28.0.2",
3645
"typescript": "4.6.4"
3746
}

app/pages/_app.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import '../styles/globals.css'
2-
import type { AppProps } from 'next/app'
1+
import '../styles/globals.css';
2+
import type { AppProps } from 'next/app';
33

44
function MyApp({ Component, pageProps }: AppProps) {
5-
return <Component {...pageProps} />
5+
return <Component {...pageProps} />;
66
}
77

8-
export default MyApp
8+
export default MyApp;

app/pages/api/hello.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
2-
import type { NextApiRequest, NextApiResponse } from 'next'
2+
import type { NextApiRequest, NextApiResponse } from 'next';
33

44
type Data = {
5-
name: string
6-
}
5+
name: string;
6+
};
77

88
export default function handler(
99
req: NextApiRequest,
1010
res: NextApiResponse<Data>
1111
) {
12-
res.status(200).json({ name: 'John Doe' })
12+
res.status(200).json({ name: 'John Doe' });
1313
}

app/pages/index.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@ const Home: NextPage = () => {
1616

1717
<main className={styles.main}>
1818
<h1 className={styles.title}>
19-
Welcome to your <a href="https://github.com/navapbc/template-application-nextjs">Next.js Template!</a>
19+
Welcome to your{' '}
20+
<a href="https://github.com/navapbc/template-application-nextjs">
21+
Next.js Template!
22+
</a>
2023
</h1>
2124
</main>
2225

2326
<footer className={styles.footer}>
2427
<nav aria-label="Secondary">Placeholder Footer</nav>
2528
</footer>
2629
</div>
27-
)
30+
);
2831
};
2932

30-
export default Home;
33+
export default Home;

app/test/pages/index.test.tsx

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
// test/pages/index.test.js
22

33
import { render, screen } from "@testing-library/react";
4-
import { axe, toHaveNoViolations } from "jest-axe";
4+
import { axe } from "jest-axe";
55
import Home from "@pages/index";
66

7-
describe("Home", () => {
8-
it("should render the heading", () => {
7+
describe('Home', () => {
8+
it('should render the heading', () => {
99
render(<Home />);
1010

11-
const heading = screen.getByText(
12-
/Next.js Template!/i
13-
);
11+
const heading = screen.getByText(/Next.js Template!/i);
1412

1513
expect(heading).toBeInTheDocument();
1614
expect(heading).toMatchSnapshot();

app/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"target": "es6",
88
"lib": ["dom", "dom.iterable", "esnext"],
99
"allowJs": true,
10+
"checkJs": true,
1011
"skipLibCheck": true,
1112
"strict": true,
1213
"forceConsistentCasingInFileNames": true,

0 commit comments

Comments
 (0)