From 271401d8827faf6fe5cb8c15d7c627307d544e7e Mon Sep 17 00:00:00 2001 From: Tomi Turtiainen <10324676+tomi@users.noreply.github.com> Date: Mon, 16 Dec 2024 15:37:24 +0200 Subject: [PATCH] test: Fix task runner tests on node 18 and 22 (#12243) --- packages/@n8n/task-runner/jest.config.js | 1 + .../errors/__tests__/execution-error.test.ts | 6 +++++- .../src/js-task-runner/errors/execution-error.ts | 11 +++++++---- packages/@n8n/task-runner/src/polyfills.ts | 5 +++++ packages/@n8n/task-runner/src/start.ts | 1 + 5 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 packages/@n8n/task-runner/src/polyfills.ts diff --git a/packages/@n8n/task-runner/jest.config.js b/packages/@n8n/task-runner/jest.config.js index 5c3abe1ef7..2a3a7a1e02 100644 --- a/packages/@n8n/task-runner/jest.config.js +++ b/packages/@n8n/task-runner/jest.config.js @@ -1,5 +1,6 @@ /** @type {import('jest').Config} */ module.exports = { ...require('../../../jest.config'), + setupFilesAfterEnv: ['n8n-workflow/test/setup.ts'], testTimeout: 10_000, }; diff --git a/packages/@n8n/task-runner/src/js-task-runner/errors/__tests__/execution-error.test.ts b/packages/@n8n/task-runner/src/js-task-runner/errors/__tests__/execution-error.test.ts index c85e52f977..53270c2599 100644 --- a/packages/@n8n/task-runner/src/js-task-runner/errors/__tests__/execution-error.test.ts +++ b/packages/@n8n/task-runner/src/js-task-runner/errors/__tests__/execution-error.test.ts @@ -36,7 +36,11 @@ describe('ExecutionError', () => { it('should serialize correctly', () => { const error = new Error('a.unknown is not a function'); - error.stack = defaultStack; + Object.defineProperty(error, 'stack', { + value: defaultStack, + enumerable: true, + }); + // error.stack = defaultStack; const executionError = new ExecutionError(error, 1); diff --git a/packages/@n8n/task-runner/src/js-task-runner/errors/execution-error.ts b/packages/@n8n/task-runner/src/js-task-runner/errors/execution-error.ts index 63a2dd5e0b..ef593d9589 100644 --- a/packages/@n8n/task-runner/src/js-task-runner/errors/execution-error.ts +++ b/packages/@n8n/task-runner/src/js-task-runner/errors/execution-error.ts @@ -10,8 +10,6 @@ export class ExecutionError extends SerializableError { context: { itemIndex: number } | undefined = undefined; - stack = ''; - lineNumber: number | undefined = undefined; constructor(error: ErrorLike, itemIndex?: number) { @@ -22,7 +20,12 @@ export class ExecutionError extends SerializableError { this.context = { itemIndex: this.itemIndex }; } - this.stack = error.stack ?? ''; + // Override the stack trace with the given error's stack trace. Since + // node v22 it's not writable, so we can't assign it directly + Object.defineProperty(this, 'stack', { + value: error.stack, + enumerable: true, + }); this.populateFromStack(); } @@ -31,7 +34,7 @@ export class ExecutionError extends SerializableError { * Populate error `message` and `description` from error `stack`. */ private populateFromStack() { - const stackRows = this.stack.split('\n'); + const stackRows = (this.stack ?? '').split('\n'); if (stackRows.length === 0) { this.message = 'Unknown error'; diff --git a/packages/@n8n/task-runner/src/polyfills.ts b/packages/@n8n/task-runner/src/polyfills.ts new file mode 100644 index 0000000000..7d8a83dd0e --- /dev/null +++ b/packages/@n8n/task-runner/src/polyfills.ts @@ -0,0 +1,5 @@ +// WebCrypto Polyfill for older versions of Node.js 18 +if (!globalThis.crypto?.getRandomValues) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access + globalThis.crypto = require('node:crypto').webcrypto; +} diff --git a/packages/@n8n/task-runner/src/start.ts b/packages/@n8n/task-runner/src/start.ts index 69e9462516..391b6ba156 100644 --- a/packages/@n8n/task-runner/src/start.ts +++ b/packages/@n8n/task-runner/src/start.ts @@ -1,3 +1,4 @@ +import './polyfills'; import type { ErrorReporter } from 'n8n-core'; import { ensureError, setGlobalState } from 'n8n-workflow'; import Container from 'typedi';