Skip to content

Commit

Permalink
Merge pull request #186 from AthennaIO/develop
Browse files Browse the repository at this point in the history
fix(base): use right url path
  • Loading branch information
jlenon7 authored Sep 5, 2023
2 parents 8745d0e + d00f45d commit 5f6e8c1
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 30 deletions.
3 changes: 2 additions & 1 deletion bin/artisan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Config.set('rc.commands', {
entrypoint: '#bin/repl',
path: '#src/commands/ReplCommand',
},
greet: '#tests/stubs/commands/GreetCommand',
})

/*
Expand All @@ -52,7 +53,7 @@ Config.set('rc.commands', {
|
| Here is where your application will bootstrap. Ignite class will be res
| ponsible to bootstrap your application partial or complete. Is not reco
| mmended to bootstrap the Athenna application completelly by calling the
| mmended to bootstrap the Athenna application completely by calling the
| "fire" method, you should always let the type of application determine if
| the application should be fully bootstrapped or not.
|
Expand Down
4 changes: 4 additions & 0 deletions bin/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
* file that was distributed with this source code.
*/

import { request } from '@athenna/http/testing/plugins'
import { command } from '@athenna/artisan/testing/plugins'
import { Runner, assert, specReporter } from '@athenna/test'

process.env.CORE_TESTING = 'true'

await Runner.setTsEnv()
.addPlugin(assert())
.addPlugin(request())
.addPlugin(command())
.addReporter(specReporter())
.addPath('tests/unit/**/*.ts')
.setCliArgs(process.argv.slice(2))
Expand Down
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/core",
"version": "4.5.0",
"version": "4.6.0",
"description": "The plug and play Node.js framework.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down Expand Up @@ -70,8 +70,8 @@
"semver": "^7.5.4"
},
"devDependencies": {
"@athenna/artisan": "^4.2.0",
"@athenna/common": "^4.4.0",
"@athenna/artisan": "^4.8.0",
"@athenna/common": "^4.9.2",
"@athenna/config": "^4.3.0",
"@athenna/http": "^4.4.0",
"@athenna/ioc": "^4.1.0",
Expand Down Expand Up @@ -204,7 +204,9 @@
"provider": "./templates/provider.edge",
"service": "./templates/service.edge",
"test": "./templates/test.edge",
"testFn": "./templates/testFn.edge",
"test-cli": "./templates/test-cli.edge",
"test-rest": "./templates/test-rest.edge",
"test-fn": "./templates/test-fn.edge",
"command": "node_modules/@athenna/artisan/templates/command.edge",
"controller": "node_modules/@athenna/http/templates/controller.edge",
"middleware": "node_modules/@athenna/http/templates/middleware.edge",
Expand Down
20 changes: 19 additions & 1 deletion src/commands/MakeTestCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ export class MakeTestCommand extends BaseCommand {
})
public isUnit: boolean

@Option({
description: 'Create a REST API test.',
signature: '-r, --rest',
default: false,
})
public isRest: boolean

@Option({
description: 'Create a CLI test.',
signature: '-c, --cli',
default: false,
})
public isCli: boolean

