State management – Prepare environment (to do it properly)

To start project we will need to create node project. I assume that you have installed Node.js on your machine. If not you can do it easy with installator on https://nodejs.org/en/.

Now lets create a project. I assume that you are familiar with command line. Open command line and go to your directory in which you will create your project.

In your project directory please run command:

npm init -y

This command will initialise node.js project in your directory. Flag -y will omit prompt questions (this will help you to save few seconds).

To start project lets use Jest engine. (To get deeper knowledge of JEST https://jestjs.io/). In command line please run:

npm i jest

Now open your package.json file. Please change scripts section to "test": "jest". After saving your changes file should be simillar to this one:

{
  "name": "fedojo_com__state_management",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "jest": "^24.9.0"
  }
}

Now in command line when you will run command below:

npm run test

Jest will automatically run test in your project.

Project structure

Now lets create a project structure. In root folder please write:

mkdir src
cd src
touch state.js
mkdir _test_
cd _test_
touch state.test.js

This commands will create folder src then open it and create state.js file. After that we are creating _test_ foleder open it and create state.test.js file. Now open your favourite IDE and open state.test.js file. Inside this file lets write:

test("First test", () => {
  expect(true).toBe(true);
});

Now in command line lets run:

npm run test

In your command line you should see:

PASS  src/_test_/state.test.js
  ✓ First test (3ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.399s
Ran all test suites.

What we have done is simple check if true is equal true just to make sure that JEST is working fine.

VOILA!

Leave a Reply

Your email address will not be published. Required fields are marked *