In the previous chapter, Troubleshooting, we learned how to fix things when they break. We acted like detectives, reading error messages to find the problem.
But wouldn't it be better if we could catch problems before they break?
This chapter introduces Testing and Validation. This is the process of proving that your code works correctly before you share it with others. It is the difference between hoping a bridge holds your weight and driving a heavy truck over it to verify it is safe.
Imagine you are writing a quiz question for the app. You type the question, save the file, and send it to the project.
Later, a student tries to take the quiz, but the "Submit" button doesn't work because you forgot a comma in the code. The student is frustrated, and the project looks broken.
The Goal: You have modified the Quiz Application (perhaps adding a new translation or fixing a typo). You want to be 100% sure that your change won't crash the website.
The Solution: We run a series of Tests.
Testing in this project is divided into two categories: Manual Validation (checking with your eyes) and Automated Validation (checking with robots).
This is for the text content (.md files) and notebooks (.ipynb). Computers are bad at judging if a sentence makes sense. Humans are good at it.
We introduced this in Code Style Guidelines. In the context of Testing, the Linter is the first line of defense. If the Linter fails, the test fails.
This is the ultimate test for the Quiz App. The "Build" process tries to compress the entire application into a package ready for the internet. If there is a serious error deep in the logic, the Build will fail.
Let's walk through the steps to solve our "Broken Comma" use case using the tools in the quiz-app folder.
Before running code, just look at what you wrote.
Now, let's ask the computer to check our syntax. Open your terminal in the quiz-app folder.
# 1. Navigate to the app folder
cd quiz-app
# 2. Run the "Grammar Check"
npm run lint
Expected Output:
If everything is good, you see: No lint errors found!
If you missed a comma, it will shout: Error: Parsing error: Unexpected token.
This is the stress test. We are asking the computer: "Can you turn this code into a working website?"
# Run the build command
npm run build
What happens?
The computer reads every single file, optimizes the images, and creates a dist (distribution) folder.
Expected Output:
File Size Gzipped
dist/js/chunk-vendors.28s9.js 120.40 KiB 40.20 KiB
dist/js/app.49a2.js 15.30 KiB 5.10 KiB
DONE Build complete.
Explanation: If you see "Build complete," your code is safe! You have passed the test.
What actually happens when you run npm run build? It triggers a pipeline of checks.
package.json Scripts
How does the terminal know what run lint or run build means? These commands are defined in a file called package.json inside the quiz-app folder.
// Inside quiz-app/package.json
{
"name": "quiz-app",
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
}
}
Explanation: When you type npm run lint, npm looks at this list, finds "lint", and actually runs vue-cli-service lint. It is a shortcut system.
You might forget to run these tests on your laptop. But the project maintainers (see Contribution Guidelines) have a backup plan.
Every time you send a Pull Request, a robot (GitHub Action) runs these exact commands in the cloud.
# Simplified .github/workflows/main.yml
name: Build and Test Quiz App
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Install dependencies
run: npm install
- name: Run Build
run: npm run build
Explanation: This YAML file is the robot's instruction manual. It says: "When code is pushed, install the tools, and then try to build the app." If npm run build fails here, your Pull Request gets a red "X".
In this chapter, we learned how to ensure quality through Testing and Validation:
npm run lint): Computers checking syntax and grammar.npm run build): Computers verifying the app works as a whole.package.json and GitHub Actions run these tests for us.Now that we have validated our lessons and quiz app, we are ready to expand our reach. The world speaks many languages, and we want everyone to learn Machine Learning.
Next Chapter: Translation Workflow
Generated by Code IQ