Lemoncode Front-End Master, 7th edition. Module 5 exercise.
npm i -D jest @types/jest ts-jest
npm i -D @testing-library/react @testing-library/jest-dom
npm i -D @testing-library/react-hooks react-test-renderer
- Create folder
./config/test
- In this same folder, create file
setup-after.ts
. This file is necessary when we usejest-dom
from testing-library, and its content should be this:
import '@testing-library/jest-dom';
import '@testing-library/jest-dom/extend-expect';
- In this same folder, create file
jest.json
with this content
{
"rootDir": "../../",
"preset": "ts-jest",
"restoreMocks": true,
"setupFilesAfterEnv": ["<rootDir>/config/test/setup-after.ts"], // jest-dom
"moduleDirectories": ["<rootDir>/src", "node_modules"]
}
- Finally, add two new scripts to
package.json
"scripts": {
"start": "run-p -l start:dev start:mock-server",
"start:dev": "webpack-dev-server",
"start:mock-server": "cd ./server && npm start",
- "postinstall": "cd ./server && npm i"
+ "postinstall": "cd ./server && npm i",
+ "test": "jest -c ./config/test/jest.json --verbose",
+ "test:watch": "npm run test -- --watchAll -i"
},
To debug jest specs we will use Visual Studio Code. As we know, VS Code provides by default a node debugger. In order to debug Node.js code, this editor need a file called launch.json
. On tab Debug of VS Code we can select an option to create automatically this file on this route ./.vscode
We have to modifiy original auto created content with this content
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest single run",
"program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
"args": [
"-c",
"./config/test/jest.json",
"--verbose",
"-i",
"--no-cache"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Jest watch run",
"program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
"args": [
"-c",
"./config/test/jest.json",
"--verbose",
"-i",
"--no-cache",
"--watchAll"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Jest selected file",
"program": "${workspaceRoot}/node_modules/jest/bin/jest.js",
"args": [
"${fileBasenameNoExtension}",
"-c",
"./config/test/jest.json",
"--verbose",
"-i",
"--no-cache",
"--watchAll"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
- common/mappers/collection.mapper.spec.ts
- pods/hotel-collection/hotel-collection.mapper.spec.ts
- common/components/forms/text-field.spec.tsx
- pods/hotel-collection/hotel-collection.hook.spec.ts
- pods/login/login.component.spec.tsx
- pods/login/login.container.spec.tsx
- pods/hotel-collection/hotel-collection.component.spec.tsx
- pods/hotel-collection/hotel-collection.container.spec.tsx
- Create file
./config/test/jest.coverage.json
. Is almost the same asjest.json
.
{
"rootDir": "../../",
"preset": "ts-jest",
"restoreMocks": true,
"setupFilesAfterEnv": ["<rootDir>/config/test/setup-after.ts"],
"moduleDirectories": ["<rootDir>/src", "node_modules"],
+ "collectCoverage": true
}
- Install
rimraf
as a development dependency.
npm i -D rimraf
- Add two new scripts on
package.json
"scripts": {
"start": "run-p -l start:dev start:mock-server",
"start:dev": "webpack-dev-server",
"start:mock-server": "cd ./server && npm start",
"postinstall": "cd ./server && npm i",
"test": "jest -c ./config/test/jest.json --verbose",
- "test:watch": "npm run test -- --watchAll -i"
+ "test:watch": "npm run test -- --watchAll -i",
+ "pretest:coverage": "rimraf coverage",
+ "test:coverage": "jest -c ./config/test/jest.coverage.json"
},
We only have to create .travis.yml
file on root folder. Its content should be:
language: node_js
node_js:
- "10"
install:
- npm install
script:
- npm test
We only have to create config.yml
file under folder .circle.ci
. Its content should be:
version: 2.1
orbs:
node: circleci/node@1.1.6
jobs:
build-and-test:
executor:
name: node/default
steps:
- checkout
- node/with-cache:
steps:
- run: npm install
- run: npm test
workflows:
build-and-test:
jobs:
- build-and-test