Test Driven Development with ChatGPT

Andrii Kucherenko

https://t.me/mathrandomcommunity

https://mathrandom.com

YouTube: @mathrandomjavascriptcommunity

https://codeberry.club/

TDD

  • Navigation map

  • Test first

  • Assert first

  • Fail first

Navigation Map

  • Decomposition of the task
  • Less technical details
  • Clear and ready to share with ​other teams members
  • Can be changed during development

Test First

// calculator.spec.ts

test('should summarize two numbers', 
    (t: ExecutionContext) => {

    }
);

Assert First

// calculator.spec.ts

test('should summarize two numbers', 
    (t: ExecutionContext) => {
        t.is(sum(1, 2), 3);
    }
);

Fail First

➜  ava-ts git:(master) ✗ npm test

> ava-ts@1.0.0 test /Users/andrii_kucherenko/Workspace/lab/tdd-training/ava-ts
> ava



  1 uncaught exception

  Uncaught exception in __tests__/caclulator.spec.ts

  __tests__/caclulator.spec.ts(4,8): error TS2304: Cannot find name 'sum'.

npm ERR! Test failed.  See above for more details.

Fail First

➜  ava-ts git:(master) ✗ npm test

> ava-ts@1.0.0 test /Users/andrii_kucherenko/Workspace/lab/tdd-training/ava-ts
> ava



  1 uncaught exception

  Uncaught exception in __tests__/caclulator.spec.ts

  __tests__/caclulator.spec.ts(5,20): error TS2345: Argument of type '3' is not assignable to parameter of type 'void'.

Fail First

➜  ava-ts git:(master) ✗ npm test

> ava-ts@1.0.0 test /Users/andrii_kucherenko/Workspace/lab/tdd-training/ava-ts
> ava



  1 test failed

  should summarize two numbers

  /Users/andrii_kucherenko/Workspace/lab/tdd-training/ava-ts/__tests__/caclulator.spec.ts:5

   4: test('should summarize two numbers', (t: ExecutionContext) => {
   5:   t.is(sum(1, 2,), 3);
   6: });

  Difference:

  - 0
  + 3

npm ERR! Test failed.  See above for more details.

Benefits

  • Bridge from task to implementation

  • Improved code

  • Tests as documentation

  • Safe refactoring

  • No extra code
  • Increases assurance of correctness

TDD can:

  • Improve performance of development
  • Reduce the costs
  • Help to change the project
  • Reduce technical debt

TDD Kata

String Calculator

This classic kata guides you step by step through the implementation of a calculator that receives a String as input.

It is a good exercise on refactoring and incremental implementation. It is also a good candidate for practising TDD.

String calculator

First step

Create a function add that takes a String and returns a Number.

  • The method can take 0, 1 or 2 numbers separated by comma, and returns their sum.
  • An empty string will return “0”.
  • Example of inputs: "", "1", "1.1,2.2".

 

Navigation maps #1

  • return 0 if ""
  • return number if "number"
  • return a + b if "a,b"

Navigation maps #2

  • Parse string by coma
  • Validate parsed data
  • Summarize results

String calculator

Many numbers

Allow the add method to handle an unknow number of arguments.

String calculator

Newline as separator

Allow the add method to handle newlines as separators:

  • "1\n2,3" should return "6".
  • "175.2,\n35" is invalid and should return the message "Number expected but '\n' found at position 6."

String calculator

Missing number in last position

Don’t allow the input to end in a separator.

"1,3," is invalid and should return the message Number expected but EOF found.

 

Test Driven Development (Borderless.js)

By Andrey Kucherenko

Test Driven Development (Borderless.js)

  • 265