refactor: Overhaul nodes-testing setup - Part 1 (no-changelog) (#14303)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2025-04-01 10:15:13 +02:00
committed by GitHub
parent f85b851851
commit 73e8d76e13
165 changed files with 3397 additions and 6453 deletions

View File

@@ -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__';

View File

@@ -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';

View File

@@ -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;
}
};