All Projects → h3poteto → electron-mock-ipc

h3poteto / electron-mock-ipc

Licence: MIT license
Mock Electron's ipcMain, and ipcRenderer

Programming Languages

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

Projects that are alternatives of or similar to electron-mock-ipc

racket-mock
Mocking library for Racket
Stars: ✭ 22 (+4.76%)
Mutual labels:  mock
network-monitor-ios
Network Monitor SDK for iOS
Stars: ✭ 70 (+233.33%)
Mutual labels:  mock
vue-admin-work
🎉🎉🚀🚀🚀🚀vue-admin-work是一个中后台系统管理方案。使用 vue2.x 及周边全家桶工具开发而来。支持多种功能,不同角色权限🚀🚀🚀🎉🎉
Stars: ✭ 74 (+252.38%)
Mutual labels:  mock
MockAlamofire
A simple example showing how to override the URLProtocol to return mock data on Alamofire responses. Helpful if you are looking for a simple way to mock an Alamofire response, with out any additional dependencies.
Stars: ✭ 22 (+4.76%)
Mutual labels:  mock
spydriver
🕵️ Lightweight utility to intercept WebDriver and WebElement method calls.
Stars: ✭ 24 (+14.29%)
Mutual labels:  mock
h-blog
vue+elementUI模仿我的博客,简单的写的几个练习页面
Stars: ✭ 14 (-33.33%)
Mutual labels:  mock
mock-data
Mock data in PostgreSQL/Greenplum databases
Stars: ✭ 115 (+447.62%)
Mutual labels:  mock
gadgeto
Gadgeto! is a collection of tools that aim to facilitate the development of REST APIs in Go.
Stars: ✭ 38 (+80.95%)
Mutual labels:  mock
IOS-CoreBluetooth-Mock
Mocking library for CoreBluetooth framework.
Stars: ✭ 142 (+576.19%)
Mutual labels:  mock
nei-toolkit
NEI 接口文档管理平台配套自动化工具
Stars: ✭ 814 (+3776.19%)
Mutual labels:  mock
jest-ts-auto-mock
Jest test utility with automatic mock creation for interfaces and classes
Stars: ✭ 150 (+614.29%)
Mutual labels:  mock
kb-proxy
kb-proxy 是一个可本地部署的、提供代理功能、接口测试管理、支持在线Mock、Host环境管理的在线工具平台。
Stars: ✭ 52 (+147.62%)
Mutual labels:  mock
sinon-mongoose
Extend Sinon stubs for Mongoose methods to test chained methods easily
Stars: ✭ 87 (+314.29%)
Mutual labels:  mock
extractors
Data extractors for import demo-date to the api
Stars: ✭ 44 (+109.52%)
Mutual labels:  mock
vue-admin-better
🚀🚀🚀vue admin,vue3 admin,vue3.0 admin,vue后台管理,vue-admin,vue3.0-admin,admin,vue-admin,vue-element-admin,ant-design,vue-admin-beautiful-pro,vab admin pro,vab admin plus,vue admin plus,vue admin pro
Stars: ✭ 12,962 (+61623.81%)
Mutual labels:  mock
chrome-extension-mocker
The most convenient tool to mock requests for axios, with built-in Chrome extension support.
Stars: ✭ 37 (+76.19%)
Mutual labels:  mock
kmpapp
👨‍💻 Kotlin Mobile Multiplatform App (Android & iOS). One Code To Rule Them All. MVVM, DI (Kodein), coroutines, livedata, ktor, serialization, mockk, detekt, ktlint, jacoco
Stars: ✭ 34 (+61.9%)
Mutual labels:  mock
laika
Log, test, intercept and modify Apollo Client's operations
Stars: ✭ 99 (+371.43%)
Mutual labels:  mock
FireMock
Mock and stub HTTP requests. Test your apps with fake data and files responses.
Stars: ✭ 25 (+19.05%)
Mutual labels:  mock
electron-admin-element-vue
Electron Vue3.x Element-UI Admin
Stars: ✭ 37 (+76.19%)
Mutual labels:  mock

ElectronMockIPC

Test npm GitHub release npm NPM

This is a mock library for ipcMain and ipcRenderer in Electron. They communicate with each other, so you can mock ipc methods in your tests without changing your production code.

Install

$ npm install --save-dev electron-mock-ipc

or

$ yarn add --dev electron-mock-ipc

Usage

This library can use in jest, and mocha.

At first, please create a file to mock:

import createIPCMock from 'electron-mock-ipc'

const mocked = createIPCMock()
const ipcMain = mocked.ipcMain
const ipcRenderer = mocked.ipcRenderer
export { ipcMain, ipcRenderer }

and save it as spec/mock/electron-mock.ts.

Jest

In Jest, please replace electron object using moduleNameMapper. Please override it in package.json.

  "jest": {
    "moduleNameMapper": {
      "^electron$": "<rootDir>/spec/mock/electron-mock.ts"
    }
  }

After that, all ipc objects are mocked, so you can write tests as below.

import { IpcMainEvent } from 'electron'
import { ipcMain } from '~/spec/mock/electron-mock'
import { targetMethod } from '~/src/target'

describe('your test', () => {
  it('should be received', async () => {
    ipcMain.once('test-event', (event: IpcMainEvent, obj: string) => {
      event.sender.send('response-test-event', 'response' + obj)
    })
    const res = await targetMethod()
    expect(res).toEqual('responsehoge')
  })
})

Mocha

In Mocha, you can not inject a mock object easily. So, please inject the ipcRenderer object in preload.js, and use preload.js to load electron.

import { ipcRenderer } from 'electron'
import { ipcRenderer as mock } from '~/spec/mock/electron-mock'

if (process.env.NODE_ENV === 'test') {
  global.ipcRenderer = mock
} else {
  global.ipcRenderer = ipcRenderer
}

preload.js is used to disable nodeIntegration, please refer here.

And write test.

import { IpcMainEvent } from 'electron'
import { targetMethod } from '~/src/target'
import { ipcMain } from '~/spec/mock/electron'
import { describe, it } from 'mocha'
import { expect } from 'chai'

describe('your test', () => {
  it('should be received', async () => {
    ipcMain.once('test-event', (event: IpcMainEvent, obj: string) => {
      event.sender.send('response-test-event', 'response' + obj)
    })
    const res = await targetMethod()
    expect(res).to.equal('responsehoge')
  })
})

Example

I prepared a test example, please refer here.

License

The software is available as open source under the terms of the MIT License.

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