Giter Site home page Giter Site logo

jest-mock-examples's Introduction

Jest Mock Examples

Examples using Jest mock features.

Examples

See examples directory.

  • cookie.test.ts
    • jest.spyOn
    • mockFn.mockImplementation
  • navigator/
    • jest.spyOn
    • mockFn.mockReturnValue
    • document.documentElement.innerHTML
  • now.test.ts
    • jest.spyOn
  • module/
    • jest.mock
    • jest.doMock
  • lodash/
    • jest.mock
    • jest.unmock
    • __mocks__/lodash
  • module-registry/
    • jest.resetModules
    • jest.isolateModules

Other Resources

// https://github.com/facebook/jest/tree/master/examples/manual-mocks/__tests__/partial_mock.js

// Copyright 2004-present Facebook. All Rights Reserved.

/**
 * This file illustrates how to do a partial mock where a subset
 * of a module's exports have been mocked and the rest
 * keep their actual implementation.
 */
import defaultExport, {apple, strawberry} from '../fruit';
jest.mock('../fruit', () => {
  const originalModule = jest.requireActual('../fruit');
  const mockedModule = jest.createMockFromModule('../fruit');

  //Mock the default export and named export 'apple'.
  return {
    ...mockedModule, // <= My note: Is `__esModule: true,` enough instead of `...mockedModule`?
    ...originalModule,
    apple: 'mocked apple',
    default: jest.fn(() => 'mocked fruit'),
  };
});

it('does a partial mock', () => {
  const defaultExportResult = defaultExport();
  expect(defaultExportResult).toBe('mocked fruit');
  expect(defaultExport).toHaveBeenCalled();

  expect(apple).toBe('mocked apple');
  expect(strawberry()).toBe('strawberry');
});

Getting Started

npm ci
npm t

How to start testing on your application

This section describes how to create this repository's environment as an example. Jest, TypeScript, ESLint and Prettier are used.

Dependencies

# Install
npm i -D jest ts-jest typescript prettier eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-config-prettier eslint-plugin-prettier

Config files

tsconfig.json

# Create tsconfig.json
node_modules/.bin/tsc --init

jest.config.js

https://github.com/kulshekhar/ts-jest

module.exports = {
  preset: 'ts-jest',
};

.eslintrc

{
  "root": true,
  "env": {
    "browser": true,
    "node": true
  },
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:prettier/recommended",
    "prettier/@typescript-eslint"
  ],
  "rules": {}
}

.prettierrc

{
  "singleQuote": true
}

.vscode/settings.json (Optional)

{
  "editor.formatOnSave": false,
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  }
}

jest-mock-examples's People

Contributors

ryotah avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.