GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - stepanenko/javascript-testing

BranchesTags
Open more actions menu

Latest commit

 

History

33 Commits
33 Commits

Folders and files

NameName
Last commit message
Last commit date
Testing
Testing
 
 
.gitignore
.gitignore
 
 
README.md
README.md
 
 

Repository files navigation

Testing JavaScript

Testing with JEST

  • Run a specific test or test file

npm test src/views/RegistrationForm.test.tsx (.test.ts can be omitted)

Using npx + react-scripts:

npx react-scripts test src/utils/product/ - run all tests inside product folder

npx react-scripts test src/utils/product/price.test.ts - run only tests from price file (.test.ts can be omitted)

If you have Jest installed globally:

jest -f ContactCard - runs only files named ContactCard (gives each test details)

jest -f service/api - runs all tests in api folder (no test details)

-f - can be omitted

jest -t "Contact Card" - runs ALL tests but skips them except those matching name after -t

-t means --testNamePattern

check if this works: jest -t "Contact Card" "ContactCard" ("Contact Card" - test name, "ContactCard" - test file name)

If you have an it/test inside of a describe block, you can run:

jest -t [describe] [it/test]

To skip some tests in your file put only, e.g:

test.only("should return...", () => {});

it.only("should return...", () => {});

describe.only("Profile", () => { ... });

To skip a test add an x before it/test or describe:

xdescribe("Profile", () => {});

xit("should return...", () => {});

TO VERIFY: try putting skip after it/test or describe: it.skip("should ...", () => {});

More on that here: https://stackoverflow.com/questions/42827054/how-do-i-run-a-single-test-using-jest

  • Debugging tests

You can use the debug method, accessible from the screen object, to log the current HTML output of components:

it("displays contact card", () => {
   render(<ContactCard />);
   screen.debug();
});
  • Coverage report

To generate a coverage report for a specific file run:

npx react-scripts test src/hooks/useCounter.test.ts --coverage --collectCoverageFrom=src/hooks/useCounter.ts

This will also work:

npx react-scripts test useCounter.test.ts --coverage --collectCoverageFrom=src/hooks/useCounter.ts

Then open report at: my-project/coverage/lcov-report/index.html. Note that the path might be different in your project.

If your test file runs but coverage shows 0% check if the file extension was put correctly, e.g. *.tsx/*.ts/*.jsx

  • Library react-app-rewired examples:

npx react-app-rewired test -t "Contact Card" - bad as it runs and skips all except tests matching "Contact Card"

npx react-app-rewired test -f ContactCard - good as it runs only files named ContactCard, and gives each test details

npx react-app-rewired test -f service/api - runs all tests in api folder, gives no test details

-f stands for file name

To generate and log instanbul coverage html report:

npx react-app-rewired test --coverage -f ContactCard

About

No description, website, or topics provided.

Resources

Readme
Activity

Stars

1 star

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.