Skip to content

Commit

Permalink
Update codebase for Node 22
Browse files Browse the repository at this point in the history
  • Loading branch information
myrotvorets-team committed Nov 30, 2024
1 parent 9dff4dd commit 101d6bc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 119 deletions.
101 changes: 5 additions & 96 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
},
Expand Down
1 change: 0 additions & 1 deletion src/lib/wpapi.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fetch from 'node-fetch';
import type { PostData } from './types.mjs';

interface WPPost {
Expand Down
29 changes: 10 additions & 19 deletions test/unit/lib/wpapi.test.mts
Original file line number Diff line number Diff line change
@@ -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<typeof import('node-fetch').default>;
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<typeof fetchMock>();

({ 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[] = [
{
Expand All @@ -48,17 +39,17 @@ 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);
return expect(url).to.equal('https://myrotvorets.news/wp-content/uploads/2019/10/Screenshot_5-3.png');
});

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);
Expand Down

0 comments on commit 101d6bc

Please sign in to comment.