@Option({
description: 'Create the test as function instead of class.',
signature: '--function',
Expand All @@ -44,8 +58,12 @@ export class MakeTestCommand extends BaseCommand {

let template = 'test'

if (this.isCli) template = 'test-cli'
else if (this.isRest) template = 'test-rest'
else if (this.isUnit) template = 'test' // This is necessary to avoid multiple options case.

if (this.isFunction) {
template = 'testFn'
template = 'test-fn'
}

const file = await this.generator
Expand Down
6 changes: 5 additions & 1 deletion src/commands/TestCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import { Module } from '@athenna/common'
import { BaseCommand, Option } from '@athenna/artisan'
import { Option, BaseCommand, Commander } from '@athenna/artisan'

export class TestCommand extends BaseCommand {
@Option({
Expand All @@ -26,6 +26,10 @@ export class TestCommand extends BaseCommand {
return 'Run the tests of your application.'
}

public static commander(commander: Commander) {
return commander.allowUnknownOption()
}

// TODO Verify if this command still makes sense to exist.
public async handle(): Promise<void> {
if (this.env !== '') {
Expand Down
14 changes: 6 additions & 8 deletions src/testing/BaseCliTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,27 @@
* file that was distributed with this source code.
*/

import { pathToFileURL } from 'node:url'
import { Options } from '@athenna/common'
import { BeforeAll } from '@athenna/test'
import { Artisan } from '@athenna/artisan'
import { Ignite, type IgniteOptions } from '@athenna/core'
import { TestCommand } from '@athenna/artisan/testing/plugins'

export class BaseCliTest {
public ignite: Ignite
public igniteOptions: IgniteOptions = {}
public artisanPath: string = Path.bootstrap(`artisan.${Path.ext()}`)

@BeforeAll()
public async baseBeforeAll() {
TestCommand.setArtisanPath(this.artisanPath)

this.ignite = await new Ignite().load(
import.meta.url,
pathToFileURL(Path.bootstrap(`test.${Path.ext()}`)).href,
this.getIgniteOptions(),
)
}

public async execute(
command: string,
): Promise<{ stdout: string; stderr: string }> {
return Artisan.callInChild(command, Path.bootstrap('artisan.ts'))
}

private getIgniteOptions() {
return Options.create(this.igniteOptions, {
bootLogs: false,
Expand Down
3 changes: 2 additions & 1 deletion src/testing/BaseRestTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* file that was distributed with this source code.
*/

import { pathToFileURL } from 'node:url'
import { Options } from '@athenna/common'
import { ServerImpl } from '@athenna/http'
import { AfterAll, BeforeAll } from '@athenna/test'
Expand All @@ -21,7 +22,7 @@ export class BaseRestTest {
@BeforeAll()
public async baseBeforeAll() {
this.ignite = await new Ignite().load(
import.meta.url,
pathToFileURL(Path.bootstrap(`test.${Path.ext()}`)).href,
this.getIgniteOptions(),
)

Expand Down
12 changes: 12 additions & 0 deletions templates/test-cli.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Test, type Context } from '@athenna/test'
import { BaseCliTest } from '@athenna/core/testing/BaseCliTest'

export default class {{ namePascal }} extends BaseCliTest {
@Test()
public async shouldBeAbleToExecuteCliCommands({ command }: Context) {
const output = await command.run('greet')

output.assertSucceeded()
output.assertLogged('Hello World!')
}
}
File renamed without changes.
11 changes: 11 additions & 0 deletions templates/test-rest.edge
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Test, type Context } from '@athenna/test'
import { BaseRestTest } from '@athenna/core/testing/BaseRestTest'

export default class {{ namePascal }} extends BaseRestTest {
@Test()
public async shouldBeAbleToExecuteRestApiRequests({ request }: Context) {
const response = await request.get('/')

response.assertStatusCode(200)
}
}
3 changes: 1 addition & 2 deletions templates/test.edge
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Test } from '@athenna/test'
import type { Context } from '@athenna/test/types'
import { Test, type Context } from '@athenna/test'

export default class {{ namePascal }} {
@Test()
Expand Down
23 changes: 23 additions & 0 deletions tests/stubs/commands/GreetCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @athenna/core
*
* (c) João Lenon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { BaseCommand, Argument } from '@athenna/artisan'

export class GreetCommand extends BaseCommand {
@Argument()
public name: string

public static signature(): string {
return 'greet'
}

public async handle(): Promise<void> {
this.logger.simple(`({bold,green} [ HELLO ${this.name.toUpperCase()}! ])\n`)
}
}
16 changes: 16 additions & 0 deletions tests/unit/testing/BaseCliTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ArtisanProvider } from '@athenna/artisan'
import { Test, type Context } from '@athenna/test'
import { BaseCliTest as InternalBaseCliTest } from '@athenna/core/testing/BaseCliTest'

export default class BaseCliTest extends InternalBaseCliTest {
public artisanPath = Path.bin('artisan.ts')

@Test()
public async shouldBeAbleToRunTestsExtendingBaseCliTestClass({ command }: Context) {
new ArtisanProvider().register()
const output = await command.run('greet lenon')

output.assertSucceeded()
output.assertLogged('HELLO LENON!')
}
}
17 changes: 17 additions & 0 deletions tests/unit/testing/BaseRestTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Test, type Context } from '@athenna/test'
import { BaseRestTest as InternalBaseRestTest } from '@athenna/core/testing/BaseRestTest'
import type { HttpOptions } from '#src/index'

export default class BaseRestTest extends InternalBaseRestTest {
public restOptions: HttpOptions = {
routePath: Path.stubs('routes/http.ts'),
}

@Test()
public async shouldBeAbleToRunTestsExtendingBaseRestTestClass({ request }: Context) {
const response = await request.get('/hello')

response.assertStatusCode(200)
response.assertBodyContains({ ok: true })
}
}

0 comments on commit 5f6e8c1

Please sign in to comment.