CoderKungfu / tdd-lab-nodejs-jest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NodeJS - Bootstrapping Jest Test Suite

Open in Gitpod

Here's instructions on installing Jest and bootstrapping for ES6 module compilation for Node.

Note: This guide assumes you have NodeJS installed.

  1. Create a new project folder for the code:

    (Assuming project name is new_kata)

    mkdir new_kata
    cd new_kata
  2. In the new project folder, create package.json

    yarn init

    or

    npm init
  3. Add packages

    yarn add --dev jest @babel/core @babel/preset-env

    or

    npm install --save-dev jest @babel/core @babel/preset-env
  4. Add test running script in package.json

    "scripts": {
      "test": "jest",
      "test:watch": "jest --watch"
    }
  5. Create a .babelrc with these content for a smart preset babel configuration

    {
    	"presets": [
    		["@babel/preset-env",
    			{
    				"targets": {
    					"node": "10"
    				}
    			}
    		]
    	]
    }
  6. Create a code (sum.js) file

    const sum = (a, b) => a + b
    export default sum
  7. Create a test (sum.test.js) file

    import sum from './sum'
    
    test('adds 1 + 2 to equal 3', () => {
      expect(sum(1, 2)).toBe(3);
    });
  8. You can run the test:

    yarn test

    or

    npm test

    You should see this:

    yarn run v1.9.4
    $ jest
     PASS  ./sum.test.js
      ✓ adds 1 + 2 to equal 3 (3ms)
    
    Test Suites: 1 passed, 1 total
    Tests:       1 passed, 1 total
    Snapshots:   0 total
    Time:        1.286s
    Ran all test suites.
    ✨  Done in 2.39s.
  9. Read up more about the types of test matchers available in Jest: https://jestjs.io/docs/en/using-matchers

About


Languages

Language:JavaScript 60.0%Language:Dockerfile 40.0%