Skip to content

Commit 74bd68e

Browse files
Karina/wmdp 35 setup test suite (#5)
* 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 * Update app/.babelrc Co-authored-by: Rocket <[email protected]> * Update app/package.json Co-authored-by: Rocket <[email protected]> * add testRegex and testPathIgnorePatterns * setup jest-axe * add new lines add end of files * jest.config.js: add inline comments * README: add tsconfig documentation * README: add test script documentation Co-authored-by: Rocket <[email protected]>
1 parent 289e125 commit 74bd68e

File tree

10 files changed

+2735
-136
lines changed

10 files changed

+2735
-136
lines changed

app/.babelrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
/**
3+
* `next/babel` is required for our React tests to work
4+
* https://nextjs.org/docs/advanced-features/customizing-babel-config
5+
*/
6+
"presets": ["next/babel"],
7+
"plugins": []
8+
}

app/README.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
1+
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). This README has been modified from the auto-generated one.
22

33
## Getting Started
44

5-
First, run the development server:
5+
First, install dependencies:
6+
7+
```bash
8+
yarn
9+
```
10+
11+
Second, run the development server:
612

713
```bash
8-
npm run dev
9-
# or
1014
yarn dev
1115
```
1216

@@ -18,17 +22,16 @@ A starter [API route](https://nextjs.org/docs/api-routes/introduction) can be ac
1822

1923
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.
2024

21-
## Learn More
22-
23-
To learn more about Next.js, take a look at the following resources:
24-
25-
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
26-
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
27-
28-
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/).
25+
### Tsconfig additions to auto-generated file
2926

30-
## Deploy on Vercel
27+
- `target: es6` -- nextJS set this valule to `es5` by default.
28+
- `allowJS: true` -- allows ts checks on javascript files.
29+
- `checkJS: true` -- works in tandem with `allowJS`. Alternative to adding `// @ts-check` at top of Js files-- instructs ts to check all js files.
30+
- `jsx: react-jsx` -- [Documentation on this option](https://www.typescriptlang.org/docs/handbook/jsx.html). This value updates the JSX mode that TS whips with. Updating this from "preserve" helped get jest tests into a passing state. TODO: figure out why.
31+
- `incremental: true` -- enables incremental typechecking. Incremental typechecking creates a series of `.tsbuildinfo` files to dave information from last compilation. This expedites type checking during build.
32+
- `baseUrl: "."` & `paths: { @pages/*: [pages/*] }` -- These two, in tandem, setup module path aliases for cleaner imports. To utilize this, import files like: `import Home from "@pages/index";`
3133

32-
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
34+
### Package.json
3335

34-
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
36+
#### 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.

app/__mocks__/styleMock.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {}

app/jest.config.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2+
3+
module.exports = {
4+
preset: 'ts-jest',
5+
testEnvironment: 'jsdom',
6+
moduleNameMapper: {
7+
"\\.(css|scss)$": "<rootDir>/__mocks__/styleMock.js", // sets up routing to mocks style sheet
8+
"^@pages(.*)$": "<rootDir>/pages$1" //allows module imports of page components
9+
},
10+
setupFilesAfterEnv: ["<rootDir>/test/jest.setup.js"],
11+
transform: {
12+
'^.+\\.tsx?$': 'ts-jest',
13+
}, //transfrom typescript files to common js for jest compiler
14+
transformIgnorePatterns: [
15+
"/.next/",
16+
"/node_modules/",
17+
"^.+\\.module\\.(css|sass|scss)$"
18+
],
19+
testPathIgnorePatterns: [
20+
"<rootDir>/node_modules/",
21+
],
22+
testRegex: "(/test/.*(test|spec))\\.[jt]sx?$"
23+
};

app/package.json

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,36 @@
33
"version": "0.1.0",
44
"private": true,
55
"scripts": {
6-
"dev": "next dev",
76
"build": "next build",
7+
"dev": "next dev",
8+
"lint": "next lint",
89
"start": "next start",
9-
"lint": "next lint"
10+
"test": "jest --ci --coverage"
1011
},
1112
"dependencies": {
13+
"@types/jest-axe": "^3.5.3",
14+
"jest-axe": "^6.0.0",
1215
"next": "12.1.6",
1316
"react": "18.1.0",
1417
"react-dom": "18.1.0"
1518
},
1619
"devDependencies": {
20+
"@babel/preset-typescript": "^7.17.12",
21+
"@testing-library/dom": "^8.13.0",
22+
"@testing-library/jest-dom": "^5.16.4",
23+
"@testing-library/react": "^13.2.0",
24+
"@types/jest": "^27.5.1",
1725
"@types/node": "17.0.34",
1826
"@types/react": "18.0.9",
1927
"@types/react-dom": "18.0.4",
28+
"@types/testing-library__jest-dom": "^5.14.3",
29+
"@types/testing-library__react": "^10.2.0",
30+
"babel-jest": "^28.1.0",
2031
"eslint": "8.15.0",
2132
"eslint-config-next": "12.1.6",
33+
"jest": "^28.1.0",
34+
"jest-environment-jsdom": "^28.1.0",
35+
"ts-jest": "^28.0.2",
2236
"typescript": "4.6.4"
2337
}
2438
}

app/test/jest.setup.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require('@testing-library/jest-dom');
2+
const { toHaveNoViolations } = require("jest-axe");
3+
4+
expect.extend(toHaveNoViolations);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Home should render the heading 1`] = `
4+
<a
5+
href="https://github.com/navapbc/template-application-nextjs"
6+
>
7+
Next.js Template!
8+
</a>
9+
`;

app/test/pages/index.test.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// test/pages/index.test.js
2+
3+
import { render, screen } from "@testing-library/react";
4+
import { axe, toHaveNoViolations } from "jest-axe";
5+
import Home from "@pages/index";
6+
7+
describe("Home", () => {
8+
it("should render the heading", () => {
9+
render(<Home />);
10+
11+
const heading = screen.getByText(
12+
/Next.js Template!/i
13+
);
14+
15+
expect(heading).toBeInTheDocument();
16+
expect(heading).toMatchSnapshot();
17+
});
18+
19+
it("should pass accessibility scan", async () => {
20+
const { container } = render(<Home />);
21+
const results = await axe(container);
22+
23+
expect(results).toHaveNoViolations();
24+
})
25+
});

app/tsconfig.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
22
"compilerOptions": {
3+
"baseUrl": ".",
4+
"paths": {
5+
"@pages/*": ["pages/*"]
6+
},
37
"target": "es6",
48
"lib": ["dom", "dom.iterable", "esnext"],
59
"allowJs": true,
@@ -12,9 +16,9 @@
1216
"moduleResolution": "node",
1317
"resolveJsonModule": true,
1418
"isolatedModules": true,
15-
"jsx": "preserve",
19+
"jsx": "react-jsx",
1620
"incremental": true
1721
},
18-
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
22+
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "__mocks__/styleMock.js"],
1923
"exclude": ["node_modules"]
2024
}

0 commit comments

Comments
 (0)