Skip to content

Commit

Permalink
test(core): Move unit tests closer to testable components
Browse files Browse the repository at this point in the history
Unit tests have been residing in `test/unit/` directory. We want to them to be closer
to the source files. This moves all the unit tests to `__tests__` directories next to
the components that are being tested.
  • Loading branch information
tomi committed Aug 2, 2024
1 parent aa0a470 commit c926395
Show file tree
Hide file tree
Showing 80 changed files with 95 additions and 105 deletions.
4 changes: 2 additions & 2 deletions packages/cli/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ module.exports = {

overrides: [
{
files: ['./src/databases/**/*.ts', './test/**/*.ts'],
files: ['./src/databases/**/*.ts', './test/**/*.ts', './src/**/__tests__/**/*.ts'],
rules: {
'n8n-local-rules/misplaced-n8n-typeorm-import': 'off',
},
},
{
files: ['./test/**/*.ts'],
files: ['./test/**/*.ts', './src/**/__tests__/**/*.ts'],
rules: {
'n8n-local-rules/no-type-unsafe-event-emitter': 'off',
},
Expand Down
2 changes: 0 additions & 2 deletions packages/cli/src/ActiveWebhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,9 @@ export class ActiveWebhooks implements IWebhookManager {
const pathElements = path.split('/').slice(1);

// extracting params from path
// @ts-ignore
webhook.webhookPath.split('/').forEach((ele, index) => {
if (ele.startsWith(':')) {
// write params to req.params
// @ts-ignore
request.params[ele.slice(1)] = pathElements[index];
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { License } from '@/License';
import { ExternalSecretsManager } from '@/ExternalSecrets/ExternalSecretsManager.ee';
import { ExternalSecretsProviders } from '@/ExternalSecrets/ExternalSecretsProviders.ee';
import { InternalHooks } from '@/InternalHooks';
import { mockInstance } from '../../shared/mocking';
import { mockInstance } from '@test/mocking';
import {
DummyProvider,
ErrorProvider,
FailedProvider,
MockProviders,
} from '../../shared/ExternalSecrets/utils';
} from '@test/ExternalSecrets/utils';
import { mock } from 'jest-mock-extended';

describe('External Secrets Manager', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UserRepository } from '@/databases/repositories/user.repository';
import { mockInstance } from '../../shared/mocking';
import { mockInstance } from '@test/mocking';
import * as helpers from '@/Ldap/helpers.ee';
import { AuthIdentity } from '@/databases/entities/AuthIdentity';
import { User } from '@/databases/entities/User';
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/WorkflowExecuteAdditionalData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,6 @@ export async function getRunData(
const runData: IWorkflowExecutionDataProcess = {
executionMode: mode,
executionData: runExecutionData,
// @ts-ignore
workflowData,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CredentialTypes } from '@/CredentialTypes';
import { Container } from 'typedi';
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';
import { mockInstance } from '../shared/mocking';
import { mockInstance } from '@test/mocking';

describe('CredentialTypes', () => {
const mockNodesAndCredentials = mockInstance(LoadNodesAndCredentials, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { NodeTypes } from '@/NodeTypes';
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';
import { CredentialsRepository } from '@db/repositories/credentials.repository';
import { SharedCredentialsRepository } from '@db/repositories/sharedCredentials.repository';
import { mockInstance } from '../shared/mocking';
import { mockInstance } from '@test/mocking';

describe('CredentialsHelper', () => {
mockInstance(CredentialsRepository);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import config from '@/config';
import { License } from '@/License';
import { Logger } from '@/Logger';
import { N8N_VERSION } from '@/constants';
import { mockInstance } from '../shared/mocking';
import { mockInstance } from '@test/mocking';
import { OrchestrationService } from '@/services/orchestration.service';

jest.mock('@n8n_io/license-sdk');
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { VariablesService } from '@/environments/variables/variables.service.ee';
import { mockInstance } from '../shared/mocking';
import { mockInstance } from '@test/mocking';
import { MessageEventBus } from '@/eventbus/MessageEventBus/MessageEventBus';
import { getBase } from '@/WorkflowExecuteAdditionalData';
import Container from 'typedi';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import type { User } from '@db/entities/User';
import { WorkflowRunner } from '@/WorkflowRunner';
import config from '@/config';

import * as testDb from '../integration/shared/testDb';
import { setupTestServer } from '../integration/shared/utils';
import { createUser } from '../integration/shared/db/users';
import { createWorkflow } from '../integration/shared/db/workflows';
import { createExecution } from '../integration/shared/db/executions';
import * as testDb from '@test-integration/testDb';
import { setupTestServer } from '@test-integration/utils';
import { createUser } from '@test-integration/db/users';
import { createWorkflow } from '@test-integration/db/workflows';
import { createExecution } from '@test-integration/db/executions';
import { mockInstance } from '@test/mocking';
import { Telemetry } from '@/telemetry';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { main } from '@/commands/db/revert';
import { mockInstance } from '../../../shared/mocking';
import { mockInstance } from '@test/mocking';
import { Logger } from '@/Logger';
import type { IrreversibleMigration, ReversibleMigration } from '@/databases/types';
import type { Migration, MigrationExecutor } from '@n8n/typeorm';
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { InternalHooks } from '@/InternalHooks';
import { License } from '@/License';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import { UserRepository } from '@/databases/repositories/user.repository';
import { badPasswords } from '../shared/testData';
import { mockInstance } from '../../shared/mocking';
import { badPasswords } from '@test/testData';
import { mockInstance } from '@test/mocking';

const browserId = 'test-browser-id';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import type { OwnerRequest } from '@/requests';
import type { UserService } from '@/services/user.service';
import { PasswordUtility } from '@/services/password.utility';

import { mockInstance } from '../../shared/mocking';
import { badPasswords } from '../shared/testData';
import { mockInstance } from '@test/mocking';
import { badPasswords } from '@test/testData';

describe('OwnerController', () => {
const configGetSpy = jest.spyOn(config, 'getEnv');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('UserSettingsController', () => {
[],
],
[
'updates user settings, reseting to waiting state',
'updates user settings, resetting to waiting state',
{
waitingForResponse: true,
ignoredCount: 0,
Expand Down Expand Up @@ -137,7 +137,7 @@ describe('UserSettingsController', () => {
'is waitingForResponse but missing ignoredCount',
{ lastShownAt: 123, waitingForResponse: true },
],
])('thows error when request payload is %s', async (_, payload) => {
])('throws error when request payload is %s', async (_, payload) => {
const req = mock<NpsSurveyRequest.NpsSurveyUpdate>();
req.user.id = '1';
req.body = payload;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { CredentialsHelper } from '@/CredentialsHelper';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import { NotFoundError } from '@/errors/response-errors/not-found.error';

import { mockInstance } from '../../../shared/mocking';
import { mockInstance } from '@test/mocking';

describe('OAuth1CredentialController', () => {
mockInstance(Logger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { CredentialsHelper } from '@/CredentialsHelper';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import { NotFoundError } from '@/errors/response-errors/not-found.error';

import { mockInstance } from '../../../shared/mocking';
import { mockInstance } from '@test/mocking';

describe('OAuth2CredentialController', () => {
mockInstance(Logger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { mock } from 'jest-mock-extended';
import { CREDENTIAL_BLANKING_VALUE } from '@/constants';
import type { CredentialsEntity } from '@db/entities/CredentialsEntity';
import type { CredentialTypes } from '@/CredentialTypes';
import { CredentialsService } from '../credentials.service';
import { CredentialsService } from '@/credentials/credentials.service';

describe('CredentialsService', () => {
const credType = mock<ICredentialType>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { mock } from 'jest-mock-extended';

import { ExecutionEntity } from '@db/entities/ExecutionEntity';
import { ExecutionRepository } from '@db/repositories/execution.repository';
import { mockEntityManager } from '../../shared/mocking';
import { mockInstance } from '../../shared/mocking';
import { mockInstance, mockEntityManager } from '@test/mocking';

describe('ExecutionRepository', () => {
const entityManager = mockEntityManager(ExecutionEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { CredentialsEntity } from '@db/entities/CredentialsEntity';
import { SharedCredentials } from '@db/entities/SharedCredentials';
import { SharedCredentialsRepository } from '@db/repositories/sharedCredentials.repository';
import { GLOBAL_MEMBER_SCOPES, GLOBAL_OWNER_SCOPES } from '@/permissions/global-roles';
import { mockEntityManager } from '../../shared/mocking';
import { mockEntityManager } from '@test/mocking';

describe('SharedCredentialsRepository', () => {
const entityManager = mockEntityManager(SharedCredentials);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { mock, mockClear } from 'jest-mock-extended';
import { StatisticsNames, WorkflowStatistics } from '@db/entities/WorkflowStatistics';
import { WorkflowStatisticsRepository } from '@db/repositories/workflowStatistics.repository';

import { mockEntityManager } from '../../shared/mocking';
import { mockEntityManager } from '@test/mocking';

describe('insertWorkflowStatistics', () => {
const entityManager = mockEntityManager(WorkflowStatistics);
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { constants as fsConstants, accessSync } from 'fs';
import type { SourceControlledFile } from '@/environments/sourceControl/types/sourceControlledFile';
import type { SourceControlPreferences } from '@/environments/sourceControl/types/sourceControlPreferences';
import { mockInstance } from '../shared/mocking';
import { mockInstance } from '@test/mocking';

const pushResult: SourceControlledFile[] = [
{
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { restoreBinaryDataId } from '@/executionLifecycleHooks/restoreBinaryDataId';
import { BinaryDataService } from 'n8n-core';
import { mockInstance } from '../../shared/mocking';
import { mockInstance } from '@test/mocking';

import type { IRun } from 'n8n-workflow';
import config from '@/config';
Expand All @@ -24,7 +24,7 @@ function toIRun(item?: object) {
}

function getDataId(run: IRun, kind: 'binary' | 'json') {
// @ts-ignore
// @ts-expect-error The type doesn't have the correct structure
return run.data.resultData.runData.myNode[0].data.main[0][0][kind].data.id;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExecutionRepository } from '@/databases/repositories/execution.repository';
import { mockInstance } from '../../shared/mocking';
import { mockInstance } from '@test/mocking';
import { Logger } from '@/Logger';
import { saveExecutionProgress } from '@/executionLifecycleHooks/saveExecutionProgress';
import * as fnModule from '@/executionLifecycleHooks/toSaveSettings';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('failed production executions', () => {
});
});

describe('sucessful production executions', () => {
describe('successful production executions', () => {
it('should favor workflow settings over defaults', () => {
config.set('executions.saveDataOnSuccess', 'none');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PostHog } from 'posthog-node';
import { InstanceSettings } from 'n8n-core';
import { PostHogClient } from '@/posthog';
import config from '@/config';
import { mockInstance } from '../shared/mocking';
import { mockInstance } from '@test/mocking';

jest.mock('posthog-node');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { WebSocketPush } from '@/push/websocket.push';
import type { WebSocketPushRequest, SSEPushRequest } from '@/push/types';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';

import { mockInstance } from '../../shared/mocking';
import { mockInstance } from '@test/mocking';

jest.unmock('@/push');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { WebSocketPush } from '@/push/websocket.push';
import { Logger } from '@/Logger';
import type { PushDataExecutionRecovered } from '@/Interfaces';

import { mockInstance } from '../../shared/mocking';
import { mockInstance } from '@test/mocking';

jest.useFakeTimers();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Container } from 'typedi';
import { ExecutionMetadataRepository } from '@db/repositories/executionMetadata.repository';
import { ExecutionMetadataService } from '@/services/executionMetadata.service';
import { mockInstance } from '../shared/mocking';
import { mockInstance } from '@test/mocking';

describe('ExecutionMetadataService', () => {
const repository = mockInstance(ExecutionMetadataRepository);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,10 @@ import { InstalledPackagesRepository } from '@db/repositories/installedPackages.
import { InstalledNodes } from '@db/entities/InstalledNodes';
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';

import { mockInstance } from '../../shared/mocking';
import {
COMMUNITY_NODE_VERSION,
COMMUNITY_PACKAGE_VERSION,
} from '../../integration/shared/constants';
import { randomName } from '../../integration/shared/random';
import { mockPackageName, mockPackagePair } from '../../integration/shared/utils';
import { mockInstance } from '@test/mocking';
import { COMMUNITY_NODE_VERSION, COMMUNITY_PACKAGE_VERSION } from '@test-integration/constants';
import { randomName } from '@test-integration/random';
import { mockPackageName, mockPackagePair } from '@test-integration/utils';
import { InstanceSettings, PackageDirectoryLoader } from 'n8n-core';
import { Logger } from '@/Logger';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WorkflowRepository } from '@/databases/repositories/workflow.repository';
import { CredentialsRepository } from '@/databases/repositories/credentials.repository';
import { mockInstance } from '../../shared/mocking';
import { mockInstance } from '@test/mocking';
import { NamingService } from '@/services/naming.service';
import type { WorkflowEntity } from '@/databases/entities/WorkflowEntity';
import type { CredentialsEntity } from '@/databases/entities/CredentialsEntity';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { ExternalSecretsManager } from '@/ExternalSecrets/ExternalSecretsManager
import { Logger } from '@/Logger';
import { Push } from '@/push';
import { ActiveWorkflowManager } from '@/ActiveWorkflowManager';
import { mockInstance } from '../../shared/mocking';
import { mockInstance } from '@test/mocking';
import { RedisClientService } from '@/services/redis/redis-client.service';

const instanceSettings = Container.get(InstanceSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { SharedWorkflowRepository } from '@db/repositories/sharedWorkflow.reposi
import { SharedWorkflow } from '@db/entities/SharedWorkflow';
import { User } from '@db/entities/User';
import type { SharedCredentials } from '@db/entities/SharedCredentials';
import { mockInstance } from '../../shared/mocking';
import { mockInstance } from '@test/mocking';
import { WorkflowEntity } from '@/databases/entities/WorkflowEntity';
import { UserRepository } from '@/databases/repositories/user.repository';
import { mock } from 'jest-mock-extended';
import { Project } from '@/databases/entities/Project';
import { ProjectRelationRepository } from '@/databases/repositories/projectRelation.repository';
import { ProjectRelation } from '@/databases/entities/ProjectRelation';
import { mockCredential, mockProject } from '../shared/mockObjects';
import { mockCredential, mockProject } from '@test/mockObjects';

describe('OwnershipService', () => {
const userRepository = mockInstance(UserRepository);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Container from 'typedi';
import { Logger } from '@/Logger';
import config from '@/config';
import { RedisService } from '@/services/redis.service';
import { mockInstance } from '../../shared/mocking';
import { mockInstance } from '@test/mocking';

jest.mock('ioredis', () => {
const Redis = require('ioredis-mock');
Expand All @@ -14,7 +14,7 @@ jest.mock('ioredis', () => {
};
}
// second mock for our code
return function (...args: any) {
return function (...args: unknown[]) {
return new Redis(args);
};
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { v4 as uuid } from 'uuid';
import { User } from '@db/entities/User';
import { UserService } from '@/services/user.service';
import { UrlService } from '@/services/url.service';
import { mockInstance } from '../../shared/mocking';
import { mockInstance } from '@test/mocking';
import { UserRepository } from '@/databases/repositories/user.repository';
import { GlobalConfig } from '@n8n/config';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { WebhookRepository } from '@db/repositories/webhook.repository';
import { CacheService } from '@/services/cache/cache.service';
import { WebhookService } from '@/services/webhook.service';
import { WebhookEntity } from '@db/entities/WebhookEntity';
import { mockInstance } from '../../shared/mocking';
import { mockInstance } from '@test/mocking';

const createWebhook = (method: string, path: string, webhookId?: string, pathSegments?: number) =>
Object.assign(new WebhookEntity(), {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { WorkflowStatisticsRepository } from '@db/repositories/workflowStatistic
import { WorkflowStatisticsService } from '@/services/workflow-statistics.service';
import { UserService } from '@/services/user.service';
import { OwnershipService } from '@/services/ownership.service';
import { mockInstance } from '../../shared/mocking';
import { mockInstance } from '@test/mocking';
import type { Project } from '@/databases/entities/Project';

describe('WorkflowStatisticsService', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mock } from 'jest-mock-extended';
import type express from 'express';
import { SamlService } from '@/sso/saml/saml.service.ee';
import { mockInstance } from '../../../shared/mocking';
import { mockInstance } from '@test/mocking';
import { UrlService } from '@/services/url.service';
import { Logger } from '@/Logger';
import type { IdentityProviderInstance, ServiceProviderInstance } from 'samlify';
Expand Down
Loading

0 comments on commit c926395

Please sign in to comment.