All Projects → cedrickchee → react-typescript-jest-enzyme-testing

cedrickchee / react-typescript-jest-enzyme-testing

Licence: MIT license
Testing React.JS + TypeScript component with Jest and Enzyme. A simple example for reference.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-typescript-jest-enzyme-testing

Hydrafw
HydraFW official firmware for HydraBus/HydraNFC for researcher, hackers, students, embedded software developers or anyone interested in debugging/hacking/developing/penetration testing
Stars: ✭ 165 (+211.32%)
Mutual labels:  educational
LLVM-JVM
[W.I.P] A Just-In-Time Java Virtual Machine written in Haskell
Stars: ✭ 22 (-58.49%)
Mutual labels:  educational
cnode-react
a web app for cnode.org with react + react-router + react-redux
Stars: ✭ 23 (-56.6%)
Mutual labels:  jest-tests
Socialfish
Phishing Tool & Information Collector
Stars: ✭ 2,522 (+4658.49%)
Mutual labels:  educational
curriculum
A roadmap for Boot.dev's CS curriculum for backend developers
Stars: ✭ 492 (+828.3%)
Mutual labels:  educational
finance-project-ddd
Projeto financeiro usando domain driven design, tdd, arquitetura hexagonal e solid
Stars: ✭ 67 (+26.42%)
Mutual labels:  jest-tests
C Plus Plus
Collection of various algorithms in mathematics, machine learning, computer science and physics implemented in C++ for educational purposes.
Stars: ✭ 17,151 (+32260.38%)
Mutual labels:  educational
V2releases
A friendly ARM assembler and simulator for educational use
Stars: ✭ 46 (-13.21%)
Mutual labels:  educational
ims-tutorials
Interactive tutorials developed with the learnr package supporting the textbook OpenIntro::Introduction to Modern Statistics.
Stars: ✭ 25 (-52.83%)
Mutual labels:  educational
Some Pentesters SecurityResearchers RedTeamers
Some Pentesters, Security Researchers, Red Teamers which i learned from them a lot...
Stars: ✭ 60 (+13.21%)
Mutual labels:  educational
React Gh Pages
Deploying a React App (created using create-react-app) to GitHub Pages
Stars: ✭ 2,801 (+5184.91%)
Mutual labels:  educational
kilopp
The Kilo editor in modern C++
Stars: ✭ 113 (+113.21%)
Mutual labels:  educational
tutorials
All of the code for my Medium articles
Stars: ✭ 130 (+145.28%)
Mutual labels:  educational
Blocklike
Bridging the gap between block programming and JavaScript.
Stars: ✭ 177 (+233.96%)
Mutual labels:  educational
Algorithms-for-Automated-Driving
Each chapter of this (mini-)book guides you in programming one important software component for automated driving.
Stars: ✭ 153 (+188.68%)
Mutual labels:  educational
Topocm content
Course on topology in condensed matter
Stars: ✭ 161 (+203.77%)
Mutual labels:  educational
material-aulas
Material para ensino introdutório de programação com Python em um contexto visual
Stars: ✭ 85 (+60.38%)
Mutual labels:  educational
WtfReactNativeTesting
Testing React Native Apps ✔️ ✅
Stars: ✭ 62 (+16.98%)
Mutual labels:  jest-tests
tensorflowjs-remove-background
Remove Background from the picture using WebAssembly & TensorFlow.js
Stars: ✭ 79 (+49.06%)
Mutual labels:  jest-tests
PyTherm-applied-thermodynamics
Educational ipython source code for applied thermodynamics.
Stars: ✭ 62 (+16.98%)
Mutual labels:  educational

Jest and Enzyme Testing with TypeScript

This is an example (for reference purposes) on how to use Jest and Enzyme to test React.JS 16.x component developed in TypeScript 2.x.

Jest is a decent unit testing option which provides great TypeScript support.

Getting Started

  1. Clone this repo:
git clone https://github.com/cedrickchee/react-typescript-jest-enzyme-testing react-typescript
cd react-typescript
  1. Install project dependencies:
yarn

# If you are using NPM
npm install
  1. Start Jest to run tests:
# start Jest in watch mode
yarn test -- --watch

# or if you are using NPM
npm t -- --watch
  1. There is no step 4. You can start developing your React component and write unit test along the way.

Project

How's this project was created? From a clean project setup, here are the steps to recreate this example:

  • Step 0: Pre-requisite

Install React.JS and TypeScript:

yarn add react react-dom typescript

# or if you are using NPM
npm i react react-dom typescript
  • Step 1: Install Jest

Install the following using yarn/npm:

yarn add jest @types/jest ts-jest --dev

# or if you are using NPM
npm i jest @types/jest ts-jest -D

Explanation:

  • Install Jest framework (jest).

  • Install the types for Jest (@types/jest).

  • Install the TypeScript preprocessor for Jest (ts-jest) which allows jest to transpile TypeScript on the fly and have source-map support built in.

  • Save all of these to your dev dependencies (testing is almost always a npm dev-dependency).

  • Step 2: Configure Jest

Add the following jest.config.js file to the root of your project:

module.exports = {
  "roots": [
    "<rootDir>/src"
  ],
  "transform": {
    "^.+\\.tsx?$": "ts-jest"
  },
  "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js",
    "jsx",
    "json",
    "node"
  ],
}

Explanation:

  • We always recommend having all TypeScript files in a src folder in your project. We assume this is true and specify this using the roots option.

  • The transform config just tells Jest to use ts-jest for ts / tsx files.

  • The testRegex tells Jest to look for tests in any __tests__ folder AND also any files anywhere that use the (.test|.spec).(ts|tsx) extension e.g. checkbox.test.tsx etc.

  • The moduleFileExtensions tells Jest to recognize our file extensions. This is needed as we add ts/tsx into the defaults (js|jsx|json|node).

  • Step 3: Run tests

Run npx jest --watch (or ./node_modules/.bin/jest --watch) from your project root and Jest will execute any tests you have.

Optional: Add script target for npm scripts

Add this in package.json:

{
  "test": "jest --watch"
}
  • This allows you to run the tests in watch mode with a simple yarn test or npm t.

  • Step 4: Setup Enzyme

Enzyme allows you to test React components with DOM support. There are three steps to setting up Enzyme:

  1. Install Enzyme, types for Enzyme, a better snapshot serializer for Enzyme, enzyme-adapter-react for your React version:
yarn add enzyme @types/enzyme enzyme-to-json enzyme-adapter-react-16 --dev

# or if you are using NPM
npm i enzyme @types/enzyme enzyme-to-json enzyme-adapter-react-16 -D
  1. Add "snapshotSerializers" and "setupTestFrameworkScriptFile" to your jest.config.js:
module.exports = {
  // ... ... ... TRUNCATED. Other parts as before ... ... ...

  // Setup Enzyme
  "snapshotSerializers": ["enzyme-to-json/serializer"],
  "setupTestFrameworkScriptFile": "<rootDir>/src/setupEnzyme.ts",
}
  1. Create src/setupEnzyme.ts file.
import { configure } from 'enzyme';
import EnzymeAdapter from 'enzyme-adapter-react-16';

configure({ adapter: new EnzymeAdapter() });

That's all to it!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].