mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
refactor: Overhaul nodes-testing setup - Part 1 (no-changelog) (#14303)
This commit is contained in:
committed by
GitHub
parent
f85b851851
commit
73e8d76e13
@@ -1,6 +1,7 @@
|
||||
const { NODE_ENV } = process.env;
|
||||
export const inProduction = NODE_ENV === 'production';
|
||||
export const inDevelopment = !NODE_ENV || NODE_ENV === 'development';
|
||||
export const inTest = NODE_ENV === 'test';
|
||||
|
||||
export const CUSTOM_EXTENSION_ENV = 'N8N_CUSTOM_EXTENSIONS';
|
||||
export const PLACEHOLDER_EMPTY_EXECUTION_ID = '__UNKNOWN__';
|
||||
|
||||
@@ -2,6 +2,10 @@ import vm from 'vm';
|
||||
|
||||
import { loadClassInIsolation } from '../load-class-in-isolation';
|
||||
|
||||
jest.mock('@/constants', () => ({
|
||||
inTest: false,
|
||||
}));
|
||||
|
||||
describe('loadClassInIsolation', () => {
|
||||
const filePath = '/path/to/TestClass.js';
|
||||
const className = 'TestClass';
|
||||
|
||||
@@ -1,10 +1,19 @@
|
||||
import { createContext, Script } from 'vm';
|
||||
|
||||
import { inTest } from '@/constants';
|
||||
|
||||
const context = createContext({ require });
|
||||
export const loadClassInIsolation = <T>(filePath: string, className: string) => {
|
||||
if (process.platform === 'win32') {
|
||||
filePath = filePath.replace(/\\/g, '/');
|
||||
}
|
||||
const script = new Script(`new (require('${filePath}').${className})()`);
|
||||
return script.runInContext(context) as T;
|
||||
|
||||
// Note: Skip the isolation because it breaks nock mocks in tests
|
||||
if (inTest) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access
|
||||
return new (require(filePath)[className])() as T;
|
||||
} else {
|
||||
const script = new Script(`new (require('${filePath}').${className})()`);
|
||||
return script.runInContext(context) as T;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user