From 01dbfc99ac8f3d9c66d5bac8e5f7f1308f04b402 Mon Sep 17 00:00:00 2001 From: James Sumners <321201+jsumners@users.noreply.github.com> Date: Wed, 19 Jun 2024 15:38:49 -0400 Subject: [PATCH] Merge `next` into `main` (#142) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --------- Co-authored-by: Gürgün Dayıoğlu Co-authored-by: Giovanni Bucci Co-authored-by: Giovanni Bucci --- .eslintignore | 1 - .github/workflows/ci.yml | 40 +------ jest.config.json | 30 ----- package.json | 16 +-- ...xManager.spec.js => awilixManager.test.js} | 11 +- ...js => fastifyAwilixPlugin.dispose.test.js} | 107 +++++++++++------- ...in.spec.js => fastifyAwilixPlugin.test.js} | 32 +++--- 7 files changed, 105 insertions(+), 132 deletions(-) delete mode 100644 .eslintignore delete mode 100644 jest.config.json rename test/{awilixManager.spec.js => awilixManager.test.js} (91%) rename test/{fastifyAwilixPlugin.dispose.spec.js => fastifyAwilixPlugin.dispose.test.js} (78%) rename test/{fastifyAwilixPlugin.spec.js => fastifyAwilixPlugin.test.js} (84%) diff --git a/.eslintignore b/.eslintignore deleted file mode 100644 index 26afc79..0000000 --- a/.eslintignore +++ /dev/null @@ -1 +0,0 @@ -test/fastifyAwilixPlugin.spec.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fad8675..5395d92 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,4 +1,4 @@ -name: ci +name: CI on: push: @@ -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/plugins-ci.yml@v4.1.0 + with: + license-check: true + lint: true diff --git a/jest.config.json b/jest.config.json deleted file mode 100644 index f1e38db..0000000 --- a/jest.config.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "moduleFileExtensions": [ - "ts", - "js", - "json" - ], - "testMatch": [ - "/test/**/*.(spec|test).js" - ], - "testEnvironment": "node", - "reporters": [ - "default" - ], - "coveragePathIgnorePatterns": [ - "/node_modules/", - "/coverage/", - "/test/" - ], - "coverageThreshold": { - "global": { - "statements": 100, - "branches": 100, - "functions": 100, - "lines": 100 - } - }, - "collectCoverageFrom": [ - "lib/**/*.js" - ] -} diff --git a/package.json b/package.json index a2feece..d8f4df7 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" }, diff --git a/test/awilixManager.spec.js b/test/awilixManager.test.js similarity index 91% rename from test/awilixManager.spec.js rename to test/awilixManager.test.js index 35b47e2..e66a61b 100644 --- a/test/awilixManager.spec.js +++ b/test/awilixManager.test.js @@ -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 @@ -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() @@ -68,7 +73,7 @@ describe('awilixManager', () => { }) await app.ready() - expect(isInittedGlobal).toBe(true) + assert.equal(isInittedGlobal, true) }) it('performs async init if enabled', async () => { @@ -86,7 +91,7 @@ describe('awilixManager', () => { }) await app.ready() - expect(isInittedGlobal).toBe(true) + assert.equal(isInittedGlobal, true) }) it('performs async dispose if enabled', async () => { @@ -105,7 +110,7 @@ describe('awilixManager', () => { await app.ready() await app.close() - expect(isDisposedGlobal).toBe(true) + assert.equal(isDisposedGlobal, true) }) }) }) diff --git a/test/fastifyAwilixPlugin.dispose.spec.js b/test/fastifyAwilixPlugin.dispose.test.js similarity index 78% rename from test/fastifyAwilixPlugin.dispose.spec.js rename to test/fastifyAwilixPlugin.dispose.test.js index a158a5c..3c83306 100644 --- a/test/fastifyAwilixPlugin.dispose.spec.js +++ b/test/fastifyAwilixPlugin.dispose.test.js @@ -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 { @@ -28,14 +31,29 @@ 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) => { @@ -43,7 +61,8 @@ describe('fastifyAwilixPlugin', () => { 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', @@ -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', @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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, @@ -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, @@ -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) }) }) }) diff --git a/test/fastifyAwilixPlugin.spec.js b/test/fastifyAwilixPlugin.test.js similarity index 84% rename from test/fastifyAwilixPlugin.spec.js rename to test/fastifyAwilixPlugin.test.js index 96ce138..e9a9662 100644 --- a/test/fastifyAwilixPlugin.spec.js +++ b/test/fastifyAwilixPlugin.test.js @@ -2,6 +2,9 @@ const fastify = require('fastify') const { asValue, asFunction, asClass, Lifetime } = require('awilix') +const { describe, it, afterEach } = require('node:test') +const assert = require('node:assert') + const { diContainer, diContainerClassic, fastifyAwilixPlugin } = require('../lib') class UserServiceClassic { @@ -61,6 +64,7 @@ const variations = [ describe('fastifyAwilixPlugin', () => { let app + afterEach(() => { return app.close() }) @@ -86,12 +90,15 @@ describe('fastifyAwilixPlugin', () => { app = fastify({ logger: true }) const endpoint = async (req, res) => { const userService = app.diContainer.resolve('userService') - expect(userService.userRepository.id).toBe('userRepository') - expect(userService.maxUserName).toBe(10) - expect(userService.maxEmail).toBe(40) + + assert.equal(userService.userRepository.id, 'userRepository') + assert.equal(userService.maxUserName, 10) + assert.equal(userService.maxEmail, 40) const maxUserPassword = await req.diScope.resolve('maxUserPassword') - expect(maxUserPassword).toBe(20) + + assert.equal(maxUserPassword, 20) + res.send({ status: 'OK', }) @@ -115,7 +122,8 @@ describe('fastifyAwilixPlugin', () => { await app.ready() const response = await app.inject().post('/').end() - expect(response.statusCode).toBe(200) + + assert.equal(response.statusCode, 200) }) }) }) @@ -125,14 +133,12 @@ describe('fastifyAwilixPlugin', () => { it('throws an error if both injection mode and container are specified', async () => { app = fastify({ logger: true }) - await expect(() => - app.register(fastifyAwilixPlugin, { - injectionMode: 'CLASSIC', - container: diContainerClassic, - }), - ).rejects.toThrow( - /If you are passing pre-created container explicitly, you cannot specify injection mode/, - ) + await assert.rejects(async () => { + await app.register(fastifyAwilixPlugin, { + injectionMode: 'PROXY', + container: diContainer, + }) + }, /If you are passing pre-created container explicitly, you cannot specify injection mode/) }) }) })