面试自我介绍可以开玩笑嘛_从开玩笑开始

面试自我介绍可以开玩笑嘛

As I have done a couple of backend testing blogs with RSpec and Ruby on Rails. I have decided to do a couple on frontend testing with Jest. To begin with, I was looking at frontend testing and wasn’t sure whether to start learning Jest or Mocha. I found a great blog explaining them both and would highly recommend reading it as it briefly discusses both of them.

我已经用RSpec和Ruby on Rails做了两个后端测试博客。 我决定与Jest进行一些前端测试。 首先,我正在研究前端测试,不确定是否要开始学习Jest或Mocha。 我找到了一个很棒的博客,向他们解释了这两个方面,并强烈建议您在简短地讨论这两个方面的同时阅读它们。

Jest Vs Mocha Blog!

笑话与摩卡博客!

I ended up looking at Jest simply because I felt like I wanted to use something with an easy setup and ready to use virtually straight away. A testing frameworks job is to tell a developer what’s broken as quickly as possible and a quick setup is always handy. Which is exactly what Jest is!

我最终看了Jest只是因为我觉得我想使用一种易于设置的东西并准备立即使用。 测试框架的工作是尽可能快地告诉开发人员哪些问题,并且快速设置总是很方便的。 杰斯特到底是什么!

To testing!

要测试!

So firstly, let’s create our Javascript project. As seen below.

因此,首先,让我们创建我们的Javascript项目。 如下所示。

Answer a couple of questions to setup your project folder.

回答几个问题以设置您的项目文件夹。

The first line creates our project and the second adds jest to your devDependencies meaning it doesn’t need to be downloaded for production, but is only needed for local development and testing.

第一行创建了我们的项目,第二行为您的devDependencies添加了笑话,这意味着无需下载即可进行生产,而仅需进行本地开发和测试即可。

Image for post

Next we need to add the following section to our package.json.

接下来,我们需要将以下部分添加到package.json中。

Image for post

You will likely already have a script section so just add the test line.

您可能已经有一个脚本部分,因此只需添加测试行即可。

Perfect! We are all setup to try some basic testing in our Javascript project.

完善! 我们都准备在我们的Javascript项目中尝试一些基本测试。

In our src folder, let’s type a quick function or two in main.js.

在我们的src文件夹中,让我们在main.js中键入一两个快速函数。

Image for post

Here we have written some basic functions inside of main.js and we have exported the functions at the very bottom.

在这里,我们在main.js内部编写了一些基本函数,并且在最底部导出了这些函数。

Image for post

Next create a folder called ‘test’ within the src. Within here is where we are going to write our files for testing. Typically, you would write a test file for every js.file you create for your project. This allows the testing to give clarity to what file it is testing and what sections of your project.

接下来,在src中创建一个名为“ test”的文件夹。 在这里,我们将要编写文件进行测试。 通常,您将为为项目创建的每个js.file编写一个测试文件。 这样可以使测试清楚地说明正在测试的文件以及项目的哪些部分。

Now let’s write a basic test within this file. Firstly, we want to make a call to the js file we are testing at the very top of our test file.

现在让我们在该文件中编写一个基本测试。 首先,我们要在测试文件的最顶部调用要测试的js文件。

Image for post

Once you have required the file we are testing, use the concept of destructuring to extract the functions I wanted to test from the main.js file. Destructuring makes your code more readable and easier to understand.

一旦您需要我们要测试的文件,请使用解构概念从main.js文件中提取我要测试的功能。 解构可以使您的代码更具可读性并更易于理解。

Blog on Destructuring.

关于解构的博客

Let’s break this code snippet down so we can understand it. As mentioned, firstly we call the file we are testing, with the functions extracted. Next we use an inbuilt method called describe.

让我们分解一下代码片段,以便我们理解它。 如前所述,首先我们调用我们正在测试的文件,并提取函数。 接下来,我们使用一种称为describe的内置方法。

Describe is a Jest method for describing and containing our tests. As you can see, I have given it a descriptive sentence to allow another developer to understand what tests are contained within the block. It takes two arguments of which one is a string for describing the test suite, and the other a callback function for wrapping the actual test.

