Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate to node test runner #1129

Merged
merged 12 commits into from Feb 1, 2024
12,192 changes: 4,227 additions & 7,965 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions package.json
Expand Up @@ -15,7 +15,7 @@
"db:migrate": "postgrator",
"db:down": "docker compose down",
"lint": "eslint --ext .ts,.js ./src",
"test": "tap && c8 report --check-coverage --temp-directory .tap/coverage --100"
"test": "c8 --check-coverage --100 node --test"
barelyhuman marked this conversation as resolved.
Show resolved Hide resolved
},
"dependencies": {
"@fastify/autoload": "^5.7.1",
Expand All @@ -39,7 +39,6 @@
"@types/http-errors": "^2.0.2",
"@types/node": "^20.8.6",
"@types/sinon": "^17.0.1",
"@types/tap": "^15.0.8",
"@typescript-eslint/eslint-plugin": "^5.60.1",
"@typescript-eslint/parser": "^5.62.0",
"c8": "^8.0.1",
Expand All @@ -49,7 +48,6 @@
"postgrator-cli": "^7.0.0",
"prettier": "^3.0.3",
"sinon": "^17.0.1",
"tap": "^18.7.0",
"ts-node-dev": "^2.0.0",
"typescript": "^5.2.2"
},
Expand Down
44 changes: 21 additions & 23 deletions slides.md
Expand Up @@ -653,7 +653,7 @@ https://www.fastify.io/docs/latest/Guides/Testing/
<div class="dense">

- Write a unit test for the `index.js` module
- Use `node-tap`
- Use `node --test`
- Use `fastify.inject`
- Check that GETting the `/users` route:
- Responds with status code 200
Expand All @@ -669,21 +669,20 @@ https://www.fastify.io/docs/latest/Guides/Testing/

```js
// test/index.test.js
import t from 'tap'

import buildServer from '../index.js'

const { test } = t
import {test} from "node:test"
import assert from "node:assert/strict"

test('GET /users', async t => {
t.test('returns users', async t => {
await t.test('returns users', async t => {
const fastify = buildServer()

const res = await fastify.inject('/users')

t.equal(res.statusCode, 200)
assert.equal(res.statusCode, 200)

t.same(res.json(), [
assert.deepEqual(res.json(), [
{ username: 'alice' },
{ username: 'bob' },
])
Expand All @@ -699,22 +698,21 @@ test('GET /users', async t => {

```bash
❯ npm run test
$ tap
test/index.test.js 1> [1612531547285] INFO (63699 on Softwares-MBP): Fastify is starting up!
test/index.test.js 1> [1612531547371] INFO (63699 on Softwares-MBP): incoming request
test/index.test.js 1> ...
PASS test/index.test.js 2 OK 123.827ms

🌈 SUMMARY RESULTS 🌈

Suites: 1 passed, 1 of 1 completed
Asserts: 2 passed, of 2
Time: 770.511ms
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 0 | 0 | 0 | 0 | |
----------|----------|----------|----------|----------|-------------------|
$ node --test
[10:30:06.058] INFO (1601): Fastify is starting up!
[10:30:06.098] INFO (1601): incoming request
...
✔ test/index.test.js (123.827ms)

ℹ tests 3
ℹ suites 0
ℹ pass 3
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 346.373708

