Exercise: Game Night Project Setup

Take a working but messy Game Night scoreboard and set it up like a professional project, with npm scripts, ESLint, Prettier and a .gitignore, until npm run lint, npm run format:check and the tests all pass.

Journeyman · JavaScript · Node.js · Software Development · Updated

By

Every Saturday, four friends play game night, and one of them wrote a small program to keep score. It works: it adds up every round and names a winner. But it's messy. The quotes don't match, the indents wander, and a reviewer would flag a few lines right away. In this exercise you set the project up the way a professional team would, with npm scripts, ESLint, Prettier and a .gitignore, and clean the code up without changing what it does. Tests check every step.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-javascript-game-night-project-setup, also linked from the Exercise box beside the lesson. Clone it. In a terminal:

git clone https://github.com/ask-codey/exercise-javascript-game-night-project-setup.git

Git makes a folder, exercise-javascript-game-night-project-setup, with the exercise code inside.

Then open the folder in VS Code.

The folder holds:

  • scores.js: the scoreboard's functions (totalFor, standings, roundWinner and winner) and the night's four rounds. It works, but ESLint will find problems in it. You fix them.
  • main.js: prints the scoreboard. It works, but it needs formatting.
  • package.json and .gitignore: you finish both.
  • scores.test.js: checks that the scoreboard still works. It passes now, and it must still pass at the end.
  • setup.test.js: checks your setup, one step at a time. You don't need to change either test file.

What to do

Here's package.json as it comes. It names the project and makes its files modules, and that's all:

{
  "name": "game-night-scoreboard",
  "version": "1.0.0",
  "description": "A scoreboard for game night: the players, the rounds and the winner.",
  "private": true,
  "type": "module"
}

The README in the folder has every step in detail. In short:

  1. Tell Git to ignore node_modules, in .gitignore.
  2. Install ESLint, @eslint/js, globals and Prettier as devDependencies, at the exact versions the README gives, with one npm install command.
  3. Add four npm scripts: test (node --test), lint (eslint .), format (prettier --write .) and format:check (prettier --check .).
  4. Make eslint.config.js, with ESLint's recommended rules, the globals for Node, and the rules eqeqeq and prefer-const set to "error".
  5. Fix every problem ESLint reports, in the code itself.
  6. Format every file with Prettier.

Check your work

In VS Code's terminal, in the project folder:

node --test

At first the 5 scoreboard tests pass and the 6 setup tests fail. That's expected: each step you finish turns a setup test green. A failing test's message says what's missing, for example Add the script "lint": "eslint .", or Run npm install first.

Once your scripts are in, npm test runs the same tests, and npm run lint and npm run format:check show you exactly what ESLint and Prettier still want.

Hints

  • Start with the README's install command. Copy it whole: it installs all four tools at once, with --save-dev and --save-exact.
  • Your config is close to the lesson's. Clean Code with ESLint and Prettier shows a complete eslint.config.js for a web page. This project runs in Node, and it has no dist folder to skip.
  • Read each ESLint line from right to left. The rule's name at the end says what kind of fix it wants. Clean Code with ESLint and Prettier has a table of the three you'll meet.
  • Fix, don't silence. A rule switched off or an eslint-disable comment makes the warning go away, but the mistake stays, and the tests check for both.
  • JSON is strict. If a script won't run, look in package.json for a missing comma between scripts, or a comma after the last one.

When you're done

Every test is green:

ℹ tests 11
ℹ suites 0
ℹ pass 11
ℹ fail 0

npm run format:check ends with All matched files use Prettier code style!, npm run lint prints no problems, and node main.js prints the same scoreboard it printed before you started:

Game Night Scoreboard

Trivia: won by Maya
Charades: won by Jordan
Word Scramble: won by Priya
Card Race: won by Jordan

1. Jordan: 27 points
2. Maya: 26 points
3. Leo: 26 points
4. Priya: 25 points

Winner: Jordan

Look back at the three kinds of problem ESLint found. Which of them could have caused a real bug one day, and which only made the code harder to read?

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. You don't need Git for this: you upload your files in the browser, as the lesson Put Your Code on GitHub (Module 1) shows screen by screen.

  1. On GitHub, click +, then New repository. (No account yet? Sign up at github.com: it's free.) Name it (for example exercise-javascript-game-night-project-setup), choose Public, turn Add README on so the upload button appears, and click Create repository. Your folder's own README replaces GitHub's when you upload it.

  2. Choose Add file, then Upload files. In your exercise folder, select everything (Ctrl+A, or Cmd+A on a Mac), then Ctrl+click (Cmd+click on a Mac) the node_modules folder to leave it out, and drag the rest onto the upload area. Drag what's inside the folder, not the folder itself, so package.json lands at the top of your repo.

    Why leave out node_modules? It isn't your work: it's the more than a thousand files npm install downloaded for the tests, far more than GitHub's upload takes at once, and npm install makes it again whenever it's needed.

  3. Under Commit changes, type a short note, such as My solution, and choose Commit changes.

  4. Paste your repo's address, such as https://github.com/your-name/exercise-javascript-game-night-project-setup, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Upload the changed files again (each one replaces the file with the same name), commit, and submit again: your latest submission is the one that counts.

So what

This is the setup a team expects before it reads a line of your code. Every project you start from here on can begin the same way: package.json, a .gitignore, ESLint, Prettier and a test script.

Key terms

Related