Git Product home page Git Product logo

react-testing's Introduction

TDD 방식을 이용하여 만든 간단한 Counter App

Installing and setting up Jest for testing

npm install -D jest @testing-library/react @testing-library/jest-dom @testing-library/dom babel-jest

babel.config.js : configuration file to instruct babel-jest to use the custom preset for Next.js.

module.exports = {
  presets: ["next/babel"]
};

jest.config.js

module.exports = {
  testPathIgnorePatterns: ["<rootDir>/.next/", "<rootDir>/node_modules/"],
  transform: {
    "^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest"
  },
  moduleNameMapper: {
    "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
    "\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js"
  }
};

package.json

...
"scripts": {
    ...
    "test": "jest"
},
...

Mocking static assets

__mocks__/fileMock.js

module.exports = "placeholder-file";

__mocks__/fileMock.js

module.exports = {};

Adding tests

__tests__/HomeTest.js

import { render, screen } from "@testing-library/react";
import "@testing-library/jest-dom/extend-expect";
import Home from "../pages/index";

test("Check for Getting Started Text", () => {
  const { getByText } = render(<Home />);
  expect(getByText("Get started by editing")).toBeInTheDocument();
});

.eslintr.js env 수정

jest 글로벌 변수를 추가하지 않으면 eslint 규칙에 걸리게 됩니다.

module.exports = {
  ...
  env: {
    browser: true,
    es2021: true,
    node: true,
    jest: true,
  },
  ...
};

참고 링크 1

참고 링크 2

react-testing's People

Contributors

hokyunrhee avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.