All Projects → karlhadwen → jest-playground

karlhadwen / jest-playground

Licence: other
Playing around with Jest - Subscribe to my YouTube channel: https://bit.ly/CognitiveSurge

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects
shell
77523 projects

Projects that are alternatives of or similar to jest-playground

jest-extended-snapshot
Additional Jest matchers for snapshot testing.
Stars: ✭ 18 (-25%)
Mutual labels:  jest, jest-snapshots
angularjs-jest
Testing AngularJS applications with Jest
Stars: ✭ 24 (+0%)
Mutual labels:  jest, jest-snapshots
firestore-jest-mock
Jest Helper library for mocking Cloud Firestore
Stars: ✭ 128 (+433.33%)
Mutual labels:  jest, jest-mocking
mock-spy-module-import
JavaScript import/require module testing do's and don'ts with Jest
Stars: ✭ 40 (+66.67%)
Mutual labels:  jest, jest-mocking
snappify
A generator of Jest snapshot-based tests for React components written with TypeScript 📸
Stars: ✭ 64 (+166.67%)
Mutual labels:  jest, jest-snapshots
nuxt-boilerplate
Nuxt.js Boilerplate
Stars: ✭ 25 (+4.17%)
Mutual labels:  jest, jest-snapshots
testing-reactjs-examples
🧪 "What should we test in our React components" - presentation examples.
Stars: ✭ 23 (-4.17%)
Mutual labels:  jest, jest-snapshots
Jest Image Snapshot
✨ Jest matcher for image comparisons. Most commonly used for visual regression testing.
Stars: ✭ 2,940 (+12150%)
Mutual labels:  jest, jest-snapshots
react-initial-bootstrap
an "almost complete" template for your new projects
Stars: ✭ 24 (+0%)
Mutual labels:  jest
express-typeorm-rest-boilerplate
Boilerplate code to get started with building RESTful API Services (Express, TypeORM MongoDB stack)
Stars: ✭ 53 (+120.83%)
Mutual labels:  jest
ionic2-jest-example
Example of how to test an Ionic2 app with Jest
Stars: ✭ 43 (+79.17%)
Mutual labels:  jest
aws-nestjs-starter
Serverless, AWS, NestJS, GraphQL and DynamoDB starter
Stars: ✭ 200 (+733.33%)
Mutual labels:  jest
nimbus
Centralized CLI for JavaScript and TypeScript developer tools.
Stars: ✭ 112 (+366.67%)
Mutual labels:  jest
sentry
👁 A self hosted dashboard to monitor servers and services.
Stars: ✭ 19 (-20.83%)
Mutual labels:  jest
node-js-starter-kit
This is the starter kit project for node js REST API development with express js, mongodb, typescript, webpack specially designed for REST API projects.
Stars: ✭ 14 (-41.67%)
Mutual labels:  jest
ts-detox-example
Example TypeScript + React-Native + Jest project that integrates Detox for writing end-to-end tests
Stars: ✭ 54 (+125%)
Mutual labels:  jest
generator-node
🔧 Yeoman generator for Node projects.
Stars: ✭ 16 (-33.33%)
Mutual labels:  jest
gobarber-api-gostack11
API GoBarber / NodeJS / Express / Typescript / SOLID
Stars: ✭ 39 (+62.5%)
Mutual labels:  jest
webpack
Готовая сборка webpack
Stars: ✭ 21 (-12.5%)
Mutual labels:  jest
Express-REST-API-Template
Minimal starter project for a Node.js RESTful API based off express generator
Stars: ✭ 26 (+8.33%)
Mutual labels:  jest

Jest playground

import pizzas from '../data';

// very basic test to notify the user if our pizza data has changed
test('the pizza data is correct', () => {
  expect(pizzas).toMatchSnapshot();
  expect(pizzas).toHaveLength(4);
  expect(pizzas.map(pizza => pizza.name)).toEqual([
    'Chicago Pizza',
    'Neapolitan Pizza',
    'New York Pizza',
    'Sicilian Pizza',
  ]);
});

