diff --git a/package.json b/package.json index ecdcf3aab2..177e026a0c 100644 --- a/package.json +++ b/package.json @@ -28,9 +28,9 @@ "worker": "./packages/cli/bin/n8n worker", "cypress:install": "cypress install", "cypress:open": "CYPRESS_BASE_URL=http://localhost:8080 cypress open", - "test:e2e:ui": "cross-env E2E_TESTS=true NODE_OPTIONS=--dns-result-order=ipv4first start-server-and-test start http://localhost:5678/favicon.ico 'cypress open'", - "test:e2e:dev": "cross-env E2E_TESTS=true NODE_OPTIONS=--dns-result-order=ipv4first CYPRESS_BASE_URL=http://localhost:8080 start-server-and-test dev http://localhost:8080/favicon.ico 'cypress open'", - "test:e2e:all": "cross-env E2E_TESTS=true NODE_OPTIONS=--dns-result-order=ipv4first start-server-and-test start http://localhost:5678/favicon.ico 'cypress run --headless'" + "test:e2e:ui": "scripts/run-e2e.js ui", + "test:e2e:dev": "scripts/run-e2e.js dev", + "test:e2e:all": "scripts/run-e2e.js all" }, "dependencies": { "n8n": "workspace:*" diff --git a/packages/cli/src/config/index.ts b/packages/cli/src/config/index.ts index fb43b41f8e..f733869c58 100644 --- a/packages/cli/src/config/index.ts +++ b/packages/cli/src/config/index.ts @@ -1,19 +1,14 @@ import convict from 'convict'; import dotenv from 'dotenv'; -import { tmpdir } from 'os'; -import { mkdirSync, mkdtempSync, readFileSync } from 'fs'; -import { join } from 'path'; +import { readFileSync } from 'fs'; import { setGlobalState } from 'n8n-workflow'; import { schema } from './schema'; import { inTest, inE2ETests } from '@/constants'; if (inE2ETests) { - const testsDir = join(tmpdir(), 'n8n-e2e/'); - mkdirSync(testsDir, { recursive: true }); // Skip loading config from env variables in end-to-end tests process.env = { E2E_TESTS: 'true', - N8N_USER_FOLDER: mkdtempSync(testsDir), EXECUTIONS_PROCESS: 'main', N8N_DIAGNOSTICS_ENABLED: 'false', N8N_PUBLIC_API_DISABLED: 'true', diff --git a/scripts/run-e2e.js b/scripts/run-e2e.js new file mode 100755 index 0000000000..f8a9b59c59 --- /dev/null +++ b/scripts/run-e2e.js @@ -0,0 +1,70 @@ +#!/usr/bin/env node +const { spawn } = require('child_process'); +const { mkdirSync, mkdtempSync } = require('fs'); +const { tmpdir } = require('os'); +const { join } = require('path'); + +function runTests(options) { + const testsDir = join(tmpdir(), 'n8n-e2e/'); + mkdirSync(testsDir, { recursive: true }); + + const userFolder = mkdtempSync(testsDir); + + process.env.N8N_USER_FOLDER = userFolder; + process.env.E2E_TESTS = 'true'; + process.env.NODE_OPTIONS = '--dns-result-order=ipv4first'; + + if (options.customEnv) { + Object.keys(options.customEnv).forEach((key) => { + process.env[key] = options.customEnv[key]; + }); + } + + const cmd = `start-server-and-test ${options.startCommand} ${options.url} '${options.testCommand}'`; + const testProcess = spawn(cmd, [], { stdio: 'inherit', shell: true }); + + // Listen for termination signals to properly kill the test process + process.on('SIGINT', () => { + testProcess.kill('SIGINT'); + }); + + process.on('SIGTERM', () => { + testProcess.kill('SIGTERM'); + }); + + testProcess.on('exit', (code) => { + process.exit(code); + }); +} + +const scenario = process.argv[2]; + +switch (scenario) { + case 'ui': + runTests({ + startCommand: 'start', + url: 'http://localhost:5678/favicon.ico', + testCommand: 'cypress open', + }); + break; + case 'dev': + runTests({ + startCommand: 'dev', + url: 'http://localhost:8080/favicon.ico', + testCommand: 'cypress open', + customEnv: { + CYPRESS_BASE_URL: 'http://localhost:8080', + }, + }); + break; + case 'all': + runTests({ + startCommand: 'start', + url: 'http://localhost:5678/favicon.ico', + testCommand: 'cypress run --headless', + }); + break; + default: + console.error('Unknown scenario'); + process.exit(1); +}