generated from AthennaIO/Template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(test): add command assert plugin
- Loading branch information
Showing
9 changed files
with
267 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @athenna/artisan | ||
* | ||
* (c) João Lenon <lenon@athenna.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { Artisan } from '#src' | ||
import { Assert } from '@japa/assert' | ||
import type { CommandOutput } from '@athenna/common' | ||
import { TestOutput } from '#src/testing/plugins/command/TestOutput' | ||
|
||
export class TestCommand { | ||
/** | ||
* The Artisan file path that will be used to run commands. | ||
*/ | ||
public static artisanPath = undefined | ||
|
||
/** | ||
* Set the artisan file path. | ||
*/ | ||
public static setArtisanPath(path: string): typeof TestCommand { | ||
this.artisanPath = path | ||
|
||
return this | ||
} | ||
|
||
/** | ||
* Japa assert class instance. | ||
*/ | ||
public assert = new Assert() | ||
|
||
/** | ||
* Instantiate TestOutput class from stdout, stderr and exitCode. | ||
*/ | ||
public createOutput(output: CommandOutput): TestOutput { | ||
return new TestOutput(this.assert, output) | ||
} | ||
|
||
/** | ||
* Run the command and return the TestOutput instance | ||
* to make assertions. | ||
*/ | ||
public async run(command: string): Promise<TestOutput> { | ||
return Artisan.callInChild(command, TestCommand.artisanPath).then(output => | ||
this.createOutput(output), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* @athenna/artisan | ||
* | ||
* (c) João Lenon <lenon@athenna.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { inspect } from 'node:util' | ||
import type { Assert } from '@athenna/test' | ||
import type { CommandOutput } from '@athenna/common' | ||
|
||
export class TestOutput { | ||
/** | ||
* Japa assert class instance. | ||
*/ | ||
public assert: Assert | ||
|
||
/** | ||
* The command output object. | ||
*/ | ||
public output: CommandOutput | ||
|
||
public constructor(assert: Assert, output: CommandOutput) { | ||
this.assert = assert | ||
this.output = output | ||
} | ||
|
||
/** | ||
* Assert the exit code of the output. | ||
* | ||
* @example | ||
* ```js | ||
* output.assertExitCode(1) | ||
* ``` | ||
*/ | ||
public assertExitCode(code: number) { | ||
this.assert.deepEqual(this.output.exitCode, code) | ||
} | ||
|
||
/** | ||
* Assert the exit code of the output. | ||
* | ||
* @example | ||
* ```js | ||
* output.assertIsNotExitCode(1) | ||
* ``` | ||
*/ | ||
public assertIsNotExitCode(code: number) { | ||
this.assert.notDeepEqual(this.output.exitCode, code) | ||
} | ||
|
||
/** | ||
* Assert the command exists with zero exit code. | ||
*/ | ||
public assertSucceeded() { | ||
return this.assertExitCode(0) | ||
} | ||
|
||
/** | ||
* Assert the command exists with non-zero exit code. | ||
*/ | ||
public assertFailed() { | ||
return this.assertIsNotExitCode(0) | ||
} | ||
|
||
/** | ||
* Assert command to log the expected message. | ||
*/ | ||
public assertLogged(message: string, stream?: 'stdout' | 'stderr') { | ||
const existsInStdout = this.output.stdout.includes(message) | ||
const existsInStderr = this.output.stdout.includes(message) | ||
|
||
this.assert.isTrue(existsInStdout || existsInStderr) | ||
|
||
if (stream === 'stdout' && existsInStderr) { | ||
return this.assert.fail( | ||
`Expected message "${message}" to be logged in "stdout" but it was logged in "stderr"`, | ||
) | ||
} | ||
|
||
if (stream === 'stderr' && existsInStdout) { | ||
return this.assert.fail( | ||
`Expected message "${message}" to be logged in "stderr" but it was logged in "stdout"`, | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* Assert command to log the expected message. | ||
*/ | ||
public assertLogMatches(regex: RegExp, stream?: 'stdout' | 'stderr') { | ||
const existsInStdout = regex.test(this.output.stdout) | ||
const existsInStderr = regex.test(this.output.stderr) | ||
|
||
this.assert.isTrue(existsInStdout || existsInStderr) | ||
|
||
if (stream === 'stdout' && existsInStderr) { | ||
return this.assert.fail( | ||
`Expected message to be matched in ${inspect( | ||
'stdout', | ||
)} but it was found in ${inspect('stderr')}`, | ||
) | ||
} | ||
|
||
if (stream === 'stderr' && existsInStdout) { | ||
return this.assert.fail( | ||
`Expected message to be matched in ${inspect( | ||
'stderr', | ||
)} but it was found in ${inspect('stdout')}`, | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* @athenna/artisan | ||
* | ||
* (c) João Lenon <lenon@athenna.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { TestCommand } from '#src/testing/plugins/command/TestCommand' | ||
|
||
declare module '@japa/runner' { | ||
interface TestContext { | ||
command: TestCommand | ||
} | ||
} | ||
|
||
export * from '#src/testing/plugins/command/TestCommand' | ||
export * from '#src/testing/plugins/command/TestOutput' | ||
|
||
/** | ||
* Command plugin registers the command macro to the test context. | ||
*/ | ||
export function command() { | ||
return function (_config, _runner, classes) { | ||
classes.TestContext.macro('command', new TestCommand()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* @athenna/artisan | ||
* | ||
* (c) João Lenon <lenon@athenna.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { Test, type Context } from '@athenna/test' | ||
import { BaseCommandTest } from '#tests/helpers/BaseCommandTest' | ||
|
||
export default class CommandPluginTest extends BaseCommandTest { | ||
@Test() | ||
public async shouldBeAbleToExecuteCommandsUsingCommandPlugin({ command }: Context) { | ||
const output = await command.run('test hello') | ||
|
||
output.assertSucceeded() | ||
output.assertLogged('hello notRequiredArg undefined notRequiredOption') | ||
output.assertLogged("true tests|node_modules [ 'tests', 'node_modules' ]") | ||
} | ||
|
||
@Test() | ||
public async shouldBeAbleToAssertThatTheExitCodeIsNotSomeValue({ command }: Context) { | ||
const output = await command.run('test hello') | ||
|
||
output.assertIsNotExitCode(1) | ||
} | ||
|
||
@Test() | ||
public async shouldBeAbleToAssertThatTheLogMessageMatchesARegex({ command }: Context) { | ||
const output = await command.run('test hello') | ||
|
||
output.assertLogMatches(/hello notRequiredArg undefined notRequiredOption/) | ||
} | ||
|
||
@Test() | ||
public async shouldBeAbleToAssertThatTheCommandFailed({ command }: Context) { | ||
const output = await command.run('test') | ||
|
||
output.assertFailed() | ||
} | ||
|
||
@Test() | ||
public async shouldBeAbleToCheckAndManipulateTheCommandOutput({ assert, command }: Context) { | ||
const output = await command.run('test hello') | ||
|
||
assert.isDefined(output.output.stdout) | ||
assert.isDefined(output.output.stderr) | ||
assert.isDefined(output.output.exitCode) | ||
} | ||
} |