Strapi has been there around for quite some time now with a lot of organisations using it as their go to production framework for building cool apps using React , angular etc.
However the more you search but less you find is about unit testing in strapi. There are lot of blogs that are termed as Unit testing in strapi however they are technically doing end to end testing using a sqlite database making reals hit to apis and generating a real output.
The goal of this blog post is to shed some light on building unit tests for strapi, testing custom controller methods and services using unit tests.
Getting started
First and foremost create a sample strapi project. Below is the link for the same.
- Strapi Docs - Strapi Getting Started Documentation.
Install Dependencies
Run the below command to install jest dependency
npm install jest supertest --save-dev
Set-up Run comand
Add the below command in package.json file. You may tweak the start command SET NODE_ENV==test based on your OS. This command is specific to windows environment.
"SET NODE_ENV==test & jest --runInBand --detectOpenHandles --testPathPattern=unit-tests/"
Create Custom Controller Method
Add the below command in package.json file. You may tweak the start command SET NODE_ENV==test based on your OS. This command is specific to windows environment.
Create Custom Service Method
Add the below command in package.json file. You may tweak the start command SET NODE_ENV==test based on your OS. This command is specific to windows environment.
Create Strapi Unit Tests For Controller
Add the below command in package.json file. You may tweak the start command SET NODE_ENV==test based on your OS. This command is specific to windows environment.
Create Strapi Unit tests For Services
Add the below command in package.json file. You may tweak the start command SET NODE_ENV==test based on your OS. This command is specific to windows environment.
Run Unit Tests
Add the below command in package.json file. You may tweak the start command SET NODE_ENV==test based on your OS. This command is specific to windows environment.
Github Repository
First and foremost create a sample strapi project. Below is the link for the same.
- Strapi Docs - Strapi Getting Started Documentation.
Top comments (0)