본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

테스팅 프레임워크 통합

원문 보기 위키 갱신

테스팅 프레임워크 통합 (Testing frameworks integration)

이 문서는 다양한 테스팅 프레임워크를 Cloudscape 컴포넌트에 통합하는 방법을 설명해요. Vitest(권장), Jest, Mocha를 다루며, 각 프레임워크에서 테스트 설정 파일과 구성 방법, 그리고 흔히 발생하는 오류와 해결책을 제공해요.

출처: 문서

본문

Testing frameworks integration

How to integrate different testing frameworks with Cloudscape components.

다양한 테스팅 프레임워크를 Cloudscape 컴포넌트에 통합하는 방법이에요.

우리 패키지는 이 프레임워크에서 특별한 구성이 필요하지 않아요.

최상의 테스트 경험을 위해 vitest를 react testing library와 결합할 것을 권장해요. 각각의 의존성을 설치한 후, 예를 들어 tests/setup.js 같은 테스트 설정 파일을 만드세요:

import { expect, afterEach } from 'vitest';
import { cleanup } from '@testing-library/react';

afterEach(() => {
  cleanup();
});

vite.config.js 를 업데이트하세요:

export default defineConfig({
  // ... your other configuration ...
  test: {
    environment: "jsdom",
    setupFiles: ["./test/setup.js"],
  },
});

Common errors

우리 패키지에서 "SyntaxError: Cannot use import statement outside a module" 오류가 발생한다면, 중간 의존성에 구성 오류가 있을 가능성이 높아요. 더 자세한 지침은 이 vitest 이슈와 다른 유사 이슈를 확인하세요.

Using Jest

Preset configuration

테스트 호환성을 보장하려면 Jest로 우리 컴포넌트를 테스트할 때 우리의 구성 프리셋을 사용해야 해요. 예를 들어 이 프리셋은 테스트에서 발생할 수 있는 SyntaxError: Unexpected token 'export' 를 고쳐 줘요.

통합 지침은 패키지 readme를 참고하세요.

Snapshot testing

설계상 우리 컴포넌트는 안정적인 스냅샷을 만들기 위한 것이 아니에요. 우리가 계속해서 새 기능과 개선을 추가하기 때문이에요. 예상치 못한 변경으로부터 스냅샷을 보호하려면 컴포넌트 모듈을 모킹(mocking)해서 테스트를 격리하세요.

If you use main import path

// in a file with snapshot tests
jest.mock('@cloudscape-design/components', () => {
  const Components = jest.genMockFromModule('@cloudscape-design/components');
  for (const componentName of Object.keys(Components)) {
    Components[componentName] = componentName;
  }
  return Components;
})

If you use individual component imports

component-mocks.js 파일을 만드세요:

// This has to be a separate file because Jest does not support for-loops in the mocks:
// https://github.com/facebook/jest/issues/11063
import kebabCase from 'lodash/kebabCase';
const Components = jest.requireActual('@cloudscape-design/components');
for (const mockComponentName of Object.keys(Components)) {
  jest.mock(`@cloudscape-design/components/${kebabCase(mockComponentName).replace('s-3', 's3')}`, () => mockComponentName);
}

테스트 파일에서 이를 가져오세요:

// this line must be above the source code import
import './component-mocks';
import YourComponent from './your-code';

Using Mocha

Configure require hooks

우리 컴포넌트는 ES-module 형식으로만 제공되므로 몇 가지 사전 설정이 필요해요. setup.js 파일을 만들고 --require 옵션을 사용해서 Mocha에 로드하세요:

require('ignore-styles');
require('@babel/register')({
  only: [/node_modules\/@cloudscape-design\//],
  ignore: [/test-utils/],
  plugins: [require.resolve('@babel/plugin-transform-modules-commonjs')],
  sourceMap: 'inline',
});

이 파일에 사용되는 모듈이 프로젝트에 설치되지 않았을 수 있어요. 필요하면 설치하세요.

Configure JSDOM

Mocha는 시뮬레이션된 DOM 환경을 제공하지 않아요. 하지만 global-jsdom 모듈로 얻을 수 있어요. 설정 파일에 이 줄을 추가하세요:

require('global-jsdom')(undefined, { pretendToBeVisual: true });

pretendToBeVisual 플래그는 React의 요구사항인 requestAnimationFrame 용 shim을 활성화하는 데 필요해요.

더 알아보기 (Learn more)