Introduction to Testing
Apart from writing code in any programming language, It's also very important to write error-free code. Error-free code is critical for keeping the apps and business processes running smoothly.
If we talk about the type of testing, Then there are mainly 3 types of testing.
A. Unit Testing: As the name suggests, this is the testing of the smallest testable unit of your code. For example, testing of functions or modules in your code. You can test for the return type or value of your function, its parameter or the logic your function performs.
B. Integration Testing: Here, you test the interaction between 2 or more functions. That is the testing of how 2 or more units work together.
C. End-to-End Testing: This is the testing of the combination of multiple integrations. It may include testing of an API, which connects to the database and saves/retrieves data. This test comprises of many integrations.
Now I am going to introduce you to the tools mostly used for automated testing. Apart from Nodejs inbuilt tools, there are more popular tools, which also happen to work very well together.
Mocha: A test framework
describe() denotes an arbitrarily nested grouping of test cases (a describe() can contain other describe()s).
it() denotes a single test case.
Both functions take two arguments:
A description that appears in the test report
A callback function
Chai: An assertion library
Assert: assertEqual(1, 1)
BDD (behavior-driven development): expect(1 === 1).to.be.true or expect(1).to.equal(1)
Sinon: A library of test doubles
Sinon is a block of code that replaces some portion of the production code for testing purposes. Test doubles are useful when it’s inconvenient, or even impossible, to run test cases against production code.
Istanbul: A library for testing code coverage
Istanbul is a code quality metric that measures how much of the potentially executable code under test was actually executed when the tests ran (that is, during a single invocation of npm test, as you’ll see shortly).
ESlint: A pluggable linting utility
A linter is a tool that analyzes your code for potential errors, which is sometimes called static code analysis.
Running a linter on your code is called linting, a technique that can be very handy for discovering issues like:
Unused variables or functions
Long source code lines
Poorly formatted comments
Missing documentation comments
Want to know more about the it and sample application to get started? Click here: https://jsonworld.com/demo/testing-nodejs-api-with-mocha-and-chai
In this article, We get brief idea about the topics before we start writing the test cases for the NodeJS API Application. In the next We will set up a project and write tests for the Nodejs API.