在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:ironhack-labs/lab-javascript-koans开源软件地址:https://github.com/ironhack-labs/lab-javascript-koans开源编程语言:JavaScript 100.0%开源软件介绍:LAB | JS KoansKoans
What are the Koans?Koans (公案) originate from Zen Buddhism, and are paradoxical riddles or stories used to test "students" on their path to enlightenment. They are designed to provoke thought or doubt in the student's mind. We are here to learn to code, so... what are the Koans? The Koans are a series of assertions you must solve to understand how a programming language works. This is the first step to become a better developer. The Koans become increasingly more difficult as you continue, so don't feel discouraged as you move forward through them. There are Koans for all the programming languages. We will work with JavaScript Koans. The mechanism is the following:
We are going to test the code assertions through Jest. We have introduced two new concepts here: tests and Jest. Let's do a brief introduction to both of them. TestingWhen we are coding, we have to be sure that our code is working as we expect. More than that, when we update our existing code, we have to be 100% sure that our old code is still working. As our website becomes larger, it becomes more difficult to check that all our features are working as we expect. How can we automatize this process? The answer is with testing. Introduction to testingTesting allows us to check if the full set of features we have build is working as we expect. Sometimes you can create new features that override or damage the old ones. We avoid this with testing. For now, you have enough with this brief introduction. We will learn testing in depth later on the course. Anatomy of a testThe syntax to create a test depends on the framework we are using. So we will explain the basic anatomy of a test with pseudocode. Given a function with some parameters, we expect to get a result. If the result is what we are expecting, the test will pass. If we get an unexpected result, the test will fail. The framework will show the tests that are passing in green, and the tests that are failing in red. Let's suppose we have the following function: function add(num1, num2) {
// ...
} If we pass as parameters to the If we get 3 as a result, the test will pass. If we get any other result, the test will fail. In pseudo code, it would be something like this:
This is the anatomy of a test. We have the knowledge to understand what is a test and how it works. Now we will introduce you to a JavaScript framework to test our application. JestJest is a JavaScript testing framework used to test JavaScript code. If you take a look at the documentation, you will see that it has many options to test our code. In this lab, we will work with Describe, it, expect and matchersTo understand better what are the parts of the test in Jest, we will walk through an example. This is the first test we will find in our Koans: describe('the JavaScript language', () => {
describe('has different types and operators', () => {
it('considers numbers to be equal to their string representation', () => {
expect(1 == '1').toBeTruthy();
expect(1 != '1').toBeFalsy();
});
});
}); We will go through each different part of the test to explain all of them: describeIt's used to group different tests on our code. This is very helpful when we see the results of the tests. In the code above, we have two different describes:
As you can see, those are just information strings. When we execute our tests, they appear in the page grouping the different tests we have. This is very helpful when we have a lot of tests to identify where is a test that is not passing. itIt receives a expectWhat we are expecting in the test. This function contains the expression we want to test. This expression has to coincide with the matcher of the test. If they agree, the test will pass. If they disagree, the test will fail. matcherThe matchers are going to determine if a test will pass or not. The expression in the expect has to agree with the matcher. In our example, we are testing that JavaScript considers the numbers to be equal to their string representation. The matcher we selected is So, the test
We will see there are many matchers we can use. Right now, we just need the ones described above to do the Koans. ExerciseRequirements
SubmissionUpon completion, run the following commands: $ git add .
$ git commit -m "done"
$ git push origin master Create Pull Request so your TAs can check up your work. InstructionsWe need to execute our tests. After you forked and cloned this repo, open your terminal, change directories into the root of the lab, and run In the beginning, you will see all the tests in green. This is because only the tests that are passing are un-commented. The tests we have to implement are commented out. All the tests are located inside the it("surprises me, NaN is not comparable with NaN", () => { {
expect(5 / "a").toEqual(5 / "a");
//expect(typeof(NaN)).toEqual();
expect(isNaN(5 / "a")).toBeTruthy();
}); ⠀ When we uncomment the line and save the file, tests will refresh and we will see something like that: The main goal is not to finish all the tests. We want you to understand why each test is failing and how does JavaScript work in specific scopes. To do that, the correct workflow is the one used on TDD:
⠀ This process has to be done for each test. Do not uncomment all the tests and launch the app. It will be more difficult for you to see if your code is passing the tests. As we said, this is the first step to become a better developer. Good luck to all of you and happy coding. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论