Comments (9)
I think I get the idea, but can you also provide more concrete code and references? For example, what is "RSpec around
"? also I would assume you're using sequelize.transaction
with async hook mode https://sequelize.org/docs/v6/other-topics/transactions/#automatically-pass-transactions-to-all-queries but it's not entirely clear from the given code.
We are not necessary familiar with all those stuff, so providing as much context as possible would help triage the request better without misunderstanding.
from vitest.
I think what people do so far to wrap a whole test function is to either create an own wrapper of it
like effect
https://github.com/Effect-TS/effect/blob/40c2b1d9234bcfc9ab4039282b621ca092f800cd/packages/vitest/src/index.ts#L55-L64 or I think it's also possible to use custom task function https://vitest.dev/advanced/runner.html#your-task-function
from vitest.
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,
})
}
)
from vitest.
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 π€
from vitest.
I think I get the idea, but can you also provide more concrete code and references? For example, what is "RSpec
around
"? also I would assume you're usingsequelize.transaction
with async hook mode https://sequelize.org/docs/v6/other-topics/transactions/#automatically-pass-transactions-to-all-queries but it's not entirely clear from the given code.We are not necessary familiar with all those stuff, so providing as much context as possible would help triage the request better without misunderstanding.
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.
from vitest.
I think what people do so far to wrap a whole test function is to either create an own wrapper of
it
likeeffect
https://github.com/Effect-TS/effect/blob/40c2b1d9234bcfc9ab4039282b621ca092f800cd/packages/vitest/src/index.ts#L55-L64 or I think it's also possible to use custom task function https://vitest.dev/advanced/runner.html#your-task-function
I'll see if I can make this work.
Maybe I can add a describe({ type: 'database' }, () => {}
or something that using that effect pattern you shared.
from vitest.
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 aroundEach
, using the task collector? (or something), so database cleanup is handled at the highest level. For context I'm writing tests for the back-end of a web app, so 90% of tests will be database tests. I'd like to operate so that tests run the database cleaner by default, and if there are some non-database tests that we want maximum speed for, we can set a flag to disable them.
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.
from vitest.
After all my efforts and this probably? working aroundEach
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
}
}
})
from vitest.
Unless anyone still thinks this feature is valuable, I'm going to close the request, as my primary use case fails for unrelated reasons.
from vitest.
Related Issues (20)
- inner suite `{ sequential: true }` doesn't override outer suite `{ concurrent: true }`
- ζ§θ‘ vitest ζΆζ₯ι ast.body is not iterable HOT 1
- Remove version labels for Vitest 2 documentation
- Text reporter still creates an empty coverage folder HOT 3
- using setupFiles from parent dir causes 'Failed to load url' HOT 2
- Check for listen port collision for browser tests HOT 6
- Segmentation fault - Test fails when using Vitest, but passes when using Jest HOT 2
- Mixing browser and non-browser projects in vitest workspace causes error HOT 2
- testNamePattern doesn't parse RegEx'es in the CLI, or documentation is unclear HOT 2
- get error "Could not parse CSS stylesheet" when CSS file contains nested rules HOT 2
- Issue with `vitest --run --changed` when used with lint-staged on Windows HOT 2
- Default reporter emits `prepare` longer than `Duration` when prepare takes longer than 1s HOT 3
- test.each does not print -0 and -NaN correctly
- `vi.fn()` performs an extra _await_ leading to mismatching behaviour when compared with normal function HOT 6
- restoreAllMocks doesn't restore mocks HOT 4
- API for timer mocks that are safely isolated between tests HOT 3
- There's no console.log output from 'beforeAll' and 'afterAll' when running tests via Webstorm / Aqua IDE HOT 1
- Add support for Node.js 20 `--env-file` flag HOT 6
- vitest fails to load typescript worker code in node (but does so correctly in browser mode) HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vitest.