beforeAll 和写在代码顶部有什么区别?

What is the difference between beforeAll and writing at the top of the code?

在 Jest 和其他测试框架中?写 beforeAll 有什么意义?

以下 link 没有帮助:

Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll

beforeAll(() => server.listen());

上面的行放在我正在检查的代码库的全局范围内。

如果我重写如下,会有什么变化?

server.listen()

当您有需要 运行 的异步函数 (async/promise) 时,如果没有样板代码,它们可能无法在测试开始前完成,通过使用 before all 或 after all,您可以确保异步函数调用在开始前解析。

此外,使用 beforeAllafterAll 之类的方法可以调用一次性 setup/teardown 函数。如果这些失败(抛出异常),那么框架知道所有后续测试都将失败并且不会 运行 它们。

您可以阅读有关 setup/teardown 方法的更多信息 here