|
| 1 | +# Example code |
| 2 | + |
| 3 | +In this section, we'll take a look at some example code for tests. It's possible |
| 4 | +you've never seen a unit test before, but you'll find that they're actually |
| 5 | +quite readable, even without knowing the framework. |
| 6 | + |
| 7 | +Let's take a look at this example from |
| 8 | +[Jest](https://jestjs.io/docs/getting-started), one of the most popular testing |
| 9 | +frameworks in JavaScript. Most testing frameworks work in approximately the same |
| 10 | +fashion, simply using different syntax. |
| 11 | + |
| 12 | +```javascript |
| 13 | +test('adds 1 + 2 to equal 3', () => { |
| 14 | + expect(sum(1, 2)).toBe(3); |
| 15 | + }); |
| 16 | +``` |
| 17 | + |
| 18 | +What does this test do? To start off, consider what a function called `sum` |
| 19 | +should do. Even though we can't see the implementation, this is an example of |
| 20 | +how simply reading the test can tell you how the function should behave. |
| 21 | + |
| 22 | +In this test, when the sum function is given the arguments `sum(1, 2)`, we call |
| 23 | +a function `expect` and assert the result `toBe` `3`. Even above that, there is |
| 24 | +a written string (anything can be written there), that tells us that `adds 1 + 2 |
| 25 | +to equal 3`. Again, even though we have no idea how sum is written internally |
| 26 | +(although in this case, writing such a function should be quite trivial), **only |
| 27 | +from reading the test**, the behavior of the function is documented in some |
| 28 | +fashion. |
| 29 | + |
| 30 | +Let's continue reading some tests, without even looking at the |
| 31 | +implementation of the tested functions for illustrative purposes. |
| 32 | + |
| 33 | +In this example, let's look at the function `absolute`. You may be able to guess |
| 34 | +what the function does from the name, but if not, read the code below, and try |
| 35 | +to figure out what it does. |
| 36 | + |
| 37 | +```javascript |
| 38 | +describe('absolute', () => { |
| 39 | + it('should return positive number if input positive', () => { |
| 40 | + const result = absolute(1); |
| 41 | + expect(result).toBe(1); |
| 42 | + }); |
| 43 | + it('should return positive number if input negative', () => { |
| 44 | + const result = absolute(-1); |
| 45 | + expect(result).toBe(1); |
| 46 | + }); |
| 47 | + it('should return zero if input is zero', () => { |
| 48 | + const result = absolute(0); |
| 49 | + expect(result).toBe(0); |
| 50 | + }); |
| 51 | +}); |
| 52 | +``` |
| 53 | + |
| 54 | +In this example, the `describe` function is used to group together a related set |
| 55 | +of tests. Note how the code almost reads like a written description of the |
| 56 | +behavior of the test. |
| 57 | + |
| 58 | +Finally, let's read one more example of a slightly more complex test, written in |
| 59 | +TypeScript. Again, understanding every line is not important, and some |
| 60 | +implementations, such as `makeExpressRequest`, have been omitted. But the |
| 61 | +important part is to try to read the test and see how it helps us understand the |
| 62 | +behavior of a function. |
| 63 | + |
| 64 | +```typescript |
| 65 | +describe('makeExpressRequest()', () => { |
| 66 | + it('should return token when cookie token is provided', () => { |
| 67 | + const request = makeExpressRequest('111.222.333', null); |
| 68 | + expect(getToken(request)).toStrictEqual('111.222.333'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should extract token when bearer schema is provided', () => { |
| 72 | + const request = makeExpressRequest(null, 'Bearer xxx.yyy.zzz'); |
| 73 | + expect(getToken(request)).toStrictEqual('xxx.yyy.zzz'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should throw error when the both are provided', () => { |
| 77 | + const request = makeExpressRequest('111.222.333', 'Bearer xxx.yyy.zzz'); |
| 78 | + const expectToThrow = async () => getToken(request); |
| 79 | + expect.assertions(1); |
| 80 | + return expectToThrow().catch((e) => { |
| 81 | + expect(e).toBeDefined(); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should return null if none is provided', () => { |
| 86 | + const request = makeExpressRequest(null, null); |
| 87 | + expect(getToken(request)).toBeNull(); |
| 88 | + }); |
| 89 | +}); |
| 90 | +``` |
| 91 | + |
| 92 | +Try to answer the following questions about the above example: |
| 93 | +* What does the first argument to `makeExpressRequest` represent? |
| 94 | +* What does the second argument represent? |
| 95 | +* Are you supposed to pass in both arguments at once? |
| 96 | +* If you pass in the second argument, what does it do to the argument? |
| 97 | + |
| 98 | +By reading the tests in this section, this has given you a more concrete |
| 99 | +understanding of what tests may look like, how they verify correctness, and how |
| 100 | +they help others understand the behavior of a function. |
0 commit comments