【jest 运行顺序之 beforeEach/beforeAll】

beforeEach()函数用于在每个测试运行之前运行一段代码。它允许我们在每个测试之前设置共享的测试环境或变量。这样可以确保每个测试都在相同的环境下开始。

beforeAll()函数在所有测试之前只运行一次,所以上下文会有影响。

同理afterEach,afterAll,分别是每个测试之后运行,和所有测试之后只运行一次。

代码举例:

安装jest相关依赖

# jest本体
npm install --save-dev jest
# jest的类型声明
npm install --save-dev @types/jest
# typescript中使用
npm install --save-dev ts-jest

代码示例

class Calculator {
    count: number;
    constructor(count) {
      this.count = count;
    }
    add(val) {
      this.count = (this.count + val);
      return this.count;
    }
    subtract(val) {
      this.count=  (this.count - val);
      return this.count;
    }
}

export { Calculator };

Test beforeEach

import { Calculator } from "./Calculator";

describe("Calculator", () => {
  let calculator: Calculator;

  beforeEach(() => {
    calculator = new Calculator(10);
  });

  it("should add numbers correctly", () => {
    const result = calculator.add(1);
    expect(result).toEqual(11);
  });

  it("should subtract numbers correctly", () => {
    const result = calculator.subtract(1);
    expect(result).toEqual(9);
  });
});

Test beforeAll

import { Calculator } from "./Calculator";

describe("Calculator", () => {
  let calculator: Calculator;

  beforeAll(() => {
    calculator = new Calculator(10);
  });

  it("should add numbers correctly", () => {
    const result = calculator.add(1);
    expect(result).toEqual(11);
  });

  it("should subtract numbers correctly", () => {
    const result = calculator.subtract(1);
    expect(result).toEqual(9);
  });
});

可以看出,beforeEach每次厕所都会生成新的calculator,而beforeAll只会生成一次calculator。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Introduction T he CCENT certification is a new, fast-growing certification that tests your knowledge of basic Cisco device-management and networking concepts. It is a great stepping stone to the CCNA certification and other Cisco certification tracks. The CCENT exam tests your knowledge of real-world networking concepts and Cisco features found on most networks today! About This Book CCENT Certification All-In-One For Dummies is designed to be a hands-on, practical guide to help you pass the CCENT certification exam. This book is written in a way that helps you not only understand complex technical content, but also prepares you to apply that knowledge to real-world scenarios. I understand the value of a book that covers the points needed to pass the exam, but I also understand the value of ensuring that the information helps you perform IT-related tasks when you are on the job. That is what this book offers you — key points to pass the exam combined with practical information to help you in the real world, which means that you can use this book in more than one way. ✦ An exam preparation tool: Because my goal is to help you pass the CCENT certification exam, this book is packed with exam-specific information and tips to help you with tricky exam questions. You should understand everything that is in this book before taking the exam, but to identify key points that you must know, look for icons named For the Exam. In those paragraphs, you will find helpful tips on topics you are certain to be tested on. ✦ A reference: Rely on my extensive experience in the IT industry not only to study for (and pass) the exam, but also to help you perform common network-related tasks on the job. I hope you find this book a useful tool that you can refer to time and time again in your career as you configure networks and Cisco devices. Conventions Used in This Book Each chapter in this book has different elements that help you prepare to pass your CCENT, including the following features: 03_647486-intro.indd 103_647486-intro.indd 1 10/15/10 11:18 PM10/15/10 11:18 PM Conventions Used in This Book 2 ✦ Quick Assessments: Located at the beginning of each chapter is a Quick Assessment section that gives a number of questions related to the chapter content for you to assess whether you have the knowledge already in that chapter. It is highly recommended to read all chapters in the book, but if you find you are limited on study time you may want to focus on the topics you know the least about — the Quick Assessments help you determine what topics you know and what you need more work on. ✦ Icons: Look for the icons used in each chapter to draw your attention to information needed for the exam or in the real world. For more details on the icons I use, check out the later section, “Icons Used in This Book.” ✦ Chapter Summary: Found at the end of each chapter, the “Chapter Summary” section covers key points you should remember for the exam. ✦ Labs: Lab exercises offer the opportunity to get your hands dirty with a particular topic with real-world experience performing specific tasks. In order to totally grasp the topics discussed in a chapter, be sure to perform the lab exercises. The CCENT certification has a number of simulators that will test your real-world knowledge so you really need to know how to perform the different tasks to pass the exam. Due to the fact that you may have different configurations when you do the labs, there are no lab answers within the Labs section. ✦ Prep Test: Following each “Chapter Summary” section, you can find questions to help review the chapter content and prepare you for the CCENT certification exam. Be sure to answer the review questions in each chapter! Then, after you finish reading the entire book and do the lab exercises, check out the practice exams on the companion CD-ROM, which is designed to function like the real exam, with the same level of difficulty. ✦ Monofont text: To help you distinguish commands you type or text you should see on the screen I apply the monofont style to the text. Examples where you see this style are on router commands, IP addresses, and names of devices. ✦ Boldface text: To help identify new commands that you are learning within a code listing the boldface text style is applied. Although you should read over all code in a code example, using the boldface text will help draw your attention to the new commands presented in a code listing.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值