A collection of JavaScript practice exercises with automated tests using Jest.
Before you begin, make sure you have the following installed on your computer:
- Node.js (version 14 or higher)
- Download from nodejs.org
- To check if you have it installed, run:
node --version
- npm (comes with Node.js)
- To check if you have it installed, run:
npm --version
- To check if you have it installed, run:
First, you need to install the testing framework (Jest) and other dependencies. Open your terminal in this project directory and run:
npm installThis will download all the necessary packages into a node_modules folder.
Each exercise is in its own folder under javascript-exercises/:
javascript-exercises/
├── 01_helloWorld/
│ ├── helloWorld.js # Your code goes here
│ ├── helloWorld.spec.js # Tests for this exercise
│ └── solution/ # Reference solution (don't peek!)
├── 02_addNumbers/
│ ├── addNumbers.js
│ ├── addNumbers.spec.js
│ └── solution/
└── ...
To run all tests in the project:
npm testThis will run every test file and show you which exercises are passing and which are failing.
To run tests for just one exercise, specify the path:
npm test 01_helloWorldOr be more specific:
npm test javascript-exercises/01_helloWorld/helloWorld.spec.jsWatch mode automatically re-runs tests when you save your files:
npm run test:watchTo watch a specific exercise:
npm run test:watch -- 01_helloWorldPress q to quit watch mode.
For more detailed test output:
npm run test:verboseTo see how much of your code is covered by tests:
npm run test:coverage- Choose an exercise - Start with
01_helloWorldand work your way up - Read the test file - Open the
.spec.jsfile to understand what's expected - Write your code - Edit the corresponding
.jsfile - Run the tests - Use
npm test <exercise-name>to check your work - Iterate - Keep refining your code until all tests pass
# Start working on the helloWorld exercise
cd javascript-exercises/01_helloWorld
# Open the test file to see what's expected
cat helloWorld.spec.js
# Edit your solution
# (open helloWorld.js in your editor)
# Run the tests
npm test 01_helloWorld
# Keep editing and testing until all tests pass!When you run tests, you'll see output like this:
PASS javascript-exercises/01_helloWorld/helloWorld.spec.js
Hello World
✓ says "Hello, World!" (2 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
- ✓ means the test passed
- ✗ means the test failed (along with details about what went wrong)
If a test fails, Jest will show you:
- What was expected
- What your code actually returned
- The difference between them
If you see this error, make sure you:
- Ran
npm installfirst - Are in the correct directory
- Have the correct file paths in your
require()statements
Make sure:
- You're in the project root directory
- You have
node_modulesfolder (runnpm installif not) - Your test file ends with
.spec.js
This means Jest isn't installed. Run:
npm install- Read the tests first - They tell you exactly what your function should do
- Start simple - Get basic functionality working before handling edge cases
- Use console.log() - Add
console.log()statements to debug your code - Run tests frequently - Don't write too much code before testing
- Check the solution if stuck - But try for at least 15-20 minutes first!
If you're stuck:
- Read the test file carefully - it shows exactly what's expected
- Check the error messages - they often point to the problem
- Try using
console.log()to inspect your variables - Look at the solution files (but only after trying yourself!)
Happy coding!