Coder Social home page Coder Social logo

course-jest-freecodecamp's Introduction

course-jest-freecodecamp

After installing Jest, you have to update your script commands to use it.

  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview",
    "test": "jest"
  },

What's an unit test?

It's the proccess of testing a small piece of code, such as basic functions and basic classes. We do that in order to check small errors that can be produced by these small functions and classes.

Useful basic info

Whichever file you call <fileName>.text.js it will be automatically interpreted as a test file by JavaScript.

Syntax

The basic syntax is:

test('description', testFunction => {})

The complete syntax of a basic test is:

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

Quick disclaimer about exports/imports

For a unknown reason I already despise, there's a couple linked syntax configs that you should know when dealing with exports and imports in vanilla JS:

  1. Create your function inside a .js file without exporting it.
  2. Export it using module.exports = <functionName>
  3. Import your function at the top of your test.js file using const <functionName> = require("<pathToFile")

Vite's linter will warn you that there's a better way to write ES module imports. Ignore it if you don't know what you're dealing with. Trust me.

The main expect() methods

  • .toBe() is used to expect primitive values (e.g. numbers, strings, booleans)
  • toEqual() compares values of objects of arrays (i.e.: non-primitive values)
  • toBeFalsy() checks values expected to be falsy. Simple.
  • toBeTruthy(), same thing but on the contrary
  • toThrow(), useful when a function is built to throw errors in given scenarios

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.