# Jest js

### Writing test cases

* Written in files with extension `.test.js`
* Can be setup in `package.json` script `test: jest --watchAll --verbose`
  * `--watchAll` allows our code to be watched in the background and re run the tests when changes

```js
class Stack {
	constructor() {
		this.top = -1;
		this.items = {};
	}

	get peek() {
		return this.items[this.top];
	}

	push(value) {
		this.top += 1;
		this.items[this.top] = value;
	}

	pop() {
		this.top -= 1;
	}
}

// describe is used to define test suite

describe('My Stack', () => {
	let stack;

	// jest has helper functions like beforeEach, afterEach, beforeAll, afterAll
	beforeEach(() => {
		stack = new Stack();
	});

	// test/it function used for defining individual tests
	it('is created empty', () => {
		// toBe is a matcher which is used to match actual value with expected value
		expect(stack.top).toBe(-1);

		// toEqual matcher checks for value equality instead of object reference itself
		expect(stack.items).toEqual({});
	});

	it('can push to the top', () => {
		stack.push(2)

		expect(stack.top).toBe(0)
		expect(stack.peek).toBe(2)
	});


	// it.todo used to pass test while you fix the issues
	it('can pop off', () => {
		stack.push(2)
		stack.push(3)

		expect(stack.top).toBe(1)
		expect(stack.peek).toBe(3)

		stack.pop()

		expect(stack.top).toBe(0)
		expect(stack.peek).toBe(2)
	});
})
```

***

### Code Coverage

* Tells us how much our tests cover our source code
* We can add `--coverage` option in our `package.json` jest script section

***

### End-to-End Testing

* Done in jest by using the `jest-puppeteer` package

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.nomanaziz.me/development/testing/jest-js.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
