|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +import path from 'node:path'; |
| 9 | +import fs from 'node:fs'; |
| 10 | +import { expect } from 'chai'; |
| 11 | +import { TestSession } from '@salesforce/cli-plugins-testkit'; |
| 12 | +import axios from 'axios'; |
| 13 | +import * as dotenv from 'dotenv'; |
| 14 | +import { toKebabCase } from './helpers/utils.js'; |
| 15 | +import { createSfdxProject, createLwcComponent } from './helpers/projectSetup.js'; |
| 16 | +import { startLightningDevServer } from './helpers/devServerUtils.js'; |
| 17 | + |
| 18 | +// Load environment variables from .env file |
| 19 | +dotenv.config(); |
| 20 | + |
| 21 | +const INSTANCE_URL = process.env.TESTKIT_HUB_INSTANCE; |
| 22 | +const TEST_TIMEOUT_MS = 60_000; |
| 23 | +const STARTUP_DELAY_MS = 5000; |
| 24 | +const DEV_SERVER_PORT = 3000; |
| 25 | + |
| 26 | +// Skip this test in CI environment - run only locally |
| 27 | +const shouldSkipTest = process.env.CI === 'true' || process.env.CI === '1'; |
| 28 | + |
| 29 | +(shouldSkipTest ? describe.skip : describe)('LWC Local Preview Integration', () => { |
| 30 | + let session: TestSession; |
| 31 | + let componentName: string; |
| 32 | + let projectDir: string; |
| 33 | + |
| 34 | + before(async () => { |
| 35 | + componentName = 'helloWorld'; |
| 36 | + |
| 37 | + session = await TestSession.create({ devhubAuthStrategy: 'JWT' }); |
| 38 | + |
| 39 | + const timestamp = Date.now(); |
| 40 | + projectDir = path.join(session.dir, `lwc-project-${timestamp}`); |
| 41 | + fs.mkdirSync(projectDir, { recursive: true }); |
| 42 | + |
| 43 | + await Promise.all([ |
| 44 | + createSfdxProject(projectDir, INSTANCE_URL ?? ''), |
| 45 | + createLwcComponent(projectDir, componentName), |
| 46 | + ]); |
| 47 | + }); |
| 48 | + |
| 49 | + after(async () => { |
| 50 | + await session?.clean(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should start lightning dev server and respond to /c-hello-world/ URL', async function () { |
| 54 | + this.timeout(TEST_TIMEOUT_MS); |
| 55 | + |
| 56 | + let stderrOutput = ''; |
| 57 | + let stdoutOutput = ''; |
| 58 | + let exitedEarly = false; |
| 59 | + let exitCode: number | null = null; |
| 60 | + |
| 61 | + const serverProcess = startLightningDevServer(projectDir, componentName); |
| 62 | + |
| 63 | + serverProcess.stderr?.on('data', (data: Buffer) => { |
| 64 | + stderrOutput += data.toString(); |
| 65 | + }); |
| 66 | + |
| 67 | + serverProcess.stdout?.on('data', (data: Buffer) => { |
| 68 | + stdoutOutput += data.toString(); |
| 69 | + }); |
| 70 | + |
| 71 | + serverProcess.on('exit', (code: number) => { |
| 72 | + exitedEarly = true; |
| 73 | + exitCode = code; |
| 74 | + }); |
| 75 | + |
| 76 | + serverProcess.on('error', (error) => { |
| 77 | + exitedEarly = true; |
| 78 | + stderrOutput += `Process error: ${String(error)}\n`; |
| 79 | + }); |
| 80 | + |
| 81 | + // Wait for server startup |
| 82 | + await new Promise((r) => setTimeout(r, STARTUP_DELAY_MS)); |
| 83 | + |
| 84 | + // Test the kebab-case component URL with /c- prefix |
| 85 | + const componentKebabName = toKebabCase(componentName); |
| 86 | + const componentUrl = `http://localhost:${DEV_SERVER_PORT}/c-${componentKebabName}/`; |
| 87 | + let componentHttpSuccess = false; |
| 88 | + |
| 89 | + try { |
| 90 | + const componentResponse = await axios.get(componentUrl, { timeout: 2000 }); |
| 91 | + componentHttpSuccess = componentResponse.status === 200; |
| 92 | + } catch (error) { |
| 93 | + const err = error as { message?: string }; |
| 94 | + stderrOutput += `Component URL HTTP request failed: ${err.message ?? 'Unknown error'}\n`; |
| 95 | + componentHttpSuccess = false; |
| 96 | + } |
| 97 | + |
| 98 | + // Clean up |
| 99 | + try { |
| 100 | + if (serverProcess.pid && process.kill(serverProcess.pid, 0)) { |
| 101 | + process.kill(serverProcess.pid, 'SIGKILL'); |
| 102 | + } |
| 103 | + } catch (error) { |
| 104 | + const err = error as NodeJS.ErrnoException; |
| 105 | + if (err.code !== 'ESRCH') throw error; |
| 106 | + } |
| 107 | + |
| 108 | + // Stderr error check |
| 109 | + const criticalPatterns = [ |
| 110 | + 'FATAL', |
| 111 | + 'Cannot find module', |
| 112 | + 'ENOENT', |
| 113 | + 'Unable to find component', |
| 114 | + 'command lightning:dev:component not found', |
| 115 | + ]; |
| 116 | + const hasCriticalError = criticalPatterns.some((pattern) => stderrOutput.includes(pattern)); |
| 117 | + |
| 118 | + expect( |
| 119 | + exitedEarly, |
| 120 | + `Dev server exited early with code ${exitCode}. Full stderr: ${stderrOutput}. Full stdout: ${stdoutOutput}` |
| 121 | + ).to.be.false; |
| 122 | + expect(hasCriticalError, `Critical stderr output detected:\n${stderrOutput}`).to.be.false; |
| 123 | + expect( |
| 124 | + componentHttpSuccess, |
| 125 | + `Dev server did not respond with HTTP 200 for component URL. Tried URL: ${componentUrl}` |
| 126 | + ).to.be.true; |
| 127 | + }); |
| 128 | +}); |
0 commit comments