Coder Social home page Coder Social logo

unittesting's Introduction

Unit Testing with Jest

Why unit test? --saves time --Creates reliable software --Gives flexiblity to developers -Refactoring -Collaborating -Profiling --Peace of mind

Basic tutorial:

  1. Create a function:

let sum = (a, b) => { return a + b }

  1. Create a test for the created function:

test('Should sum two arguments', () => { const num = sum(2, 2)

  expect(num).toBe(4)

})

The sum function will be passed the arguments we specified. And the results will be stored in the constant 'num'. Then num will be checked aginst the toBe property which we passed a value of 4.

If they match, then the test is passed, otherwise it will fail.

JEST: "Jest uses "matchers" to let you test values in different ways." List of all matches available to the expect object: https://jestjs.io/docs/en/expect

TESTING ASYNC CODE (https://jestjs.io/docs/en/asynchronous) Jest needs to know when a async function is finished before it can move onto another test.

  1. Callbacks

Example per docs:

test('the data is peanut butter', done => { function callback(data) { expect(data).toBe('peanut butter'); done(); }

fetchData(callback); });

  1. Promises "Return a promise from your test, and Jest will wait for that promise to resolve. If the promise is rejected, the test will automatically fail."

Exampled per docs:

test('the data is peanut butter', () => { return fetchData().then(data => { expect(data).toBe('peanut butter'); }); });

  1. .resolves / .rejects

test('the data is peanut butter', () => { return expect(fetchData()).resolves.toBe('peanut butter'); });

test('the fetch fails with an error', () => { return expect(fetchData()).rejects.toMatch('error'); });

  1. Async/Await Probably the best way to test for async functions is by using async/await as the syntax is 'nicer'.

Below is an example of how to incorporate this method:

test('the data is peanut butter', async () => { const data = await fetchData(); expect(data).toBe('peanut butter'); });

test('the fetch fails with an error', async () => { expect.assertions(1); try { await fetchData(); } catch (e) { expect(e).toMatch('error'); } });

Furthermore, you can use .resolves and .rejects with async/await:

test('the data is peanut butter', async () => { await expect(fetchData()).resolves.toBe('peanut butter'); });

test('the fetch fails with an error', async () => { await expect(fetchData()).rejects.toThrow('error'); });

unittesting's People

Contributors

rizahkhan avatar

Watchers

James Cloos avatar  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.