Skip to content

Commit d6681a9

Browse files
authored
chore: review tstyche test (#207)
* chore: review tstyche test * refactor: types test * refactor: change arrow functions to regular functions in test setup * refactor: change import to type for Orama types * refactor: update tsconfig and remove unused tstyche config * refactor: simplify typescript script and remove tstyche config
1 parent 0d672fd commit d6681a9

File tree

5 files changed

+93
-88
lines changed

5 files changed

+93
-88
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ declare const fastifyOrama: FastifyPluginCallback<FastifyOramaPluginOptions>
3333

3434
declare const oramaInternals: typeof internals
3535

36-
interface OramaApi<T> {
36+
export interface OramaApi<T> {
3737
insert: (document: PartialSchemaDeep<TypedDocument<Orama<T>>>) => Promise<string>,
3838
search: (params: SearchParams<Orama<Schema<T>>, T>) => Promise<Results<Schema<T>>>,
3939
persist?: () => Promise<any>,

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"lint": "standard | snazzy",
1414
"lint:fix": "standard --fix | snazzy",
1515
"pretest": "npm run clean",
16-
"test": "npm run lint && npm run unit && npm run typescript && ts-node test/types/index.ts",
16+
"test": "npm run lint && npm run unit && npm run typescript",
1717
"posttest": "npm run clean",
1818
"typescript": "tstyche",
1919
"prepare": "husky",
@@ -48,10 +48,10 @@
4848
"borp": "^0.20.0",
4949
"c8": "^10.1.3",
5050
"fastify": "^5.4.0",
51+
"fastify-tsconfig": "^3.0.0",
5152
"husky": "^9.1.7",
5253
"snazzy": "^9.0.0",
5354
"standard": "^17.1.2",
54-
"ts-node": "^10.9.2",
5555
"tstyche": "^4.0.2",
5656
"typescript": "^5.8.3"
5757
},

test/types/index.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

test/types/index.tst.ts

Lines changed: 87 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,121 @@
1-
import { expect, test } from 'tstyche'
1+
import { describe, expect, test } from 'tstyche'
22

3-
import { InternalTypedDocument, Orama, PartialSchemaDeep, Results, Schema, SearchParams, TypedDocument } from '@orama/orama'
3+
import type { InternalTypedDocument, Orama, PartialSchemaDeep, Results, Schema, SearchParams, TypedDocument } from '@orama/orama'
44
import Fastify from 'fastify'
55
import fp from 'fastify-plugin'
66

7-
import { PersistenceInFile, PersistenceInMemory, fastifyOrama } from '../../index.js'
8-
9-
const app = Fastify()
7+
import { OramaApi, PersistenceInFile, PersistenceInMemory, fastifyOrama } from '../../index.js'
108

119
const mySchema = {
1210
quote: 'string',
1311
author: 'string'
1412
} as const
13+
1514
type MySchema = Schema<typeof mySchema>
1615

16+
function baseSetup() {
17+
const app = Fastify()
1718

18-
app.register(fastifyOrama, {
19-
schema: mySchema,
20-
prefix: '/api/orama',
21-
language: 'en'
22-
})
19+
app.register(fastifyOrama, {
20+
schema: mySchema,
21+
persistence: new PersistenceInMemory()
22+
})
2323

24-
app.register(fastifyOrama, {
25-
persistence: new PersistenceInMemory()
26-
})
24+
return app.withOrama<typeof mySchema>()
25+
}
2726

28-
app.register(fastifyOrama, {
29-
persistence: new PersistenceInMemory({
30-
jsonIndex: 'index.json'
31-
})
32-
})
27+
function setupWithMorePersistenceOptions() {
28+
const app = baseSetup()
3329

34-
app.register(fastifyOrama, {
35-
persistence: new PersistenceInFile()
36-
})
37-
app.register(fastifyOrama, {
38-
persistence: new PersistenceInFile({
39-
filePath: 'index.json',
40-
format: 'json',
41-
mustExistOnStart: true
30+
app.register(fastifyOrama, {
31+
schema: mySchema,
32+
prefix: '/api/orama',
33+
language: 'en'
4234
})
43-
})
4435

45-
const appWithOrama = app.withOrama<typeof mySchema>()
36+
app.register(fastifyOrama, {
37+
persistence: new PersistenceInMemory({
38+
jsonIndex: 'index.json'
39+
})
40+
})
4641

47-
test('should enable the insertion of documents', () => {
48-
appWithOrama.orama.insert({ quote: 'Hello', author: 'World' }).then(id => {
49-
expect(id).type.toBe<string>()
42+
app.register(fastifyOrama, {
43+
persistence: new PersistenceInFile()
44+
})
45+
app.register(fastifyOrama, {
46+
persistence: new PersistenceInFile({
47+
filePath: 'index.json',
48+
format: 'json',
49+
mustExistOnStart: true
50+
})
5051
})
51-
})
5252

53-
test('should enable the searching of documents', () => {
54-
appWithOrama.get('/hello', async () => {
53+
return app.withOrama<typeof mySchema>()
54+
}
5555

56-
const {orama} = appWithOrama
57-
const result = await orama.search({ term: 'hello' })
56+
describe('Fastify Orama', () => {
5857

59-
expect(result).type.toBe<Results<InternalTypedDocument<MySchema>>>()
60-
expect(result.hits[0].document.author).type.toBe<string>()
58+
test('should expose the Orama instance', async () => {
59+
await (async function () {
60+
const appWithOrama = baseSetup()
61+
const id = await appWithOrama.orama.insert({ quote: 'Hello', author: 'World' })
62+
expect(id).type.toBe<string>()
6163

62-
return {
63-
hello: result.hits
64-
}
64+
appWithOrama.get('/hello', async () => {
65+
66+
const {orama} = appWithOrama
67+
expect(orama).type.toBe<OramaApi<typeof mySchema>>()
68+
const result = await orama.search({ term: 'hello' })
69+
70+
return {
71+
hello: result.hits
72+
}
73+
})
74+
})()
75+
});
76+
77+
test('should enable the insertion of documents', () => {
78+
const appWithOrama = setupWithMorePersistenceOptions()
79+
appWithOrama.orama.insert({ quote: 'Hello', author: 'World' }).then(id => {
80+
expect(id).type.toBe<string>()
81+
})
6582
})
66-
})
6783

68-
test('should expose the Orama API', () => {
69-
expect(appWithOrama.orama).type.toBe<{
70-
insert: (document: PartialSchemaDeep<TypedDocument<Orama<typeof mySchema>>>) => Promise<string>,
71-
search: (params: SearchParams<Orama<Schema<typeof mySchema>>, typeof mySchema>) => Promise<Results<Schema<typeof mySchema>>>,
72-
persist?: () => Promise<any>,
73-
}>()
74-
})
84+
test('should enable the searching of documents', () => {
85+
const appWithOrama = setupWithMorePersistenceOptions()
86+
appWithOrama.get('/hello', async () => {
7587

88+
const {orama} = appWithOrama
89+
const result = await orama.search({ term: 'hello' })
7690

77-
test('should enable the withOrama method', () => {
78-
fp(function(fastify) {
79-
const fastifyWithOrama = fastify.withOrama<typeof mySchema>()
91+
expect(result).type.toBe<Results<InternalTypedDocument<MySchema>>>()
92+
expect(result.hits[0].document.author).type.toBe<string>()
8093

81-
expect(fastifyWithOrama.orama).type.toBe<{
94+
return {
95+
hello: result.hits
96+
}
97+
})
98+
})
99+
100+
test('should expose the Orama API', () => {
101+
const appWithOrama = setupWithMorePersistenceOptions()
102+
expect(appWithOrama.orama).type.toBe<{
82103
insert: (document: PartialSchemaDeep<TypedDocument<Orama<typeof mySchema>>>) => Promise<string>,
83104
search: (params: SearchParams<Orama<Schema<typeof mySchema>>, typeof mySchema>) => Promise<Results<Schema<typeof mySchema>>>,
84105
persist?: () => Promise<any>,
85106
}>()
86107
})
108+
109+
110+
test('should enable the withOrama method', () => {
111+
fp(function(fastify) {
112+
const fastifyWithOrama = fastify.withOrama<typeof mySchema>()
113+
114+
expect(fastifyWithOrama.orama).type.toBe<{
115+
insert: (document: PartialSchemaDeep<TypedDocument<Orama<typeof mySchema>>>) => Promise<string>,
116+
search: (params: SearchParams<Orama<Schema<typeof mySchema>>, typeof mySchema>) => Promise<Results<Schema<typeof mySchema>>>,
117+
persist?: () => Promise<any>,
118+
}>()
119+
})
120+
})
87121
})

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2+
"extends": "fastify-tsconfig",
23
"compilerOptions": {
3-
"esModuleInterop": true,
4-
},
4+
"strict": true,
5+
}
56
}

0 commit comments

Comments
 (0)