Skip to content

Instantly share code, notes, and snippets.

@hillal20
Forked from luishrd/index.js
Created May 21, 2018 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hillal20/7aeaf05892b2c6d9dcbf2b16d5844869 to your computer and use it in GitHub Desktop.
Save hillal20/7aeaf05892b2c6d9dcbf2b16d5844869 to your computer and use it in GitHub Desktop.
Client Testing
// inside __tests__ folder
const utilities = require('../index');
describe('default', () => {
it('run the tests', () => {});
});
describe('add function', () => {
it('should add two numbers', () => {
// arrange
const add = utilities.add;
// act
const seven = add(3, 4);
const four = add(0, 4);
const minusThree = add(1, -4);
// assert
expect(seven).toBe(7);
expect(four).toBe(4);
expect(minusThree).toEqual(-3);
});
it('checks identity', () => {
const numbers = [1, 2, 3];
// const actual = [1, 2, 3];
const actual = numbers;
expect(numbers).toBe(actual);
});
it('checks that it is an array', () => {
const numbers = [1, 2, 3];
const expected = 'array';
const actual = Array.isArray(numbers);
expect(actual).toBe(true);
});
});

(Automated) Testing

  • why?
  • how?
  • what? comes from experience and the system we're testing and even the tech stack.
  • when? test-first vs test after.

Confidence (in the sense of trusting your code). Verifying correctess.

We write code to test our code.

Why learn it?

  • good for resume.
  • can work on QA and DevOps.
  • helps understand how an application works.
  • prevents regressions.
  • safety net.

Tooling: Test runner (Mocha) + assertion library (Chai).

We are using Jest.

What could happen if I have no tests.

  • test manually, as the app grows, the time needed will not scale.
  • regressions, code that used to work breaks.
  • you just don't know if the code is correct for all cases.
  • adding new features becomes slow.

Types

  • unit tests. Fastest.
  • integration tests.
  • end to end. Slow.
  • component testing. (for component based frameworks, like React, Angular, Vue, etc.)
  • snapshot testing. (unique to our stack/tools)
  • coverage test.
  • functional testing.
  • performance testing.
  • aceptance test.

We'll concentrate on:

  • unit
  • component
  • snapshot

when, then

{
"name": "testing",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest --watch"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"jest": "^22.4.4"
}
}
// at root folder, rename to index.js had to name it this becuase two files can't have the same name on a gist
function add(a, b) {
return a + b;
}
module.exports = {
add: add,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment