Answers for "javascript unit test"

0

javascript unit test

// JEST UNIT TESTING

// INSTALL
`yarn add --dev jest`

// FUNCTION TO UNIT TEST
function sum(a, b) {
  return a + b;
}
module.exports = sum;
// sum.js

// JEST SUM TEST
const sum = require('./sum');

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});
// sum.test.js

// ADD TEST SCRIPT TO PACKAGE.JSON
{
  "scripts": {
    "test": "jest"
  }
}
// package.json

`yarn test`
// PASS  ./sum.test.js
// ✓ adds 1 + 2 to equal 3 (5ms)
Posted by: Guest on July-17-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language