// let's test that each item in the pizza data has the correct properties
for (let i = 0; i < pizzas.length; i += 1) {
  it(`pizza[${i}] should have properties (id, name, image, desc, price)`, () => {
    expect(pizzas[i]).toHaveProperty('id');
    expect(pizzas[i]).toHaveProperty('name');
    expect(pizzas[i]).toHaveProperty('image');
    expect(pizzas[i]).toHaveProperty('desc');
    expect(pizzas[i]).toHaveProperty('price');
  });
}

// default jest mock function
test('mock implementation of a basic function', () => {
  const mock = jest.fn(() => 'I am a mock function');

  expect(mock('Calling my mock function!')).toBe('I am a mock function');
  expect(mock).toHaveBeenCalledWith('Calling my mock function!');
});

// let's mock the return value and test calls
test('mock return value of a function one time', () => {
  const mock = jest.fn();

  // we can chain these!
  mock.mockReturnValueOnce('Hello').mockReturnValueOnce('there!');

  mock(); // first call 'Hello'
  mock(); // second call 'there!'

  expect(mock).toHaveBeenCalledTimes(2); // we know it's been called two times

  mock('Hello', 'there', 'Steve'); // call it with 3 different arguments
  expect(mock).toHaveBeenCalledWith('Hello', 'there', 'Steve');

  mock('Steve'); // called with 1 argument
  expect(mock).toHaveBeenLastCalledWith('Steve');
});

// let's mock the return value
// difference between mockReturnValue & mockImplementation
test('mock implementation of a function', () => {
  const mock = jest.fn().mockImplementation(() => 'United Kingdom');
  expect(mock('Location')).toBe('United Kingdom');
  expect(mock).toHaveBeenCalledWith('Location');
});

// spying on a single function of an imported module, we can spy on its usage
// by default the original function gets called, we can change this
test('spying using original implementation', () => {
  const pizza = {
    name: n => `Pizza name: ${n}`,
  };
  const spy = jest.spyOn(pizza, 'name');
  expect(pizza.name('Cheese')).toBe('Pizza name: Cheese');
  expect(spy).toHaveBeenCalledWith('Cheese');
});

// we can mock the implementation of a function from a module
test('spying using mockImplementation', () => {
  const pizza = {
    name: n => `Pizza name: ${n}`,
  };
  const spy = jest.spyOn(pizza, 'name');
  spy.mockImplementation(n => `Crazy pizza!`);

  expect(pizza.name('Cheese')).toBe('Crazy pizza!');
  spy.mockRestore(); // back to original implementation
  expect(pizza.name('Cheese')).toBe('Pizza name: Cheese');
});

// let's test pizza return output
test('pizza returns new york pizza last', () => {
  const pizza1 = pizzas[0];
  const pizza2 = pizzas[1];
  const pizza3 = pizzas[2];
  const pizza = jest.fn(currentPizza => currentPizza.name);

  pizza(pizza1); // chicago pizza
  pizza(pizza2); // neapolitan pizza
  pizza(pizza3); // new york pizza

  expect(pizza).toHaveLastReturnedWith('New York Pizza');
});

// let's match some data against our object
test('pizza data has new york pizza and matches as an object', () => {
  const newYorkPizza = {
    id: 3,
    name: 'New York Pizza',
    image: '/images/ny-pizza.jpg',
    desc:
      'New York-style pizza has slices that are large and wide with a thin crust that is foldable yet crispy. It is traditionally topped with tomato sauce and mozzarella cheese.',
    price: 8,
  };
  expect(pizzas[2]).toMatchObject(newYorkPizza);
});

// async example, always return a promise (can switch out resolves with reject)
test('expect a promise to resolve', async () => {
  const user = {
    getFullName: jest.fn(() => Promise.resolve('Karl Hadwen')),
  };
  await expect(user.getFullName('Karl Hadwen')).resolves.toBe('Karl Hadwen');
});

test('expect a promise to reject', async () => {
  const user = {
    getFullName: jest.fn(() =>
      Promise.reject(new Error('Something went wrong'))
    ),
  };
  await expect(user.getFullName('Karl Hadwen')).rejects.toThrow(
    'Something went wrong'
  );
});
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].