From 101d6bc9e072031211b7e6fe15ecc6d456c6a76e Mon Sep 17 00:00:00 2001 From: Myrotvorets Date: Sat, 30 Nov 2024 07:42:50 +0200 Subject: [PATCH] Update codebase for Node 22 --- package-lock.json | 101 ++--------------------------------- package.json | 5 +- src/lib/wpapi.mts | 1 - test/unit/lib/wpapi.test.mts | 29 ++++------ 4 files changed, 17 insertions(+), 119 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5fa5ce22..a42d96fc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,8 +21,7 @@ "envalid": "^8.0.0", "grammy": "^1.19.2", "knex": "^3.1.0", - "mysql2": "^3.6.5", - "node-fetch": "^3.3.2" + "mysql2": "^3.6.5" }, "devDependencies": { "@myrotvorets/eslint-config-myrotvorets-ts": "^2.24.0", @@ -46,7 +45,7 @@ "mocha-reporter-sonarqube": "^2.0.6", "nodemon": "^3.0.3", "supertest": "^7.0.0", - "testdouble": "^3.20.1", + "testdouble": "^3.20.2", "ts-node": "^10.9.2", "typescript": "^5.3.3" } @@ -5178,9 +5177,9 @@ "license": "MIT" }, "node_modules/cross-spawn": { - "version": "7.0.3", - "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", - "integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==", + "version": "7.0.6", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", + "integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==", "dev": true, "license": "MIT", "dependencies": { @@ -5200,15 +5199,6 @@ "license": "BSD-2-Clause", "peer": true }, - "node_modules/data-uri-to-buffer": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz", - "integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==", - "license": "MIT", - "engines": { - "node": ">= 12" - } - }, "node_modules/data-view-buffer": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/data-view-buffer/-/data-view-buffer-1.0.1.tgz", @@ -7160,29 +7150,6 @@ "reusify": "^1.0.4" } }, - "node_modules/fetch-blob": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz", - "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/jimmywarting" - }, - { - "type": "paypal", - "url": "https://paypal.me/jimmywarting" - } - ], - "license": "MIT", - "dependencies": { - "node-domexception": "^1.0.0", - "web-streams-polyfill": "^3.0.3" - }, - "engines": { - "node": "^12.20 || >= 14.13" - } - }, "node_modules/file-entry-cache": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz", @@ -7332,18 +7299,6 @@ "node": ">= 6" } }, - "node_modules/formdata-polyfill": { - "version": "4.0.10", - "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", - "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==", - "license": "MIT", - "dependencies": { - "fetch-blob": "^3.1.2" - }, - "engines": { - "node": ">=12.20.0" - } - }, "node_modules/formidable": { "version": "3.5.2", "resolved": "https://registry.npmjs.org/formidable/-/formidable-3.5.2.tgz", @@ -9619,43 +9574,6 @@ "node": ">=10" } }, - "node_modules/node-domexception": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", - "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/jimmywarting" - }, - { - "type": "github", - "url": "https://paypal.me/jimmywarting" - } - ], - "license": "MIT", - "engines": { - "node": ">=10.5.0" - } - }, - "node_modules/node-fetch": { - "version": "3.3.2", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz", - "integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==", - "license": "MIT", - "dependencies": { - "data-uri-to-buffer": "^4.0.0", - "fetch-blob": "^3.1.4", - "formdata-polyfill": "^4.0.10" - }, - "engines": { - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/node-fetch" - } - }, "node_modules/node-releases": { "version": "2.0.18", "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.18.tgz", @@ -12126,15 +12044,6 @@ "url": "https://opencollective.com/eslint" } }, - "node_modules/web-streams-polyfill": { - "version": "3.3.3", - "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz", - "integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==", - "license": "MIT", - "engines": { - "node": ">= 8" - } - }, "node_modules/webidl-conversions": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", diff --git a/package.json b/package.json index f83eea13..9794d3e5 100644 --- a/package.json +++ b/package.json @@ -31,8 +31,7 @@ "envalid": "^8.0.0", "grammy": "^1.19.2", "knex": "^3.1.0", - "mysql2": "^3.6.5", - "node-fetch": "^3.3.2" + "mysql2": "^3.6.5" }, "devDependencies": { "@myrotvorets/eslint-config-myrotvorets-ts": "^2.24.0", @@ -56,7 +55,7 @@ "mocha-reporter-sonarqube": "^2.0.6", "nodemon": "^3.0.3", "supertest": "^7.0.0", - "testdouble": "^3.20.1", + "testdouble": "^3.20.2", "ts-node": "^10.9.2", "typescript": "^5.3.3" }, diff --git a/src/lib/wpapi.mts b/src/lib/wpapi.mts index 1f7928c1..a54f4320 100644 --- a/src/lib/wpapi.mts +++ b/src/lib/wpapi.mts @@ -1,4 +1,3 @@ -import fetch from 'node-fetch'; import type { PostData } from './types.mjs'; interface WPPost { diff --git a/test/unit/lib/wpapi.test.mts b/test/unit/lib/wpapi.test.mts index 1afcde4b..cd146e0c 100644 --- a/test/unit/lib/wpapi.test.mts +++ b/test/unit/lib/wpapi.test.mts @@ -1,28 +1,19 @@ +import { mock } from 'node:test'; import { expect } from 'chai'; -import { TestDouble, func, matchers, replaceEsm, when } from 'testdouble'; +import { afterEach } from 'mocha'; import { getFeaturedImageResponse, getFeaturedImageResponseBadURL } from './../../fixtures/featuredimage.mjs'; import { getPostsResponse } from './../../fixtures/posts.mjs'; import type { PostData } from '../../../src/lib/types.mjs'; +import { getFeaturedImageUrl, getPosts } from '../../../src/lib/wpapi.mjs'; describe('wpapi', function () { - let fetchMock: TestDouble; - let Response: typeof import('node-fetch').Response; - let getFeaturedImageUrl: typeof import('../../../src/lib/wpapi.mjs').getFeaturedImageUrl; - let getPosts: typeof import('../../../src/lib/wpapi.mjs').getPosts; - - before(async function () { - fetchMock = func(); - - ({ Response } = await import('node-fetch')); - - await replaceEsm('node-fetch', undefined, fetchMock); - - ({ getFeaturedImageUrl, getPosts } = await import('../../../src/lib/wpapi.mjs')); + afterEach(function () { + mock.reset(); }); describe('getPosts', function () { it('should properly extract data', async function () { - when(fetchMock(matchers.isA(String) as string)).thenResolve(new Response(JSON.stringify(getPostsResponse))); + mock.method(globalThis, 'fetch', () => Promise.resolve(new Response(JSON.stringify(getPostsResponse)))); const expected: PostData[] = [ { @@ -48,8 +39,8 @@ describe('wpapi', function () { describe('getFeaturedImageURL', function () { it('should return image URL', async function () { - when(fetchMock(matchers.isA(String) as string)).thenResolve( - new Response(JSON.stringify(getFeaturedImageResponse)), + mock.method(globalThis, 'fetch', () => + Promise.resolve(new Response(JSON.stringify(getFeaturedImageResponse))), ); const url = await getFeaturedImageUrl('https://example.test', 43762); @@ -57,8 +48,8 @@ describe('wpapi', function () { }); it('should return empty URL if the original image URL is malformed', async function () { - when(fetchMock(matchers.isA(String) as string)).thenResolve( - new Response(JSON.stringify(getFeaturedImageResponseBadURL)), + mock.method(globalThis, 'fetch', () => + Promise.resolve(new Response(JSON.stringify(getFeaturedImageResponseBadURL))), ); const url = await getFeaturedImageUrl('https://example.test', 43762);