Allow sandboxed-module for model mockery

GitOrigin-RevId: 79dfe75088b760968fdc1b6e5dce4278f44a0dae
This commit is contained in:
Andrew Rumble
2025-10-23 08:06:24 +00:00
committed by Copybot
parent c5ca824d86
commit 7a3b71503f
2 changed files with 48 additions and 0 deletions
@@ -0,0 +1,19 @@
// When importing models in vitest unit tests we need to import them from a CJS file
// instead of directly from the test file so that we can mock the model when it is imported via CJS
// and make sure that is in the require cache for other imports.
//
// TODO: This helper can be removed when all models are converted to ESM and vitest can handle them correctly.
/**
*
* @param {string[]} models
*/
module.exports = function (models) {
return models.reduce((exports, model) => {
const module = require(`./models/${model}`)
return {
...exports,
...module,
}
}, {})
}
@@ -4,6 +4,35 @@ import sinon from 'sinon'
import logger from '@overleaf/logger'
import sinonChai from 'sinon-chai'
import chaiAsPromised from 'chai-as-promised'
import SandboxedModule from 'sandboxed-module'
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
SandboxedModule.configure({
ignoreMissing: true,
requires: {
// This is already imported the same way in the mocha bootstrap
// eslint-disable-next-line import/no-extraneous-dependencies
sshpk: require('sshpk'),
},
globals: {
AbortController,
AbortSignal,
Buffer,
Promise,
console,
process,
URL,
TextEncoder,
TextDecoder,
},
sourceTransformers: {
removeNodePrefix: function (source) {
return source.replace(/require\(['"]node:/g, "require('")
},
},
})
/*
* Chai configuration