✨ Done in 2.70s.
```

Expand Down
2 changes: 1 addition & 1 deletion src/step-03-logging/routes/users.js
Expand Up @@ -2,7 +2,7 @@
* @type {import('fastify').FastifyPluginAsync}
* */
export default async function users(fastify) {
fastify.get('/users', async (req) => {
fastify.get('/users', async req => {
req.log.info('Users route called')

return [{ username: 'alice' }, { username: 'bob' }]
Expand Down
2 changes: 1 addition & 1 deletion src/step-04-validation/routes/login.js
Expand Up @@ -19,6 +19,6 @@ export default async function login(fastify) {
async req => {
const { username, password } = req.body
return { username, password }
}
},
)
}
2 changes: 1 addition & 1 deletion src/step-04-validation/routes/users.js
Expand Up @@ -2,7 +2,7 @@
* @type {import('fastify').FastifyPluginAsync}
* */
export default async function users(fastify) {
fastify.get('/users', async (req) => {
fastify.get('/users', async req => {
req.log.info('Users route called')

return [{ username: 'alice' }, { username: 'bob' }]
Expand Down
2 changes: 1 addition & 1 deletion src/step-05-constraints/routes/login.js
Expand Up @@ -19,6 +19,6 @@ export default async function login(fastify) {
async req => {
const { username, password } = req.body
return { username, password }
}
},
)
}
2 changes: 1 addition & 1 deletion src/step-05-constraints/routes/users.js
Expand Up @@ -2,7 +2,7 @@
* @type {import('fastify').FastifyPluginAsync}
* */
export default async function users(fastify) {
fastify.get('/users', async (req) => {
fastify.get('/users', async req => {
req.log.info('Users route called')

return [{ username: 'alice' }, { username: 'bob' }]
Expand Down
2 changes: 1 addition & 1 deletion src/step-05-constraints/routes/version.js
Expand Up @@ -2,7 +2,7 @@
* @type {import('fastify').FastifyPluginAsync}
* */
export default async function version(fastify) {
fastify.route({
fastify.route({
method: 'GET',
url: '/version',
constraints: { version: '1.0.0' },
Expand Down
2 changes: 1 addition & 1 deletion src/step-06-testing/package.json
Expand Up @@ -6,6 +6,6 @@
"version": "1.0.0",
"scripts": {
"start": "node server",
"test": "tap --allow-empty-coverage && c8 report --check-coverage --temp-directory .tap/coverage --100"
"test": "c8 --check-coverage --100 node --test"
}
}
2 changes: 1 addition & 1 deletion src/step-06-testing/routes/login.js
Expand Up @@ -19,6 +19,6 @@ export default async function login(fastify) {
async req => {
const { username, password } = req.body
return { username, password }
}
},
)
}
2 changes: 1 addition & 1 deletion src/step-06-testing/routes/users.js
Expand Up @@ -2,7 +2,7 @@
* @type {import('fastify').FastifyPluginAsync}
* */
export default async function users(fastify) {
fastify.get('/users', async (req) => {
fastify.get('/users', async req => {
req.log.info('Users route called')
return [{ username: 'alice' }, { username: 'bob' }]
})
Expand Down
73 changes: 39 additions & 34 deletions src/step-06-testing/test/login.test.js
@@ -1,41 +1,46 @@
import t from 'tap'

const { test } = t
import { test } from 'node:test'
import assert from 'node:assert/strict'

import buildServer from '../index.js'

test('POST /login', async t => {
t.test('returns 400 with an invalid post payload', async t => {
const fastify = buildServer()

const res = await fastify.inject({
url: '/login',
method: 'POST',
body: {
name: 'alice',
passcode: 'alice',
},
})

t.equal(res.statusCode, 400)
})

t.test('returns the data with valid post payload', async t => {
const fastify = buildServer()

const res = await fastify.inject({
url: '/login',
method: 'POST',
body: {
await t.test(
'returns 400 with an invalid post payload',
async () => {
const fastify = buildServer()

const res = await fastify.inject({
url: '/login',
method: 'POST',
body: {
name: 'alice',
passcode: 'alice',
},
})

assert.equal(res.statusCode, 400)
},
)

await t.test(
'returns the data with valid post payload',
async () => {
const fastify = buildServer()

const res = await fastify.inject({
url: '/login',
method: 'POST',
body: {
username: 'alice',
password: 'alice',
},
})

assert.equal(res.statusCode, 200)
assert.deepEqual(res.json(), {
username: 'alice',
password: 'alice',
},
})

t.equal(res.statusCode, 200)
t.same(res.json(), {
username: 'alice',
password: 'alice',
})
})
})
},
)
})
11 changes: 5 additions & 6 deletions src/step-06-testing/test/users.test.js
@@ -1,18 +1,17 @@
import t from 'tap'
import { test } from 'node:test'
import assert from 'node:assert/strict'

import buildServer from '../index.js'

const { test } = t

test('GET /users', async t => {
t.test('returns users', async t => {
await t.test('returns users', async () => {
const fastify = buildServer()

const res = await fastify.inject('/users')

t.equal(res.statusCode, 200)
assert.equal(res.statusCode, 200)

t.same(res.json(), [
assert.deepEqual(res.json(), [
{ username: 'alice' },
{ username: 'bob' },
])
Expand Down
2 changes: 1 addition & 1 deletion src/step-07-serialization/package.json
Expand Up @@ -6,6 +6,6 @@
"version": "1.0.0",
"scripts": {
"start": "node server",
"test": "tap --allow-empty-coverage && c8 report --check-coverage --temp-directory .tap/coverage --100"
"test": "c8 --check-coverage --100 node --test"
}
}
7 changes: 4 additions & 3 deletions src/step-07-serialization/routes/login.js
Expand Up @@ -22,7 +22,8 @@ export default async function login(fastify) {
* @type {import('fastify').RouteHandler<{ Body: { username: string; password: string } }>}
* */
async req => {
const { username, password } = req.body
return { username, password }
})
const { username, password } = req.body
return { username, password }
},
)
}
4 changes: 2 additions & 2 deletions src/step-07-serialization/routes/users.js
Expand Up @@ -3,7 +3,7 @@ import S from 'fluent-json-schema'
const schema = {
response: {
200: S.array().items(
S.object().prop('username', S.string().required())
S.object().prop('username', S.string().required()),
),
},
}
Expand All @@ -12,7 +12,7 @@ const schema = {
* @type {import('fastify').FastifyPluginAsync}
* */
export default async function users(fastify) {
fastify.get('/users', { schema }, async (req) => {
fastify.get('/users', { schema }, async req => {
req.log.info('Users route called')

return [{ username: 'alice' }, { username: 'bob' }]
Expand Down
73 changes: 39 additions & 34 deletions src/step-07-serialization/test/login.test.js
@@ -1,41 +1,46 @@
import t from 'tap'

const { test } = t
import { test } from 'node:test'
import assert from 'node:assert/strict'

import buildServer from '../index.js'

test('POST /login', async t => {
t.test('returns 400 with an invalid post payload', async t => {
const fastify = buildServer()

const res = await fastify.inject({
url: '/login',
method: 'POST',
body: {
name: 'alice',
passcode: 'alice',
},
})

t.equal(res.statusCode, 400)
})

t.test('returns the data with valid post payload', async t => {
const fastify = buildServer()

const res = await fastify.inject({
url: '/login',
method: 'POST',
body: {
await t.test(
'returns 400 with an invalid post payload',
async () => {
const fastify = buildServer()

const res = await fastify.inject({
url: '/login',
method: 'POST',
body: {
name: 'alice',
passcode: 'alice',
},
})

assert.equal(res.statusCode, 400)
},
)

await t.test(
'returns the data with valid post payload',
async () => {
const fastify = buildServer()

const res = await fastify.inject({
url: '/login',
method: 'POST',
body: {
username: 'alice',
password: 'alice',
},
})

assert.equal(res.statusCode, 200)
assert.deepEqual(res.json(), {
username: 'alice',
password: 'alice',
},
})

t.equal(res.statusCode, 200)
t.same(res.json(), {
username: 'alice',
password: 'alice',
})
})
})
},
)
})