Skip to content

Commit 548e371

Browse files
author
Kent C. Dodds
committed
init
0 parents  commit 548e371

File tree

11 files changed

+199
-0
lines changed

11 files changed

+199
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<h1 align="center">
2+
JavaScript Testing Fundamentals
3+
</h1>
4+
5+
<p align="center" style="font-size: 1.2rem;">
6+
Learn how automated JavaScript testing works by building your own framework!
7+
</p>
8+
9+
<hr />
10+
11+
Order of material:
12+
13+
1. `simple.js`
14+
2. `assertion-library.js`
15+
3. `testing-framework.js`
16+
4. `async-await.js`
17+
5. `globals.js`
18+
6. `jest.test.js`
19+
20+
The files are intended to test the `math` module.
21+
22+
To run the files, run `node lessons/<lesson-filename>.js`.
23+
24+
> For the `global` one, run `node -r ./setup-globals.js ./lessons/globals.js`
25+
> For the `jest.test.js` one, run `npx jest`.

lessons/assertion-library.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}

lessons/async-await.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}

lessons/globals.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
})

lessons/jest.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
})

lessons/simple.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}

lessons/testing-framework.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}

math.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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}

package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 <kent@doddsfamily.us> (http://kentcdodds.com/)",
10+
"license": "MIT",
11+
"devDependencies": {
12+
"jest": "^23.1.0"
13+
}
14+
}

0 commit comments

Comments
 (0)