Skip to content

Commit 9416bbd

Browse files
committed
refactor: convert project to ES modules (ESM)
- Update tsconfig.json to output ES2022 modules instead of CommonJS - Convert all 14 test files from .js to .ts with ESM syntax - Replace all require() statements with import statements - Replace all module.exports with export/export default - Update source code exports to use ESM syntax - Add tsx as dev dependency for TypeScript test execution - Update mocha configuration to handle TypeScript files - All tests passing with ESM module system
1 parent 3f7d4ee commit 9416bbd

25 files changed

+2403
-2003
lines changed

package-lock.json

Lines changed: 505 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"release:major": "npm version major && npm publish",
3939
"changelog": "github_changelog_generator && git add CHANGELOG.md && git commit -am \"Updating changelog\"",
4040
"lint": "eslint --fix .",
41-
"mocha": "mocha --recursive test/",
41+
"mocha": "mocha --require tsx --recursive 'test/**/*.ts'",
4242
"coverage": "nyc npm run mocha",
4343
"test": "npm run lint && npm run build && npm run coverage",
4444
"docker:up": "docker-compose up -d",
@@ -82,6 +82,7 @@
8282
"shx": "^0.3.4",
8383
"sinon": "^21.0.0",
8484
"sqlite3": "^5.1.2",
85+
"tsx": "^4.20.6",
8586
"typescript": "^4.8.4"
8687
}
8788
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,5 @@ function service(options: ElasticsearchServiceOptions) {
167167
return new Service(options)
168168
}
169169

170-
// CommonJS compatible export
171-
export = service
170+
// ESM default export
171+
export default service
Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
1-
const { expect } = require('chai')
2-
const errors = require('@feathersjs/errors')
1+
import { expect } from 'chai'
2+
import errors from '@feathersjs/errors'
33

