Skip to content

Commit

Permalink
Merge next into main (#142)
Browse files Browse the repository at this point in the history
* Update for v5 (#133)

* up

* add precommit

* eslint 8

* lexical

* test: migrate away from jest (#134)

* almost everything is working, struggling with the stream

* went away from the stream and just reading the logs

* cleaner code

* removed the exception for linting

* the test path was useless

* scripts are in alphabetical order

* updated the package.json file to remove the :coverage job and moved that logic into the base test one

* removed jest, after it has been reintroduced merging the branch "next"

---------

Co-authored-by: Giovanni Bucci <[email protected]>

---------

Co-authored-by: Gürgün Dayıoğlu <[email protected]>
Co-authored-by: Giovanni Bucci <[email protected]>
Co-authored-by: Giovanni Bucci <[email protected]>
  • Loading branch information
4 people authored Jun 19, 2024
1 parent b34cc61 commit 01dbfc9
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 132 deletions.
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

40 changes: 6 additions & 34 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: ci
name: CI

on:
push:
Expand All @@ -16,36 +16,8 @@ on:
- '*.md'

jobs:
build:
runs-on: ${{ matrix.os }}

strategy:
matrix:
node-version: [14, 16, 18, 20]
os: [macos-latest, ubuntu-latest]

steps:
- uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Install
run: |
npm install
- name: Run Tests
run: |
npm run test:ci
automerge:
needs: build
runs-on: ubuntu-latest
permissions:
pull-requests: write
contents: write
steps:
- uses: fastify/github-action-merge-dependabot@v3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
test:
uses: fastify/workflows/.github/workflows/[email protected]
with:
license-check: true
lint: true
30 changes: 0 additions & 30 deletions jest.config.json

This file was deleted.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
"type": "commonjs",
"types": "lib/index.d.ts",
"scripts": {
"test": "jest --config=jest.config.json",
"test:coverage": "jest --config=jest.config.json --coverage",
"test:ci": "npm run lint && npm run test:typescript && npm run test:coverage",
"test": "c8 --100 node --test",
"test:ci": "npm run lint && npm run test:typescript && npm run test",
"test:typescript": "tsd",
"lint": "eslint \"lib/**/*.js\" lib/index.js",
"lint:fix": "eslint --fix \"lib/**/*.js\" lib/index.js",
Expand All @@ -30,13 +29,14 @@
"fastify": "^4.0.0"
},
"devDependencies": {
"@types/node": "^20.8.6",
"awilix": "^10.0.1",
"eslint": "^8.51.0",
"@fastify/pre-commit": "^2.1.0",
"@types/node": "^20.12.7",
"awilix": "^10.0.2",
"c8": "^9.1.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"fastify": "^4.24.2",
"jest": "^29.7.0",
"fastify": "^4.26.2",
"prettier": "^3.2.5",
"tsd": "^0.31.0"
},
Expand Down
11 changes: 8 additions & 3 deletions test/awilixManager.spec.js → test/awilixManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

const { asClass } = require('awilix')
const fastify = require('fastify')
const { describe, it, beforeEach, afterEach } = require('node:test')
const assert = require('node:assert')

const { diContainer, diContainerClassic, fastifyAwilixPlugin } = require('../lib')

let isInittedGlobal = false
Expand Down Expand Up @@ -40,10 +43,12 @@ describe('awilixManager', () => {
variations.forEach((variation) => {
describe(variation.injectionMode, () => {
let app

beforeEach(() => {
isInittedGlobal = false
isDisposedGlobal = false
})

afterEach(async () => {
await variation.container.dispose()

Expand All @@ -68,7 +73,7 @@ describe('awilixManager', () => {
})
await app.ready()

expect(isInittedGlobal).toBe(true)
assert.equal(isInittedGlobal, true)
})

it('performs async init if enabled', async () => {
Expand All @@ -86,7 +91,7 @@ describe('awilixManager', () => {
})
await app.ready()

expect(isInittedGlobal).toBe(true)
assert.equal(isInittedGlobal, true)
})

it('performs async dispose if enabled', async () => {
Expand All @@ -105,7 +110,7 @@ describe('awilixManager', () => {
await app.ready()
await app.close()

expect(isDisposedGlobal).toBe(true)
assert.equal(isDisposedGlobal, true)
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

const fastify = require('fastify')
const { asClass, Lifetime } = require('awilix')
const { describe, it, beforeEach, afterEach } = require('node:test')
const assert = require('node:assert')

const { fastifyAwilixPlugin, diContainer, diContainerClassic } = require('../lib')

class UserRepository {
Expand All @@ -28,22 +31,38 @@ const variations = [
},
]

describe('fastifyAwilixPlugin', () => {
let app
afterEach(() => {
return app.close()
function getCompletedRequests(output) {
return output.filter((line) => {
try {
return JSON.parse(line).msg === 'request completed'
} catch (e) {
return false
}
})
}

describe('fastifyAwilixPlugin', () => {
let app, output
let write = process.stdout.write

beforeEach(() => {
storedUserRepository = undefined
output = []
process.stdout.write = (str) => output.push(str)
})

afterEach(() => {
process.stdout.write = write
return app.close()
})

variations.forEach((variation) => {
describe(variation.injectionMode, () => {
const endpoint = async (req, res) => {
const userRepository = app.diContainer.resolve('userRepository')
storedUserRepository = userRepository
expect(userRepository.disposeCounter).toBe(0)

assert.equal(userRepository.disposeCounter, 0)

res.send({
status: 'OK',
Expand All @@ -56,8 +75,9 @@ describe('fastifyAwilixPlugin', () => {
const userRepositoryScoped = req.diScope.resolve('userRepositoryScoped')
storedUserRepository = userRepository
storedUserRepositoryScoped = userRepositoryScoped
expect(userRepository.disposeCounter).toBe(0)
expect(userRepositoryScoped.disposeCounter).toBe(0)

assert.equal(userRepository.disposeCounter, 0)
assert.equal(userRepositoryScoped.disposeCounter, 0)

res.send({
status: 'OK',
Expand All @@ -82,11 +102,13 @@ describe('fastifyAwilixPlugin', () => {
await app.ready()

const response = await app.inject().post('/').end()
expect(response.statusCode).toBe(200)
expect(storedUserRepository.disposeCounter).toBe(0)

assert.equal(response.statusCode, 200)
assert.equal(storedUserRepository.disposeCounter, 0)

await app.close()
expect(storedUserRepository.disposeCounter).toBe(1)

assert.equal(storedUserRepository.disposeCounter, 1)
})

it('do not dispose app-scoped singletons on sending response', async () => {
Expand All @@ -107,11 +129,13 @@ describe('fastifyAwilixPlugin', () => {
await app.ready()

const response = await app.inject().post('/').end()
expect(response.statusCode).toBe(200)
expect(storedUserRepository.disposeCounter).toBe(0)

assert.equal(response.statusCode, 200)
assert.equal(storedUserRepository.disposeCounter, 0)

await app.close()
expect(storedUserRepository.disposeCounter).toBe(0)

assert.equal(storedUserRepository.disposeCounter, 0)
})

it('do not attempt to dispose request scope if response was returned before it was even created', async () => {
Expand Down Expand Up @@ -142,12 +166,14 @@ describe('fastifyAwilixPlugin', () => {
await app.ready()

const response = await app.inject().options('/').end()
expect(response.statusCode).toBe(200)
expect(storedUserRepository).toBeUndefined()

assert.equal(response.statusCode, 200)
assert.equal(storedUserRepository, undefined)

await app.close()
expect(storedError).toBeNull()
expect(storedUserRepository).toBeUndefined()

assert.equal(storedError, null)
assert.equal(storedUserRepository, undefined)
})

it('dispose request-scoped singletons on sending response', async () => {
Expand Down Expand Up @@ -178,13 +204,15 @@ describe('fastifyAwilixPlugin', () => {
await app.ready()

const response = await app.inject().post('/').end()
expect(response.statusCode).toBe(200)
expect(storedUserRepositoryScoped.disposeCounter).toBe(1)
expect(storedUserRepository.disposeCounter).toBe(0)

assert.equal(response.statusCode, 200)
assert.equal(storedUserRepositoryScoped.disposeCounter, 1)
assert.equal(storedUserRepository.disposeCounter, 0)

await app.close()
expect(storedUserRepositoryScoped.disposeCounter).toBe(1)
expect(storedUserRepository.disposeCounter).toBe(0)

assert.equal(storedUserRepositoryScoped.disposeCounter, 1)
assert.equal(storedUserRepository.disposeCounter, 0)
})

it('do not dispose request-scoped singletons twice on closing app', async () => {
Expand Down Expand Up @@ -215,25 +243,20 @@ describe('fastifyAwilixPlugin', () => {
await app.ready()

const response = await app.inject().post('/').end()
expect(response.statusCode).toBe(200)
expect(storedUserRepositoryScoped.disposeCounter).toBe(1)
expect(storedUserRepository.disposeCounter).toBe(0)

assert.equal(response.statusCode, 200)
assert.equal(storedUserRepositoryScoped.disposeCounter, 1)
assert.equal(storedUserRepository.disposeCounter, 0)

await app.close()
expect(storedUserRepositoryScoped.disposeCounter).toBe(1)
expect(storedUserRepository.disposeCounter).toBe(1)

assert.equal(storedUserRepositoryScoped.disposeCounter, 1)
assert.equal(storedUserRepository.disposeCounter, 1)
})
})

describe('response logging', () => {
it('should only produce one "request completed" log with dispose settings enabled', async () => {
let requestCompletedLogCount = 0
jest.spyOn(process.stdout, 'write').mockImplementation((data) => {
const log = JSON.parse(data)
if (log.msg === 'request completed') requestCompletedLogCount += 1
return true
})

app = fastify({ logger: true })
app.register(fastifyAwilixPlugin, {
disposeOnClose: true,
Expand All @@ -253,18 +276,14 @@ describe('fastifyAwilixPlugin', () => {
await app.inject().post('/').end()

await app.close()
expect(requestCompletedLogCount).toBe(1)

const completedRequests = getCompletedRequests(output)
assert.equal(completedRequests.length, 1)
})

it('should only produce one "request completed" log with dispose settings disabled', async () => {
let requestCompletedLogCount = 0
jest.spyOn(process.stdout, 'write').mockImplementation((data) => {
const log = JSON.parse(data)
if (log.msg === 'request completed') requestCompletedLogCount += 1
return true
})

app = fastify({ logger: true })

app.register(fastifyAwilixPlugin, {
disposeOnClose: false,
disposeOnResponse: false,
Expand All @@ -283,7 +302,9 @@ describe('fastifyAwilixPlugin', () => {
await app.inject().post('/').end()

await app.close()
expect(requestCompletedLogCount).toBe(1)

const completedRequests = getCompletedRequests(output)
assert.equal(completedRequests.length, 1)
})
})
})
Expand Down
Loading

0 comments on commit 01dbfc9

Please sign in to comment.