Skip to content

Instantly share code, notes, and snippets.

@tatsuyasusukida
Last active May 12, 2022 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tatsuyasusukida/4e90436db3bf48ab6e840c098c701ea3 to your computer and use it in GitHub Desktop.
Save tatsuyasusukida/4e90436db3bf48ab6e840c098c701ea3 to your computer and use it in GitHub Desktop.
πŸ™†β€β™‚οΈ How to automate unit testing with Node.js using only standard library [video version available]

πŸ™†β€β™‚οΈ How to automate unit testing with Node.js using only standard library [video version available]

How to automate unit testing with Node.js using only standard library

About this article

This article describes you how to automate unit tests using only the Node.js standard library assert.

Procedure

The procedure is as follows:

  1. Preparing for writing source code
  2. Writing source code
  3. Preparing for writing test code
  4. Writing test code
  5. Operation check

Preparing for writing source code

Execute the following command in the terminal to prepare for coding.

mkdir nodejs-test-unit
cd nodejs-test-unit
touch MyMath.js

Writing source code

MyMath.js

Open MyMath.js in an editor and enter the following content.

Click to go to MyMath.js

Preparing for writing test code

Execute the following command in the terminal to prepare for coding.

touch MyMathTest.js

Writing test code

MyMathTest.js

Open MyMathTest.js in an editor and enter the following content.

Click to go to MyMathTest.js

The points are as follows:

  1. Import the source code to be tested.
  2. The main function lists the test cases to be executed.
  3. Run the test cases in sequence using the for loop.
  4. If no exception occurs, OK is output.
  5. If an exception occurs, an error object is output in addition to NG.
  6. In the test case, first define 4 variables; the arguments n1 and n2 to be passed to the test target, the actual result, and the expected result.
  7. Then call the assert.strictEqual function.

Operation check

Run the following command in the terminal to verify that addTest succeeds and alwaysFailTest fails.

node MyMathTest.js

The execution result of the above command is as follows:

OK addTest
NG alwaysFailTest
AssertionError [ERR_ASSERTION]: alwaysFail
    at alwaysFailTest (/Users/susukida/workspace/tech-blog/articles/nodejs-test-unit/MyMathTest.js:39:10)
    at main (/Users/susukida/workspace/tech-blog/articles/nodejs-test-unit/MyMathTest.js:17:15) {
  generatedMessage: false,
  code: 'ERR_ASSERTION',
  actual: undefined,
  expected: undefined,
  operator: 'fail'
}

Conclusion

Mocha is famous for unit testing in Node.js. Mocha uses the it function to explain what test cases are doing in English, but I find it difficult for non-native English speakers like me to do so. If your app is small and you don't need to use Mocha, the method presented in this article will suffice. However, as your app grows in size, it becomes increasingly difficult to maintain, so it's better to use Mocha.

In unit tests, it's annoying if the test target contains external dependencies such as accessing the database. In such a case, it is better to cut out only the functional part (the part where the output is determined according to the input only) and perform the unit test, and leave the rest to the API test and UI test. Ideally, you should write source code that is easy to unit test, but you risk sacrificing readability and maintainability. I think it's important to balance just one thing without overweight.

I develop web applications based on the three basics of unit testing, API testing, and UI testing, but I have never systematically learned testing. If you have any recommended books or websites about testing, we would appreciate your guidance and comments. Other comments are welcome. Thank you for reading!

Related articles

function add (n1, n2) {
return n1 + n2
}
module.exports.add = add
const assert = require('assert')
const {add} = require('./MyMath') // <1>
if (require.main === module) {
main()
}
async function main () {
try {
const tests = [ // <2>
addTest,
alwaysFailTest,
]
for (const test of tests) { // <3>
try {
await test()
console.info(`OK ${test.name}`) // <4>
} catch (err) {
console.info(`NG ${test.name}`) // <5>
console.error(err)
}
}
} catch (err) {
console.error(err)
}
}
async function addTest () {
const n1 = 1 // <6>
const n2 = 2
const actual = add(n1, n2)
const expected = 3
assert.strictEqual(actual, expected) // <7>
}
async function alwaysFailTest () {
assert.fail('alwaysFail')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment