test: Fix task runner tests on node 18 and 22 (#12243)

This commit is contained in:
Tomi Turtiainen 2024-12-16 15:37:24 +02:00 committed by GitHub
parent 2ce1644d01
commit 271401d882
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 19 additions and 5 deletions

View file

@ -1,5 +1,6 @@
/** @type {import('jest').Config} */
module.exports = {
...require('../../../jest.config'),
setupFilesAfterEnv: ['n8n-workflow/test/setup.ts'],
testTimeout: 10_000,
};

View file

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

View file

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

View file

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

View file

@ -1,3 +1,4 @@
import './polyfills';
import type { ErrorReporter } from 'n8n-core';
import { ensureError, setGlobalState } from 'n8n-workflow';
import Container from 'typedi';