Javascript Jest-描述块的顺序执行

Javascript Jest-描述块的顺序执行,javascript,jestjs,Javascript,Jestjs,我正在使用jest执行descripe()块。 在每个test()之间,我希望以同步方式执行代码,例如: description('matching cities to foods',()=>{ //仅适用于此描述块中的测试 在每个之前(()=>{ 返回initializeFoodDatabase(); }); const city=getCity(); test(可能您误解了beforeach。beforeach块将在每次test()之前被多次调用。因此,在您的情况下,按以下顺序执行测试:

我正在使用jest执行descripe()块。 在每个test()之间,我希望以同步方式执行代码,例如:

description('matching cities to foods',()=>{
//仅适用于此描述块中的测试
在每个之前(()=>{
返回initializeFoodDatabase();
});
const city=getCity();

test(可能您误解了beforeach
beforeach
块将在每次
test()
之前被多次调用。因此,在您的情况下,按以下顺序执行测试:

  • 之前
  • getCity
  • 测试1
  • getCity2
  • 测试2
  • 您可以在适当的测试块中使用
    before
    ,然后调用
    getCity()
    getCity2()
    ,如下所示:

    describe('matching cities to foods', () => {
      // Applies only to tests in this describe block
      beforeAll(() => {
        return initializeFoodDatabase();
      });
    
      test('Vienna <3 sausage', () => {
        const city = getCity();
        expect(isValidCityFoodPair(city, 'Wiener Schnitzel')).toBe(true);
      });
    
    
      test('San Juan <3 plantains', () => {
        const city2 = getCity2();
        expect(isValidCityFoodPair(city2, 'Mofongo')).toBe(true);
      });
    });
    
    description('matching cities to foods',()=>{
    //仅适用于此描述块中的测试
    以前(()=>{
    返回initializeFoodDatabase();
    });
    测试('Vienna副本