Describe是描述和包含测试的Jest方法。 如您所见,我给它提供了一个描述性的句子,以允许其他开发人员了解该块中包含哪些测试。 它带有两个参数,其中一个是用于描述测试套件的字符串,另一个是用于包装实际测试的回调函数。

Then we use another inbuilt function called ‘test’. This is pretty self-explanatory. It’s a block containing the actual code we are wanting to test!

然后,我们使用另一个内置函数“ test”。 这是不言自明的。 这是一个包含我们要测试的实际代码的块!

Okay, let’s dive into the actual test block itself.

好的,让我们深入实际的测试块本身。

Image for post

Here the test method takes two arguments, similar to the describe method. Here we want to be descriptive of the method we are testing and what we expect it to do. Next, we use a Jest inbuilt method called expect.

这里的测试方法有两个参数,类似于describe方法。 在这里,我们要描述我们正在测试的方法以及我们希望它做些什么。 接下来,我们使用Jest内置方法称为Expect。

The expect method contains the function we extracted from main.js at the top of our test file and passes the two arguments required for the function to operate.

Expect方法在测试文件顶部包含从main.js提取的函数,并传递该函数运行所需的两个参数。

What happens if we pass one?

如果我们通过一个会怎样?

Image for post

This is where Jest shines. As you can see, if I try pass just one parameter in and run our test, it fails. But Jest gives us a perfect breakdown of what it was expecting and what it received. You can see it does a deep equality check meaning it is also checking the number of arguments being passed to each test, fix the issue if you wanted to check the failure message for yourself and move on.

这就是杰斯特的光芒。 如您所见,如果我尝试仅传入一个参数并运行测试,它将失败。 但是Jest为我们提供了它所期望和得到的完美细分。 您会看到它进行了深入的相等性检查,这意味着它还在检查传递给每个测试的参数数量,如果您想自己检查失败消息并继续进行,请解决此问题。

Next, we have another inbuilt method .toEqual. The .toEqual inbuilt method is where we output the expected result for our function to match against. Again, if I was to incorrectly enter the wrong result in our .toEqual parentheses, Jest would fail the test.

接下来,我们还有另一个内置方法.toEqual。 .toEqual内置方法是我们输出预期结果以匹配函数的地方。 同样,如果我在.toEqual括号中错误地输入了错误的结果,Jest将使测试失败。

Once finished, write ‘npm run test’ in your terminal and you should get:

完成后,在终端中输入“ npm run test”,您将获得:

Image for post

Voila! The very basics of Jest testing. If you want to play around with it, try writing a couple more functions, importing them into your test file and playing around.

瞧! Jest测试的基础知识。 如果您想使用它,请尝试编写更多函数,将它们导入测试文件中并进行游戏。

You can also use the inbuilt method of .not.

您也可以使用.not的内置方法。

.not essentially checks the resulting tested function output does not equal the code inside the .not parenthesis. A rudimentary example to give you an understanding is below:

.not本质上检查生成的测试函数输出不等于.not括号内的代码。 下面是一个可以使您理解的基本示例:

Image for post
Image for post

Here subtracting 3 from 2 is not equal to 10000 (as far as I’m aware). So the test passes.

这里从2中减去3不等于10000(据我所知)。 这样测试通过了。

Finally, if you are using decimals (float) be careful which inbuilt method you use.

最后,如果您使用小数(浮点数),请小心使用哪种内置方法。

Image for post

.toEqual will actually fail due to an eventual rounding error. You have to use .toBeCloseTo for a passing test.

.toEqual实际上将由于最终的舍入错误而失败。 您必须使用.toBeCloseTo进行通过测试。

Image for post
Image for post

In my next blog I am going to delve deeper into Jest looking at more method matchers and then moving onto async functions!

在我的下一个博客中,我将深入研究Jest,研究更多的方法匹配器,然后转向异步函数!

翻译自: https://medium.com/swlh/starting-out-with-jest-b755756792b3

面试自我介绍可以开玩笑嘛

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值