devDependencies에 install
$ yarn add -D cypress
info Direct dependencies
└─ cypress@6.7.1
info All dependencies
├─ @cypress/listr-verbose-renderer@0.4.1
├─ @cypress/request@2.88.5
├─ @cypress/xvfb@1.2.4
$ yarn add -D @cypress/instrument-cra @cypress/code-coverage
info Direct dependencies
├─ @cypress/code-coverage@3.9.2
└─ @cypress/instrument-cra@1.4.0
cypress 관련 파일들이 src 폴더 아래에서 cypress가 작동하도록 pacakge.json과 동일한 위치에 cypress.json 파일을 생성후 아래 내용을 작성함.
{
"baseUrl": "http://localhost:3000",
"integrationFolder": "src/cypress/integration",
"fixturesFolder": "src/cypress/fixtures",
"supportFile": "src/cypress/support/index.js",
"pluginsFile": "src/cypress/plugins/index.js"
}
** 참고 : Cypress 폴더 Structure
// /cypress - to hold all things Cypress i.e. tests, fixtures, page objects, utils, plugins, commands
// |--> /fixtures - JSON files of common data objects needed in tests
// |--> /integration - all of our tests, we would often create sub-folders per page/feature or even by group of tests
// |--> /pages - page objects and each feature would often have its own sub-folder for the pages related to it
// |--> /plugins - custom plugins to run in a Node server, each feature/page would have its own sub-folder for API teardown/setup
// |--> /support - custom commands and types here
// |--> /utils - extra utility files to be used throughout
// |--> /config - environment configuration JSON files to extend/override the base cypress.json file - not all teams did this but it's another approach
import '@cypress/code-coverage/support'
module.exports = (on, config) => {
require('@cypress/code-coverage/task')(on, config)
// add other tasks to be registered here
// IMPORTANT to return the config object
// with the any changed environment variables
return config
}
{
"name": "Using fixtures to represent data",
"email": "hello@cypress.io",
"body": "Fixtures are a great way to mock data for responses to routes"
}
뒤쪽에서 작성할예정
아래내용 추가해주기 (cypress - typescript 사용위한 설정)
"compilerOptions": {
...
**"types": [
"cypress"
]**
},
# cypress
cypress/videos
cypress/screenshots
.nyc_output
integration
파일이름.spec.tsx
(~~~.spec.js
) 의 네이밍 컨벤션 따름// integration/app.spec.tsx
it('테스트코드 작성 중',()=>{
cy.visit('http://localhost:3000/requests/order')
cy.findAllByText('원룸이사').should('exist')
})
해결방법1 (채택)
export {}
을 삽입export {}
it('테스트코드 작성 중',()=>{
cy.visit('http://localhost:3000/requests/order')
cy.findAllByText('원룸이사').should('exist')
})
해결방법2
isolatedModules
"을 true에서 false
로 변경{
"compilerOptions": {
"isolatedModules": false
}
}
scripts 에서 기존 test:jest를 제거하고 아래 처럼 변경
"scripts": {
"start": "react-scripts start",
"build": "react-app-rewired build",
**"cypress:start:app": "cross-env BROWSER=none react-scripts -r @cypress/instrument-cra start",
"cypress:start:wait": "start-server-and-test cypress:start:app http://localhost:3000",
"cypress:open": "npm run cypress:start:wait -- \"cypress open\"",
"cypress:run": "npm run cypress:start:wait -- \"cypress run\"",
"eject": "react-app-rewired eject",**
"lint": "eslint . --ext .tsx --ext .ts --ext .js",
"dev": "BROWSER=none react-app-rewired start"
},
$ npm run cypress:open
$ npm run cypress:run // headless모드로 CI서버에서 실행 / 터미널에서 실행되고 비디오 리코딩됨
커버리지 report html으로 보기
$ open coverage/lcov-report/index.html
이렇게 커버리지 리포트가 나온다.
(참고 : https://docs.cypress.io/guides/tooling/code-coverage#Code-coverage-as-a-guide)
** 필요에 따라서 설치
npm install --save-dev start-server-and-test
npm install cross-env
npm install --save-dev @testing-library/cypress
타입스크립트를 같이 사용하기위해 tsconfig.json 에 아래 내용추가 !
{
"compilerOptions": {
"types": ["cypress", "@testing-library/cypress"]
}
}
아랫줄 추가 in cypress/support/commands.js
`import '@testing-library/cypress/add-commands'`
아래 플러그인도 추천
설치하기
npm i --save -D cypress-react-selector
Update Cypress/support/index.js
에 아래 내용 추가하기
import 'cypress-react-selector'
typescript 사용시
{
"compilerOptions": {
"sourceType": "module",
"types": ["node", "cypress", "cypress-react-selector"]
}
}
문제가 되는 내용이 있다면 알려주세요!
References