Skip to content

Commit

Permalink
test: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Xstoudi committed May 9, 2024
1 parent da189f8 commit b80771a
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ test/__app
build
dist
shrinkwrap.yaml
tmp
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
3 changes: 2 additions & 1 deletion bin/test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { assert } from '@japa/assert'
import { configure, processCLIArgs, run } from '@japa/runner'
import { fileSystem } from '@japa/file-system'

processCLIArgs(process.argv.splice(2))

configure({
files: ['tests/**/*.spec.ts'],
plugins: [assert()],
plugins: [assert(), fileSystem()],
})

run()
12 changes: 3 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
/*
|--------------------------------------------------------------------------
| Package entrypoint
|--------------------------------------------------------------------------
|
| Export values from the package entrypoint as you see fit.
|
*/

export { GeoLite2 } from './src/geolite2.js'
export { configure } from './configure.js'
export { stubsRoot } from './stubs/main.js'
export { defineConfig } from './src/define_config.js'
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
},
"type": "module",
"files": [
"build/src",
"build/providers",
"build/stubs",
"build/index.d.ts",
"build/index.js"
"build",
"!build/bin",
"!build/tests"
],
"exports": {
".": "./build/index.js",
Expand All @@ -27,9 +25,8 @@
"pretest": "npm run lint",
"test": "c8 npm run quick:test",
"prebuild": "npm run lint && npm run clean",
"build": "tsc",
"build": "tsup",
"postbuild": "npm run copy:templates",
"release": "np",
"version": "npm run build",
"prepublishOnly": "npm run build"
},
Expand Down Expand Up @@ -57,6 +54,7 @@
"@adonisjs/prettier-config": "^1.3.0",
"@adonisjs/tsconfig": "^1.3.0",
"@japa/assert": "^2.1.0",
"@japa/file-system": "^2.3.0",
"@japa/runner": "^3.1.1",
"@swc/core": "^1.4.6",
"@types/node": "^20.11.25",
Expand All @@ -67,7 +65,7 @@
"np": "^10.0.0",
"prettier": "^3.2.5",
"ts-node": "^10.9.2",
"typescript": "^5.4.2"
"typescript": "5.3.3"
},
"peerDependencies": {
"@adonisjs/core": "^6.2.0"
Expand All @@ -88,14 +86,16 @@
"html"
],
"exclude": [
"tests/**"
"tests/**",
"stubs/**"
]
},
"eslintConfig": {
"extends": "@adonisjs/eslint-config/package"
},
"prettier": "@adonisjs/prettier-config",
"dependencies": {
"@poppinss/utils": "^6.7.3",
"geolite2-redist": "^3.0.4",
"maxmind": "^4.3.19"
},
Expand All @@ -108,7 +108,8 @@
"outDir": "./build",
"clean": true,
"format": "esm",
"dts": true,
"dts": false,
"sourcemap": true,
"target": "esnext"
},
"volta": {
Expand Down
10 changes: 5 additions & 5 deletions providers/geolite2_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ import { RuntimeException } from '@poppinss/utils'

declare module '@adonisjs/core/types' {
export interface ContainerBindings {
'geolite2.manager': GeoLite2Service
geolite2: GeoLite2Service
}
}

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

register() {
this.app.container.singleton('geolite2.manager', async () => {
this.app.container.singleton('geolite2', async () => {
const geolite2Config = this.app.config.get<GeoLite2Config>('geolite2')
const config = await configProvider.resolve<ResolvedGeoLite2Config>(this.app, geolite2Config)

if (!config) {
throw new RuntimeException(
'Invalid config exported from "config/geolite2.ts" file. Make sure to use the defineConfig method'
'Invalid default export from "config/geolite2.ts" file. Make sure to use defineConfig method'
)
}

Expand All @@ -31,7 +31,7 @@ export default class GeoLite2Provider {
}

async boot() {
const manager = await this.app.container.make('geolite2.manager')
const manager = await this.app.container.make('geolite2')
await manager.init()

HttpContext.getter(
Expand All @@ -44,7 +44,7 @@ export default class GeoLite2Provider {
}

async shutdown() {
const manager = await this.app.container.make('geolite2.manager')
const manager = await this.app.container.make('geolite2')
manager.close()
}
}
3 changes: 2 additions & 1 deletion src/define_config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { resolve } from 'node:path'
import { configProvider } from '@adonisjs/core'
import { GeoLite2Config, ResolvedGeoLite2Config } from './types.js'
import { ConfigProvider } from '@adonisjs/core/types'

export function defineConfig(config: GeoLite2Config): ConfigProvider<ResolvedGeoLite2Config> {
return configProvider.create(async (_app) => {
return {
downloadDirectory: config.downloadDirectory,
downloadDirectory: resolve(config.downloadDirectory),
cache: config.cache,
}
})
Expand Down
4 changes: 2 additions & 2 deletions src/geolite2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AllReaders, GeoLite2Contract } from './types.js'
import { AsnResponse, CityResponse, CountryResponse } from 'maxmind'
import type { AllReaders, GeoLite2Contract } from './types.js'
import type { AsnResponse, CityResponse, CountryResponse } from 'maxmind'
import { HttpContext } from '@adonisjs/core/http'

declare module '@adonisjs/core/http' {
Expand Down
6 changes: 1 addition & 5 deletions src/manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as geolite2 from 'geolite2-redist'
import { type AllReaders, type GeoLite2Service, type ResolvedGeoLite2Config } from './types.js'
import type { AllReaders, GeoLite2Service, ResolvedGeoLite2Config } from './types.js'
import maxmind, { type CountryResponse, type CityResponse, type AsnResponse } from 'maxmind'
import { GeoIpDbName } from 'geolite2-redist'

Expand Down Expand Up @@ -47,10 +47,6 @@ export default class GeoLite2Manager implements GeoLite2Service {
}

close() {
if (this.readers === null) {
return
}

this.onClose()
}

Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type AsnResponse, type CityResponse, type CountryResponse, type Reader } from 'maxmind'
import type { AsnResponse, CityResponse, CountryResponse, Reader } from 'maxmind'

export type AllReaders = {
country: Reader<CountryResponse>
Expand Down
3 changes: 2 additions & 1 deletion stubs/config.stub
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
import { defineConfig } from '@stouder-io/adonis-geolite2'

export default defineConfig({
downloadDir: path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', 'dbs')
downloadDirectory: '../dbs',
cache: 6000
})
43 changes: 43 additions & 0 deletions tests/configure.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { test } from '@japa/runner'
import { fileURLToPath } from 'node:url'
import { IgnitorFactory } from '@adonisjs/core/factories'
import Configure from '@adonisjs/core/commands/configure'

const BASE_URL = new URL('./tmp/', import.meta.url)

test.group('Configure', (group) => {
group.each.setup(({ context }) => {
context.fs.baseUrl = BASE_URL
context.fs.basePath = fileURLToPath(BASE_URL)
})

test('create config file, register provider and update meta files', async ({ fs, assert }) => {
const ignitor = new IgnitorFactory()
.withCoreProviders()
.withCoreConfig()
.create(BASE_URL, {
importer: (filePath) => {
if (filePath.startsWith('./') || filePath.startsWith('../')) {
return import(new URL(filePath, BASE_URL).href)
}

return import(filePath)
},
})

await fs.create('.env', '')
await fs.createJson('tsconfig.json', {})
await fs.create('start/kernel.ts', `router.use([])`)
await fs.create('adonisrc.ts', `export default defineConfig({}) {}`)

const app = ignitor.createApp('web')
await app.init()
await app.boot()

const ace = await app.container.make('ace')
const command = await ace.create(Configure, ['../../index.js'])
await command.exec()

await assert.fileExists('config/geolite2.ts')
}).timeout(60 * 1000)
})
7 changes: 0 additions & 7 deletions tests/example.spec.ts

This file was deleted.

54 changes: 54 additions & 0 deletions tests/geolite2_provider.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { test } from '@japa/runner'
import { IgnitorFactory } from '@adonisjs/core/factories'
import { defineConfig } from '../src/define_config.js'
import GeoLite2Manager from '../src/manager.js'

const BASE_URL = new URL('./tmp/', import.meta.url)

test.group('GeoLite2 Provider', () => {
test('register geolite2 provider', async ({ assert }) => {
const ignitor = new IgnitorFactory()
.withCoreConfig()
.withCoreProviders()
.merge({
config: {
geolite2: defineConfig({
downloadDirectory: './tmp',
cache: 6000,
}),
},
rcFileContents: {
providers: [() => import('../providers/geolite2_provider.js')],
},
})
.create(BASE_URL)

const app = ignitor.createApp('web')
await app.init()
await app.boot()
assert.instanceOf(await app.container.make('geolite2'), GeoLite2Manager)
await app.terminate()
})

test('throw error when config is invalid', async () => {
const ignitor = new IgnitorFactory()
.withCoreConfig()
.withCoreProviders()
.merge({
config: {
geolite2: {},
},
rcFileContents: {
providers: [() => import('../providers/geolite2_provider.js')],
},
})
.create(BASE_URL)

const app = ignitor.createApp('web')
await app.init()
await app.boot()
await app.container.make('geolite2')
}).throws(
'Invalid default export from "config/geolite2.ts" file. Make sure to use defineConfig method'
)
})
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"extends": "@adonisjs/tsconfig/tsconfig.package.json",
"compilerOptions": {
"rootDir": "./",
"outDir": "./build",
"preserveSymlinks": true
"outDir": "./build"
}
}

0 comments on commit b80771a

Please sign in to comment.