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

feat: upgrade versions and move to node test runner #32

Merged
merged 5 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions fastify-postgres/.postgratorrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"migrationPattern": "migrations/*",
"driver": "pg",
"host": "localhost",
"port": 5432,
"database": "fastify_postgres",
"username": "postgres",
"password": "postgres"
}
5 changes: 0 additions & 5 deletions fastify-postgres/compose-file/migration-down.yaml

This file was deleted.

5 changes: 0 additions & 5 deletions fastify-postgres/compose-file/migration-up.yaml

This file was deleted.

5 changes: 0 additions & 5 deletions fastify-postgres/compose-file/test.yaml

This file was deleted.

27 changes: 0 additions & 27 deletions fastify-postgres/migration.js

This file was deleted.

14 changes: 7 additions & 7 deletions fastify-postgres/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"description": "Example on how to use, fastify with postgres database unit and integration test",
"main": "index.js",
"scripts": {
"test": "tap",
"test": "node --test",
"start": "node index.js",
"start:db": "docker-compose up -d db",
"migrate:up": "MIGRATE_ACTION=do node migration.js",
"migrate:down": "MIGRATE_ACTION=undo node migration.js"
"migrate:up": "npx postgrator",
"migrate:down": "npx postgrator 0"
},
"keywords": [
"postgres",
Expand All @@ -17,11 +17,11 @@
"author": "Manda Putra <manda@omg.lol> (https://github.com/mandaputtra)",
"license": "MIT",
"dependencies": {
"fastify": "^3.20.2",
"fastify-postgres": "^3.6.0",
"pg": "^8.7.1"
"fastify": "^5.2.1",
"fastify-postgres": "^3.7.0",
"pg": "^8.13.1"
},
"devDependencies": {
"tap": "^15.0.9"
"postgrator-cli": "^9.0.1"
}
}
38 changes: 19 additions & 19 deletions fastify-postgres/test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"use strict";

const tap = require("tap");
const {test} = require("node:test");
const fastify = require("./server");

