From b80771aa505a153aa4f6d1471de6f41ff300cb0d Mon Sep 17 00:00:00 2001 From: Xavier Stouder Date: Thu, 9 May 2024 11:51:04 +0200 Subject: [PATCH] test: improve tests --- .gitignore | 1 + .npmrc | 1 + bin/test.ts | 3 +- index.ts | 12 ++------ package.json | 21 +++++++------ providers/geolite2_provider.ts | 10 +++--- src/define_config.ts | 3 +- src/geolite2.ts | 4 +-- src/manager.ts | 6 +--- src/types.ts | 2 +- stubs/config.stub | 3 +- tests/configure.spec.ts | 43 ++++++++++++++++++++++++++ tests/example.spec.ts | 7 ----- tests/geolite2_provider.spec.ts | 54 +++++++++++++++++++++++++++++++++ tsconfig.json | 3 +- 15 files changed, 129 insertions(+), 44 deletions(-) create mode 100644 .npmrc create mode 100644 tests/configure.spec.ts delete mode 100644 tests/example.spec.ts create mode 100644 tests/geolite2_provider.spec.ts diff --git a/.gitignore b/.gitignore index 337d7e5..fd5f45d 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ test/__app build dist shrinkwrap.yaml +tmp diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/bin/test.ts b/bin/test.ts index 7978bf2..67ff247 100644 --- a/bin/test.ts +++ b/bin/test.ts @@ -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() diff --git a/index.ts b/index.ts index 5843ae1..fe5c9f2 100644 --- a/index.ts +++ b/index.ts @@ -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' diff --git a/package.json b/package.json index 0b8aeaa..d485b9c 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" }, @@ -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", @@ -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" @@ -88,7 +86,8 @@ "html" ], "exclude": [ - "tests/**" + "tests/**", + "stubs/**" ] }, "eslintConfig": { @@ -96,6 +95,7 @@ }, "prettier": "@adonisjs/prettier-config", "dependencies": { + "@poppinss/utils": "^6.7.3", "geolite2-redist": "^3.0.4", "maxmind": "^4.3.19" }, @@ -108,7 +108,8 @@ "outDir": "./build", "clean": true, "format": "esm", - "dts": true, + "dts": false, + "sourcemap": true, "target": "esnext" }, "volta": { diff --git a/providers/geolite2_provider.ts b/providers/geolite2_provider.ts index e5ab3fe..1ef3bc6 100644 --- a/providers/geolite2_provider.ts +++ b/providers/geolite2_provider.ts @@ -8,7 +8,7 @@ import { RuntimeException } from '@poppinss/utils' declare module '@adonisjs/core/types' { export interface ContainerBindings { - 'geolite2.manager': GeoLite2Service + geolite2: GeoLite2Service } } @@ -16,13 +16,13 @@ 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('geolite2') const config = await configProvider.resolve(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' ) } @@ -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( @@ -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() } } diff --git a/src/define_config.ts b/src/define_config.ts index a95919b..9e2faef 100644 --- a/src/define_config.ts +++ b/src/define_config.ts @@ -1,3 +1,4 @@ +import { resolve } from 'node:path' import { configProvider } from '@adonisjs/core' import { GeoLite2Config, ResolvedGeoLite2Config } from './types.js' import { ConfigProvider } from '@adonisjs/core/types' @@ -5,7 +6,7 @@ import { ConfigProvider } from '@adonisjs/core/types' export function defineConfig(config: GeoLite2Config): ConfigProvider { return configProvider.create(async (_app) => { return { - downloadDirectory: config.downloadDirectory, + downloadDirectory: resolve(config.downloadDirectory), cache: config.cache, } }) diff --git a/src/geolite2.ts b/src/geolite2.ts index 81f1bf5..2a17def 100644 --- a/src/geolite2.ts +++ b/src/geolite2.ts @@ -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' { diff --git a/src/manager.ts b/src/manager.ts index ee75fb4..c198ba4 100644 --- a/src/manager.ts +++ b/src/manager.ts @@ -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' @@ -47,10 +47,6 @@ export default class GeoLite2Manager implements GeoLite2Service { } close() { - if (this.readers === null) { - return - } - this.onClose() } diff --git a/src/types.ts b/src/types.ts index f42138d..1a13598 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 diff --git a/stubs/config.stub b/stubs/config.stub index 51993dd..68b3924 100644 --- a/stubs/config.stub +++ b/stubs/config.stub @@ -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 }) diff --git a/tests/configure.spec.ts b/tests/configure.spec.ts new file mode 100644 index 0000000..565f871 --- /dev/null +++ b/tests/configure.spec.ts @@ -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) +}) diff --git a/tests/example.spec.ts b/tests/example.spec.ts deleted file mode 100644 index 893ea1a..0000000 --- a/tests/example.spec.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { test } from '@japa/runner' - -test.group('Example', () => { - test('add two numbers', ({ assert }) => { - assert.equal(1 + 1, 2) - }) -}) diff --git a/tests/geolite2_provider.spec.ts b/tests/geolite2_provider.spec.ts new file mode 100644 index 0000000..e33a8f5 --- /dev/null +++ b/tests/geolite2_provider.spec.ts @@ -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' + ) +}) diff --git a/tsconfig.json b/tsconfig.json index a7394ac..ad0cc44 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,6 @@ "extends": "@adonisjs/tsconfig/tsconfig.package.json", "compilerOptions": { "rootDir": "./", - "outDir": "./build", - "preserveSymlinks": true + "outDir": "./build" } }