Last time, we talked a lot about managing node packages and versions (so check it out if you missed it). This week, you are probably starting the holiday season, or frantically wrapping up work, so we will keep it short. I will just talk about a few tricks.
Scripts in Your package.json
When you open a package.json, you will likely see a script section.
"scripts": {
"start": "react-scripts start",
"test": "react-scripts test",
"test:e2e": "npx playwright test"
}
When you use these scripts, they are quiet literally running the same commands as you would be running in the command line. That is in the above scripts, you can type in the command line npm run test:e2e
and behind the scenes, you are using the playwright package installed in your node_modules folder to execute the command npx playwright test.
Passing Additional Arguments
You can pass additional flags to the command line arguments by ending the npm run command with two dashes: npm run test:e2e — —workers 1
would be the same as running npx playwight test —workers 1.
Using built in shortcuts
npm has a lot of nice shorthands. Instead of typing npm run test
, you can type npm test
or even just npm t.
Similarly, you can just type npm start
instead of npm run start.
Pre and Post scripts
You can automatically run a pre and post script. For instance, suppose you want to run commands before and after your test suite. You could set it up like this
"pretest": "echo 'test is about to start'",
"test": "react-scripts test",
"posttest": "echo 'testing is done'"
and then by just running npm t
you can execute all three commands in order.
Wrap Up
So, short and sweet this week, but hopefully you found something helpful.
Thanks for reading and happy holidays,
Stephen