Skip to content

Commit a9bf511

Browse files
committed
esm version
1 parent f2a1ac1 commit a9bf511

16 files changed

+663
-258
lines changed

src/__tests__/routes.test.js

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,54 @@
1-
const request = require('supertest');
2-
const { app } = require('../index');
1+
import request from 'supertest';
2+
import { jest } from '@jest/globals';
3+
4+
// Mock the entire utils/db module using ESM-compatible mocking
5+
jest.unstable_mockModule('../utils/db.js', () => {
6+
const mockDoc = {
7+
data: () => ({
8+
technology: 'WordPress',
9+
category: 'CMS',
10+
description: 'A popular content management system',
11+
icon: 'wordpress.svg',
12+
origins: ['WordPress Foundation'],
13+
rank: 'ALL',
14+
geo: 'ALL',
15+
date: '2023-01-01'
16+
}),
17+
get: (field) => {
18+
const mockData = {
19+
technology: 'WordPress',
20+
category: 'CMS',
21+
rank: 'ALL',
22+
geo: 'ALL',
23+
date: '2023-01-01'
24+
};
25+
return mockData[field] || 'Mock Value';
26+
}
27+
};
28+
29+
const mockQuerySnapshot = {
30+
empty: false,
31+
forEach: (callback) => {
32+
callback(mockDoc);
33+
},
34+
docs: [mockDoc]
35+
};
36+
37+
// Create a chainable query mock - avoid infinite recursion
38+
const mockQuery = {
39+
where: jest.fn(),
40+
orderBy: jest.fn(),
41+
limit: jest.fn(),
42+
get: jest.fn().mockResolvedValue(mockQuerySnapshot)
43+
};
44+
45+
// Make the methods chainable by returning the same mock object
46+
mockQuery.where.mockReturnValue(mockQuery);
47+
mockQuery.orderBy.mockReturnValue(mockQuery);
48+
mockQuery.limit.mockReturnValue(mockQuery);
349

4-
// Mock Firestore
5-
jest.mock('../utils/db', () => {
650
const mockFirestoreInstance = {
7-
collection: jest.fn().mockReturnThis(),
8-
where: jest.fn().mockReturnThis(),
9-
orderBy: jest.fn().mockReturnThis(),
10-
limit: jest.fn().mockReturnThis(),
11-
get: jest.fn().mockResolvedValue({
12-
empty: false,
13-
forEach: (callback) => {
14-
// Mock technology data
15-
callback({
16-
data: () => ({
17-
technology: 'WordPress',
18-
category: 'CMS',
19-
description: 'A popular content management system',
20-
icon: 'wordpress.svg',
21-
origins: ['WordPress Foundation']
22-
}),
23-
get: (field) => {
24-
const mockData = {
25-
technology: 'WordPress',
26-
category: 'CMS',
27-
rank: 'ALL',
28-
geo: 'ALL',
29-
date: '2023-01-01'
30-
};
31-
return mockData[field] || 'Mock Value';
32-
}
33-
});
34-
},
35-
docs: [{
36-
data: () => ({
37-
technology: 'WordPress',
38-
category: 'CMS',
39-
description: 'A popular content management system',
40-
icon: 'wordpress.svg',
41-
origins: ['WordPress Foundation'],
42-
date: '2023-01-01'
43-
})
44-
}]
45-
})
51+
collection: jest.fn().mockImplementation((collectionName) => mockQuery)
4652
};
4753

4854
return {
@@ -51,6 +57,9 @@ jest.mock('../utils/db', () => {
5157
};
5258
});
5359

60+
// Import app after mocking
61+
const { app } = await import('../index.js');
62+
5463
describe('API Routes', () => {
5564
describe('Health Check', () => {
5665
it('should return a health check response', async () => {

src/controllers/adoptionController.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
const { firestoreOld } = require('../utils/db');
1+
import { firestoreOld } from '../utils/db.js';
22
const firestore = firestoreOld;
33

4-
const { createSuccessResponse } = require('../utils/helpers');
5-
const {
4+
import { createSuccessResponse } from '../utils/helpers.js';
5+
import {
66
REQUIRED_PARAMS,
77
validateRequiredParams,
88
sendValidationError,
99
applyDateFilters,
1010
applyStandardFilters,
1111
preprocessParams,
1212
handleControllerError
13-
} = require('../utils/controllerHelpers');
13+
} from '../utils/controllerHelpers.js';
1414

1515
const TABLE = 'adoption';
1616

@@ -63,6 +63,6 @@ const listAdoptionData = async (req, res) => {
6363
}
6464
};
6565

66-
module.exports = {
67-
listAdoption: listAdoptionData
66+
export {
67+
listAdoptionData
6868
};

src/controllers/categoriesController.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const { firestore } = require('../utils/db');
2-
const { createSuccessResponse } = require('../utils/helpers');
3-
const { applyArrayFilter, selectFields, handleControllerError } = require('../utils/controllerHelpers');
1+
import { firestore } from '../utils/db.js';
2+
import { createSuccessResponse } from '../utils/helpers.js';
3+
import { applyArrayFilter, selectFields, handleControllerError } from '../utils/controllerHelpers.js';
44

55
/**
66
* List categories with optional filtering and field selection
@@ -42,6 +42,4 @@ const listCategories = async (req, res) => {
4242
}
4343
};
4444

45-
module.exports = {
46-
listCategories
47-
};
45+
export { listCategories };

src/controllers/cwvtechController.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
const { firestoreOld } = require('../utils/db');
1+
import { firestoreOld } from '../utils/db.js';
22
const firestore = firestoreOld;
33

4-
const { createSuccessResponse } = require('../utils/helpers');
5-
const {
4+
import { createSuccessResponse } from '../utils/helpers.js';
5+
import {
66
REQUIRED_PARAMS,
77
validateRequiredParams,
88
sendValidationError,
99
applyDateFilters,
1010
applyStandardFilters,
1111
preprocessParams,
1212
handleControllerError
13-
} = require('../utils/controllerHelpers');
13+
} from '../utils/controllerHelpers.js';
1414

1515
const TABLE = 'core_web_vitals';
1616

@@ -63,6 +63,6 @@ const listCWVTechData = async (req, res) => {
6363
}
6464
};
6565

66-
module.exports = {
67-
listCwvtech: listCWVTechData
66+
export {
67+
listCWVTechData
6868
};

src/controllers/geosController.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const { firestore } = require('../utils/db');
2-
const { createSuccessResponse } = require('../utils/helpers');
3-
const { handleControllerError } = require('../utils/controllerHelpers');
1+
import { firestore } from '../utils/db.js';
2+
import { createSuccessResponse } from '../utils/helpers.js';
3+
import { handleControllerError } from '../utils/controllerHelpers.js';
44

55
/**
66
* List all geographic locations from database
@@ -22,6 +22,6 @@ const listGeos = async (req, res) => {
2222
}
2323
};
2424

25-
module.exports = {
25+
export {
2626
listGeos
2727
};

src/controllers/lighthouseController.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
const { firestoreOld } = require('../utils/db');
1+
import { firestoreOld } from '../utils/db.js';
22
const firestore = firestoreOld;
33

4-
const { createSuccessResponse } = require('../utils/helpers');
5-
const {
4+
import { createSuccessResponse } from '../utils/helpers.js';
5+
import {
66
REQUIRED_PARAMS,
77
validateRequiredParams,
88
sendValidationError,
99
applyDateFilters,
1010
applyStandardFilters,
1111
preprocessParams,
1212
handleControllerError
13-
} = require('../utils/controllerHelpers');
13+
} from '../utils/controllerHelpers.js';
1414

1515
const TABLE = 'lighthouse';
1616

@@ -63,6 +63,6 @@ const listLighthouseData = async (req, res) => {
6363
}
6464
};
6565

66-
module.exports = {
67-
listLighthouse: listLighthouseData
66+
export {
67+
listLighthouseData
6868
};

src/controllers/pageWeightController.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
const { firestoreOld } = require('../utils/db');
1+
import { firestoreOld } from '../utils/db.js';
22
const firestore = firestoreOld;
33

4-
const { createSuccessResponse } = require('../utils/helpers');
5-
const {
4+
import { createSuccessResponse } from '../utils/helpers.js';
5+
import {
66
REQUIRED_PARAMS,
77
validateRequiredParams,
88
sendValidationError,
99
applyDateFilters,
1010
applyStandardFilters,
1111
preprocessParams,
1212
handleControllerError
13-
} = require('../utils/controllerHelpers');
13+
} from '../utils/controllerHelpers.js';
1414

1515
const TABLE = 'page_weight';
1616

@@ -63,6 +63,6 @@ const listPageWeightData = async (req, res) => {
6363
}
6464
};
6565

66-
module.exports = {
67-
listPageWeight: listPageWeightData
66+
export {
67+
listPageWeightData
6868
};

src/controllers/ranksController.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const { firestore } = require('../utils/db');
2-
const { createSuccessResponse } = require('../utils/helpers');
3-
const { handleControllerError } = require('../utils/controllerHelpers');
1+
import { firestore } from '../utils/db.js';
2+
import { createSuccessResponse } from '../utils/helpers.js';
3+
import { handleControllerError } from '../utils/controllerHelpers.js';
44

55
/**
66
* List all rank options from database
@@ -23,6 +23,6 @@ const listRanks = async (req, res) => {
2323
}
2424
};
2525

26-
module.exports = {
26+
export {
2727
listRanks
2828
};

src/controllers/technologiesController.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const { firestore } = require('../utils/db');
2-
const { createSuccessResponse } = require('../utils/helpers');
3-
const { applyArrayFilter, selectFields, handleControllerError } = require('../utils/controllerHelpers');
1+
import { firestore } from '../utils/db.js';
2+
import { createSuccessResponse } from '../utils/helpers.js';
3+
import { applyArrayFilter, selectFields, handleControllerError } from '../utils/controllerHelpers.js';
44

55
// Technology Presenter - optimized with destructuring
66
const presentTechnology = ({ technology, category, description, icon, origins }) => ({
@@ -52,6 +52,6 @@ const listTechnologies = async (req, res) => {
5252
}
5353
};
5454

55-
module.exports = {
55+
export {
5656
listTechnologies
5757
};

src/controllers/versionsController.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const { firestore } = require('../utils/db');
2-
const { createSuccessResponse } = require('../utils/helpers');
3-
const { applyArrayFilter, handleControllerError } = require('../utils/controllerHelpers');
1+
import { firestore } from '../utils/db.js';
2+
import { createSuccessResponse } from '../utils/helpers.js';
3+
import { applyArrayFilter, handleControllerError } from '../utils/controllerHelpers.js';
44

55
/**
66
* List versions with optional technology filtering
@@ -30,6 +30,6 @@ const listVersions = async (req, res) => {
3030
}
3131
};
3232

33-
module.exports = {
33+
export {
3434
listVersions
3535
};

0 commit comments

Comments
 (0)