Giter Club home page Giter Club logo

ibm-cloud-garage-tdd's Introduction

Set up your project

  1. Clone this git repository

    • In your profile > settings > SSH Keys confirm you have set up an SSH Key for Github. If not, then set up SSH access to github. Connecting to GitHub with SSH
  2. Install Node.js (v8.1.4 or above)

  3. From your terminal, run npm init -y

Install Jest

Jest is a JavaScript test framework that we can use for testing both front-end and back-end code.

One of Jest's philosophies is to provide an integrated "zero-configuration" experience for writing tests. Conveniently, this means that it includes everything you need for TDD in a single package (test runner, assertions, matchers, spies, stubs and mocks)

The jest documentation can be found here: https://jestjs.io/docs/en/getting-started

  1. Install jest with npm install --save-dev jest

  2. Modify your package.json file, replacing:

    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    

    with

    "scripts": {
        "test": "jest --verbose",
        "tdd": "jest --verbose --watch --onlyChanged"
    },
    
  3. Run npm test

    This command runs all tests and exits. Typically, we will call this from within the build pipeline.

  4. Run npm run tdd

    This command continuously watches files for changes and rerun tests related to changed files. This is the command we will be using most often during TDD.

Configure pre-commit hooks [Optional]

To prevent bad commits, we can configure pre-commit hooks to ensure that unit tests are run before every commit.

  1. Install husky with npm install --save-dev husky

  2. Modify your package.json file, replacing:

    "scripts": {
        "test": "jest --verbose",
        "tdd": "jest --verbose --watch --onlyChanged"
    },
    
    

    with

    "scripts": {
        "test": "jest --verbose",
        "tdd": "jest --verbose --watch --onlyChanged",
        "precommit": "npm test"
    },
    
    

Configure your IDE [Optional]

IntelliJ

In Preferences:

  1. Under Language & Frameworks > JavaScript, set your JavaScript language version to "ECMAScript 6".

  2. Under Language & Frameworks > JavaScript > Libraries, download and enable both jest and node.

Explore the canary test

Open up server/00-canary/canary.spec.js, and note that:

  1. For jest to find your tests, you need to place your tests in a __tests__ folder, or name your test files with a .spec.js or .test.js extension.

  2. describe creates a block that groups together several related tests in one "test suite".

    You can also nest describe blocks if you have a hierarchy of tests.

  3. it takes two arguments. The first argument describes the test; the second argument is a function that contains the expectations to test.

  4. When you're writing tests, you often need to check that values meet certain conditions. expect gives you access to a number of "matchers" that let you validate different things.

    A good introduction to the available matchers can be found here: https://facebook.github.io/jest/docs/en/using-matchers.html

  5. Together, the descriptions that we use in the describe and it blocks are shown in the test output. The aim is to structure and name our tests in such a way that the test output reads like a "requirements spec".

Let's get started!

Now that you're all set, let's get started with the TDD exercises.

As you go through the TDD exercises, keep in mind the Four Rules of Simple Design, and the Transformation Priority Premise.

  • Passes the tests
  • Reveals intention
  • No duplication
  • Fewest elements
(01) [{} –> nil] no code => return nil

(02) [nil->constant] nil => simple constant

(03) [constant->constant+] simple constant => complex constant

(04) [constant->scalar] complex constant => variable or an argument

(05) [statement->statements] adding more unconditional statements.

(06) [unconditional->if] splitting the execution path

(07) [scalar->array]

(08) [array->container]

(09) [statement->recursion]

(10) [if->while]

(11) [expression->function] replacing an expression with a function or algorithm

(12) [variable->assignment] replacing the value of a variable.
(01) constant => a value

(02) scalar => a local binding, or variable

(03) invocation => calling a function/method

(04) conditional => if/switch/case/cond

(05) while loop => applies to for loops as well

(06) assignment => replacing the value of a variable

ibm-cloud-garage-tdd's People

Contributors

domharries avatar lsteck avatar moberlin8 avatar vadimdez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ibm-cloud-garage-tdd's Issues

Tutorial information is wrong

Hi!
On "Stack Kata", the information for point 3 on correspond to the prime factors kata. I tried to solve the kata by myself but I consider the first two tests achieve the same. Aren't "starts empty" and "starts with stack size of 0" checking the same? Shouldn't expect(stack.length).toBe(0); solve both assertions? Or is this one of your little hacker mind tricks? :-D

npm install --save-dev jest fails

2020-05-08T10_41_47_968Z-debug.log
~/git/ibm-cloud-garage-tdd   master  npm install --save-dev jest
npm WARN deprecated [email protected]: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated [email protected]: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated [email protected]: request has been deprecated, see request/request#3142
npm WARN [email protected] requires a peer of canvas@^2.5.0 but none was installed.
npm WARN [email protected] requires a peer of bufferutil@^4.0.1 but none was installed.
npm WARN [email protected] requires a peer of utf-8-validate@^5.0.2 but none was installed.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/jsdom):
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: EEXIST: file already exists, mkdir '/Users/ozzy/git/ibm-cloud-garage-tdd/node_modules/.staging/jsdom-94824825/lib/jsdom/living/generated'
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: File exists: /Users/ozzy/git/ibm-cloud-garage-tdd/node_modules/.staging/jsdom-94824825/lib/jsdom/living/generated
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/lodash):
npm WARN enoent SKIPPING OPTIONAL DEPENDENCY: ENOENT: no such file or directory, open '/Users/ozzy/git/ibm-cloud-garage-tdd/node_modules/.staging/lodash-c4d8c21d/fp/assignAll.js'

npm ERR! path /Users/ozzy/git/ibm-cloud-garage-tdd/node_modules/.staging/char-regex-b7144f72/index.d.ts
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall open
npm ERR! Error: EACCES: permission denied, open '/Users/ozzy/git/ibm-cloud-garage-tdd/node_modules/.staging/char-regex-b7144f72/index.d.ts'
npm ERR! { Error: EACCES: permission denied, open '/Users/ozzy/git/ibm-cloud-garage-tdd/node_modules/.staging/char-regex-b7144f72/index.d.ts'
npm ERR! cause:
npm ERR! { Error: EACCES: permission denied, open '/Users/ozzy/git/ibm-cloud-garage-tdd/node_modules/.staging/char-regex-b7144f72/index.d.ts'
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'open',
npm ERR! path: '/Users/ozzy/git/ibm-cloud-garage-tdd/node_modules/.staging/char-regex-b7144f72/index.d.ts' },
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'open',
npm ERR! path: '/Users/ozzy/git/ibm-cloud-garage-tdd/node_modules/.staging/char-regex-b7144f72/index.d.ts',
npm ERR! parent: 'ibm-cloud-garage-tdd' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR! /Users/ozzy/.npm/_logs/2020-05-08T10_41_47_968Z-debug.log

then i run sudo npm install --save-dev jest
no errors

Change the Package.json as described in readme.. then run

npm test

[email protected] test /Users/ozzy/git/ibm-cloud-garage-tdd
jest --verbose

/Users/ozzy/git/ibm-cloud-garage-tdd/node_modules/find-up/index.js:29
const foundPath = await runMatcher({...options, cwd: directory});
^^^

SyntaxError: Unexpected token ...
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:533:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at Object. (/Users/ozzy/git/ibm-cloud-garage-tdd/node_modules/pkg-dir/index.js:3:16)

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.