From 1691f7e80d0cac5c6aa844386f65b5c99a5a732b Mon Sep 17 00:00:00 2001 From: Marc Littlemore Date: Wed, 9 Aug 2017 00:06:16 +0100 Subject: [PATCH 1/2] Initial day 2 tests --- .babelrc | 3 +++ package.json | 8 +++++++- src/day2.js | 13 +++++++++++++ test/day2.test.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 .babelrc create mode 100644 src/day2.js create mode 100644 test/day2.test.js diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..2f01e1d --- /dev/null +++ b/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["env"] +} \ No newline at end of file diff --git a/package.json b/package.json index dd01169..a11bcf5 100644 --- a/package.json +++ b/package.json @@ -16,5 +16,11 @@ "bugs": { "url": "https://github.com/MarcL/javascript-testing-beginners-course/issues" }, - "homepage": "https://github.com/MarcL/javascript-testing-beginners-course#readme" + "homepage": "https://github.com/MarcL/javascript-testing-beginners-course#readme", + "devDependencies": { + "babel-preset-env": "~1.6.0", + "babel-register": "~6.24.1", + "chai": "~4.1.1", + "mocha": "~3.5.0" + } } diff --git a/src/day2.js b/src/day2.js new file mode 100644 index 0000000..5824674 --- /dev/null +++ b/src/day2.js @@ -0,0 +1,13 @@ +function day2(data) { + if (typeof data === 'object') { + return Object.assign({}, data); + } + + if ((typeof data === 'string') && (data === 'error')) { + throw new Error('Cannot pass error'); + } + + return data; +} + +export default day2; diff --git a/test/day2.test.js b/test/day2.test.js new file mode 100644 index 0000000..53974a4 --- /dev/null +++ b/test/day2.test.js @@ -0,0 +1,46 @@ +import {expect} from 'chai'; +import day2 from '../src/day2'; + +describe('day2 tests', () => { + it('should return undefined when no parameters are passed', () => { + expect(day2()).to.be.undefined; + }); + + it('should return a string when a string is passed', () => { + expect(day2('a string')).to.be.a('string'); + }); + + it('should return a number when a number is passed', () => { + expect(day2(10)).to.be.a('Number'); + }); + + it('should not be a string when a number is passed', () => { + expect(day2(10)).to.not.be.a('string'); + }); + + it('should equal the string passed', () => { + expect(day2('same string')).to.equal('same string'); + }); + + it('should deep equal the object passed', () => { + const givenObject = { + hello: 'world' + }; + + expect(day2(givenObject)).to.deep.equal(givenObject); + }); + + it('should contain part of the string passed', () => { + const givenString = 'hello world'; + + expect(day2(givenString)).to.contain('world'); + }); + + it('should throw an error when "error" is passed', () => { + function wrappedFunction() { + day2('error'); + } + + expect(wrappedFunction).to.throw('Cannot pass error'); + }); +}); \ No newline at end of file From c606be2bb44ad03682b627701211fb2e96ba66ba Mon Sep 17 00:00:00 2001 From: Marc Littlemore Date: Wed, 9 Aug 2017 00:09:08 +0100 Subject: [PATCH 2/2] Add groups to test suite --- test/day2.test.js | 62 ++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/test/day2.test.js b/test/day2.test.js index 53974a4..1462f54 100644 --- a/test/day2.test.js +++ b/test/day2.test.js @@ -2,45 +2,53 @@ import {expect} from 'chai'; import day2 from '../src/day2'; describe('day2 tests', () => { - it('should return undefined when no parameters are passed', () => { - expect(day2()).to.be.undefined; - }); + describe('check data type', () => { + it('should return undefined when no parameters are passed', () => { + expect(day2()).to.be.undefined; + }); - it('should return a string when a string is passed', () => { - expect(day2('a string')).to.be.a('string'); - }); + it('should return a string when a string is passed', () => { + expect(day2('a string')).to.be.a('string'); + }); - it('should return a number when a number is passed', () => { - expect(day2(10)).to.be.a('Number'); - }); + it('should return a number when a number is passed', () => { + expect(day2(10)).to.be.a('Number'); + }); - it('should not be a string when a number is passed', () => { - expect(day2(10)).to.not.be.a('string'); + it('should not be a string when a number is passed', () => { + expect(day2(10)).to.not.be.a('string'); + }); }); - it('should equal the string passed', () => { - expect(day2('same string')).to.equal('same string'); - }); + describe('checking equals', () => { + it('should equal the string passed', () => { + expect(day2('same string')).to.equal('same string'); + }); - it('should deep equal the object passed', () => { - const givenObject = { - hello: 'world' - }; + it('should deep equal the object passed', () => { + const givenObject = { + hello: 'world' + }; - expect(day2(givenObject)).to.deep.equal(givenObject); + expect(day2(givenObject)).to.deep.equal(givenObject); + }); }); - it('should contain part of the string passed', () => { - const givenString = 'hello world'; + describe('checking contains', () => { + it('should contain part of the string passed', () => { + const givenString = 'hello world'; - expect(day2(givenString)).to.contain('world'); + expect(day2(givenString)).to.contain('world'); + }); }); - it('should throw an error when "error" is passed', () => { - function wrappedFunction() { - day2('error'); - } + describe('checking errors', () => { + it('should throw an error when "error" is passed', () => { + function wrappedFunction() { + day2('error'); + } - expect(wrappedFunction).to.throw('Cannot pass error'); + expect(wrappedFunction).to.throw('Cannot pass error'); + }); }); }); \ No newline at end of file