From 2693f91084d6f09a984244d2c01bcda135916420 Mon Sep 17 00:00:00 2001 From: nelsonic Date: Sat, 27 Oct 2018 22:31:51 +0100 Subject: [PATCH] show how to update a test file from Tape to Tap for #34 --- tap-advanced-testing.md | 86 +++++++++++++++++++++++++++++++++++++---- test/change-tap.test.js | 30 ++++++++++++++ 2 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 test/change-tap.test.js diff --git a/tap-advanced-testing.md b/tap-advanced-testing.md index b42f904..c7cbd36 100644 --- a/tap-advanced-testing.md +++ b/tap-advanced-testing.md @@ -4,7 +4,8 @@ In _most_ situations **`Tape`** will be _exactly_ what you need to write and run your Node.js/JavaScript tests.
**`Tape`** is minimalist, fast and has flexibility when you need it. _Occasionally_ however, the needs of the project -require a few extra features from the testing framework. +require a few extra features from the testing framework, +that is when we use **`Tap`**. If you find yourself (_your project_) needing to do "setup" **`before`** running a test or "teardown" **`after`** the test is complete @@ -13,28 +14,94 @@ If you find yourself (_your project_) needing to do "setup" to run (_e.g: more than 10 seconds_), then you will benefit form switching/upgrading to **`Tap`**. +The **`Tap`** repo has quite a good breakdown of the _reasons_ +why you should consider using **`Tap`**: +https://github.com/tapjs/node-tap#why-tap + +> _**`Tape`** has 5x more usage than **`Tap`** +according to NPM install stats.
+> This is due to a number of factors including "founder effect".
+But the fact that **`Tape`** has **fewer features** +has contributed to it's enduring/growing popularity.
+Ensure you have **used** **`Tape`** in at least one "real" project +**`before`** considering **`Tap`**, otherwise you risk complexity!_ + + # _What_? -**`Tap`** is testing framework that -has a few _useful_ extra features without being "bloated". +**`Tap`** is a testing framework that +has a few _useful_ extra features _without_ being "bloated". + ++ [x] `beforeEach` ++ [x] `afterEach` ++ [x] _parallel_ test execution ++ [x] built-in code coverage + +We will cover each of these features below.
+ +> **`Tap`** has _many_ more "advanced" features, +if you are _curious_ see: https://www.node-tap.org/advanced
+But don't get distracted by the "bells and whistles", +focus on building your App/Project and if you find +that you are repeating yourself a lot in your tests, +open an issue describing the problem e.g: + # _How_? +Continuing our theme of a "_vending machine_", +let's consider the following _real world_ use cases: +1. The Vending Machine "runs out" of coins and needs to be _re-filled_! +2. _Multiple_ Vending Machines! +We will add this functionality to the `calculateChange` function +and showcase two useful **`Tap`** features. +Let's get started by re-purposing the Tap test: +## 1. Install `Tap` as a Dev Dependency +Start by installing `Tap` ] +and saving it to your `package.json`: +```sh +npm install tap --save-dev +``` +## 2. Copy the **`Tape`** Test +Copy the **`Tape`** test +so we can repurpose it for **`Tap`**: +```js +cp test/change-calculator.test.js test/change-tap.test.js +``` +## 3. Change the First Line of the `test/change-tap.test.js` File +From: +```js +const test = require('tape'); +``` +To: +```js +const test = require('tap').test; +``` +## 4. Run the Test +Run the **`Tap`** tests: +```sh +node test/change-tap.test.js +``` + +The output is slightly different from **`Tape`**, +but the tests still pass: + +![tap-tests-pass](https://user-images.githubusercontent.com/194400/47609430-48d7ee00-da36-11e8-9f3c-f448f78311bb.png) @@ -54,7 +121,10 @@ has a few _useful_ extra features without being "bloated". -
+ + + + single speed bicycle - perfect for short trips on fairly flat ground @@ -79,7 +149,7 @@ for commuting from home to work, going to the shops, etc.
- + geared bicycle bicycle - for longer distances and hilly terrain @@ -118,7 +188,7 @@ the only "cost" to adopting a new tool is **_learning_ time**. Given that **`Tap`** is a "drop-in replacement" for **`Tape`** in _most_ cases, the switching cost is just **`npm install tap --save-dev`** followed by a find-and-replace across the files in your project from: -**`require('tape')`** to **`require('tap')`**. +**`require('tape')`** to **`require('tap').test`**. Over the last 5 years @dwyl we have tested **200+** projects using **`Tape`** (_both Open Source and Client projects_). @@ -137,3 +207,5 @@ and running them in ***parallel*** significantly reduces waiting time. For an _extended_ practical example of where writing tests with **`Tap`** _instead_ of **`Tape`** was worth the switch, see: https://github.com/dwyl/todomvc-vanilla-javascript-elm-architecture-example + +--> diff --git a/test/change-tap.test.js b/test/change-tap.test.js new file mode 100644 index 0000000..2b4d728 --- /dev/null +++ b/test/change-tap.test.js @@ -0,0 +1,30 @@ +const test = require('tap').test; +const calculateChange = require('../lib/change-calculator.js'); // require the calculator module + +test('calculateChange(215, 300) should return [50, 20, 10, 5]', function(t) { + const result = calculateChange(215, 300); // expect an array containing [50,20,10,5] + const expected = [50, 20, 10, 5]; + t.deepEqual(result, expected); + t.end(); +}); + +test('calculateChange(486, 600) should equal [100, 10, 2, 2]', function(t) { + const result = calculateChange(486, 600); + const expected = [100, 10, 2, 2]; + t.deepEqual(result, expected); + t.end(); +}); + +test('calculateChange(12, 400) should return [200, 100, 50, 20, 10, 5, 2, 1]', function(t) { + const result = calculateChange(12, 400); + const expected = [200, 100, 50, 20, 10, 5, 2, 1]; + t.deepEqual(result, expected); + t.end(); +}); + +test('calculateChange(1487,10000) should equal [5000, 2000, 1000, 500, 10, 2, 1 ]', function(t) { + const result = calculateChange(1487,10000); + const expected = [5000, 2000, 1000, 500, 10, 2, 1 ]; + t.deepEqual(result, expected); + t.end(); +});