Gathering detailed insights and metrics for @harelpls/pusher-js-mock
Gathering detailed insights and metrics for @harelpls/pusher-js-mock
Gathering detailed insights and metrics for @harelpls/pusher-js-mock
Gathering detailed insights and metrics for @harelpls/pusher-js-mock
npm install @harelpls/pusher-js-mock
Typescript
Module System
Min. Node Version
Fix presence channel members property
Updated on May 13, 2025
Return mock channel instance from a few methods
Updated on Nov 06, 2021
Add allChannels method
Updated on Oct 27, 2021
Fix a bug with trigger function being echoed to the originator
Updated on Aug 06, 2021
▶️ Add support for channel.trigger
Updated on Apr 25, 2021
Add a method to unbind all callbacks
Updated on Feb 22, 2021
TypeScript (98.88%)
JavaScript (1.12%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
32 Stars
266 Commits
8 Forks
2 Watchers
4 Branches
9 Contributors
Updated on May 23, 2025
Latest Version
0.2.2
Package Id
@harelpls/pusher-js-mock@0.2.2
Unpacked Size
44.91 kB
Size
10.78 kB
File Count
25
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Mock Pusher.js in your JavaScript tests with ease
Using yarn:
yarn add --dev pusher-js-mock
Or using npm:
npm install -D pusher-js-mock
For more detailed examples, check out examples
directory
inside the project!
Also, you can check out the Docs for even more information.
If you need to mock a Pusher object in your tests that can subscribe to channel, it's best to use PusherMock.
1import { PusherMock } from "pusher-js-mock"; 2 3// initializing PusherMock 4const pusher = new PusherMock(); 5 6// subscribing to a Pusher channel 7const channel = pusher.subscribe("my-channel"); 8 9// emitting an event 10channel.emit("event-name");
If you're using Pusher in your code in this or similar manner:
1import Pusher from "pusher-js";
You will need to mock Pusher in a specific way.
I suggest you use Jest to test your code. To do this in Jest, you'll need something like this:
1jest.mock("pusher-js", () => { 2 const Pusher = require("pusher-js-mock").PusherMock; 3 return Pusher; 4});
If you have tips on how to mock this using other testing frameworks, please submit an issue or a pull request.
This shows how to stub a pusher if you're attaching it to window object in your
project. If you're attaching a PusherFactory to a window
object like this in
your code:
1window.PusherFactory = { 2 pusherClient: function(pusherKey) { 3 return new Pusher(pusherKey); 4 }, 5};
It's best for you to use PusherFactoryMock.
1import { PusherFactoryMock } from "pusher-js-mock"; 2 3// initialize instance of PusherFactoryMock 4const pusherFactoryMock = new PusherFactoryMock(); 5// replace it with the object that is attached to a window 6window.PusherFactory = pusherFactoryMock; 7 8// get the Pusher client reference 9pusher = pusherFactoryMock.pusherClient();
This way you'll just replace your PusherFactory with PusherFactoryMock.
This package also supports using presence channels for multiple clients. The mock will automatically detect when presence-
is in the channel name and return a presence channel with channel.members
filled out as expected. You can pass in IDs and info via a custom authorizer, just as you would with the real package.
Unfortunately due to the nature of async testing in jest, there are a few rules:
{id, info}
where the auth key would normally go in the callback, i.e.1authorize: (socketId, callback) => callback(false, { id, info });
process.nextTick
to allow the promise to resolve and set the ID & info.1// given this Pusher config with async auth 2{ 3 authorizer: () => ({ 4 authorize: async (socketId, callback) => 5 Promise.resolve().then(() => callback(false, { id, info })) 6 }) 7} 8 9// do this in your tests 10const channel = client.subscribe("presence-channel") 11process.nextTick(() => { 12 expect(channel.members.myID).toBe("my-id") 13}) 14...
await new Promise(setImmediate)
above your assertions to flush internal promises and apply your id & info to the client:1// given this Pusher config with sync auth 2{ 3 authorizer: () => ({ 4 authorize: (socketId, callback) => callback(false, { id, info }), 5 }); 6} 7 8// do this in your tests 9const channel = client.subscribe("presence-channel"); 10await new Promise(setImmediate); 11expect(channel.members.myID).toBe("my-id");
If you're using React, you'll have to wrap it further in act:
1await act(async () => await new Promise(setImmediate));
Here's an example:
1// create-client.js 2import Pusher from "pusher-js"; 3import { getAuthSomehow } from "./getAuthSomehow"; 4 5export const createClient = ({ id, info }) => 6 new Pusher("APP_KEY", { 7 cluster: "APP_CLUSTER", 8 // see https://github.com/pusher/pusher-js#authorizer-function 9 authorizer: ({ name }) => ({ 10 authorize: async (socketId, callback) => { 11 const auth = await getAuthSomehow(id, info); 12 callback(false, auth); 13 }, 14 }), 15 }); 16 17export default createClient;
1// create-client.spec.js 2import createClient from "../create-client"; 3 4// mock the authorize function and pusher 5jest.mock("pusher-js", () => require("pusher-js-mock")); 6jest.mock("../getAuthSomehow", () => ({ 7 // async auth result resolves to { id, info } object, which gets set in the client 8 getAuthSomehow: (id, info) => Promise.resolve({ id, info }), 9})); 10 11it("should create a presence channel", async () => { 12 // arrange: create pusher client 13 const pusher = createClient({ id: "my-id", info: { role: "moderator" } }); 14 15 // act: required to ensure pusher events are called, i.e. pusher:member_added 16 const presenceChannel = await pusher.subscribe("presence-channel"); 17 18 // process.nextTick wraps our assertions to ensure the promise has resolved. 19 process.nextTick(() => { 20 // assert: presenceChannel has the properties we expect it to. 21 expect(presenceChannel.members.myID).toBe("my-id"); 22 expect(presenceChannel.members.me).toEqual({ 23 id: "my-id", 24 info: { role: "moderator" }, 25 }); 26 expect(presenceChannel.members.members).toEqual({ 27 "my-id": { role: "moderator" }, 28 }); 29 }); 30});
Check out a code example of using presence channels
Photo by Octavian Rosca on Unsplash
No vulnerabilities found.
Reason
25 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
security policy file detected
Details
Reason
license file detected
Details
Reason
branch protection is not maximal on development and all release branches
Details
Reason
Found 1/8 approved changesets -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
22 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More