mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
test: Add unit testing to nodes (no-changelog) (#4890)
* 🧪 Add base for building unit testing within nodes * Improve helper functions * 🧪 If node test * 🧪 Airtable node test * 🧪 If node test improvements * 🧪 Airtable node test improvements * ♻️ cleanup node unit tests * ♻️ refactor getting node result data to use helper method * ⚡ removed unused variables * ♻️ Helper to read json files --------- Co-authored-by: Marcus <marcus@n8n.io> Co-authored-by: Michael Kret <michael.k@radency.com>
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import { INodeType } from 'n8n-workflow';
|
||||
import { executeWorkflow } from '../ExecuteWorkflow';
|
||||
import * as Helpers from '../Helpers';
|
||||
import { WorkflowTestData } from '../types';
|
||||
import nock from 'nock';
|
||||
|
||||
import { ManualTrigger } from '../../../nodes/ManualTrigger/ManualTrigger.node';
|
||||
import { Airtable } from '../../../nodes/Airtable/Airtable.node';
|
||||
|
||||
const records = [
|
||||
{
|
||||
id: 'rec2BWBoyS5QsS7pT',
|
||||
createdTime: '2022-08-25T08:22:34.000Z',
|
||||
fields: {
|
||||
name: 'Tim',
|
||||
email: 'tim@email.com',
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
describe('Execute Airtable Node', () => {
|
||||
beforeEach(() => {
|
||||
nock.disableNetConnect();
|
||||
nock('https://api.airtable.com/v0')
|
||||
.get('/appIaXXdDqS5ORr4V/tbljyBEdYzCPF0NDh?pageSize=100')
|
||||
.reply(200, { records });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
nock.restore();
|
||||
});
|
||||
|
||||
const tests: Array<WorkflowTestData> = [
|
||||
{
|
||||
description: 'List Airtable Records',
|
||||
input: {
|
||||
workflowData: Helpers.readJsonFileSync('test/nodes/Airtable/workflow.json'),
|
||||
},
|
||||
output: {
|
||||
nodeData: {
|
||||
Airtable: [[...records]],
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const nodes: INodeType[] = [new ManualTrigger(), new Airtable()];
|
||||
const nodeTypes = Helpers.setup(nodes);
|
||||
|
||||
for (const testData of tests) {
|
||||
test(testData.description, async () => {
|
||||
// execute workflow
|
||||
const { result } = await executeWorkflow(testData, nodeTypes);
|
||||
|
||||
// check if result node data matches expected test data
|
||||
const resultNodeData = Helpers.getResultNodeData(result, testData);
|
||||
resultNodeData.forEach(({ nodeName, resultData }) =>
|
||||
expect(resultData).toEqual(testData.output.nodeData[nodeName]),
|
||||
);
|
||||
|
||||
expect(result.finished).toEqual(true);
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user