Giter Club home page Giter Club logo

introducing-jest's Introduction

Introducing Jest

1. Prerequisites

$ node -v
v10.16.0
$ npm -v
6.9.0

2. Creating a Node Project

$ mkdir tdd-roman-numerals-kata
$ cd tdd-roman-numerals-kata

2.1. Create package.json with npm init.

Press ↩️ at each prompt to accept the default response

$ npm init
...
package name: (tdd-roman-numerals-kata) 
version: (1.0.0) 
description: 
entry point: (index.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
...
Is this OK? (yes)

Add "private": true to package.json to npm will refuse to publish it (this is a way to prevent accidental publication of private repositorie)

2.2. Add Jest as Dependency

Use npm to install the jest package

$ npm install --save-dev [email protected]
npm notice created a lockfile as package-lock.json. You should commit this file.
+ [email protected]
added 479 packages from 344 contributors and audited 873786 packages in 12.856s
found 0 vulnerabilities

3. Running Package Binaries with npx

npm is a package manager, npx is a package runner.

$ npx jest
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In ~/tdd-roman-numerals-kata
  2 files checked.
  testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 0 matches
  testPathIgnorePatterns: /node_modules/ - 2 matches
  testRegex:  - 0 matches
Pattern:  - 0 matches
$ npx jest --version
24.8.0

4. Running Project Scripts with npm

The best way to do that is to list those commands in the scripts section of your project’s package.json.

{
  "name": "tdd-roman-numerals-kata",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "jest": "^24.8.0"
  }
}
$ npm test
> [email protected] test ~/tdd-roman-numerals-kata
> jest

No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In ~/tdd-roman-numerals-kata
  2 files checked.
  testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 0 matches
  testPathIgnorePatterns: /node_modules/ - 2 matches
  testRegex:  - 0 matches
Pattern:  - 0 matches
npm ERR! Test failed.  See above for more details.

5. Writing a test

Create a file called src/tests/romanNumerals.test.js :

const arabicToRoman = arabic => `I`;

describe('arabicToRoman()', () => {
  it('convert number one', () => {
    expect(arabicToRoman(1)).toBe('I');
  });
});

Run all tests with:

$ npm test
...
 PASS  src/tests/romanNumerals.test.js
  arabicToRoman()
    ✓ convert number one (2ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.624s, estimated 1s
Ran all test suites.

6. Test-Driven Development

6.1. Starting from Failure

Create src/romanNumerals.js file:

module.exports = (number) => 'I';

And chage the src/tests/romanNumerals.test.js:

const arabicToRoman = require('../romanNumerals');

describe('arabicToRoman()', () => {
  it('convert number one', () => {
    expect(arabicToRoman(1)).toBe('I');
  });
  it('convert number two', () => {
    expect(arabicToRoman(2)).toBe('II');
  });
});

6.2. Running Jest Tests Automatically

$ npx jest --watchAll

6.3 Measuring Test Coverage

$ npm run test -- --coverage --coverageReporters=text

introducing-jest's People

Contributors

rydnr avatar

Watchers

 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.