assertSingleton warns, not throws (#455)

This commit is contained in:
Jaco Greeff
2019-08-09 10:13:43 +02:00
committed by GitHub
parent 16340c59d8
commit 09e0b80dc6
5 changed files with 37 additions and 28 deletions
@@ -7,15 +7,19 @@ import { assertSingletonPackage } from '.';
describe('assertSingletonPackage', (): void => {
const NAME = 'assertSingletonPackage';
it('should not throw the first time', (): void => {
expect(
(): void => assertSingletonPackage(NAME)
).not.toThrow();
it('should not log the first time', (): void => {
const spy = jest.spyOn(console, 'warn');
assertSingletonPackage(NAME);
expect(spy).not.toHaveBeenCalled();
spy.mockRestore();
});
it('should throw the second time', (): void => {
expect(
(): void => assertSingletonPackage(NAME)
).toThrow(/Multiple versions of assertSingletonPackage detected/);
it('should log the second time', (): void => {
const spy = jest.spyOn(console, 'warn');
assertSingletonPackage(NAME);
expect(spy).toHaveBeenCalledWith('Multiple versions of assertSingletonPackage detected, ensure that there is only one version in your dependency tree');
spy.mockRestore();
});
});
+3 -2
View File
@@ -3,7 +3,6 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import isUndefined from './is/undefined';
import assert from './assert';
interface PjsChecks {
__polkadotjs: Record<string, boolean>;
@@ -25,7 +24,9 @@ export default function assertSingletonPackage (name: string): void {
_global.__polkadotjs = {};
}
assert(isUndefined(_global.__polkadotjs[name]), `Multiple versions of ${name} detected, ensure that there is only version one in your dependency tree`);
if (!isUndefined(_global.__polkadotjs[name])) {
console.warn(`Multiple versions of ${name} detected, ensure that there is only one version in your dependency tree`);
}
_global.__polkadotjs[name] = true;
}