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..1462f54 --- /dev/null +++ b/test/day2.test.js @@ -0,0 +1,54 @@ +import {expect} from 'chai'; +import day2 from '../src/day2'; + +describe('day2 tests', () => { + 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 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'); + }); + }); + + 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' + }; + + expect(day2(givenObject)).to.deep.equal(givenObject); + }); + }); + + describe('checking contains', () => { + it('should contain part of the string passed', () => { + const givenString = 'hello world'; + + expect(day2(givenString)).to.contain('world'); + }); + }); + + describe('checking errors', () => { + 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