From 34b43ed88187ffa455be20093cc57e97fd8dce27 Mon Sep 17 00:00:00 2001 From: Mark Tyers Date: Fri, 13 Sep 2019 13:01:23 +0100 Subject: [PATCH] added base code for todo --- .vscode/launch.json | 32 +++++++++++--- 07 Unit Testing.md | 8 ++++ .../07_unit_testing/todo/.vscode/launch.json | 36 +++++++++++++++ .../07_unit_testing/todo/jest-test.config.js | 26 +++++++++++ .../07_unit_testing/todo/modules/todo.js | 5 +++ exercises/07_unit_testing/todo/package.json | 11 ++++- exercises/07_unit_testing/todo/readme.md | 6 +++ .../todo/unit tests/todo.spec.js | 44 +++++++++++++++++++ 8 files changed, 162 insertions(+), 6 deletions(-) create mode 100644 exercises/07_unit_testing/todo/.vscode/launch.json create mode 100644 exercises/07_unit_testing/todo/jest-test.config.js create mode 100644 exercises/07_unit_testing/todo/readme.md create mode 100644 exercises/07_unit_testing/todo/unit tests/todo.spec.js diff --git a/.vscode/launch.json b/.vscode/launch.json index f0cac870..5c075a55 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,14 +1,36 @@ + { "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", - "name": "Jest Tests", - "program": "${workspaceFolder}/exercises/12_spa/books/node_modules/.bin/jest", + "name": "Jest All", + "program": "${workspaceFolder}/exercises/07_unit_testing/todo/node_modules/jest/bin/jest", + "args": ["--runInBand"], + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen", + "disableOptimisticBPs": true, + "windows": { + "program": "${workspaceFolder}/exercises/07_unit_testing/todo/node_modules/jest/bin/jest", + } + }, + { + "type": "node", + "request": "launch", + "name": "Jest Current File", + "program": "${workspaceFolder}/exercises/07_unit_testing/todo/node_modules/jest/bin/jest", "args": [ - "-i" - ] + "${fileBasenameNoExtension}", + "--config", + "jest.config.js" + ], + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen", + "disableOptimisticBPs": true, + "windows": { + "program": "${workspaceFolder}/exercises/07_unit_testing/todo/node_modules/jest/bin/jest", + } } ] -} \ No newline at end of file +} diff --git a/07 Unit Testing.md b/07 Unit Testing.md index 54e06b9f..6f565a21 100644 --- a/07 Unit Testing.md +++ b/07 Unit Testing.md @@ -4,3 +4,11 @@ In this worksheet you will learn the basics of using automated tests to improve the quality of your code. Before you start you need to pull any _upstream changes_. Detailed instructions can be found in the **Setup** lab. + +We will be using both the Jest commandline tools and the integrated unit testing tools in Visual Studio Code, you should be familiar with both approaches. + +## Visual Studio Code Extensions + +One of the powerful features of VS Code is its support for **Extensions** which allow you to add additional capabilities to the editor. + +You should start by checking that the following VS Code Extensions are installed and install them if missing. \ No newline at end of file diff --git a/exercises/07_unit_testing/todo/.vscode/launch.json b/exercises/07_unit_testing/todo/.vscode/launch.json new file mode 100644 index 00000000..2f317bea --- /dev/null +++ b/exercises/07_unit_testing/todo/.vscode/launch.json @@ -0,0 +1,36 @@ + +{ + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Jest All", + "program": "${workspaceFolder}/node_modules/.bin/jest", + "args": ["--runInBand"], + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen", + "disableOptimisticBPs": true, + "windows": { + "program": "${workspaceFolder}/node_modules/jest/bin/jest", + } + }, + { + "type": "node", + "request": "launch", + "name": "Jest Current File", + "program": "${workspaceFolder}/node_modules/.bin/jest", + "args": [ + "${fileBasenameNoExtension}", + "--config", + "jest.config.js" + ], + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen", + "disableOptimisticBPs": true, + "windows": { + "program": "${workspaceFolder}/node_modules/jest/bin/jest", + } + } + ] +} diff --git a/exercises/07_unit_testing/todo/jest-test.config.js b/exercises/07_unit_testing/todo/jest-test.config.js new file mode 100644 index 00000000..08634676 --- /dev/null +++ b/exercises/07_unit_testing/todo/jest-test.config.js @@ -0,0 +1,26 @@ + +'use strict'; + +module.exports = { + displayName: 'test', + verbose: true, + collectCoverage: true, + testPathIgnorePatterns: [ + '/node_modules/', + '/__tests__/fixtures/', + ], +} + +// "jest": { +// "testEnvironment": "node", +// "verbose": true, +// "collectCoverage": true, +// "coverageThreshold": { +// "global": { +// "branches": 0, +// "functions": 0, +// "lines": 0, +// "statements": 0 +// } +// } +// } \ No newline at end of file diff --git a/exercises/07_unit_testing/todo/modules/todo.js b/exercises/07_unit_testing/todo/modules/todo.js index 9218ff7c..8320b622 100644 --- a/exercises/07_unit_testing/todo/modules/todo.js +++ b/exercises/07_unit_testing/todo/modules/todo.js @@ -8,6 +8,7 @@ module.exports.clear = () => { } module.exports.add = (item, qty) => { + if(typeof qty !== 'number') throw new Error('qty must be a number') data.push({item: item, qty: qty}) } @@ -21,3 +22,7 @@ module.exports.delete = key => { console.log(`delete key ${key}`) return } + +module.exports.countItems = () => data.length + +module.exports.count = data.length diff --git a/exercises/07_unit_testing/todo/package.json b/exercises/07_unit_testing/todo/package.json index b5cb55d7..9de06ac3 100644 --- a/exercises/07_unit_testing/todo/package.json +++ b/exercises/07_unit_testing/todo/package.json @@ -4,7 +4,8 @@ "description": "", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "linter": "node_modules/.bin/eslint .", + "test": "node_modules/.bin/jest --coverage --runInBand" }, "author": "", "license": "ISC", @@ -17,5 +18,13 @@ "koa-router": "^7.4.0", "koa-static": "^5.0.0", "koa-views": "^6.2.1" + }, + "devDependencies": { + "jest": "^24.9.0" + }, + "jest": { + "projects": [ + "/jest-test.config.js" + ] } } diff --git a/exercises/07_unit_testing/todo/readme.md b/exercises/07_unit_testing/todo/readme.md new file mode 100644 index 00000000..c2b28bf0 --- /dev/null +++ b/exercises/07_unit_testing/todo/readme.md @@ -0,0 +1,6 @@ + +Install + +1. coverage-gutters +2. jest +3. test-explorer diff --git a/exercises/07_unit_testing/todo/unit tests/todo.spec.js b/exercises/07_unit_testing/todo/unit tests/todo.spec.js new file mode 100644 index 00000000..f83f42de --- /dev/null +++ b/exercises/07_unit_testing/todo/unit tests/todo.spec.js @@ -0,0 +1,44 @@ + +'use strict' + +const todo = require('../modules/todo.js') + +beforeAll( async() => { + // stuff to do before any of the tests run +}) + +afterAll( async() => { + // runs after all the tests have completed +}) + +describe('add()', () => { + // block of tests + beforeEach( async() => { + todo.clear() + }) + afterEach( async() => { + // runs after each test completes + }) + test('add a single item', async done => { + expect.assertions(1) + try { + todo.add('bread', 3) + expect(todo.countItems()).toBe(1) + } catch(err) { + done.fail(err) + } finally { + done() + } + }) + test('qty must be a number', async done => { + expect.assertions(1) + try { + todo.add('bread', 'three') + done.fail('test failed') + } catch(err) { + expect(err.message).toBe('qty must be a number') + } finally { + done() + } + }) +})