From d4cab4db4d17afb98d84732a8ac5e03f9dc4e32d Mon Sep 17 00:00:00 2001 From: Cacie Prins Date: Fri, 22 Mar 2024 10:49:28 -0400 Subject: [PATCH] fix putProtocolArtifact specs when run as a part of the unit test suite --- packages/server/test/mockery_helper.ts | 22 ++++++++++++++++++ packages/server/test/spec_helper.js | 23 ++++--------------- .../cloud/api/putProtocolArtifact_spec.ts | 14 +++++++++++ 3 files changed, 40 insertions(+), 19 deletions(-) create mode 100644 packages/server/test/mockery_helper.ts diff --git a/packages/server/test/mockery_helper.ts b/packages/server/test/mockery_helper.ts new file mode 100644 index 00000000000..e54e5934548 --- /dev/null +++ b/packages/server/test/mockery_helper.ts @@ -0,0 +1,22 @@ +import mockery from 'mockery' +import path from 'path' + +export const mockElectron = (mockeryInstance: typeof mockery) => { + // stub out the entire electron object for our stub + // we must use an absolute path here because of the way mockery internally loads this + // module - meaning the first time electron is required it'll use this path string + // so because its required from a separate module we must use an absolute reference to it + mockeryInstance.registerSubstitute( + 'electron', + path.join(__dirname, './support/helpers/electron_stub'), + ) + + // stub out electron's original-fs module which is available when running in electron + mockeryInstance.registerMock('original-fs', {}) +} + +export const enable = (mockeryInstance: typeof mockery) => { + mockeryInstance.enable({ + warnOnUnregistered: false, + }) +} diff --git a/packages/server/test/spec_helper.js b/packages/server/test/spec_helper.js index 2a2afc6dbf4..1b45b4f45a5 100644 --- a/packages/server/test/spec_helper.js +++ b/packages/server/test/spec_helper.js @@ -1,6 +1,8 @@ /* eslint-disable no-console */ require('../lib/environment') +const { enable, mockElectron } = require('./mockery_helper') + const chai = require('chai') chai.use(require('chai-subset')) @@ -14,7 +16,6 @@ global.proxyquire = require('proxyquire') global.sinon = require('sinon') const _ = require('lodash') const Promise = require('bluebird') -const path = require('path') const cache = require('../lib/cache') require('chai') @@ -79,32 +80,16 @@ sinon.restore = function (...args) { return restore.apply(sinon, args) } -// stub out the entire electron object for our stub -// we must use an absolute path here because of the way mockery internally loads this -// module - meaning the first time electron is required it'll use this path string -// so because its required from a separate module we must use an absolute reference to it -mockery.registerSubstitute( - 'electron', - path.join(__dirname, './support/helpers/electron_stub'), -) +enable(mockery) -// stub out electron's original-fs module which is available when running in electron -mockery.registerMock('original-fs', {}) +mockElectron(mockery) before(function () { - mockery.enable({ - warnOnUnregistered: false, - }) - if (hasOnly) { this.test.parent._onlyTests = [true] } }) -after((() => { - mockery.disable() -})) - // appData.ensure() const { setCtx, getCtx, clearCtx, makeDataContext } = require('../lib/makeDataContext') diff --git a/packages/server/test/unit/cloud/api/putProtocolArtifact_spec.ts b/packages/server/test/unit/cloud/api/putProtocolArtifact_spec.ts index a6ba15a363b..dbf2daddf8e 100644 --- a/packages/server/test/unit/cloud/api/putProtocolArtifact_spec.ts +++ b/packages/server/test/unit/cloud/api/putProtocolArtifact_spec.ts @@ -1,4 +1,5 @@ import mockery from 'mockery' +import { enable as enableMockery, mockElectron } from '../../../mockery_helper' import sinon from 'sinon' import chai, { expect } from 'chai' import sinonChai from 'sinon-chai' @@ -24,6 +25,19 @@ describe('putProtocolArtifact', () => { let putProtocolArtifact: (artifactPath: string, maxFileSize: number, destinationUrl: string) => Promise + before(() => { + if (global.mockery) { + global.mockery.disable() + } + }) + + after(() => { + if (global.mockery) { + enableMockery(global.mockery) + mockElectron(global.mockery) + } + }) + beforeEach(() => { mockery.enable({ useCleanCache: true }) mockery.registerAllowables(['../../../../lib/cloud/api/putProtocolArtifact', 'tslib'])