tap.test("should test all API endpoint", async (t) => {
test("should test all API endpoint", async (t) => {
let book = {};

const app = await fastify();

t.teardown(() => app.close());
t.after(() => app.close());
// only to test error path
t.test("POST / should success create item", async (t) => {
await t.test("POST / should success create item", async (t) => {
const response = await app.inject({
method: "POST",
url: "/",
Expand All @@ -19,34 +19,34 @@ tap.test("should test all API endpoint", async (t) => {
},
});
const json = response.json();
t.equal(response.statusCode, 200);
t.equal(json[0].title, "Hello, World!");
t.assert.deepStrictEqual(response.statusCode, 200);
t.assert.deepStrictEqual(json[0].title, "Hello, World!");
// assign created to use as compare value
book = json[0];
});

t.test("GET / should success return items", async (t) => {
await t.test("GET / should success return items", async (t) => {
const response = await app.inject({
method: "GET",
url: "/",
});
const json = response.json();
t.equal(response.statusCode, 200);
t.equal(json.length > 0, true);
t.equal(json[0].title, "Hello, World!");
t.assert.deepStrictEqual(response.statusCode, 200);
t.assert.deepStrictEqual(json.length > 0, true);
t.assert.deepStrictEqual(json[0].title, "Hello, World!");
});

t.test("GET /:id should success return item", async (t) => {
await t.test("GET /:id should success return item", async (t) => {
const response = await app.inject({
method: "GET",
url: `/${book.id}`,
});
const json = response.json();
t.equal(response.statusCode, 200);
t.equal(json[0].title, "Hello, World!");
t.assert.deepStrictEqual(response.statusCode, 200);
t.assert.deepStrictEqual(json[0].title, "Hello, World!");
});

t.test("UPDATE /:id should success return item", async (t) => {
await t.test("UPDATE /:id should success return item", async (t) => {
const response = await app.inject({
method: "PATCH",
url: `/${book.id}`,
Expand All @@ -55,17 +55,17 @@ tap.test("should test all API endpoint", async (t) => {
},
});
const json = response.json();
t.equal(response.statusCode, 200);
t.equal(json[0].title, "Hello again, World!");
t.assert.deepStrictEqual(response.statusCode, 200);
t.assert.deepStrictEqual(json[0].title, "Hello again, World!");
});

t.test("DELETE /:id should success delete item", async (t) => {
await t.test("DELETE /:id should success delete item", async (t) => {
const response = await app.inject({
method: "DELETE",
url: `/${book.id}`,
});
const json = response.json();
t.equal(response.statusCode, 200);
t.equal(json.rowCount, 1);
t.assert.deepStrictEqual(response.statusCode, 200);
t.assert.deepStrictEqual(json.rowCount, 1);
});
});
9 changes: 4 additions & 5 deletions fastify-session-authentication/authentication.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const fastify = require('fastify')
const fastifySession = require('@fastify/session')
const fastifyCookie = require('@fastify/cookie')
const fastifyFormbody = require('@fastify/formbody')
Expand All @@ -22,25 +21,25 @@ function plugin (instance, options, next) {
reply.type('text/html')
reply.send(loginPage())
})

// add a login route that handles the actual login
instance.post('/login', (request, reply) => {
const { email, password } = request.body

if (password === 'abcdef') {
request.session.authenticated = true
reply.type('text/html')
reply.send(defaultPage(true))
} else {
reply.redirect(401, '/login')
reply.redirect('/login', 401)
}
});

instance.get('/', (request, reply) => {
reply.type('text/html')
reply.send(defaultPage(request.session.authenticated))
});

// add a logout route
instance.get('/logout', (request, reply) => {
if (request.session.authenticated) {
Expand Down
46 changes: 23 additions & 23 deletions fastify-session-authentication/authentication.test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const plugin = require('./authentication')
const axios = require('axios')
const fastify = require('fastify')

test('should be able to login', async (t) => {
t.plan(3)

const app = await server()
const { port } = app.server.address()

const result0 = await performLogin(port, 'abcdef')
const result1 = await toHomePage(port, result0.sessionCookie)

t.ok(result0.body.includes('logged in'))
t.ok(result1.body.includes('logged in'))
t.equal(result1.sessionCookie, result0.sessionCookie)
t.assert.ok(result0.body.includes('logged in'))
t.assert.ok(result1.body.includes('logged in'))
t.assert.equal(result1.sessionCookie, result0.sessionCookie)
app.close()
})

test('should not be able to login with wrong password', async (t) => {
t.plan(2)

const app = await server()
const { port } = app.server.address()

const { location, statusCode } = await performLogin(port, '123456')

t.equal(location, '/login')
t.equal(statusCode, 401)
t.assert.equal(location, '/login')
t.assert.equal(statusCode, 401)
app.close()
})

Expand All @@ -38,12 +38,12 @@ test('should be not logged in', async (t) => {

const app = await server()
const { port } = app.server.address()

// login route
const { sessionCookie, body } = await requestPath(port)

t.ok(body.includes('please login'))
t.ok(sessionCookie.includes('sessionId'))
t.assert.ok(body.includes('please login'))
t.assert.ok(sessionCookie.includes('sessionId'))
app.close()
})

Expand All @@ -52,16 +52,16 @@ test('should be able to logout', async (t) => {

const app = await server()
const { port } = app.server.address()

const result0 = await performLogin(port, 'abcdef')
const result1 = await toHomePage(port, result0.sessionCookie)
const result2 = await logout(port, result0.sessionCookie)
const result3 = await toHomePage(port, result2.sessionCookie)

t.ok(result1.body.includes('logged in'))
t.ok(result3.body.includes('please login'))
t.ok(result3.sessionCookie === result2.sessionCookie)
t.ok(result0.sessionCookie !== result2.sessionCookie)
t.assert.ok(result1.body.includes('logged in'))
t.assert.ok(result3.body.includes('please login'))
t.assert.ok(result3.sessionCookie === result2.sessionCookie)
t.assert.ok(result0.sessionCookie !== result2.sessionCookie)
app.close()
})

Expand All @@ -70,25 +70,25 @@ test('should be able to call logout if not logged in', async (t) => {

const app = await server()
const { port } = app.server.address()

// logout route
const result0 = await requestPath(port, 'logout')
const result1 = await toHomePage(port, result0.sessionCookie)
t.ok(result1.body.includes('please login'))
t.ok(result1.sessionCookie === result0.sessionCookie)

t.assert.ok(result1.body.includes('please login'))
t.assert.ok(result1.sessionCookie === result0.sessionCookie)
app.close()
})

test('should be able to request login page', async (t) => {
t.plan(1)

const app = await server()
const { port } = app.server.address()

const { statusCode } = await toLoginPage(port)

t.equal(statusCode, 200)
t.assert.equal(statusCode, 200)
app.close()
})

Expand Down
15 changes: 7 additions & 8 deletions fastify-session-authentication/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
"main": "authentication.js",
"scripts": {
"start": "fastify start authentication.js -l debug -P",
"test": "tap authentication.test.js"
"test": "node --test"
},
"author": "Denis Fäcke",
"license": "MIT",
"dependencies": {
"@fastify/cookie": "^7.2.0",
"@fastify/formbody": "^7.0.1",
"@fastify/session": "^9.0.0",
"fastify": "^4.2.1"
"@fastify/cookie": "^11.0.2",
"@fastify/formbody": "^8.0.2",
"@fastify/session": "^11.1.0",
"fastify": "^5.2.1"
},
"devDependencies": {
"axios": "^0.27.2",
"fastify-cli": "^4.3.0",
"tap": "^16.3.0"
"axios": "^1.7.9",
"fastify-cli": "^7.3.0"
}
}
2 changes: 1 addition & 1 deletion tests/database-connection/mongodb-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function build (t) {
const app = Fastify()
app.register(fp(App), config())

t.teardown(async () => {
t.after(async () => {
await clean(app.mongo.client.db(database))
await app.close()
})
Expand Down
15 changes: 6 additions & 9 deletions tests/database-connection/mongodb.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
const { test } = require('tap')
const { test } = require('node:test')
const { build } = require('./mongodb-setup')

test('GET / • returns 200', (t) => {
test('GET / • returns 200', async (t) => {
t.plan(2);
const fastify = build(t);

fastify.inject(
const response = await fastify.inject(
{
method: 'GET',
url: '/',
},
(err, response) => {
t.error(err);
t.strictEqual(response.statusCode, 200);
},
);
});

t.assert.strictEqual(response.statusCode, 200);
});
Loading