All Projects → userlike → joke

userlike / joke

Licence: MIT license
Typesafe mock utility with minimal boilerplate for jest

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to joke

DataStore
Visual develop tool of creating mocked Json
Stars: ✭ 30 (+87.5%)
Mutual labels:  mock
electron-admin-antd-vue
Electron Vue3.x Ant Design Admin template
Stars: ✭ 21 (+31.25%)
Mutual labels:  mock
htest
htest is a http-test package
Stars: ✭ 24 (+50%)
Mutual labels:  mock
mocat
🐈 Mocat is a mocking toolbar that allows you to interactively develop and test network requests.
Stars: ✭ 27 (+68.75%)
Mutual labels:  mock
moq.ts
Moq for Typescript
Stars: ✭ 107 (+568.75%)
Mutual labels:  mock
entity-framework-mock
Easy Mock wrapper for mocking EF6 DbContext and DbSet using Moq or NSubstitute
Stars: ✭ 45 (+181.25%)
Mutual labels:  mock
web3-mock
🤡 JavaScript library to mock web3 responses either by emulating web3 wallets or web3 RPC requests.
Stars: ✭ 54 (+237.5%)
Mutual labels:  mock
shai
数据模拟生成库
Stars: ✭ 55 (+243.75%)
Mutual labels:  mock
dextool
Suite of C/C++ tooling built on LLVM/Clang
Stars: ✭ 81 (+406.25%)
Mutual labels:  mock
rocket-pipes
Powerful pipes for TypeScript, that chain Promise and ADT for you 🚌 -> ⛰️ -> 🚠 -> 🏂 -> 🚀
Stars: ✭ 18 (+12.5%)
Mutual labels:  mock
instant-mock
Quick and Easy web API mock server.
Stars: ✭ 27 (+68.75%)
Mutual labels:  mock
dexopener
An Android library that provides the ability to mock your final classes on Android devices.
Stars: ✭ 112 (+600%)
Mutual labels:  mock
chip
📦 🐳 🚀 - Smart "dummy" mock for cloud native tests
Stars: ✭ 19 (+18.75%)
Mutual labels:  mock
mockingbird
🐦 Decorator Powered TypeScript Library for Creating Mocks
Stars: ✭ 70 (+337.5%)
Mutual labels:  mock
interface-forge
Graceful mock-data and fixtures generation using TypeScript
Stars: ✭ 58 (+262.5%)
Mutual labels:  mock
ruby-dns-mock
DNS mock server written on 💎 Ruby. Mimic any DNS records for your test environment with fake DNS server.
Stars: ✭ 50 (+212.5%)
Mutual labels:  mock
admin-base-tmpl
⚡️基于vite2构建的vue2+typescript+elementUI 的后台基础套件,预览地址
Stars: ✭ 52 (+225%)
Mutual labels:  mock
Paw-FakerDynamicValue
A dynamic value extension for Paw using Faker to generate data
Stars: ✭ 16 (+0%)
Mutual labels:  mock
Mokku
Mock API calls seamlessly
Stars: ✭ 109 (+581.25%)
Mutual labels:  mock
axios-mock-server
RESTful mock server using axios.
Stars: ✭ 33 (+106.25%)
Mutual labels:  mock

🍭 joke

joke is a typesafe, boilerplate free version of jest.mock.

Advantages

  • Less boilerplate than jest.mock.
  • Type-safe imports. No more type-casting.
  • TS/JS Language Server recognizes them when moving files around.
  • Supports partial mocking (mockSome).

Usage

Install

npm install --saveDev @userlike/joke @userlike/babel-plugin-joke
yarn add -D @userlike/joke @userlike/babel-plugin-joke

Babel config

Add @userlike/babel-plugin-joke to your babel plugins.

If you use ts-jest, please additionally refer to the "Usage with ts-jest" section below.

And use

import { mock } from "@userlike/joke";

const { fetchUser } = mock(import("./service"));

fetchUser.mockReturnValue(Promise.resolve({ id: 1, name: "Jane Doe" }));

Full mocking

Auto-mock the whole module.

import { mock } from "@userlike/joke";

const { fetchUser } = mock(import("./service"));

fetchUser.mockReturnValue(Promise.resolve({ id: 1, name: "Jane Doe" }));

Full mocking with partial implementation

Use the second argument of mock to provide a partial implementation. Behind the scenes, it extends auto-mocked module with the given implementation using Object.assign.

import { mock } from "@userlike/joke";

const { fetchUser } = mock(import("./service"), () => ({
  fetchUser: () => Promise.resolve({ id: 1, name: "Jane Doe" })
}));

Partial mocking

When you need to mock a module partially and want to keep the rest of the module unmocked, you can use mockSome. Behind the scenes, it uses jest.requireActual by extending its actual implementation with the given implementation using Object.assign.

import { mockSome } from "@userlike/joke";
import { renderUser } from "./my-component";

// fetchUser is mocked, getUserId is the real implementation
const { fetchUser, getUserId } = mockSome(import("./service"), () => ({
  fetchUser: jest.fn()
}));

test(async () => {
  const user = { id: 1, name: "Jane Doe" };
  fetchUser.mockReturnValue(Promise.resolve(user));

  await renderUser();

  expect(document.getElementById("#user-id").innerText).toBe(getUserId(user));
});

Full replacement

When you want to skip auto-mocking, you can use mockAll. It's equivalent to jest.mock(module, moduleFactory).

import { mockAll } from "@userlike/joke";
import { renderUser } from "./my-component";

const { fetchUser } = mockAll(import("./service"), () => ({
  fetchUser: jest.fn()
}));

test(async () => {
  const user = { id: 1, name: "Jane Doe" };
  fetchUser.mockReturnValue(Promise.resolve(user));

  await renderUser();

  expect(document.getElementById("#user-id").innerText).toBe(getUserId(user));
});

Usage with ts-jest

If you use ts-jest instead of Babel, you need to additionally ensure each of the following:

  • That Babel preprocessing is enabled in your ts-jest configuration section.
  • That Babel is configured to use joke as a plugin.
  • That the module key of tsconfig.json's compilerOptions is set to at least es2020, or esnext to support dynamic imports. You may also need to set moduleResolution to node for the general import syntax to work properly.
  • That the code is transpiled down to the JS syntax jest understands (you may use @babel/preset-env for that purpose).

Note: if you don't want to modify your main tsconfig.json file, you can introduce a separate configuration named e.g. tsconfig.tests.json.

Example Typescript configuration for tests:

{
  "extends": "tsconfig.json",
  "compilerOptions": {
    "module": "ES2020",
    "moduleResolution": "node"
  }
}

To enable Babel preprocessing in ts-jest, as well as to configure the tsconfig file you want use for tests, add or update the globals section in your jest config.

Example with separate Babel and Typescript configuration files:

"globals": {
  'ts-jest': {
    "babelConfig": "true",
    "tsConfig": "tsconfig.test.json"
  }
}

Example with inline Typescript and Babel configuration:

"globals": {
  'ts-jest': {
    "babelConfig": {
      "plugins": ["@userlike/babel-plugin-joke"],
      "presets": [
        [
          "@babel/preset-env",
          {
            "targets": {
              "node": 'current'
            }
          }
        ]
      ]
    },
    "tsConfig": {
      "module": "es2020",
      "moduleResolution": "node",
    }
  }
}

For details, see ts-jest configuration docs.

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].