Fix type issues in test

This commit is contained in:
Elias Meire 2024-09-26 13:16:53 +02:00
parent bc1fa990cd
commit 426b8ad8f0
No known key found for this signature in database
4 changed files with 31 additions and 11 deletions

View file

@ -1,9 +1,9 @@
import { mock, type CalledWithMock } from 'jest-mock-extended';
import type { MqttClient } from 'mqtt';
import { mock } from 'jest-mock-extended';
import type { ICredentialDataDecryptedObject, IExecuteFunctions } from 'n8n-workflow';
import { Mqtt } from '../Mqtt.node';
import { createClient } from '../GenericFunctions';
import { Mqtt } from '../Mqtt.node';
jest.mock('../GenericFunctions', () => {
const mockMqttClient = mock<MqttClient>();
@ -12,6 +12,8 @@ jest.mock('../GenericFunctions', () => {
};
});
type GetCredentialsMock = CalledWithMock<Promise<ICredentialDataDecryptedObject>, [string]>;
describe('MQTT Node', () => {
const credentials = mock<ICredentialDataDecryptedObject>();
const executeFunctions = mock<IExecuteFunctions>();
@ -19,7 +21,10 @@ describe('MQTT Node', () => {
beforeEach(() => {
jest.clearAllMocks();
executeFunctions.getCredentials.calledWith('mqtt').mockResolvedValue(credentials);
// Jest always uses the last fn overload's types, override to use the overload we need
(executeFunctions.getCredentials as unknown as GetCredentialsMock)
.calledWith('mqtt')
.mockResolvedValue(credentials);
executeFunctions.getInputData.mockReturnValue([{ json: { testing: true } }]);
executeFunctions.getNodeParameter.calledWith('topic', 0).mockReturnValue('test/topic');
executeFunctions.getNodeParameter.calledWith('options', 0).mockReturnValue({});

View file

@ -1,10 +1,10 @@
import { captor, mock, type CalledWithMock } from 'jest-mock-extended';
import type { MqttClient, OnMessageCallback } from 'mqtt';
import { returnJsonArray } from 'n8n-core';
import { captor, mock } from 'jest-mock-extended';
import type { ICredentialDataDecryptedObject, ITriggerFunctions } from 'n8n-workflow';
import { MqttTrigger } from '../MqttTrigger.node';
import { createClient } from '../GenericFunctions';
import { MqttTrigger } from '../MqttTrigger.node';
jest.mock('../GenericFunctions', () => {
const mockMqttClient = mock<MqttClient>();
@ -13,6 +13,8 @@ jest.mock('../GenericFunctions', () => {
};
});
type GetCredentialsMock = CalledWithMock<Promise<ICredentialDataDecryptedObject>, [string]>;
describe('MQTT Trigger Node', () => {
const topic = 'test/topic';
const payload = Buffer.from('{"testing": true}');
@ -24,7 +26,10 @@ describe('MQTT Trigger Node', () => {
beforeEach(() => {
jest.clearAllMocks();
triggerFunctions.getCredentials.calledWith('mqtt').mockResolvedValue(credentials);
// Jest always uses the last fn overload's types, override to use the overload we need
(triggerFunctions.getCredentials as unknown as GetCredentialsMock)
.calledWith('mqtt')
.mockResolvedValue(credentials);
triggerFunctions.getNodeParameter.calledWith('topics').mockReturnValue(topic);
});

View file

@ -1,6 +1,6 @@
import { mock } from 'jest-mock-extended';
import { mock, type CalledWithMock } from 'jest-mock-extended';
import type { RedisClientType } from '@redis/client';
import type { IExecuteFunctions } from 'n8n-workflow';
import type { ICredentialDataDecryptedObject, IExecuteFunctions } from 'n8n-workflow';
const mockClient = mock<RedisClientType>();
const createClient = jest.fn().mockReturnValue(mockClient);
@ -9,6 +9,8 @@ jest.mock('redis', () => ({ createClient }));
import { Redis } from '../Redis.node';
import { setupRedisClient } from '../utils';
type GetCredentialsMock = CalledWithMock<Promise<ICredentialDataDecryptedObject>, [string]>;
describe('Redis Node', () => {
const node = new Redis();
@ -61,7 +63,10 @@ describe('Redis Node', () => {
password: 'random',
};
thisArg.getCredentials.calledWith('redis').mockResolvedValue(mockCredential);
// Jest always uses the last fn overload's types, override to use the overload we need
(thisArg.getCredentials as unknown as GetCredentialsMock)
.calledWith('redis')
.mockResolvedValue(mockCredential);
afterEach(() => {
expect(createClient).toHaveBeenCalled();

View file

@ -1,10 +1,12 @@
import { returnJsonArray } from 'n8n-core';
import { captor, mock } from 'jest-mock-extended';
import { captor, mock, type CalledWithMock } from 'jest-mock-extended';
import type { ICredentialDataDecryptedObject, ITriggerFunctions } from 'n8n-workflow';
import { RedisTrigger } from '../RedisTrigger.node';
import { type RedisClientType, setupRedisClient } from '../utils';
type GetCredentialsMock = CalledWithMock<Promise<ICredentialDataDecryptedObject>, [string]>;
jest.mock('../utils', () => {
const mockRedisClient = mock<RedisClientType>();
return {
@ -22,7 +24,10 @@ describe('Redis Trigger Node', () => {
beforeEach(() => {
jest.clearAllMocks();
triggerFunctions.getCredentials.calledWith('redis').mockResolvedValue(credentials);
// Jest always uses the last fn overload's types, override to use the overload we need
(triggerFunctions.getCredentials as unknown as GetCredentialsMock)
.calledWith('redis')
.mockResolvedValue(credentials);
triggerFunctions.getNodeParameter.calledWith('channels').mockReturnValue(channel);
});