Skip to content

Commit

Permalink
fix putProtocolArtifact specs when run as a part of the unit test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
cacieprins committed Mar 25, 2024
1 parent 4e839a4 commit d4cab4d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
22 changes: 22 additions & 0 deletions packages/server/test/mockery_helper.ts
Original file line number Diff line number Diff line change
@@ -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,
})
}
23 changes: 4 additions & 19 deletions packages/server/test/spec_helper.js
Original file line number Diff line number Diff line change
@@ -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'))
Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand Down
14 changes: 14 additions & 0 deletions packages/server/test/unit/cloud/api/putProtocolArtifact_spec.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -24,6 +25,19 @@ describe('putProtocolArtifact', () => {

let putProtocolArtifact: (artifactPath: string, maxFileSize: number, destinationUrl: string) => Promise<void>

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'])
Expand Down

0 comments on commit d4cab4d

Please sign in to comment.