Notes Used to Craft this Lesson
A common naming convention for tests in JavaScript is to name the test with a brief sentence outlining:
What is being tested
The scenario or condition
And, the expected outcome.
Often, you can start this silently with "it should" followed by your test name. For example, let's make a new spec called "state management."
node ace make:test state_management --suite=unitCopied!
Inside the group, let's add a counter variable and set it to 0.
test.group("State management", () => { let counter = 0; test("example test", async ({ assert }) => {}); });Copied!
What we'll be doing here is incrementing this counter as the tests run to showcase the state. So, for our first test, it should increment the counter by one. If we drop "it should", we have a perfect test name.
test("increment the counter by one", async ({ assert }) => { counter++; assert.equal(counter, 1); });Copied!
Let's add another to increment it again, for which "it should" increment the counter by one again. And, we'll assert that it should be two now.
test("increment the counter by one again", async ({ assert }) => { counter++; assert.equal(counter, 2); });Copied!
Perfect! Lastly, I want to note that, as good practice, it's generally recommended that each test be uniquely named. This helps reduce confusion and makes the tests easier to find.
Additionally, it's typically considered bad practice to have tests rely on other tests, as we're doing here. We'll take a look at a solution for this in the next lesson.