1
0
Fork 0
mirror of https://github.com/n8n-io/n8n.git synced 2025-03-05 20:50:17 -08:00

fix: Fix console log handling in JS task runner (no-changelog) ()

This commit is contained in:
Tomi Turtiainen 2024-10-08 16:32:50 +03:00 committed by GitHub
parent 606eedbf1b
commit 18c0c8612c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 56 deletions
packages/@n8n/task-runner/src/js-task-runner

View file

@ -1,4 +1,4 @@
import type { CodeExecutionMode, IDataObject, WorkflowExecuteMode } from 'n8n-workflow';
import type { CodeExecutionMode, IDataObject } from 'n8n-workflow';
import { ValidationError } from '@/js-task-runner/errors/validation-error';
import {
@ -31,30 +31,13 @@ describe('JsTaskRunner', () => {
});
describe('console', () => {
test.each<[CodeExecutionMode, WorkflowExecuteMode]>([
['runOnceForAllItems', 'cli'],
['runOnceForAllItems', 'error'],
['runOnceForAllItems', 'integrated'],
['runOnceForAllItems', 'internal'],
['runOnceForAllItems', 'retry'],
['runOnceForAllItems', 'trigger'],
['runOnceForAllItems', 'webhook'],
['runOnceForEachItem', 'cli'],
['runOnceForEachItem', 'error'],
['runOnceForEachItem', 'integrated'],
['runOnceForEachItem', 'internal'],
['runOnceForEachItem', 'retry'],
['runOnceForEachItem', 'trigger'],
['runOnceForEachItem', 'webhook'],
])(
'should make an rpc call for console log in %s mode when workflow mode is %s',
async (nodeMode, workflowMode) => {
jest.spyOn(console, 'log').mockImplementation(() => {});
test.each<[CodeExecutionMode]>([['runOnceForAllItems'], ['runOnceForEachItem']])(
'should make an rpc call for console log in %s mode',
async (nodeMode) => {
jest.spyOn(jsTaskRunner, 'makeRpcCall').mockResolvedValue(undefined);
const task = newTaskWithSettings({
code: "console.log('Hello', 'world!'); return {}",
nodeMode,
workflowMode,
});
await execTaskWithParams({
@ -62,34 +45,11 @@ describe('JsTaskRunner', () => {
taskData: newAllCodeTaskData([wrapIntoJson({})]),
});
expect(console.log).toHaveBeenCalledWith('[JS Code]', 'Hello world!');
expect(jsTaskRunner.makeRpcCall).toHaveBeenCalledWith(task.taskId, 'logNodeOutput', [
'Hello world!',
]);
},
);
test.each<[CodeExecutionMode, WorkflowExecuteMode]>([
['runOnceForAllItems', 'manual'],
['runOnceForEachItem', 'manual'],
])(
"shouldn't make an rpc call for console log in %s mode when workflow mode is %s",
async (nodeMode, workflowMode) => {
jest.spyOn(jsTaskRunner, 'makeRpcCall').mockResolvedValue(undefined);
const task = newTaskWithSettings({
code: "console.log('Hello', 'world!'); return {}",
nodeMode,
workflowMode,
});
await execTaskWithParams({
task,
taskData: newAllCodeTaskData([wrapIntoJson({})]),
});
expect(jsTaskRunner.makeRpcCall).not.toHaveBeenCalled();
},
);
});
describe('runOnceForAllItems', () => {

View file

@ -74,8 +74,6 @@ type CustomConsole = {
log: (...args: unknown[]) => void;
};
const noop = () => {};
export class JsTaskRunner extends TaskRunner {
constructor(
taskType: string,
@ -110,16 +108,14 @@ export class JsTaskRunner extends TaskRunner {
});
const customConsole = {
log:
settings.workflowMode === 'manual'
? noop
: (...args: unknown[]) => {
const logOutput = args
.map((arg) => (typeof arg === 'object' && arg !== null ? JSON.stringify(arg) : arg))
.join(' ');
console.log('[JS Code]', logOutput);
void this.makeRpcCall(task.taskId, 'logNodeOutput', [logOutput]);
},
// Send log output back to the main process. It will take care of forwarding
// it to the UI or printing to console.
log: (...args: unknown[]) => {
const logOutput = args
.map((arg) => (typeof arg === 'object' && arg !== null ? JSON.stringify(arg) : arg))
.join(' ');
void this.makeRpcCall(task.taskId, 'logNodeOutput', [logOutput]);
},
};
const result =