Jestjs jest有类似aroundEach的东西吗?

Jestjs jest有类似aroundEach的东西吗?,jestjs,Jestjs,测试库jest是否有类似于其他测试库中的aroundach?我不确定它是不在那里,还是我用了错误的东西搜索。我指的是允许我编写如下代码的东西: aroundEach(test => { console.log("Printed before each test") test() // Runs the actual test console.log("Printed after each test") }) function wrapp

测试库jest是否有类似于其他测试库中的
aroundach
?我不确定它是不在那里,还是我用了错误的东西搜索。我指的是允许我编写如下代码的东西:

aroundEach(test => {
  console.log("Printed before each test")
  test() // Runs the actual test
  console.log("Printed after each test")
})
function wrappedTest(name: string, fn?: ProvidesCallback, timeout?: number) {
  let wrappedFn = fn
  if (fn) {
    wrappedFn = (done) => {
      console.log("Before the test")
      fn(done)
      console.log("After the test")
    }
  }
  test(name, wrappedFn, timeout)
}
wrappedTest("Example test", async (done) => {
  console.log("this is the actual test")
  done()
})
例如,为了能够编写使用cls hooked在特定上下文中运行测试的代码,这是必需的。例如:

import {createNamespace} from "cls-hooked"

aroundEach(test => {
  let context = createNamespace("context")
  context.run(() => {
    context.set("a_number", 123456)
    test() // Runs the actual test
  })
})
下面是一个关于如何使用Ruby MiniTest的示例:

下面是关于如何在Ruby中使用Rspec的教程:

为了实现这一点,我尝试创建自己的函数,
wrappedTest
来替换(并包装)
test
,如下所示:

aroundEach(test => {
  console.log("Printed before each test")
  test() // Runs the actual test
  console.log("Printed after each test")
})
function wrappedTest(name: string, fn?: ProvidesCallback, timeout?: number) {
  let wrappedFn = fn
  if (fn) {
    wrappedFn = (done) => {
      console.log("Before the test")
      fn(done)
      console.log("After the test")
    }
  }
  test(name, wrappedFn, timeout)
}
wrappedTest("Example test", async (done) => {
  console.log("this is the actual test")
  done()
})
然后对于我的测试,我只使用
wrappedTest
而不是
test

wrappedTest("Example test", async () => {
  console.log("this is the actual test")
})
不幸的是,它因以下错误而失败:

  console.log src/entity/User.test.ts:24
    Before the test

  console.log src/entity/User.test.ts:33
    this is the actual test

  console.log src/entity/User.test.ts:26
    After the test


: Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
我尝试添加
await
,但没有效果:

function wrappedTest(name: string, fn?: ProvidesCallback, timeout?: number) {
  let wrappedFn = fn
  if (fn) {
    wrappedFn = async (done) => {
      console.log("Before the test")
      await fn(done)
      console.log("After the test")
    }
  }
  test(name, wrappedFn, timeout)
}
如果我在实际测试中调用
done
,如下所示:

aroundEach(test => {
  console.log("Printed before each test")
  test() // Runs the actual test
  console.log("Printed after each test")
})
function wrappedTest(name: string, fn?: ProvidesCallback, timeout?: number) {
  let wrappedFn = fn
  if (fn) {
    wrappedFn = (done) => {
      console.log("Before the test")
      fn(done)
      console.log("After the test")
    }
  }
  test(name, wrappedFn, timeout)
}
wrappedTest("Example test", async (done) => {
  console.log("this is the actual test")
  done()
})
它可以工作,但在我使用
test
时它不是必需的,所以我不明白为什么在使用
wrappedTest
时它是必需的


哦。。。不管怎样,这种方法可能完全错误。

你是说每次测试之前和之后?@sdgluck:不,这些方法不允许你包装测试,只能在测试之前或之后运行。这似乎是XY问题。你到底想做什么,而不是在每次之前/之后都能做到?这是怎么回事?不,没有开玩笑的事情,因为它可能不需要。@EstusFlask:正如我在问题中所说的,它在数据库事务中的回调中运行代码。大多数测试框架都提供某种设置和拆卸,这就是您在aroundEach中所做的一切。尽管你坚持某种看起来像“包装器”的东西,但每次前后的
都应该很好地符合这个要求。