Unit Testing (Javascript)
Installation
Install nvm to handle node and npm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
Initialize your project’s package.json
npm init
… Add scripts and dependencies
npm pkg set 'type'='module' && npm pkg set 'scripts.test'='mocha' && npm install --save-dev mocha chai
Run the test!
npm test
Unit testing sample
Function example src/functions.js
export function helloWorld(){
return "Hello World! xD"
}
Test example test/test.js
import assert from 'assert';
import {helloWorld} from '../src/functions.js';
describe("A simple string comparison", ()=>{
it("String matches", ()=>{
assert.deepEqual(helloWorld(), "Hello World! xD")
})
})
Assumed project structure
myProject/
├── package.json
├── src
│ └── functions.js
└── test
└── test.js