Skip to content

Commit 5b1d866

Browse files
committed
add nonumber to code snippets
1 parent eaf3a78 commit 5b1d866

File tree

7 files changed

+18
-18
lines changed

7 files changed

+18
-18
lines changed

exercises/02.functions/01.solution.mock-functions/README.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
In the `emitter.test.ts`, I will start from creating the `listener` function but it won't be a regular JavaScript function. Instead, I will use the `vi.fn()` API from Vitest, which creates a _special_ kind of function.
66

7-
```ts filename=emitter.test.ts
7+
```ts filename=emitter.test.ts nonumber
88
const listener = vi.fn()
99
```
1010

@@ -16,13 +16,13 @@ Calling `vi.fn()` returns a function imbued with superpowers, one of which is th
1616
1717
Now that the mock function is ready, I will use it as a listener argument for the `hello` event:
1818

19-
```ts filename=emitter.test.ts
19+
```ts filename=emitter.test.ts nonumber
2020
emitter.on('hello', listener)
2121
```
2222

2323
Everything up to this point was the setup for this test. The action here would be calling the `.emit()` method to emit the `hello` event because the listeners are only called when the respective event gets emitted.
2424

25-
```ts filename=emitter.test.ts
25+
```ts filename=emitter.test.ts nonumber
2626
emitter.emit('hello', 'John')
2727
```
2828

@@ -35,14 +35,14 @@ The `expect()` function from Vitest comes with handy assertions to describe both
3535

3636
First, I will use the `.toHaveBeenCalledOnce()` assertion:
3737

38-
```ts filename=emitter.test.ts
38+
```ts filename=emitter.test.ts nonumber
3939
expect(listener).toHaveBeenCalledOnce()
4040
```
4141

4242
This will only pass if the `listener` function has been called exactly once. If it gets called any other number of times, it's a bug, and the test will fail.
4343