4-
function create (app, serviceName) {
4+
function create(app: any, serviceName: string) {
55
describe('create()', () => {
66
it('should create an item with provided id', () => {
7-
return app.service(serviceName)
7+
return app
8+
.service(serviceName)
89
.create({ name: 'Bob', id: 'BobId' })
9-
.then(result => {
10+
.then((result: any) => {
1011
expect(result.name).to.equal('Bob')
1112
expect(result.id).to.equal('BobId')
1213

1314
return app.service(serviceName).get('BobId')
1415
})
15-
.then(result => {
16+
.then((result: any) => {
1617
expect(result.name).to.equal('Bob')
1718

1819
return app.service(serviceName).remove('BobId')
1920
})
2021
})
2122

2223
it('should throw Conflict when trying to create an element with existing id', () => {
23-
return app.service(serviceName)
24+
return app
25+
.service(serviceName)
2426
.create({ name: 'Bob', id: 'BobId' })
2527
.then(() => app.service(serviceName).create({ name: 'Bob', id: 'BobId' }))
26-
.then(() => { throw new Error('Should never get here') })
27-
.catch(error => {
28+
.then(() => {
29+
throw new Error('Should never get here')
30+
})
31+
.catch((error: any) => {
2832
expect(error instanceof errors.Conflict).to.be.true
2933

3034
return app.service(serviceName).remove('BobId')
@@ -37,85 +41,84 @@ function create (app, serviceName) {
3741
return service
3842
.create({ name: 'Bob', id: 'BobId' })
3943
.then(() => service.create({ name: 'Box', id: 'BobId' }, { upsert: true }))
40-
.then(result => {
44+
.then((result: any) => {
4145
expect(result.name).to.equal('Box')
4246
expect(result.id).to.equal('BobId')
4347

4448
return service.get('BobId')
4549
})
46-
.then(result => {
50+
.then((result: any) => {
4751
expect(result.name).to.equal('Box')
4852

4953
return service.remove('BobId')
5054
})
5155
})
5256

5357
it('should create items with provided ids (bulk)', () => {
54-
return app.service(serviceName)
58+
return app
59+
.service(serviceName)
5560
.create([
5661
{ name: 'Cal', id: 'CalId' },
57-
{ name: 'Max', id: 'MaxId' }
62+
{ name: 'Max', id: 'MaxId' },
5863
])
59-
.then(results => {
64+
.then((results: any[]) => {
6065
expect(results[0].name).to.equal('Cal')
6166
expect(results[1].name).to.equal('Max')
6267

6368
return app.service(serviceName).find({
6469
query: {
65-
id: { $in: ['CalId', 'MaxId'] }
66-
}
70+
id: { $in: ['CalId', 'MaxId'] },
71+
},
6772
})
6873
})
69-
.then(results => {
74+
.then((results: any[]) => {
7075
expect(results[0].name).to.equal('Cal')
7176
expect(results[1].name).to.equal('Max')
7277

73-
return app.service(serviceName).remove(
74-
null,
75-
{ query: { id: { $in: ['CalId', 'MaxId'] } } }
76-
)
78+
return app
79+
.service(serviceName)
80+
.remove(null, { query: { id: { $in: ['CalId', 'MaxId'] } } })
7781
})
7882
})
7983

8084
it('should return created items in the same order as requested ones along with the errors (bulk)', () => {
81-
return app.service(serviceName)
85+
return app
86+
.service(serviceName)
8287
.create([
8388
{ name: 'Catnis', id: 'CatnisId' },
8489
{ name: 'Catnis', id: 'CatnisId' },
85-
{ name: 'Mark', id: 'MarkId' }
90+
{ name: 'Mark', id: 'MarkId' },
8691
])
87-
.then(results => {
92+
.then((results: any[]) => {
8893
expect(results[0].name).to.equal('Catnis')
8994
expect(results[1]._meta.status).to.equal(409)
9095
expect(results[2].name).to.equal('Mark')
9196

92-
return app.service(serviceName).remove(
93-
null,
94-
{ query: { id: { $in: ['CatnisId', 'MarkId'] } } }
95-
)
97+
return app
98+
.service(serviceName)
99+
.remove(null, { query: { id: { $in: ['CatnisId', 'MarkId'] } } })
96100
})
97101
})
98102

99103
it('should create an item with provided parent', () => {
100-
return app.service('aka')
104+
return app
105+
.service('aka')
101106
.create({ name: 'Bobster McBobface', parent: 'bob', aka: 'alias' })
102-
.then(result => {
107+
.then((result: any) => {
103108
expect(result.name).to.equal('Bobster McBobface')
104109
expect(result._meta._parent).to.equal('bob')
105-
return app.service('aka').remove(
106-
result.id,
107-
{ query: { parent: 'bob' } }
108-
)
110+
return app.service('aka').remove(result.id, { query: { parent: 'bob' } })
109111
})
110112
})
111113

112114
it('should create items with provided parents (bulk)', () => {
113-
return app.service('aka')
115+
return app
116+
.service('aka')
114117
.create([
115118
{ name: 'Bobster', parent: 'bob', id: 'bobAka', aka: 'alias' },
116-
{ name: 'Sunshine', parent: 'moody', aka: 'alias' }
119+
{ name: 'Sunshine', parent: 'moody', aka: 'alias' },
117120
])
118-
.then(results => {
121+
.then((results: any[]) => {
119122
const [bobAka, moodyAka] = results
120123

121124
expect(results.length).to.equal(2)
@@ -124,20 +127,20 @@ function create (app, serviceName) {
124127
expect(moodyAka.name).to.equal('Sunshine')
125128
expect(moodyAka._meta._parent).to.equal('moody')
126129

127-
return app.service('aka').remove(
128-
null,
129-
{ query: { id: { $in: [bobAka.id, moodyAka.id] } } }
130-
)
130+
return app
131+
.service('aka')
132+
.remove(null, { query: { id: { $in: [bobAka.id, moodyAka.id] } } })
131133
})
132134
})
133135

134136
it('should return only raw response if no items were created (bulk)', () => {
135-
return app.service(serviceName)
137+
return app
138+
.service(serviceName)
136139
.create([
137140
{ name: { first: 'Douglas' }, id: 'wrongDouglas' },
138-
{ name: { first: 'Bob' }, id: 'wrongBob' }
141+
{ name: { first: 'Bob' }, id: 'wrongBob' },
139142
])
140-
.then(results => {
143+
.then((results: any[]) => {
141144
expect(results).to.have.lengthOf(2)
142145
expect(results).to.have.nested.property('[0].id', 'wrongDouglas')
143146
expect(results).to.have.nested.property('[0]._meta.error')
@@ -150,4 +153,4 @@ function create (app, serviceName) {
150153
})
151154
}
152155

153-
module.exports = create
156+
export default create

0 commit comments

Comments
 (0)