From f35c6f92fc89766f49e01aa43ee76c465d2387fd Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Sat, 24 Jun 2023 17:09:26 +0530 Subject: [PATCH] feat: add resource method to create test aware resources --- index.ts | 25 ++++++++++++++++++++++++- modules/core/main.ts | 7 ++++++- package.json | 2 +- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/index.ts b/index.ts index 5c82f51..ba00794 100644 --- a/index.ts +++ b/index.ts @@ -18,13 +18,18 @@ import { CliParser } from './src/cli_parser.js' import type { CLIArgs, Config } from './src/types.js' import { ConfigManager } from './src/config_manager.js' import { createTest, createTestGroup } from './src/create_test.js' -import { Emitter, Group, Runner, Suite, TestContext } from './modules/core/main.js' +import { Emitter, Group, Runner, Suite, Test, TestContext } from './modules/core/main.js' /** * Global emitter instance used by the test */ const emitter = new Emitter() +/** + * The current active test + */ +let activeTest: Test | undefined + /** * Parsed commandline arguments */ @@ -59,6 +64,13 @@ export function test(title: string, callback?: TestExecutor { + activeTest = t + return () => { + activeTest = undefined + } + }) + if (callback) { testInstance.run(callback) } @@ -99,6 +111,17 @@ export function configure(options: Config) { runnerConfig = new ConfigManager(options, cliArgs || {}).hydrate() } +/** + * Create a resource that has access to the current test + */ +export function resource(callback: (test: Test | null) => T) { + return { + create(): T { + return callback(activeTest || null) + }, + } +} + /** * Execute Japa tests. Calling this function will import the test * files behind the scenes diff --git a/modules/core/main.ts b/modules/core/main.ts index 53722bc..867fc4a 100644 --- a/modules/core/main.ts +++ b/modules/core/main.ts @@ -49,7 +49,12 @@ export class Test extends BaseTest< /** * @inheritdoc */ - static disposeCallbacks = [] + static executedCallbacks = [] + + /** + * @inheritdoc + */ + static executingCallbacks = [] } /** diff --git a/package.json b/package.json index a77b45d..7058fcb 100644 --- a/package.json +++ b/package.json @@ -66,7 +66,7 @@ "typescript": "^5.1.3" }, "dependencies": { - "@japa/core": "^8.0.0-1", + "@japa/core": "^8.0.0-2", "@japa/errors-printer": "^3.0.0-0", "@poppinss/cliui": "^6.1.1-2", "@poppinss/hooks": "^7.1.1-3",