Giter Site home page Giter Site logo

test's Introduction

test

Sure, here is a simple README file for a testing tutorial for adding two numbers and testing it with Jest framework for GitHub:

Adding Two Numbers and Testing with Jest

This tutorial demonstrates how to write tests for a simple function that adds two numbers using Jest, a JavaScript testing framework.

Setting up the Project

  1. Create a new directory for your project and initialize it as a npm package using the following command:
npm init -y
  1. Install Jest using the following command:
npm install --save-dev jest

Writing the Function to Add Two Numbers

Create a file named sum.js and add the following code:

function sum(a, b) {
  return a + b;
}

module.exports = sum;

This function simply adds two numbers and returns the result.

Writing Tests for the Function

Create a file named sum.test.js and add the following code:

const sum = require('./sum');

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

test('adds 3 + 4 to equal 7', () => {
  expect(sum(3, 4)).toBe(7);
});

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

These tests cover different scenarios for adding two numbers, including positive, negative, and mixed numbers.

Running the Tests

To run the tests, open your terminal and navigate to the project directory. Then, run the following command:

npm test

Jest will execute the tests and report the results. If all tests pass, you should see the following output:

PASS ./sum.test.js
  ✓ adds 1 + 2 to equal 3 (2ms)
  ✓ adds 3 + 4 to equal 7 (1ms)
  ✓ adds negative numbers -1 + -2 to equal -3 (1ms)

Congratulations! You have successfully written tests for the function using Jest.

test's People

Contributors

rafaelo8 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.