-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add test.result(testName) to get results #10077
Comments
@SimenB if I were to make a PR that implements this, would it have a chance of being merged? |
Hi! I think marking a dependency inside of another test would mean we'd still have to run it, then bail with a special error. Some API where the dependency is marked as part of the test declaration is needed, I think... Also, how would this work with concurrent tests? Still experimental, but still |
Hi @SimenB :) the dependency is handled the way you describe and it should work correctly with concurrent tests. Here's my latest version: // https://github.com/facebook/jest/issues/5823
class TestError extends Error {
constructor(title, error) {
super(title)
if (error instanceof TestError) {
// A dependency threw this
this.message = error.deep
? error.message
: `Dependency "${error.title}" failed`
this.stack = ' ' // truthy empty error
this.deep = true
} else {
this.message = `test "${title}" failed: ${error.message}`
this.stack = error.stack
this.title = title
}
}
}
const origTest = global.test
const origDescribe = global.describe
const runners = {}
let prefix = ''
global.describe = (name, fn) => {
const prev = prefix
// Note, using async local storage, this could be used automatically
prefix = `${prefix}${name} | `
origDescribe(name, fn)
prefix = prev
}
global.test = (title, fn) => {
const name = `${prefix}${title}`
let alreadyRan = false
let result = undefined
let fnError = undefined
const handleError = (error, saveError) => {
const newError = new TestError(name, error)
if (saveError) fnError = newError
throw newError
}
const runFnOnce = () => {
if (alreadyRan) {
if (fnError) throw fnError
else return result
}
alreadyRan = true
try {
result = fn()
} catch (err) {
// synchronous error
handleError(err, true)
}
// async error
if (result?.catch) result = result.catch(err => handleError(err, false))
return result
}
runners[name] = runFnOnce
origTest(title, runFnOnce)
return runFnOnce
}
// add .each etc
Object.assign(test, origTest)
test.result = title => {
if (runners[title]) return runners[title]()
else {
const msg = `test.result(title): unknown test "${title}". Known:\n${Object.keys(
runners
).join('\n')}`
throw new Error(msg)
}
} and a types.d.ts file to match: declare namespace jest {
interface It {
result: (testName: string) => any
}
} |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
Still interested in this |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
@SimenB any chance of this? |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
🚀 Feature Proposal
This is a more concrete version of #5823, with a working prototype. I'm opening it to increase visibility; if this stil counts as a duplicate I apologize, please close but please consider this option.
Motivation
It is hard to write tests for an expensive operation that requires many steps. It would be great if individual steps could be written as tests and a dependency on other tests expressed.
Example
I'd like to propose this API, getting a return value from
test
and friends which can be called to get the test result; as a side effect it can await Promises:Now, if you only run the
'can doBar'
test, it will actually run'gets config'
and then'can create'
first, and error out if those fail.Pitch
It's a very intuitive interface fitting right in with the rest of the API. It makes it easy to express dependencies, and if you don't use it, there's no change.
Prototype
I put this in a file and import it at the beginning of a test file. It works great, but it won't detect dependency loops, and if you only run a single test, then the tests it runs for dependencies won't count as the tests that ran. To fix those, it would need integration into Jest.
The text was updated successfully, but these errors were encountered: