Skip to content

Commit

Permalink
test: Fix task runner tests on node 18 and 22 (n8n-io#12243)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomi authored Dec 16, 2024
1 parent 2ce1644 commit 271401d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/@n8n/task-runner/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @type {import('jest').Config} */
module.exports = {
...require('../../../jest.config'),
setupFilesAfterEnv: ['n8n-workflow/test/setup.ts'],
testTimeout: 10_000,
};
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ export class ExecutionError extends SerializableError {

context: { itemIndex: number } | undefined = undefined;

stack = '';

lineNumber: number | undefined = undefined;

constructor(error: ErrorLike, itemIndex?: number) {
Expand All @@ -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();
}
Expand All @@ -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';
Expand Down
5 changes: 5 additions & 0 deletions packages/@n8n/task-runner/src/polyfills.ts
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions packages/@n8n/task-runner/src/start.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './polyfills';
import type { ErrorReporter } from 'n8n-core';
import { ensureError, setGlobalState } from 'n8n-workflow';
import Container from 'typedi';
Expand Down

0 comments on commit 271401d

Please sign in to comment.