├── .gitignore ├── CODE_OF_CONDUCT.md ├── LICENSE.md ├── lessons ├── simple.js ├── globals.js ├── jest.test.js ├── assertion-library.js ├── testing-framework.js └── async-await.js ├── package.json ├── math.js ├── setup-globals.js ├── .all-contributorsrc ├── CONTRIBUTING.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .eslintcache 3 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | Please refer to [kentcdodds.com/conduct/](https://kentcdodds.com/conduct/) 2 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | This material is available for private, non-commercial use under the 2 | [GPL version 3](http://www.gnu.org/licenses/gpl-3.0-standalone.html). If you 3 | would like to use this material to conduct your own workshop, please contact me 4 | at me@kentcdodds.com 5 | -------------------------------------------------------------------------------- /lessons/simple.js: -------------------------------------------------------------------------------- 1 | const {sum, subtract} = require('../math') 2 | 3 | let result, expected 4 | 5 | result = sum(3, 7) 6 | expected = 10 7 | if (result !== expected) { 8 | throw new Error(`${result} is not equal to ${expected}`) 9 | } 10 | 11 | result = subtract(7, 3) 12 | expected = 4 13 | if (result !== expected) { 14 | throw new Error(`${result} is not equal to ${expected}`) 15 | } 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "js-testing-fundamentals", 3 | "version": "1.0.0", 4 | "description": "A repo for my JavaScript Testing Fundamentals course", 5 | "scripts": { 6 | "test": "jest" 7 | }, 8 | "keywords": [], 9 | "author": "Kent C. Dodds (https://kentcdodds.com/)", 10 | "license": "MIT", 11 | "devDependencies": { 12 | "jest": "^26.6.3" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /math.js: -------------------------------------------------------------------------------- 1 | // sum is intentionally broken so you can see errors in the tests 2 | const sum = (a, b) => a - b 3 | const subtract = (a, b) => a - b 4 | 5 | // these are kinda pointless I know, but it's just to simulate an async function 6 | const sumAsync = (...args) => Promise.resolve(sum(...args)) 7 | const subtractAsync = (...args) => Promise.resolve(subtract(...args)) 8 | 9 | module.exports = {sum, subtract, sumAsync, subtractAsync} 10 | -------------------------------------------------------------------------------- /lessons/globals.js: -------------------------------------------------------------------------------- 1 | const {sumAsync, subtractAsync} = require('../math') 2 | 3 | test('sumAsync adds numbers asynchronously', async () => { 4 | const result = await sumAsync(3, 7) 5 | const expected = 10 6 | expect(result).toBe(expected) 7 | }) 8 | 9 | test('subtractAsync subtracts numbers asynchronously', async () => { 10 | const result = await subtractAsync(7, 3) 11 | const expected = 4 12 | expect(result).toBe(expected) 13 | }) 14 | -------------------------------------------------------------------------------- /lessons/jest.test.js: -------------------------------------------------------------------------------- 1 | const {sumAsync, subtractAsync} = require('../math') 2 | 3 | test('sumAsync adds numbers asynchronously', async () => { 4 | const result = await sumAsync(3, 7) 5 | const expected = 10 6 | expect(result).toBe(expected) 7 | }) 8 | 9 | test('subtractAsync subtracts numbers asynchronously', async () => { 10 | const result = await subtractAsync(7, 3) 11 | const expected = 4 12 | expect(result).toBe(expected) 13 | }) 14 | -------------------------------------------------------------------------------- /lessons/assertion-library.js: -------------------------------------------------------------------------------- 1 | const {sum, subtract} = require('../math') 2 | 3 | let result, expected 4 | 5 | result = sum(3, 7) 6 | expected = 10 7 | expect(result).toBe(expected) 8 | 9 | result = subtract(7, 3) 10 | expected = 4 11 | expect(result).toBe(expected) 12 | 13 | function expect(actual) { 14 | return { 15 | toBe(expected) { 16 | if (actual !== expected) { 17 | throw new Error(`${actual} is not equal to ${expected}`) 18 | } 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /setup-globals.js: -------------------------------------------------------------------------------- 1 | async function test(title, callback) { 2 | try { 3 | await callback() 4 | console.log(`✓ ${title}`) 5 | } catch (error) { 6 | console.error(`✕ ${title}`) 7 | console.error(error) 8 | } 9 | } 10 | 11 | function expect(actual) { 12 | return { 13 | toBe(expected) { 14 | if (actual !== expected) { 15 | throw new Error(`${actual} is not equal to ${expected}`) 16 | } 17 | } 18 | } 19 | } 20 | 21 | global.test = test 22 | global.expect = expect 23 | -------------------------------------------------------------------------------- /lessons/testing-framework.js: -------------------------------------------------------------------------------- 1 | const {sum, subtract} = require('../math') 2 | 3 | test('sum adds numbers', () => { 4 | const result = sum(3, 7) 5 | const expected = 10 6 | expect(result).toBe(expected) 7 | }) 8 | 9 | test('subtract subtracts numbers', () => { 10 | const result = subtract(7, 3) 11 | const expected = 4 12 | expect(result).toBe(expected) 13 | }) 14 | 15 | function test(title, callback) { 16 | try { 17 | callback() 18 | console.log(`✓ ${title}`) 19 | } catch (error) { 20 | console.error(`✕ ${title}`) 21 | console.error(error) 22 | } 23 | } 24 | 25 | function expect(actual) { 26 | return { 27 | toBe(expected) { 28 | if (actual !== expected) { 29 | throw new Error(`${actual} is not equal to ${expected}`) 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /lessons/async-await.js: -------------------------------------------------------------------------------- 1 | const {sumAsync, subtractAsync} = require('../math') 2 | 3 | test('sumAsync adds numbers asynchronously', async () => { 4 | const result = await sumAsync(3, 7) 5 | const expected = 10 6 | expect(result).toBe(expected) 7 | }) 8 | 9 | test('subtractAsync subtracts numbers asynchronously', async () => { 10 | const result = await subtractAsync(7, 3) 11 | const expected = 4 12 | expect(result).toBe(expected) 13 | }) 14 | 15 | async function test(title, callback) { 16 | try { 17 | await callback() 18 | console.log(`✓ ${title}`) 19 | } catch (error) { 20 | console.error(`✕ ${title}`) 21 | console.error(error) 22 | } 23 | } 24 | 25 | function expect(actual) { 26 | return { 27 | toBe(expected) { 28 | if (actual !== expected) { 29 | throw new Error(`${actual} is not equal to ${expected}`) 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /.all-contributorsrc: -------------------------------------------------------------------------------- 1 | { 2 | "files": [ 3 | "README.md" 4 | ], 5 | "imageSize": 100, 6 | "commit": false, 7 | "contributors": [ 8 | { 9 | "login": "nkabbara", 10 | "name": "Nash Kabbara", 11 | "avatar_url": "https://avatars3.githubusercontent.com/u/31865?v=4", 12 | "profile": "http://nashkabbara.com", 13 | "contributions": [ 14 | "doc" 15 | ] 16 | }, 17 | { 18 | "login": "brunormferreira", 19 | "name": "Bruno Ramires", 20 | "avatar_url": "https://avatars3.githubusercontent.com/u/35575092?v=4", 21 | "profile": "https://brunormferreira.github.io/", 22 | "contributions": [ 23 | "doc" 24 | ] 25 | }, 26 | { 27 | "login": "pbzona", 28 | "name": "Phil Z", 29 | "avatar_url": "https://avatars.githubusercontent.com/u/16768769?v=4", 30 | "profile": "https://github.com/pbzona", 31 | "contributions": [ 32 | "doc" 33 | ] 34 | } 35 | ], 36 | "contributorsPerLine": 7, 37 | "projectName": "js-testing-fundamentals", 38 | "projectOwner": "kentcdodds", 39 | "repoType": "github", 40 | "repoHost": "https://github.com", 41 | "skipCi": true 42 | } 43 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Thanks for being willing to contribute! 4 | 5 | **Working on your first Pull Request?** You can learn how from this _free_ 6 | series [How to Contribute to an Open Source Project on GitHub][egghead] 7 | 8 | ## Project setup 9 | 10 | 1. Fork and clone the repo 11 | 2. Run `npm run setup -s` to install dependencies and run validation 12 | 3. Create a branch for your PR with `git checkout -b pr/your-branch-name` 13 | 14 | > Tip: Keep your `main` branch pointing at the original repository and make 15 | > pull requests from branches on your fork. To do this, run: 16 | > 17 | > ``` 18 | > git remote add upstream https://github.com/kentcdodds/js-testing-fundamentals.git 19 | > git fetch upstream 20 | > git branch --set-upstream-to=upstream/main main 21 | > ``` 22 | > 23 | > This will add the original repository as a "remote" called "upstream," Then 24 | > fetch the git information from that remote, then set your local `main` 25 | > branch to use the upstream main branch whenever you run `git pull`. Then you 26 | > can make all of your pull request branches based on this `main` branch. 27 | > Whenever you want to update your version of `main`, do a regular `git pull`. 28 | 29 | ## Help needed 30 | 31 | Please checkout the [the open issues][issues] 32 | 33 | Also, please watch the repo and respond to questions/bug reports/feature 34 | requests! Thanks! 35 | 36 | [egghead]: 37 | https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github 38 | [issues]: https://github.com/kentcdodds/js-testing-fundamentals/issues 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | JavaScript Testing Fundamentals 3 |

4 | 5 |
6 |

TestingJavaScript.com

7 | 8 | Learn the smart, efficient way to test any JavaScript application. 13 | 14 |
15 | 16 |
17 | 18 |

19 | Learn how automated JavaScript testing works by building your own framework! 20 |

21 | 22 |
23 | 24 | 25 | [![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors-) 26 | 27 | 28 | Order of material: 29 | 30 | 1. `simple.js` 31 | 2. `assertion-library.js` 32 | 3. `testing-framework.js` 33 | 4. `async-await.js` 34 | 5. `globals.js` 35 | 6. `jest.test.js` 36 | 37 | The files are intended to test the `math` module. 38 | 39 | To run the files, run `node lessons/.js`. 40 | 41 | > For the `global` one, run `node -r ./setup-globals.js ./lessons/globals.js` 42 | > 43 | > For the `jest.test.js` one, run `npx jest`. 44 | 45 | ## Contributors ✨ 46 | 47 | Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)): 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 |

Nash Kabbara

📖

Bruno Ramires

📖

Phil Z

📖
59 | 60 | 61 | 62 | 63 | 64 | 65 | This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! 66 | --------------------------------------------------------------------------------