4444
In the same fashion, I will apply the [`.toHaveBeenCalledWith()`](https://vitest.dev/api/expect.html#tohavebeencalledwith) assertion to check that the `listener` function gets called with the right data:
4545

46-
```ts filename=emitter.test.ts
46+
```ts filename=emitter.test.ts nonumber
4747
expect(listener).toHaveBeenCalledWith('John')
4848
```

exercises/02.functions/02.problem.spies/README.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ To make things more complicated, logging out the event is not the responsibility
2828

2929
Luckily, the class accepts a `logger` instance as an argument:
3030

31-
```ts filename=user-service.ts nocopy
31+
```ts filename=user-service.ts nocopy nonumber
3232
constructor(private logger: Logger) {}
3333
```
3434

exercises/02.functions/02.solution.spies/README.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,29 @@
44

55
Let's start by creating a _spy_ for the `logger.log()` method using the [`vi.spyOn()`](https://vitest.dev/api/vi.html#vi-spyon) function from Vitest:
66

7-
```ts filename=user-service.test.ts lines=2
7+
```ts filename=user-service.test.ts nonumber lines=2
88
const logger = new Logger()
99
vi.spyOn(logger, 'log')
1010
const service = new UserService(logger)
1111
```
1212

1313
The `spyOn()` function has a bit of unusual call signature since it _will not_ accept the `logger.log` method reference directly. Instead, it expects the object that owns the method first, and then the method name as a string:
1414

15-
```ts
15+
```ts nonumber
1616
vi.spyOn(target, method)
1717
```
1818

1919
> :owl: The reason behind this call signature is how `.spyOn()` works under the hood. It redefines the `method` on the `target` object with a spied version of that method! You can think of it as `target[method] = methodSpy`.
2020
2121
Now that I have the spy ready and recording, I can write an assertion on that method being called with the correct arguments using the [`.toHaveBeenCalledWith()`](https://vitest.dev/api/expect.html#tohavebeencalledwith) assertion:
2222

23-
```ts filename=user-service.test.ts
23+
```ts filename=user-service.test.ts nonumber
2424
expect(logger.log).toHaveBeenCalledWith('createUser', { id: 'abc-123' })
2525
```
2626

2727
Finally, I will make sure that `logger.log()` has been called exactly once:
2828

29-
```ts filename=user-service.test.ts
29+
```ts filename=user-service.test.ts nonumber
3030
expect(logger.log).toHaveBeenCalledOnce()
3131
```
3232

exercises/02.functions/03.problem.mock-implementation/README.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ In this one, we have an `OrderController` class responsible for handling orders
99

1010
The `.createOrder()` method relies on another `.isItemInStock()` method of the same class to check any given cart item's availability.
1111

12-
```ts filename=order-controller.ts nocopy lines=2-4
12+
```ts filename=order-controller.ts nocopy nonumber lines=2-4
1313
public createOrder(args: { cart: Cart }): Order {
1414
const itemsOutOfStock = args.cart.filter(
1515
(item) => !this.isItemInStock(item),
@@ -32,7 +32,7 @@ The `.createOrder()` method relies on another `.isItemInStock()` method of the s
3232

3333
The data for the item availability itself comes from the `stock.json` file that 👨‍💼 Peter the Project Manager does a faithful job of updating on a daily basis (think of this JSON file as any source of data—e.g. a database).
3434

35-
```ts filename=order-controller.ts nocopy lines=7
35+
```ts filename=order-controller.ts nocopy nonumber lines=7
3636
import stockJson from './stock.json'
3737

3838
export class OrderController {

exercises/02.functions/03.solution.mock-implementation/README.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Let's start by going to the first test case for our `OrderController` and spying on the `isItemInStock` method of the created `controller` instance:
66

7-
```ts filename=order-controller.test.ts lines=4
7+
```ts filename=order-controller.test.ts nonumber lines=4
88
test('creates an order when all items are in stock', () => {
99
const controller = new OrderController()
1010

@@ -15,7 +15,7 @@ test('creates an order when all items are in stock', () => {
1515
1616
By using [`.mockReturnValue()`](https://vitest.dev/api/mock.html#mockreturnvalue), I am forcing the `.isItemInStock()` method to always return true when run in this test. With that behavior fixed, I can write the assertions around how the new order should be created:
1717
18-
```ts filename=order-controller.test.ts lines=15-23
18+
```ts filename=order-controller.test.ts nonumber lines=15-23
1919
test('creates an order when all items are in stock', () => {
2020
const controller = new OrderController()
2121

@@ -46,7 +46,7 @@ test('creates an order when all items are in stock', () => {
4646
4747
In the next test case, I'd like to do things a little differently. I will still spy on the `.isItemInStock()` method, but instead of mocking its return value, I will _mock that method's implementation_.
4848
49-
```ts filename=order-controller.test.ts lines=4-6
49+
```ts filename=order-controller.test.ts nonumber lines=4-6
5050
test('throws an error when one of the items is out of stock', () => {
5151
const controller = new OrderController()
5252

@@ -57,7 +57,7 @@ test('throws an error when one of the items is out of stock', () => {
5757
5858
By mocking the implementation, I am creating a mock with conditional behavior. It will behave differently based on the cart `item` that's being checked. Only the item with `id` that equals `4` will be considered in stock. This way, I am able to reproduce the error throwing logic as well as assert on the `controller` class including the right item IDs in the error message.
5959
60-
```ts filename=order-controller.test.ts lines=26-28
60+
```ts filename=order-controller.test.ts nonumber lines=26-28
6161
test('throws an error when one of the items is out of stock', () => {
6262
const controller = new OrderController()
6363

exercises/03.date-and-time/02.solution.timers/README.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ vi.advanceTimersByTime(250)
4747
4848
This will immediately fastforward the test universe ahead by 250ms, allowing me to write my expectations toward the tested debounced function:
4949
50-
```ts filename=debounce.test.ts nonumber lines=2-4
50+
```ts filename=debounce.test.ts nonumber lines=3-4
5151
vi.advanceTimersByTime(250)
5252

5353
expect(fn).toHaveBeenCalledOnce()

exercises/03.date-and-time/03.problem.ticks-and-tasks/README.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class Connection extends EventTarget {
2525

2626
You'd expect the consumers of this class to be able to listen to the `connection` event but that's not going to happen:
2727

28-
```ts
28+
```ts nonumber
2929
const connection = new Connection()
3030
connection.addEventListener('connection', () => {
3131
console.log('This is never called!')

0 commit comments

Comments
 (0)