Skip to content

Commit b80771a

Browse files
committed
test: improve tests
1 parent da189f8 commit b80771a

15 files changed

+129
-44
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ test/__app
1111
build
1212
dist
1313
shrinkwrap.yaml
14+
tmp

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

bin/test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { assert } from '@japa/assert'
22
import { configure, processCLIArgs, run } from '@japa/runner'
3+
import { fileSystem } from '@japa/file-system'
34

45
processCLIArgs(process.argv.splice(2))
56

67
configure({
78
files: ['tests/**/*.spec.ts'],
8-
plugins: [assert()],
9+
plugins: [assert(), fileSystem()],
910
})
1011

1112
run()

index.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
/*
2-
|--------------------------------------------------------------------------
3-
| Package entrypoint
4-
|--------------------------------------------------------------------------
5-
|
6-
| Export values from the package entrypoint as you see fit.
7-
|
8-
*/
9-
1+
export { GeoLite2 } from './src/geolite2.js'
102
export { configure } from './configure.js'
3+
export { stubsRoot } from './stubs/main.js'
4+
export { defineConfig } from './src/define_config.js'

package.json

+11-10
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
},
88
"type": "module",
99
"files": [
10-
"build/src",
11-
"build/providers",
12-
"build/stubs",
13-
"build/index.d.ts",
14-
"build/index.js"
10+
"build",
11+
"!build/bin",
12+
"!build/tests"
1513
],
1614
"exports": {
1715
".": "./build/index.js",
@@ -27,9 +25,8 @@
2725
"pretest": "npm run lint",
2826
"test": "c8 npm run quick:test",
2927
"prebuild": "npm run lint && npm run clean",
30-
"build": "tsc",
28+
"build": "tsup",
3129
"postbuild": "npm run copy:templates",
32-
"release": "np",
3330
"version": "npm run build",
3431
"prepublishOnly": "npm run build"
3532
},
@@ -57,6 +54,7 @@
5754
"@adonisjs/prettier-config": "^1.3.0",
5855
"@adonisjs/tsconfig": "^1.3.0",
5956
"@japa/assert": "^2.1.0",
57+
"@japa/file-system": "^2.3.0",
6058
"@japa/runner": "^3.1.1",
6159
"@swc/core": "^1.4.6",
6260
"@types/node": "^20.11.25",
@@ -67,7 +65,7 @@
6765
"np": "^10.0.0",
6866
"prettier": "^3.2.5",
6967
"ts-node": "^10.9.2",
70-
"typescript": "^5.4.2"
68+
"typescript": "5.3.3"
7169
},
7270
"peerDependencies": {
7371
"@adonisjs/core": "^6.2.0"
@@ -88,14 +86,16 @@
8886
"html"
8987
],
9088
"exclude": [
91-
"tests/**"
89+
"tests/**",
90+
"stubs/**"
9291
]
9392
},
9493
"eslintConfig": {
9594
"extends": "@adonisjs/eslint-config/package"
9695
},
9796
"prettier": "@adonisjs/prettier-config",
9897
"dependencies": {
98+
"@poppinss/utils": "^6.7.3",
9999
"geolite2-redist": "^3.0.4",
100100
"maxmind": "^4.3.19"
101101
},
@@ -108,7 +108,8 @@
108108
"outDir": "./build",
109109
"clean": true,
110110
"format": "esm",
111-
"dts": true,
111+
"dts": false,
112+
"sourcemap": true,
112113
"target": "esnext"
113114
},
114115
"volta": {

providers/geolite2_provider.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ import { RuntimeException } from '@poppinss/utils'
88

99
declare module '@adonisjs/core/types' {
1010
export interface ContainerBindings {
11-
'geolite2.manager': GeoLite2Service
11+
geolite2: GeoLite2Service
1212
}
1313
}
1414

1515
export default class GeoLite2Provider {
1616
constructor(protected app: ApplicationService) {}
1717

1818
register() {
19-
this.app.container.singleton('geolite2.manager', async () => {
19+
this.app.container.singleton('geolite2', async () => {
2020
const geolite2Config = this.app.config.get<GeoLite2Config>('geolite2')
2121
const config = await configProvider.resolve<ResolvedGeoLite2Config>(this.app, geolite2Config)
2222

2323
if (!config) {
2424
throw new RuntimeException(
25-
'Invalid config exported from "config/geolite2.ts" file. Make sure to use the defineConfig method'
25+
'Invalid default export from "config/geolite2.ts" file. Make sure to use defineConfig method'
2626
)
2727
}
2828

@@ -31,7 +31,7 @@ export default class GeoLite2Provider {
3131
}
3232

3333
async boot() {
34-
const manager = await this.app.container.make('geolite2.manager')
34+
const manager = await this.app.container.make('geolite2')
3535
await manager.init()
3636

3737
HttpContext.getter(
@@ -44,7 +44,7 @@ export default class GeoLite2Provider {
4444
}
4545

4646
async shutdown() {
47-
const manager = await this.app.container.make('geolite2.manager')
47+
const manager = await this.app.container.make('geolite2')
4848
manager.close()
4949
}
5050
}

src/define_config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { resolve } from 'node:path'
12
import { configProvider } from '@adonisjs/core'
23
import { GeoLite2Config, ResolvedGeoLite2Config } from './types.js'
34
import { ConfigProvider } from '@adonisjs/core/types'
45

56
export function defineConfig(config: GeoLite2Config): ConfigProvider<ResolvedGeoLite2Config> {
67
return configProvider.create(async (_app) => {
78
return {
8-
downloadDirectory: config.downloadDirectory,
9+
downloadDirectory: resolve(config.downloadDirectory),
910
cache: config.cache,
1011
}
1112
})

src/geolite2.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { AllReaders, GeoLite2Contract } from './types.js'
2-
import { AsnResponse, CityResponse, CountryResponse } from 'maxmind'
1+
import type { AllReaders, GeoLite2Contract } from './types.js'
2+
import type { AsnResponse, CityResponse, CountryResponse } from 'maxmind'
33
import { HttpContext } from '@adonisjs/core/http'
44

55
declare module '@adonisjs/core/http' {

src/manager.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as geolite2 from 'geolite2-redist'
2-
import { type AllReaders, type GeoLite2Service, type ResolvedGeoLite2Config } from './types.js'
2+
import type { AllReaders, GeoLite2Service, ResolvedGeoLite2Config } from './types.js'
33
import maxmind, { type CountryResponse, type CityResponse, type AsnResponse } from 'maxmind'
44
import { GeoIpDbName } from 'geolite2-redist'
55

@@ -47,10 +47,6 @@ export default class GeoLite2Manager implements GeoLite2Service {
4747
}
4848

4949
close() {
50-
if (this.readers === null) {
51-
return
52-
}
53-
5450
this.onClose()
5551
}
5652

src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type AsnResponse, type CityResponse, type CountryResponse, type Reader } from 'maxmind'
1+
import type { AsnResponse, CityResponse, CountryResponse, Reader } from 'maxmind'
22

33
export type AllReaders = {
44
country: Reader<CountryResponse>

stubs/config.stub

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
import { defineConfig } from '@stouder-io/adonis-geolite2'
55

66
export default defineConfig({
7-
downloadDir: path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', 'dbs')
7+
downloadDirectory: '../dbs',
8+
cache: 6000
89
})

tests/configure.spec.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { test } from '@japa/runner'
2+
import { fileURLToPath } from 'node:url'
3+
import { IgnitorFactory } from '@adonisjs/core/factories'
4+
import Configure from '@adonisjs/core/commands/configure'
5+
6+
const BASE_URL = new URL('./tmp/', import.meta.url)
7+
8+
test.group('Configure', (group) => {
9+
group.each.setup(({ context }) => {
10+
context.fs.baseUrl = BASE_URL
11+
context.fs.basePath = fileURLToPath(BASE_URL)
12+
})
13+
14+
test('create config file, register provider and update meta files', async ({ fs, assert }) => {
15+
const ignitor = new IgnitorFactory()
16+
.withCoreProviders()
17+
.withCoreConfig()
18+
.create(BASE_URL, {
19+
importer: (filePath) => {
20+
if (filePath.startsWith('./') || filePath.startsWith('../')) {
21+
return import(new URL(filePath, BASE_URL).href)
22+
}
23+
24+
return import(filePath)
25+
},
26+
})
27+
28+
await fs.create('.env', '')
29+
await fs.createJson('tsconfig.json', {})
30+
await fs.create('start/kernel.ts', `router.use([])`)
31+
await fs.create('adonisrc.ts', `export default defineConfig({}) {}`)
32+
33+
const app = ignitor.createApp('web')
34+
await app.init()
35+
await app.boot()
36+
37+
const ace = await app.container.make('ace')
38+
const command = await ace.create(Configure, ['../../index.js'])
39+
await command.exec()
40+
41+
await assert.fileExists('config/geolite2.ts')
42+
}).timeout(60 * 1000)
43+
})

tests/example.spec.ts

-7
This file was deleted.

tests/geolite2_provider.spec.ts

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { test } from '@japa/runner'
2+
import { IgnitorFactory } from '@adonisjs/core/factories'
3+
import { defineConfig } from '../src/define_config.js'
4+
import GeoLite2Manager from '../src/manager.js'
5+
6+
const BASE_URL = new URL('./tmp/', import.meta.url)
7+
8+
test.group('GeoLite2 Provider', () => {
9+
test('register geolite2 provider', async ({ assert }) => {
10+
const ignitor = new IgnitorFactory()
11+
.withCoreConfig()
12+
.withCoreProviders()
13+
.merge({
14+
config: {
15+
geolite2: defineConfig({
16+
downloadDirectory: './tmp',
17+
cache: 6000,
18+
}),
19+
},
20+
rcFileContents: {
21+
providers: [() => import('../providers/geolite2_provider.js')],
22+
},
23+
})
24+
.create(BASE_URL)
25+
26+
const app = ignitor.createApp('web')
27+
await app.init()
28+
await app.boot()
29+
assert.instanceOf(await app.container.make('geolite2'), GeoLite2Manager)
30+
await app.terminate()
31+
})
32+
33+
test('throw error when config is invalid', async () => {
34+
const ignitor = new IgnitorFactory()
35+
.withCoreConfig()
36+
.withCoreProviders()
37+
.merge({
38+
config: {
39+
geolite2: {},
40+
},
41+
rcFileContents: {
42+
providers: [() => import('../providers/geolite2_provider.js')],
43+
},
44+
})
45+
.create(BASE_URL)
46+
47+
const app = ignitor.createApp('web')
48+
await app.init()
49+
await app.boot()
50+
await app.container.make('geolite2')
51+
}).throws(
52+
'Invalid default export from "config/geolite2.ts" file. Make sure to use defineConfig method'
53+
)
54+
})

tsconfig.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"extends": "@adonisjs/tsconfig/tsconfig.package.json",
33
"compilerOptions": {
44
"rootDir": "./",
5-
"outDir": "./build",
6-
"preserveSymlinks": true
5+
"outDir": "./build"
76
}
87
}

0 commit comments

Comments
 (0)