我正在将现有的测试迁移到Jest。 我可以在Jest中使用before而不是beforeAll吗? 我所做的是在jest.config.js中, module.exports = {
...
globals: {
before: beforeAll,
},
}; 但是如何导入beforeAll呢?require('@jest/globals')提出了: Error: Do not import `@jest/globals` outside of the Jest test environment
我正在使用jest和@testing-library/react-hooks来测试用react-query实现的钩子,这些钩子在我的React本机代码中实现。
测试正常,但最后,我得到了:
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHan
我正在编码一个不和谐的机器人,我想测试一些代码。使用discord.js库,我们必须使用客户端连接到不和谐的API。在开始时,客户端准备好时接收到一个事件:
在我的index.js里
client.on('ready', () => {})
但在我的测试中,我需要来自客户端缓存的信息(公会):
beforeAll(() => {
client.on('ready', () => {
// => guild that I need for test
guildToTest = client.guilds.cache.
在rspec中,您可以这样做:
let(:input) { 'foo' }
before_each do
setup_some_thing(input)
end
context 'when input is bar do
let(:input) { 'bar' }
it 'does something different' do
end
end
context 'when input is baz do
let(:input) { 'baz' }
it 'does someth
在这种情况下,我需要创建一个调用函数并检查其返回值的测试。它返回一个对象,所以我必须遍历它并检查大约100个值的正确性。如果其中一个失败了,我想知道是哪一个。 我不知道如何使用vanilla Jest来做这件事,这样测试是自包含的,并且在失败时我会得到一条有意义的错误消息。 例如,我可以这样做:(伪代码用于说明,而不是实际代码) describe('Test for function A', () => {
beforeAll('Create class instance', () => {
this.inst = new
我正在使用Jest进行测试。如何访问正在运行的当前测试的文件名或文件路径?
我需要一个条件语句,根据它是单元测试文件还是集成测试文件来运行不同的代码行。
下面是我正在尝试实现的一个示例:
beforeAll(() => {
if (integration_test_file){
// run this this code
} else if (unit_test_file){
// run this code instead
}
})
这是密码。当我运行测试时,我得到了错误的TypeError: setSystemTime is not available when not using modern timers。我的package.json中有package.json,因为我认为在某些依赖项中包上可能存在冲突,但问题仍然存在。
beforeAll(() => {
jest.useFakeTimers('modern');
jest.setSystemTime(new Date());
});
afterAll(() => {
jest.useRealTimers();
我正在尝试使用Jest为我的Express服务器编写集成测试。由于Jest并行运行测试(我希望避免使用--runInBand按顺序运行测试),因此我使用库来查找随机可用的端口,以便不同的测试套件不会有端口冲突。
我的测试都运行成功,唯一的问题是服务器在afterAll钩子中无法正常关闭。这会导致Jest在控制台中打印以下内容...
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren