Learn Sails.js - Part 8

von Manuel am 12.10.2025 um 12:00 Uhr

Posting guards is only half the battle. A truly professional crew constantly runs drills to ensure every system is in perfect working order before the storm hits. It’s time to build your safety net.


Part 8: Running Ship Drills (Automated Testing)


The Captain’s Briefing (Background and Theory)

Our ship is complex now. It has a routing system, a view layer, a database interface, authentication guards, and specialist services. If you change a single line of code in the AppraisalService, how can you be certain you didn’t accidentally break the login system? Do you manually click through every single feature of the entire ship after every single change?

That is the path to madness. A professional crew runs drills. An automated test is a drill for your code. It’s a script you write that checks if a piece of your application behaves exactly as you expect. You can run thousands of these drills in seconds, giving you immediate feedback.

The Drill Sergeant: Mocha

Sails.js comes equipped with a battle-hardened testing framework called Mocha right out of the box. Mocha provides a simple structure for organizing your tests:

The Test Environment

Crucially, you never want your drills to interfere with your real cargo. blog.sailscasts.com explains that Sails is configured to use a special test environment when you run your tests. As part of this setup, it’s common practice to use an in-memory or separate test database, ensuring your tests run in a clean, isolated environment and don’t touch your development data. Good tests clean up after themselves, a principle mentioned in the Sails docs for effective testing app.studyraid.com.

The Bootstrap File: Hoist the Sails!

To test any part of Sails, the app itself needs to be running. Sails provides a special file, test/bootstrap.test.js, for this purpose. Before any of your drills are run, this bootstrap file runs sails.lift() to start the app in the test environment. After all your drills are complete, it runs sails.lower() to shut it down cleanly. You don’t need to touch this file, but it’s important to know it’s working for you behind the scenes.

Today, we will write a Unit Test. A unit test focuses on the smallest possible “unit” of code—in our case, a single service function—in complete isolation.


Key Concepts Checklist


Mission Log: Quest - “The Appraiser’s Examination”

We hired a new Appraiser in part 5, but how do we know they are trustworthy? We will write a formal examination (a unit test) to verify that the AppraisalService is, and always will be, calculating taxes correctly.


Mission Debrief (Review & Outcomes)

Flawless execution! The drill was a success. You have now established a fundamental process for ensuring the quality and stability of your ship.

You’ve created a unit test that verifies your business logic, and more importantly, you’ve seen how that test protects you from introducing new bugs down the line. As your application grows, your test suite will become your most trusted advisor, giving you the confidence to refactor code, add new features, and sail your application into new waters without fear of capsizing. You can even integrate tools like Istanbul to measure your test coverage, as noted by Packt packtpub.com.


Rewards & Promotion

Incredible work. As First Mate, you are now second-in-command. You have mastered the ship’s internal workings, its crew, its defenses, and its procedures. There is only one task left: to prepare this vessel for its maiden voyage into the wider world.