Skip to content

Commit

Permalink
feat: add resource method to create test aware resources
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jun 24, 2023
1 parent c57d344 commit f35c6f9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
25 changes: 24 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> | undefined

/**
* Parsed commandline arguments
*/
Expand Down Expand Up @@ -59,6 +64,13 @@ export function test(title: string, callback?: TestExecutor<TestContext, undefin
validator.ensureIsInPlanningPhase(executionPlanState.phase)

const testInstance = createTest(title, emitter, runnerConfig!.refiner, executionPlanState)
testInstance.setup((t) => {
activeTest = t
return () => {
activeTest = undefined
}
})

if (callback) {
testInstance.run(callback)
}
Expand Down Expand Up @@ -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<T>(callback: (test: Test<any> | null) => T) {
return {
create(): T {
return callback(activeTest || null)
},
}
}

/**
* Execute Japa tests. Calling this function will import the test
* files behind the scenes
Expand Down
7 changes: 6 additions & 1 deletion modules/core/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ export class Test<TestData extends DataSetNode = undefined> extends BaseTest<
/**
* @inheritdoc
*/
static disposeCallbacks = []
static executedCallbacks = []

/**
* @inheritdoc
*/
static executingCallbacks = []
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f35c6f9

Please sign in to comment.