mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 20:54:07 -08:00
test: Automatically load workflow nodes in node tests (#5380)
test: automatically load workflow nodes in node tests
This commit is contained in:
parent
8e428c7133
commit
9a7f3da4a3
|
@ -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 () => {
|
||||
|
|
|
@ -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: () => {},
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
|
@ -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 () => {
|
||||
|
|
Loading…
Reference in a new issue