-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 aroundEach
Hook Similar to RSpec around
#5728
Comments
I think I get the idea, but can you also provide more concrete code and references? For example, what is "RSpec We are not necessary familiar with all those stuff, so providing as much context as possible would help triage the request better without misunderstanding. |
I think what people do so far to wrap a whole test function is to either create an own wrapper of |
aroundEach
Hook Similar to RSpec around
aroundEach
Hook Similar to [RSpec](https://rspec.info/features/3-13/rspec-core/hooks/around-hooks/) around
aroundEach
Hook Similar to [RSpec](https://rspec.info/features/3-13/rspec-core/hooks/around-hooks/) around
aroundEach
Hook Similar to RSpec around
Thanks for the suggestions! I've updated the request to include some references. Also realized that the repo links I shared are still private (they'll be public soon, just hasn't happened yet). I'll shared some code examples directly. |
I'll see if I can make this work. |
Last year I gave this example: #3404 (comment) Since then, the API has changed a little bit: import { createTaskCollector, getCurrentSuite } from 'vitest/suite'
export const withTransaction = createTaskCollector(
function (name, fn, timeout) {
const handler = async (...args) => {
await database.transaction(() => fn(...args))
}
getCurrentSuite().task(name, {
...this, // so "todo"/"skip"/... is tracked correctly
handler,
timeout,
})
}
) |
Maybe you can also do something like this: import { test as baseTest } from 'vitest'
export const withTransaction = baseTest.extend({
transaction: ({ database }, use) => {
await database.transaction(() => {
await use()
})
}
}) I haven't tried it, but I think it should work 🤔 |
That looks like a huge step closer to what I was trying do to, though I don't think it quite solves the usability issue. The issue in question is: If the the next developers on the project forget to use the transaction helper, it will lead to hard to understand bugs. Ideally the everyday developer wouldn't have to worry about database cleanup as it should be handled at the config level (i.e https://vitest.dev/config/#setupfiles). Given that, I'm going to see if I can implement an e.g. describe(() => {
// some database test
}) Would always run in a transaction, and auto-rollback after completion. If we have a library or utility test that doesn't use the database we could turn the database cleaner off via describe({ type: "library" }, () => {
// some test that doesn't required the database
}) Or whatever Vitest supports. |
After all my efforts and this probably? working async function aroundEach(fn: (runExample: () => Promise<void>) => void) {
beforeEach(async () => {
let resolveRunExample: (value: void | PromiseLike<void>) => void
const runExample = new Promise<void>((resolve) => {
resolveRunExample = resolve
})
fn(() => runExample)
return async () => {
resolveRunExample()
await runExample
}
})
} The following code does not pass the transaction to the various Sequelize model actions performed in the "runExample()". aroundEach(async (runExample) => {
try {
await sequelize.transaction(async () => {
await runExample()
return Promise.reject("TRIGGER DATABASE CLEANUP")
})
} catch (error) {
if (error !== "TRIGGER DATABASE CLEANUP") {
throw error
}
}
}) |
Unless anyone still thinks this feature is valuable, I'm going to close the request, as my primary use case fails for unrelated reasons. |
I was hoping to be able to use a fixture for something like this, but apparently the async context doesn't get passed to the underlying test function. See #5858 |
Clear and concise description of the problem
I want an
aroundEach
so that I can run my database tests inside a transaction and roll back the transaction after the test completes. This would vastly speed up my test runs.Currently I need to wipe each table in my database, on each test run, to be sure that test state is clean.
Naturally, due to having a great many migrations, this is more performant than dropping the whole database and then rebuilding the schema.
code I want to replace with aroundEach wrapped in a transaction
It's also possible that this is already possible, but after spending an entire day trying to get it to work, I'm at least reasonably sure it isn't.
Suggested solution
Add support for a simple
aroundEach
hook.This would vastly speed up my test runs from
To something much more reasonable.
Its true that I can do this in each test, but who wants to clutter up their test code with setup logic?
Alternative
Implement my own
aroundEach
that looks likeAdditional context
Will be used in all of the projects I'm working; pretty much anything in https://github.com/orgs/icefoganalytics/repositories?type=public (subset of https://github.com/orgs/ytgov/repositories?type=all).
Example of tests that would benefit from beforeEach transaction wrapping
Validations
The text was updated successfully, but these errors were encountered: