|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { afterEach, beforeEach, describe, it } from 'mocha'; |
| 4 | + |
| 5 | +import { checkForMultiplePackageInstances } from '../checkForMultiplePackageInstances.js'; |
| 6 | + |
| 7 | +describe('check for different library versions', () => { |
| 8 | + class OtherPackageClass {} |
| 9 | + const graphqlPackageInstanceCheckSymbol = Symbol.for( |
| 10 | + 'graphql-js:check-multiple-package-instances', |
| 11 | + ); |
| 12 | + const globalObject = globalThis as { |
| 13 | + [graphqlPackageInstanceCheckSymbol]?: unknown; |
| 14 | + }; |
| 15 | + const orig = globalObject[graphqlPackageInstanceCheckSymbol]; |
| 16 | + const origError = console.error; |
| 17 | + let errors: Array<unknown> = []; |
| 18 | + beforeEach(() => { |
| 19 | + errors = []; |
| 20 | + console.error = (...args) => { |
| 21 | + errors = args; |
| 22 | + }; |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + globalObject[graphqlPackageInstanceCheckSymbol] = orig; |
| 27 | + console.error = origError; |
| 28 | + }); |
| 29 | + |
| 30 | + it('does not log an error under normal circumstances', () => { |
| 31 | + checkForMultiplePackageInstances(); |
| 32 | + expect(errors).to.deep.equal([]); |
| 33 | + checkForMultiplePackageInstances(); |
| 34 | + expect(errors).to.deep.equal([]); |
| 35 | + checkForMultiplePackageInstances(); |
| 36 | + expect(errors).to.deep.equal([]); |
| 37 | + }); |
| 38 | + |
| 39 | + it('logs an error if another package has been loaded first', () => { |
| 40 | + // simulate other version of this lib to have been loaded before this version |
| 41 | + globalObject[graphqlPackageInstanceCheckSymbol] = new OtherPackageClass(); |
| 42 | + |
| 43 | + checkForMultiplePackageInstances(); |
| 44 | + |
| 45 | + expect(errors[0]).to.match( |
| 46 | + /Multiple colliding versions of the `graphql` package detected\./m, |
| 47 | + ); |
| 48 | + }); |
| 49 | +}); |
0 commit comments