GithubHelp home page GithubHelp logo

gomagoma676 / nextjs-testing Goto Github PK

View Code? Open in Web Editor NEW
26.0 2.0 15.0 262 KB

[テスト編] Nextjs + React-testing-libraryでモダンReactソフトウェアテスト / Section2 🚀

TypeScript 98.75% JavaScript 1.04% CSS 0.21%
nextjs react-testing-library typescipt

nextjs-testing's Introduction

This is a Next.js project bootstrapped with create-next-app.

Project setup : Nextjs+React-testing-library+TypeScript+Tailwind CSS

1. Nextjs Project 新規作成

1-1. create-next-app

npx [email protected] . --use-npm

Node.js version 10.13以降が必要です。 -> ターミナル node -vでver確認出来ます。

1-2. 必要 module のインストール

1-3. prettierの設定 : package.json

    "prettier": {
        "singleQuote": true,
        "semi": false
    }

2. React-testing-library の導入

2-1. 必要 module のインストール

2-2. Project folder 直下に".babelrc"ファイルを作成して下記設定を追加

touch .babelrc
    {
        "presets": ["next/babel"]
    }

2-3. package.json に jest の設定を追記

    "jest": {
        "testPathIgnorePatterns": [
            "<rootDir>/.next/",
            "<rootDir>/node_modules/"
        ],
        "moduleNameMapper": {
            "\\.(css)$": "<rootDir>/node_modules/jest-css-modules"
        }
    }

2-4. package.jsonに test scriptを追記

    "scripts": {
        ...
        "test": "jest --env=jsdom --verbose"
    },

3. TypeScript の導入

https://nextjs.org/learn/excel/typescript/create-tsconfig

3-1. 空のtsconfig.json作成

touch tsconfig.json

3-2. 必要moduleのインストール

npm i -D typescript @types/[email protected] @types/[email protected]

3-3. 開発server起動

npm run dev

3-4. _app.js, index.js -> tsx へ拡張子変更

3-5. AppProps型追記

    import { AppProps } from 'next/app'

    function MyApp({ Component, pageProps }: AppProps) {
        return <Component {...pageProps} />
    }

    export default MyApp

4. Tailwind CSS の導入

https://tailwindcss.com/docs/guides/nextjs

4-1. 必要moduleのインストール

npm i tailwindcss@latest postcss@latest autoprefixer@latest

4-2. tailwind.config.js, postcss.config.jsの生成

npx tailwindcss init -p

4-3. tailwind.config.jsのpurge設定追加

module.exports = {
    content: [
        "./pages/**/*.{js,ts,jsx,tsx}",
        "./components/**/*.{js,ts,jsx,tsx}",
    ],
    darkMode: false,
    theme: {
        extend: {},
    },
    variants: {
        extend: {},
    },
    plugins: [],
}

4-4. globals.cssの編集

@tailwind base;
@tailwind components;
@tailwind utilities;

5. 動作確認

5-1. index.tsxの編集

const Home: React.FC = () => {
  return (
    <div className="flex justify-center items-center flex-col min-h-screen font-mono">
      Hello Nextjs
    </div>
  )
}
export default Home

npm run dev -> Tailwind CSSが効いているかブラウザで確認

5-2. __tests__フォルダとHome.test.tsxファイルの作成

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

it('Should render hello text', () => {
  render(<Home />)
  expect(screen.getByText('Hello Nextjs')).toBeInTheDocument()
})

npm test -> テストがPASSするか確認

 PASS  __tests__/Home.test.tsx
  ✓ Should render hello text (20 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.728 s, estimated 2 s

nextjs-testing's People

Contributors

gomagoma676 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.