test: Automatically load workflow nodes in node tests (#5380)

test: automatically load workflow nodes in node tests
This commit is contained in:
Valya 2023-02-06 15:14:57 +00:00 committed by GitHub
parent 8e428c7133
commit 9a7f3da4a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 13 deletions

View file

@ -44,8 +44,7 @@ describe('Execute Airtable Node', () => {
},
];
const nodes: INodeType[] = [new ManualTrigger(), new Airtable()];
const nodeTypes = Helpers.setup(nodes);
const nodeTypes = Helpers.setup(tests);
for (const testData of tests) {
test(testData.description, async () => {

View file

@ -1,5 +1,5 @@
import { readFileSync } from 'fs';
import { Credentials } from 'n8n-core';
import { Credentials, loadClassInIsolation } from 'n8n-core';
import {
ICredentialDataDecryptedObject,
ICredentialsHelper,
@ -18,11 +18,13 @@ import {
IVersionedNodeType,
IWorkflowBase,
IWorkflowExecuteAdditionalData,
LoadingDetails,
LoggerProxy,
NodeHelpers,
WorkflowHooks,
} from 'n8n-workflow';
import { WorkflowTestData } from './types';
import path from 'path';
export class CredentialsHelper extends ICredentialsHelper {
async authenticate(
@ -143,11 +145,42 @@ export function NodeTypes(): NodeTypesClass {
return nodeTypesInstance;
}
export function setup(nodes: INodeType[]) {
const nodeTypes = NodeTypes();
for (const node of nodes) {
nodeTypes.addNode('n8n-nodes-base.' + node.description.name, node);
let knownNodes: Record<string, LoadingDetails> | null = null;
const loadKnownNodes = (): Record<string, LoadingDetails> => {
if (knownNodes === null) {
knownNodes = JSON.parse(readFileSync('dist/known/nodes.json').toString());
}
return knownNodes!;
};
export function setup(testData: Array<WorkflowTestData> | WorkflowTestData) {
if (!Array.isArray(testData)) {
testData = [testData];
}
const knownNodes = loadKnownNodes();
const nodeTypes = NodeTypes();
const nodeNames = Array.from(
new Set(testData.flatMap((data) => data.input.workflowData.nodes.map((n) => n.type))),
);
for (const nodeName of nodeNames) {
if (!nodeName.startsWith('n8n-nodes-base.')) {
throw new Error(`Unknown node type: ${nodeName}`);
}
const loadInfo = knownNodes[nodeName.replace('n8n-nodes-base.', '')];
if (!loadInfo) {
throw new Error(`Unknown node type: ${nodeName}`);
}
const node = loadClassInIsolation(
path.join(process.cwd(), loadInfo.sourcePath),
loadInfo.className,
) as INodeType;
nodeTypes.addNode(nodeName, node);
}
const fakeLogger = {
log: () => {},
debug: () => {},

View file

@ -38,8 +38,7 @@ describe('Execute If Node', () => {
},
];
const nodes: INodeType[] = [new ManualTrigger(), new Code(), new Set(), new If(), new NoOp()];
const nodeTypes = Helpers.setup(nodes);
const nodeTypes = Helpers.setup(tests);
for (const testData of tests) {
test(testData.description, async () => {

View file

@ -177,8 +177,7 @@ describe('Execute Set Node', () => {
},
];
const nodes: INodeType[] = [new Start(), new Set()];
const nodeTypes = Helpers.setup(nodes);
const nodeTypes = Helpers.setup(tests);
for (const testData of tests) {
test(testData.description, async () => {

View file

@ -30,8 +30,7 @@ describe('Execute Start Node', () => {
},
];
const nodes: INodeType[] = [new Start()];
const nodeTypes = Helpers.setup(nodes);
const nodeTypes = Helpers.setup(tests);
for (const testData of tests) {
test(testData.description, async () => {