webpack react testing

install react testing library

npm install @testing-library/react
npm install jest

package.json script 추가

"scripts": {
  "jest-test": "jest -c jest.config.js --watch"
}

create jest config file, src/jest.config.js

module.export = {
  roots: ['<rootDir>/src'],
  transform: {
    '\\.(js|jsx)?$': 'babel-jest',
  },
  testMatch: ['<rootDir>/src/**/>(*.)test.{js, jsx}'], // finds test
  moduleFileExtensions: ['js', 'jsx', 'json', 'node'],
  testPathIgnorePatterns: ['/node_modules/', '/public/'],
  setupFilesAfterEnv: [
    'jest-dom/extend-expect',
    '@testing-library/react/cleanup-after-each'
  ] // setupFiles before the tests are ran
};

component test example

  • Profile.js
import React from 'react';

const Profile = ({ username, name }) => {
  return (
      <h1>Hi {username} {name}!</h1>
  );
};

export default Profile;
  • Profile.test.js
import React from 'react'
import {
  render,
  cleanup
} from '@testing-library/react'
import Profile from './Profile'

afterEach(cleanup)

describe('This will test MyComponent', () => {
  test('renders message', () => {
    const {getByText} = render( <Profile username="Alejandro" name="Roman" /> )

    // as suggested by Giorgio Polvara a more idiomatic way:
    expect(getByText('Hi Alejandro Roman!'))
  })
})

references

Last Updated: 8/26/2019, 2:19:07 PM