From b46117c0f22e5300ad20775f39b4dbb28ccb5a0e Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" Date: Mon, 16 Apr 2018 16:14:47 -0500 Subject: [PATCH] improve the whats-a- stuff --- other/whats-a-mock/__tests__/.eslintrc | 5 + .../__tests__/thumb-war.0.solution.js | 14 -- other/whats-a-mock/__tests__/thumb-war.1.js | 134 ---------------- .../__tests__/thumb-war.1.solution.js | 22 --- .../{thumb-war.0.js => thumb-war.1.todo.js} | 1 + other/whats-a-mock/__tests__/thumb-war.2.js | 80 ---------- .../__tests__/thumb-war.2.solution.js | 18 --- .../__tests__/thumb-war.2.todo.js | 145 ++++++++++++++++++ other/whats-a-mock/__tests__/thumb-war.3.js | 123 +-------------- .../__tests__/thumb-war.3.todo.js | 124 +++++++++++++++ other/whats-a-mock/__tests__/thumb-war.4.js | 83 +--------- ...-war.3.solution.js => thumb-war.4.todo.js} | 69 +++++++++ other/whats-a-mock/__tests__/thumb-war.5.js | 74 +-------- .../__tests__/thumb-war.5.solution.js | 13 -- ...-war.4.solution.js => thumb-war.5.todo.js} | 69 +++++++++ other/whats-a-test/0.solution.js | 17 -- other/whats-a-test/1.js | 24 +-- other/whats-a-test/1.solution.js | 15 -- other/whats-a-test/{0.js => 1.todo.js} | 10 +- other/whats-a-test/2.js | 26 ++-- other/whats-a-test/2.solution.js | 12 -- other/whats-a-test/2.todo.js | 32 ++++ other/whats-a-test/3.js | 40 +++-- other/whats-a-test/3.solution.js | 21 --- other/whats-a-test/3.todo.js | 41 +++++ other/whats-a-test/4.js | 32 ++-- other/whats-a-test/4.solution.js | 33 ---- other/whats-a-test/5.js | 16 -- other/whats-a-test/jest.config.js | 2 +- 29 files changed, 581 insertions(+), 714 deletions(-) create mode 100644 other/whats-a-mock/__tests__/.eslintrc delete mode 100644 other/whats-a-mock/__tests__/thumb-war.0.solution.js delete mode 100644 other/whats-a-mock/__tests__/thumb-war.1.solution.js rename other/whats-a-mock/__tests__/{thumb-war.0.js => thumb-war.1.todo.js} (98%) delete mode 100644 other/whats-a-mock/__tests__/thumb-war.2.solution.js create mode 100644 other/whats-a-mock/__tests__/thumb-war.2.todo.js create mode 100644 other/whats-a-mock/__tests__/thumb-war.3.todo.js rename other/whats-a-mock/__tests__/{thumb-war.3.solution.js => thumb-war.4.todo.js} (53%) delete mode 100644 other/whats-a-mock/__tests__/thumb-war.5.solution.js rename other/whats-a-mock/__tests__/{thumb-war.4.solution.js => thumb-war.5.todo.js} (59%) delete mode 100644 other/whats-a-test/0.solution.js delete mode 100644 other/whats-a-test/1.solution.js rename other/whats-a-test/{0.js => 1.todo.js} (88%) delete mode 100644 other/whats-a-test/2.solution.js create mode 100644 other/whats-a-test/2.todo.js delete mode 100644 other/whats-a-test/3.solution.js create mode 100644 other/whats-a-test/3.todo.js delete mode 100644 other/whats-a-test/4.solution.js delete mode 100644 other/whats-a-test/5.js diff --git a/other/whats-a-mock/__tests__/.eslintrc b/other/whats-a-mock/__tests__/.eslintrc new file mode 100644 index 00000000..0c122883 --- /dev/null +++ b/other/whats-a-mock/__tests__/.eslintrc @@ -0,0 +1,5 @@ +{ + "rules": { + "import/namespace": "off" + } +} diff --git a/other/whats-a-mock/__tests__/thumb-war.0.solution.js b/other/whats-a-mock/__tests__/thumb-war.0.solution.js deleted file mode 100644 index e6a0bab6..00000000 --- a/other/whats-a-mock/__tests__/thumb-war.0.solution.js +++ /dev/null @@ -1,14 +0,0 @@ -import thumbWar from '../thumb-war' -import * as utils from '../utils' - -test('returns winner', () => { - const originalGetWinner = utils.getWinner - // eslint-disable-next-line import/namespace - utils.getWinner = (p1, p2) => p2 - - const winner = thumbWar('Ken Wheeler', 'Kent C. Dodds') - expect(winner).toBe('Kent C. Dodds') - - // eslint-disable-next-line import/namespace - utils.getWinner = originalGetWinner -}) diff --git a/other/whats-a-mock/__tests__/thumb-war.1.js b/other/whats-a-mock/__tests__/thumb-war.1.js index d574eb43..8f1f31ec 100644 --- a/other/whats-a-mock/__tests__/thumb-war.1.js +++ b/other/whats-a-mock/__tests__/thumb-war.1.js @@ -3,144 +3,10 @@ import * as utils from '../utils' test('returns winner', () => { const originalGetWinner = utils.getWinner - // change the getWinner implementation to a function - // that keeps track of how often it's called and - // the arguments it's called with (Hint #1) - // eslint-disable-next-line import/namespace utils.getWinner = (p1, p2) => p2 const winner = thumbWar('Ken Wheeler', 'Kent C. Dodds') expect(winner).toBe('Kent C. Dodds') - // add an assertion for how many times the getWinner function - // was supposed to be called (2 times) (Hint #2) - // - // add another assertion that every time it was called - // it was called with the right arguments: 'Ken Wheeler', 'Kent C. Dodds' - // (Hint #3) - // eslint-disable-next-line import/namespace utils.getWinner = originalGetWinner }) - -/* -Hints below: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Hint #1: - -There's nothing too magical going on here, you just need some place to store the state for every time the function is -called. Something like this should do just fine (Sorry, this is the solution I have. I couldn't think of a way to hint -at it without totally giving it away or leading you astray): - -utils.getWinner = (...args) => { - utils.getWinner.mock.calls.push(args) - return args[1] -} -utils.getWinner.mock = {calls: []} - - - - - - - -Hint #2: -Depending on how you store the state of how many times it's been called, you might either do this: - -expect(timesCalled).toBe(2) - -Or you might do this: - -expect(utils.getWinner.mock.calls).toHaveLength(2) - -Either way is fine. - - - - - - -Hint #3: - -You can have assertions within a forEach and that's not entirely uncommon. Depending on how you're storing the state -of the arguments its called with you might do this (#spoileralert this is the solution... sorry): - -utils.getWinner.mock.calls.forEach(args => { - expect(args).toEqual(['Ken Wheeler', 'Kent C. Dodds']) -}) - - - - - */ diff --git a/other/whats-a-mock/__tests__/thumb-war.1.solution.js b/other/whats-a-mock/__tests__/thumb-war.1.solution.js deleted file mode 100644 index 6e6e247b..00000000 --- a/other/whats-a-mock/__tests__/thumb-war.1.solution.js +++ /dev/null @@ -1,22 +0,0 @@ -import thumbWar from '../thumb-war' -import * as utils from '../utils' - -test('returns winner', () => { - const originalGetWinner = utils.getWinner - // eslint-disable-next-line import/namespace - utils.getWinner = (...args) => { - utils.getWinner.mock.calls.push(args) - return args[1] - } - utils.getWinner.mock = {calls: []} - - const winner = thumbWar('Ken Wheeler', 'Kent C. Dodds') - expect(winner).toBe('Kent C. Dodds') - expect(utils.getWinner.mock.calls).toHaveLength(2) - utils.getWinner.mock.calls.forEach(args => { - expect(args).toEqual(['Ken Wheeler', 'Kent C. Dodds']) - }) - - // eslint-disable-next-line import/namespace - utils.getWinner = originalGetWinner -}) diff --git a/other/whats-a-mock/__tests__/thumb-war.0.js b/other/whats-a-mock/__tests__/thumb-war.1.todo.js similarity index 98% rename from other/whats-a-mock/__tests__/thumb-war.0.js rename to other/whats-a-mock/__tests__/thumb-war.1.todo.js index 5c81ac1f..d4438589 100644 --- a/other/whats-a-mock/__tests__/thumb-war.0.js +++ b/other/whats-a-mock/__tests__/thumb-war.1.todo.js @@ -1,3 +1,4 @@ +// monkey-patching import thumbWar from '../thumb-war' // import the utils module (see hint #1 at the bottom of the file) diff --git a/other/whats-a-mock/__tests__/thumb-war.2.js b/other/whats-a-mock/__tests__/thumb-war.2.js index 394079bc..62792b07 100644 --- a/other/whats-a-mock/__tests__/thumb-war.2.js +++ b/other/whats-a-mock/__tests__/thumb-war.2.js @@ -3,8 +3,6 @@ import * as utils from '../utils' test('returns winner', () => { const originalGetWinner = utils.getWinner - // change this to a jest mock function (Hint #1) - // eslint-disable-next-line import/namespace utils.getWinner = (...args) => { utils.getWinner.mock.calls.push(args) return args[1] @@ -13,88 +11,10 @@ test('returns winner', () => { const winner = thumbWar('Ken Wheeler', 'Kent C. Dodds') expect(winner).toBe('Kent C. Dodds') - // change this to the built-in jest assertion for how many times a mock - // function was called (use toHaveBeenCalledTimes) (Hint #2) expect(utils.getWinner.mock.calls).toHaveLength(2) utils.getWinner.mock.calls.forEach(args => { expect(args).toEqual(['Ken Wheeler', 'Kent C. Dodds']) }) - // eslint-disable-next-line import/namespace utils.getWinner = originalGetWinner }) - -/* -Hints below - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Hint #1: - -You can create a jest mock function with `jest.fn()`: - -const myMockFunction = jest.fn((greeting, name) => `${greeting} ${name}`) -myMockFunction('Hey', 'Joe') // returns 'Hey Joe' - - -Jest keeps track of mock function metadata the same way we did (via myMockFunction.mock.calls) - - -Hint #2: - -You can assert the number of times called with: - -expect(myMockFunction).toHaveBeenCalledTimes(1) - - - - */ diff --git a/other/whats-a-mock/__tests__/thumb-war.2.solution.js b/other/whats-a-mock/__tests__/thumb-war.2.solution.js deleted file mode 100644 index 1dea6ba3..00000000 --- a/other/whats-a-mock/__tests__/thumb-war.2.solution.js +++ /dev/null @@ -1,18 +0,0 @@ -import thumbWar from '../thumb-war' -import * as utils from '../utils' - -test('returns winner', () => { - const originalGetWinner = utils.getWinner - // eslint-disable-next-line import/namespace - utils.getWinner = jest.fn((p1, p2) => p2) - - const winner = thumbWar('Ken Wheeler', 'Kent C. Dodds') - expect(winner).toBe('Kent C. Dodds') - expect(utils.getWinner).toHaveBeenCalledTimes(2) - utils.getWinner.mock.calls.forEach(args => { - expect(args).toEqual(['Ken Wheeler', 'Kent C. Dodds']) - }) - - // eslint-disable-next-line import/namespace - utils.getWinner = originalGetWinner -}) diff --git a/other/whats-a-mock/__tests__/thumb-war.2.todo.js b/other/whats-a-mock/__tests__/thumb-war.2.todo.js new file mode 100644 index 00000000..8a1e898f --- /dev/null +++ b/other/whats-a-mock/__tests__/thumb-war.2.todo.js @@ -0,0 +1,145 @@ +// improved assertions for mocks +import thumbWar from '../thumb-war' +import * as utils from '../utils' + +test('returns winner', () => { + const originalGetWinner = utils.getWinner + // change the getWinner implementation to a function + // that keeps track of how often it's called and + // the arguments it's called with (Hint #1) + utils.getWinner = (p1, p2) => p2 + + const winner = thumbWar('Ken Wheeler', 'Kent C. Dodds') + expect(winner).toBe('Kent C. Dodds') + // add an assertion for how many times the getWinner function + // was supposed to be called (2 times) (Hint #2) + // + // add another assertion that every time it was called + // it was called with the right arguments: 'Ken Wheeler', 'Kent C. Dodds' + // (Hint #3) + + utils.getWinner = originalGetWinner +}) + +/* +Hints below: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Hint #1: + +There's nothing too magical going on here, you just need some place to store the state for every time the function is +called. Something like this should do just fine (Sorry, this is the solution I have. I couldn't think of a way to hint +at it without totally giving it away or leading you astray): + +utils.getWinner = (...args) => { + utils.getWinner.mock.calls.push(args) + return args[1] +} +utils.getWinner.mock = {calls: []} + + + + + + + +Hint #2: +Depending on how you store the state of how many times it's been called, you might either do this: + +expect(timesCalled).toBe(2) + +Or you might do this: + +expect(utils.getWinner.mock.calls).toHaveLength(2) + +Either way is fine. + + + + + + +Hint #3: + +You can have assertions within a forEach and that's not entirely uncommon. Depending on how you're storing the state +of the arguments its called with you might do this (#spoileralert this is the solution... sorry): + +utils.getWinner.mock.calls.forEach(args => { + expect(args).toEqual(['Ken Wheeler', 'Kent C. Dodds']) +}) + + + + + */ diff --git a/other/whats-a-mock/__tests__/thumb-war.3.js b/other/whats-a-mock/__tests__/thumb-war.3.js index 29b39851..d0e76987 100644 --- a/other/whats-a-mock/__tests__/thumb-war.3.js +++ b/other/whats-a-mock/__tests__/thumb-war.3.js @@ -2,124 +2,15 @@ import thumbWar from '../thumb-war' import * as utils from '../utils' test('returns winner', () => { - // replace the next three lines with a call to jest.spyOn and - // call to mockImplementation on the mocked function (See hint #1) - const originalGetWinner = utils.getWinner - // eslint-disable-next-line import/namespace - utils.getWinner = (p1, p2) => p2 + jest.spyOn(utils, 'getWinner') + utils.getWinner.mockImplementation((p1, p2) => p2) const winner = thumbWar('Ken Wheeler', 'Kent C. Dodds') expect(winner).toBe('Kent C. Dodds') + expect(utils.getWinner).toHaveBeenCalledTimes(2) + utils.getWinner.mock.calls.forEach(args => { + expect(args).toEqual(['Ken Wheeler', 'Kent C. Dodds']) + }) - // replace the next two lines with a restoration of the original function - // (See hint #2) - // eslint-disable-next-line import/namespace - utils.getWinner = originalGetWinner + utils.getWinner.mockRestore() }) - -/* -Hints below - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Hint #1: - -Here's an example of those APIs: - -const myObject = {foo: () => 'bar'} -jest.spyOn(myObject, 'foo') -myObject.foo.mockImplementation(() => 'not-bar') -myObject.foo() === 'not-bar' // true - - -See the solution file for the solution - - - - - - - - - -Hint #2: - -If we wanted to restore the mocked `myObject.foo` function -to its original implementation, we could do: -myObject.foo.mockRestore() - -And then the original implementation will be called. -myObject.foo() === 'bar' // true - - - */ diff --git a/other/whats-a-mock/__tests__/thumb-war.3.todo.js b/other/whats-a-mock/__tests__/thumb-war.3.todo.js new file mode 100644 index 00000000..de81f030 --- /dev/null +++ b/other/whats-a-mock/__tests__/thumb-war.3.todo.js @@ -0,0 +1,124 @@ +// using jest utilities +import thumbWar from '../thumb-war' +import * as utils from '../utils' + +test('returns winner', () => { + // replace these lines with a call to jest.spyOn and + // call to mockImplementation on the mocked function (See hint #1) + const originalGetWinner = utils.getWinner + utils.getWinner = (p1, p2) => p2 + + const winner = thumbWar('Ken Wheeler', 'Kent C. Dodds') + expect(winner).toBe('Kent C. Dodds') + + // replace the next two lines with a restoration of the original function + // (See hint #2) + utils.getWinner = originalGetWinner +}) + +/* +Hints below + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Hint #1: + +Here's an example of those APIs: + +const myObject = {foo: () => 'bar'} +jest.spyOn(myObject, 'foo') +myObject.foo.mockImplementation(() => 'not-bar') +myObject.foo() === 'not-bar' // true + + +See the solution file for the solution + + + + + + + + + +Hint #2: + +If we wanted to restore the mocked `myObject.foo` function +to its original implementation, we could do: +myObject.foo.mockRestore() + +And then the original implementation will be called. +myObject.foo() === 'bar' // true + + + */ diff --git a/other/whats-a-mock/__tests__/thumb-war.4.js b/other/whats-a-mock/__tests__/thumb-war.4.js index 6818d742..385a3d74 100644 --- a/other/whats-a-mock/__tests__/thumb-war.4.js +++ b/other/whats-a-mock/__tests__/thumb-war.4.js @@ -1,84 +1,17 @@ import thumbWar from '../thumb-war' -import * as utils from '../utils' +import * as utilsMock from '../utils' -// add an inline mock with the jest.mock API -// -// jest.mock( -// relativePathToModuleToMock, -// functionThatReturnsMockObject -// ) -// -// (Hint #1) +jest.mock('../utils', () => { + return { + getWinner: jest.fn((p1, p2) => p2), + } +}) test('returns winner', () => { - // remove the next two lines - jest.spyOn(utils, 'getWinner') - utils.getWinner.mockImplementation((p1, p2) => p2) - const winner = thumbWar('Ken Wheeler', 'Kent C. Dodds') expect(winner).toBe('Kent C. Dodds') - expect(utils.getWinner).toHaveBeenCalledTimes(2) - utils.getWinner.mock.calls.forEach(args => { + expect(utilsMock.getWinner).toHaveBeenCalledTimes(2) + utilsMock.getWinner.mock.calls.forEach(args => { expect(args).toEqual(['Ken Wheeler', 'Kent C. Dodds']) }) - - // remove the next line - utils.getWinner.mockRestore() -}) - -/* -Hint below: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -jest.mock('../utils', () => { - return { - // ... - // see answer in the solution file - } }) - - */ diff --git a/other/whats-a-mock/__tests__/thumb-war.3.solution.js b/other/whats-a-mock/__tests__/thumb-war.4.todo.js similarity index 53% rename from other/whats-a-mock/__tests__/thumb-war.3.solution.js rename to other/whats-a-mock/__tests__/thumb-war.4.todo.js index d0e76987..1337efcf 100644 --- a/other/whats-a-mock/__tests__/thumb-war.3.solution.js +++ b/other/whats-a-mock/__tests__/thumb-war.4.todo.js @@ -1,7 +1,18 @@ +// avoid monkey-patching with jest.mock import thumbWar from '../thumb-war' import * as utils from '../utils' +// add an inline mock with the jest.mock API +// +// jest.mock( +// relativePathToModuleToMock, +// functionThatReturnsMockObject +// ) +// +// (Hint #1) + test('returns winner', () => { + // remove the next two lines jest.spyOn(utils, 'getWinner') utils.getWinner.mockImplementation((p1, p2) => p2) @@ -12,5 +23,63 @@ test('returns winner', () => { expect(args).toEqual(['Ken Wheeler', 'Kent C. Dodds']) }) + // remove the next line utils.getWinner.mockRestore() }) + +/* +Hint below: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +jest.mock('../utils', () => { + return { + // ... + // see answer in the solution file + } +}) + + */ diff --git a/other/whats-a-mock/__tests__/thumb-war.5.js b/other/whats-a-mock/__tests__/thumb-war.5.js index bc81b158..273adcb8 100644 --- a/other/whats-a-mock/__tests__/thumb-war.5.js +++ b/other/whats-a-mock/__tests__/thumb-war.5.js @@ -1,15 +1,7 @@ import thumbWar from '../thumb-war' import * as utilsMock from '../utils' -// remove the inline mock function and jest -// will use the one that exists in the -// __mocks__ directory which I created for you -// already (you're welcome) -jest.mock('../utils', () => { - return { - getWinner: jest.fn((p1, p2) => p2), - } -}) +jest.mock('../utils') test('returns winner', () => { const winner = thumbWar('Ken Wheeler', 'Kent C. Dodds') @@ -19,67 +11,3 @@ test('returns winner', () => { expect(args).toEqual(['Ken Wheeler', 'Kent C. Dodds']) }) }) - -/* -Hint below - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Hint #1: - -jest.mock(relativePathToModuleToMock) - - - - - */ diff --git a/other/whats-a-mock/__tests__/thumb-war.5.solution.js b/other/whats-a-mock/__tests__/thumb-war.5.solution.js deleted file mode 100644 index 273adcb8..00000000 --- a/other/whats-a-mock/__tests__/thumb-war.5.solution.js +++ /dev/null @@ -1,13 +0,0 @@ -import thumbWar from '../thumb-war' -import * as utilsMock from '../utils' - -jest.mock('../utils') - -test('returns winner', () => { - const winner = thumbWar('Ken Wheeler', 'Kent C. Dodds') - expect(winner).toBe('Kent C. Dodds') - expect(utilsMock.getWinner).toHaveBeenCalledTimes(2) - utilsMock.getWinner.mock.calls.forEach(args => { - expect(args).toEqual(['Ken Wheeler', 'Kent C. Dodds']) - }) -}) diff --git a/other/whats-a-mock/__tests__/thumb-war.4.solution.js b/other/whats-a-mock/__tests__/thumb-war.5.todo.js similarity index 59% rename from other/whats-a-mock/__tests__/thumb-war.4.solution.js rename to other/whats-a-mock/__tests__/thumb-war.5.todo.js index 385a3d74..e3c6f5be 100644 --- a/other/whats-a-mock/__tests__/thumb-war.4.solution.js +++ b/other/whats-a-mock/__tests__/thumb-war.5.todo.js @@ -1,6 +1,11 @@ +// use the jest __mocks__ directory import thumbWar from '../thumb-war' import * as utilsMock from '../utils' +// remove the inline mock function and jest +// will use the one that exists in the +// __mocks__ directory which I created for you +// already (you're welcome) jest.mock('../utils', () => { return { getWinner: jest.fn((p1, p2) => p2), @@ -15,3 +20,67 @@ test('returns winner', () => { expect(args).toEqual(['Ken Wheeler', 'Kent C. Dodds']) }) }) + +/* +Hint below + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Hint #1: + +jest.mock(relativePathToModuleToMock) + + + + + */ diff --git a/other/whats-a-test/0.solution.js b/other/whats-a-test/0.solution.js deleted file mode 100644 index 3a7fc55f..00000000 --- a/other/whats-a-test/0.solution.js +++ /dev/null @@ -1,17 +0,0 @@ -// sum is intentionally broken so you can see errors in the tests -const sum = (a, b) => a - b -const subtract = (a, b) => a - b - -let result, expected - -result = sum(3, 7) -expected = 10 -if (result !== expected) { - throw new Error(`${result} is not equal to ${expected}`) -} - -result = subtract(7, 3) -expected = 4 -if (result !== expected) { - throw new Error(`${result} is not equal to ${expected}`) -} diff --git a/other/whats-a-test/1.js b/other/whats-a-test/1.js index 95971c40..3a7fc55f 100644 --- a/other/whats-a-test/1.js +++ b/other/whats-a-test/1.js @@ -1,13 +1,17 @@ -const {sum, subtract} = require('./math') +// sum is intentionally broken so you can see errors in the tests +const sum = (a, b) => a - b +const subtract = (a, b) => a - b -/* +let result, expected -Normally your tests aren't in the same file as your source code. +result = sum(3, 7) +expected = 10 +if (result !== expected) { + throw new Error(`${result} is not equal to ${expected}`) +} -So now take your tests from before and copy it in here. - -Then run this code with `node 0` - -> Make sure you're in the right directory! - - */ +result = subtract(7, 3) +expected = 4 +if (result !== expected) { + throw new Error(`${result} is not equal to ${expected}`) +} diff --git a/other/whats-a-test/1.solution.js b/other/whats-a-test/1.solution.js deleted file mode 100644 index 6ee2966f..00000000 --- a/other/whats-a-test/1.solution.js +++ /dev/null @@ -1,15 +0,0 @@ -const {sum, subtract} = require('./math') - -let result, expected - -result = sum(3, 7) -expected = 10 -if (result !== expected) { - throw new Error(`${result} is not equal to ${expected}`) -} - -result = subtract(7, 3) -expected = 4 -if (result !== expected) { - throw new Error(`${result} is not equal to ${expected}`) -} diff --git a/other/whats-a-test/0.js b/other/whats-a-test/1.todo.js similarity index 88% rename from other/whats-a-test/0.js rename to other/whats-a-test/1.todo.js index 9c062344..76284ede 100644 --- a/other/whats-a-test/0.js +++ b/other/whats-a-test/1.todo.js @@ -1,6 +1,4 @@ -// sum is intentionally broken so you can see errors in the tests -const sum = (a, b) => a - b -const subtract = (a, b) => a - b +// the most fundamental test /* @@ -11,7 +9,7 @@ So... if (calling sum with some numbers doesn't return the right thing) { then throw an error with a helpful message } -Then run this code with `node 0` +Then run this code with `node 1.todo` > Make sure you're in the right directory! @@ -19,3 +17,7 @@ Bonus, write another test that would throw an error if the subtract function were to have a bug */ + +// sum is intentionally broken so you can see errors in the tests +const sum = (a, b) => a - b +const subtract = (a, b) => a - b diff --git a/other/whats-a-test/2.js b/other/whats-a-test/2.js index 277aab1c..32905fc1 100644 --- a/other/whats-a-test/2.js +++ b/other/whats-a-test/2.js @@ -1,15 +1,21 @@ -const assert = require('assert') const {sum, subtract} = require('./math') -/* +let result, expected -Rewrite your tests using the assert module! +result = sum(3, 7) +expected = 10 +expect(result).toBe(expected) -Hint: add `console.log(Object.keys(assert))` to know -what methods are available from the assert module! +result = subtract(7, 3) +expected = 4 +expect(result).toBe(expected) -Then run this code with `node 2` - -> Make sure you're in the right directory! - - */ +function expect(actual) { + return { + toBe(expected) { + if (actual !== expected) { + throw new Error(`${actual} is not equal to ${expected}`) + } + }, + } +} diff --git a/other/whats-a-test/2.solution.js b/other/whats-a-test/2.solution.js deleted file mode 100644 index c9834a98..00000000 --- a/other/whats-a-test/2.solution.js +++ /dev/null @@ -1,12 +0,0 @@ -const assert = require('assert') -const {sum, subtract} = require('./math') - -let result, expected - -result = sum(3, 7) -expected = 10 -assert.strictEqual(result, expected) - -result = subtract(7, 3) -expected = 4 -assert.strictEqual(result, expected) diff --git a/other/whats-a-test/2.todo.js b/other/whats-a-test/2.todo.js new file mode 100644 index 00000000..35302776 --- /dev/null +++ b/other/whats-a-test/2.todo.js @@ -0,0 +1,32 @@ +// writing an assertion library +/* + +Now let's implement our own assertion library. +Create a function called `expect` that accepts an "actual" +and returns an object of assertions. + +Tip: I want to be able to use it like so: + +> expect(actual).toBe(expected) + +Then run this code with `node 2.todo` + +> Make sure you're in the right directory! + + */ + +const {sum, subtract} = require('./math') + +let result, expected + +result = sum(3, 7) +expected = 10 +if (result !== expected) { + throw new Error(`${result} is not equal to ${expected}`) +} + +result = subtract(7, 3) +expected = 4 +if (result !== expected) { + throw new Error(`${result} is not equal to ${expected}`) +} diff --git a/other/whats-a-test/3.js b/other/whats-a-test/3.js index 6561e3d9..83af05de 100644 --- a/other/whats-a-test/3.js +++ b/other/whats-a-test/3.js @@ -1,17 +1,33 @@ const {sum, subtract} = require('./math') -/* +test('sum adds numbers', () => { + const result = sum(3, 7) + const expected = 10 + expect(result).toBe(expected) +}) -Now let's implement our own assertion library. -Create a function called `expect` that accepts an "actual" -and returns an object of assertions. +test('subtract subtracts numbers', () => { + const result = subtract(7, 3) + const expected = 4 + expect(result).toBe(expected) +}) -Tip: I want to be able to use it like so: +function test(title, callback) { + try { + callback() + console.log(`✓ ${title}`) + } catch (error) { + console.error(`✕ ${title}`) + console.error(error) + } +} -> expect(actual).toBe(expected) - -Then run this code with `node 3` - -> Make sure you're in the right directory! - - */ +function expect(actual) { + return { + toBe(expected) { + if (actual !== expected) { + throw new Error(`${actual} is not equal to ${expected}`) + } + }, + } +} diff --git a/other/whats-a-test/3.solution.js b/other/whats-a-test/3.solution.js deleted file mode 100644 index 32905fc1..00000000 --- a/other/whats-a-test/3.solution.js +++ /dev/null @@ -1,21 +0,0 @@ -const {sum, subtract} = require('./math') - -let result, expected - -result = sum(3, 7) -expected = 10 -expect(result).toBe(expected) - -result = subtract(7, 3) -expected = 4 -expect(result).toBe(expected) - -function expect(actual) { - return { - toBe(expected) { - if (actual !== expected) { - throw new Error(`${actual} is not equal to ${expected}`) - } - }, - } -} diff --git a/other/whats-a-test/3.todo.js b/other/whats-a-test/3.todo.js new file mode 100644 index 00000000..c14627c9 --- /dev/null +++ b/other/whats-a-test/3.todo.js @@ -0,0 +1,41 @@ +// writing a testing framework +/* + +Let's improve our error messages a bit, create a function +called `test` which can be given a title and a callback. + +Then add a try/catch so you can log the title with +a note of success or error. + +Then wrap each of your tests in a `test` function. + +This also means that we can run all the tests even +if one of them fails! + +Then run this code with `node 3.todo` + +> Make sure you're in the right directory! + + */ + +const {sum, subtract} = require('./math') + +let result, expected + +result = sum(3, 7) +expected = 10 +expect(result).toBe(expected) + +result = subtract(7, 3) +expected = 4 +expect(result).toBe(expected) + +function expect(actual) { + return { + toBe(expected) { + if (actual !== expected) { + throw new Error(`${actual} is not equal to ${expected}`) + } + }, + } +} diff --git a/other/whats-a-test/4.js b/other/whats-a-test/4.js index b2d353a8..54398359 100644 --- a/other/whats-a-test/4.js +++ b/other/whats-a-test/4.js @@ -1,20 +1,16 @@ +// Now we can remove our own little framework +// and run this file with jest! +// Run `./jest` const {sum, subtract} = require('./math') -/* - -Let's improve our error messages a bit, create a function -called `test` which can be given a title and a callback. - -Then add a try/catch so you can log the title with -a note of success or error. - -Then wrap each of your tests in a `test` function. - -This also means that we can run all the tests even -if one of them fails! - -Then run this code with `node 4` - -> Make sure you're in the right directory! - - */ +test('sum adds numbers', () => { + const result = sum(3, 7) + const expected = 10 + expect(result).toBe(expected) +}) + +test('subtract subtracts numbers', () => { + const result = subtract(7, 3) + const expected = 4 + expect(result).toBe(expected) +}) diff --git a/other/whats-a-test/4.solution.js b/other/whats-a-test/4.solution.js deleted file mode 100644 index 83af05de..00000000 --- a/other/whats-a-test/4.solution.js +++ /dev/null @@ -1,33 +0,0 @@ -const {sum, subtract} = require('./math') - -test('sum adds numbers', () => { - const result = sum(3, 7) - const expected = 10 - expect(result).toBe(expected) -}) - -test('subtract subtracts numbers', () => { - const result = subtract(7, 3) - const expected = 4 - expect(result).toBe(expected) -}) - -function test(title, callback) { - try { - callback() - console.log(`✓ ${title}`) - } catch (error) { - console.error(`✕ ${title}`) - console.error(error) - } -} - -function expect(actual) { - return { - toBe(expected) { - if (actual !== expected) { - throw new Error(`${actual} is not equal to ${expected}`) - } - }, - } -} diff --git a/other/whats-a-test/5.js b/other/whats-a-test/5.js deleted file mode 100644 index 54398359..00000000 --- a/other/whats-a-test/5.js +++ /dev/null @@ -1,16 +0,0 @@ -// Now we can remove our own little framework -// and run this file with jest! -// Run `./jest` -const {sum, subtract} = require('./math') - -test('sum adds numbers', () => { - const result = sum(3, 7) - const expected = 10 - expect(result).toBe(expected) -}) - -test('subtract subtracts numbers', () => { - const result = subtract(7, 3) - const expected = 4 - expect(result).toBe(expected) -}) diff --git a/other/whats-a-test/jest.config.js b/other/whats-a-test/jest.config.js index 1f9c279e..97cea009 100644 --- a/other/whats-a-test/jest.config.js +++ b/other/whats-a-test/jest.config.js @@ -1,3 +1,3 @@ module.exports = { - testMatch: ['**/5.js'] + testMatch: ['**/4.js'], }