Giter VIP home page Giter VIP logo

Comments (9)

hi-ogawa avatar hi-ogawa commented on July 17, 2024 1

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.

hi-ogawa avatar hi-ogawa commented on July 17, 2024 1

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.

sheremet-va avatar sheremet-va commented on July 17, 2024 1

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.

sheremet-va avatar sheremet-va commented on July 17, 2024 1

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.

klondikemarlen avatar klondikemarlen commented on July 17, 2024

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.

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.

klondikemarlen avatar klondikemarlen commented on July 17, 2024

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

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.

klondikemarlen avatar klondikemarlen commented on July 17, 2024

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.

klondikemarlen avatar klondikemarlen commented on July 17, 2024

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.

klondikemarlen avatar klondikemarlen commented on July 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.