Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Mar 20, 2024
1 parent d613136 commit f6f133e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
20 changes: 19 additions & 1 deletion src/__tests__/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* currently not supported in the browser lib).
*/

import posthog from '../loader-module'
import posthog, { PostHog } from '../loader-module'
import sinon from 'sinon'
import { window } from '../utils/globals'

Expand Down Expand Up @@ -48,4 +48,22 @@ describe(`Module-based loader in Node env`, () => {
it(`supports capture()`, () => {
expect(() => posthog.capture(`Pat`)).not.toThrow()
})

it(`always returns posthog from init`, () => {
console.error = jest.fn()
console.warn = jest.fn()
expect(posthog.init(`my-test`, undefined, 'sdk-1')).toBeInstanceOf(PostHog)
expect(posthog.init(``, undefined, 'sdk-2')).toBeInstanceOf(PostHog)
expect(console.error).toHaveBeenCalledTimes(1)
expect(console.error).toHaveBeenCalledWith(
'[PostHog.js]',
'PostHog was initialized without a token. This likely indicates a misconfiguration. Please check the first argument passed to posthog.init()'
)
// Already loaded
expect(posthog.init(`my-test`, undefined, 'sdk-1')).toBeInstanceOf(PostHog)
expect(console.warn).toHaveBeenCalledWith(
'[PostHog.js]',
'You have already initialized PostHog! Re-initializing is a no-op'
)
})
})
4 changes: 2 additions & 2 deletions src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export class PostHog {
*/
init(token: string, config?: Partial<PostHogConfig>, name?: string): PostHog | void {
if (!name || name === PRIMARY_INSTANCE_NAME) {
// This means we are initialising the primary instance (i.e. this)
// This means we are initializing the primary instance (i.e. this)
return this._init(token, config, name)
} else {
const namedPosthog = instances[name] ?? new PostHog()
Expand Down Expand Up @@ -308,7 +308,7 @@ export class PostHog {
}

if (this.__loaded) {
logger.warn('You have already initialized PostHog! Re-initialising is a no-op')
logger.warn('You have already initialized PostHog! Re-initializing is a no-op')
return this
}

Expand Down

0 comments on commit f6f133e

Please sign in to comment.