11import { ObjectMap , ServiceInstance } from "../service" ;
2- import type { NodeCG , ReplicantOptions , Replicant , Logger } from "nodecg-types /types/server " ;
2+ import NodeCG from "@ nodecg/types" ;
33import { EventEmitter } from "events" ;
44
55// The mock-nodecg package has a few problems like no typings and some unimplemented functions that are a dealbreaker for us.
@@ -8,7 +8,7 @@ import { EventEmitter } from "events";
88// But for now we use these mocks that we can easily change if we need something
99// that mock-nodecg hasn't implemented yet.
1010
11- export class MockNodeCG implements NodeCG {
11+ export class MockNodeCG {
1212 constructor (
1313 public bundleName : string = "nodecg-io-core" ,
1414 public bundleConfig = { } ,
@@ -31,7 +31,9 @@ export class MockNodeCG implements NodeCG {
3131 logging : { } ,
3232 sentry : { } ,
3333 login : { } ,
34- } ;
34+ exitOnUncaught : false ,
35+ bundles : { } ,
36+ } as unknown as NodeCG . Config ;
3537
3638 sendMessage = jest . fn ( ) ;
3739 sendMessageToBundle = jest . fn ( ) ;
@@ -41,8 +43,12 @@ export class MockNodeCG implements NodeCG {
4143 // We don't care about the type that all replicants have. The user has to ensure that the type they provide
4244 // to the replicant method matches the actual content of the replicant.
4345 // eslint-disable-next-line @typescript-eslint/no-explicit-any
44- private declaredReplicants : ObjectMap < ObjectMap < Replicant < any > > > = { } ;
45- Replicant < V > ( name : string , namespaceOrOpts ?: string | ReplicantOptions < V > , o ?: ReplicantOptions < V > ) : Replicant < V > {
46+ private declaredReplicants : ObjectMap < ObjectMap < MockedReplicant < any > > > = { } ;
47+ Replicant < V > (
48+ name : string ,
49+ namespaceOrOpts ?: string | NodeCG . Replicant . OptionsWithDefault < V > ,
50+ o ?: NodeCG . Replicant . OptionsWithDefault < V > ,
51+ ) : MockedReplicant < V > {
4652 const namespace = typeof namespaceOrOpts === "string" ? namespaceOrOpts : this . bundleName ;
4753
4854 // Check if this replicant was already declared once and return it if found
@@ -54,7 +60,7 @@ export class MockNodeCG implements NodeCG {
5460 // This replicant was not already declared and needs to be created.
5561
5662 const opts = typeof namespaceOrOpts === "object" ? namespaceOrOpts : o ;
57- const newReplicant = new MockReplicant ( this . log , name , namespace , opts ?? { } ) ;
63+ const newReplicant = new MockReplicant ( this . log , name , namespace , opts ?? { } ) as unknown as MockedReplicant < V > ;
5864
5965 const namespaceObj = this . declaredReplicants [ namespace ] ;
6066 if ( namespaceObj === undefined ) {
@@ -68,7 +74,7 @@ export class MockNodeCG implements NodeCG {
6874
6975 readReplicant < V > ( name : string ) : V ;
7076 readReplicant < V > ( name : string , namespace : string ) : V ;
71- readReplicant < V > ( name : string , namespace ?: string ) : V {
77+ readReplicant < V > ( name : string , namespace ?: string ) : V | undefined {
7278 return this . Replicant < V > ( name , namespace ) . value ;
7379 }
7480
@@ -82,24 +88,26 @@ export class MockNodeCG implements NodeCG {
8288}
8389
8490class MockLogger {
91+ name : "logger" ;
8592 trace = jest . fn ( ) ;
8693 debug = jest . fn ( ) ;
8794 info = jest . fn ( ) ;
8895 warn = jest . fn ( ) ;
8996 error = jest . fn ( ) ;
9097 replicants = jest . fn ( ) ;
91- static globalReconfigure = jest . fn ( ) ;
9298}
9399
94- class MockReplicant < V > extends EventEmitter implements Replicant < V > {
95- private _value : V | undefined = this . opts . defaultValue ;
100+ type MockedReplicant < V > = MockReplicant < V > & NodeCG . ServerReplicant < V > ;
101+
102+ class MockReplicant < V > extends EventEmitter {
103+ _value : V | undefined = this . opts . defaultValue ;
96104 revision = 0 ;
97105
98106 constructor (
99- public readonly log : Logger ,
107+ public readonly log : NodeCG . Logger ,
100108 public readonly name : string ,
101109 public readonly namespace : string ,
102- public readonly opts : ReplicantOptions < V > ,
110+ public readonly opts : Partial < NodeCG . Replicant . OptionsWithDefault < V > > ,
103111 ) {
104112 super ( ) ;
105113 }
@@ -124,6 +132,10 @@ class MockReplicant<V> extends EventEmitter implements Replicant<V> {
124132 }
125133}
126134
135+ export function mockNodeCG ( ) : MockNodeCG & NodeCG . ServerAPI {
136+ return new MockNodeCG ( ) as unknown as MockNodeCG & NodeCG . ServerAPI ;
137+ }
138+
127139// Test objects
128140
129141// These variables all contain a string of their name and are mainly
0 commit comments