-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: add order to function * feat: add hono files to templates * feat: add hono dependencies to package.json * test: add unit tests for the hono template * test: setup e2e tests for worker app generator * feat: create custom nx runner * feat: add extra utils * fix: bring complete implementation of runNxCommand * fix: wip works until the tests * fix: move test target to package.json * fix: bring complete implementation of run until * feat: enable all tests * fix: cleanup unneded functions * fix: lint issues
- Loading branch information
1 parent
c0ed09e
commit 5485e10
Showing
30 changed files
with
749 additions
and
261 deletions.
There are no files selected for viewing
134 changes: 111 additions & 23 deletions
134
e2e/plugins-nx-cloudflare-e2e/tests/plugins-nx-cloudflare.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,117 @@ | ||
import { execSync } from 'child_process'; | ||
import { rmSync } from 'fs'; | ||
import { | ||
uniq, | ||
fileExists, | ||
tmpProjPath, | ||
runNxCommand, | ||
} from '@nx/plugin/testing'; | ||
import { | ||
newNxProject, | ||
installPlugin, | ||
cleanup, | ||
promisifiedTreeKill, | ||
runCommandUntil, | ||
killPorts, | ||
} from '@naxodev/e2e/utils'; | ||
import { join } from 'path'; | ||
|
||
import { createTestProject, installPlugin } from '../utils'; | ||
describe('Cloudflare Worker Applications', () => { | ||
beforeEach(() => { | ||
newNxProject(); | ||
installPlugin('nx-cloudflare'); | ||
}); | ||
|
||
describe('plugins-nx-cloudflare', () => { | ||
let projectDirectory: string; | ||
afterEach(() => cleanup()); | ||
|
||
beforeAll(() => { | ||
projectDirectory = createTestProject(); | ||
installPlugin(projectDirectory, 'nx-cloudflare'); | ||
}); | ||
it('should be able to generate an empty application', async () => { | ||
const workerapp = uniq('workerapp'); | ||
|
||
afterAll(() => { | ||
// Cleanup the test project | ||
rmSync(projectDirectory, { | ||
recursive: true, | ||
force: true, | ||
}); | ||
}); | ||
runNxCommand( | ||
`generate @naxodev/nx-cloudflare:app ${workerapp} --template="none"` | ||
); | ||
|
||
it('should be installed', () => { | ||
// pnpm ls will fail if the package is not installed properly | ||
execSync('pnpm ls @naxodev/nx-cloudflare', { | ||
cwd: projectDirectory, | ||
stdio: 'inherit', | ||
}); | ||
}); | ||
expect( | ||
fileExists(join(tmpProjPath(), `apps/${workerapp}/project.json`)) | ||
).toBeTruthy(); | ||
}, 30_000); | ||
|
||
it('should be able to generate an fetch-handler application', async () => { | ||
const workerapp = uniq('workerapp'); | ||
const port = 8787; | ||
|
||
runNxCommand( | ||
`generate @naxodev/nx-cloudflare:app ${workerapp} --template="fetch-handler"` | ||
); | ||
|
||
const lintResults = runNxCommand(`lint ${workerapp}`); | ||
expect(lintResults).toContain( | ||
`NX Successfully ran target lint for project ${workerapp}` | ||
); | ||
|
||
expect( | ||
fileExists(join(tmpProjPath(), `apps/${workerapp}/src/index.ts`)) | ||
).toBeTruthy(); | ||
|
||
const p = await runCommandUntil(`serve ${workerapp}`, (output: string) => | ||
output.includes(`wrangler dev now uses local mode by default`) | ||
); | ||
|
||
if (p.pid) { | ||
await promisifiedTreeKill(p.pid, 'SIGKILL'); | ||
await killPorts(port); | ||
} | ||
}, 120_000); | ||
|
||
it('should be able to generate an scheduled-handler application', async () => { | ||
const workerapp = uniq('workerapp'); | ||
const port = 8787; | ||
|
||
runNxCommand( | ||
`generate @naxodev/nx-cloudflare:app ${workerapp} --template="scheduled-handler"` | ||
); | ||
|
||
const lintResults = runNxCommand(`lint ${workerapp}`); | ||
expect(lintResults).toContain( | ||
`NX Successfully ran target lint for project ${workerapp}` | ||
); | ||
|
||
expect( | ||
fileExists(join(tmpProjPath(), `apps/${workerapp}/src/index.ts`)) | ||
).toBeTruthy(); | ||
|
||
const p = await runCommandUntil(`serve ${workerapp}`, (output: string) => | ||
output.includes(`wrangler dev now uses local mode by default`) | ||
); | ||
|
||
if (p.pid) { | ||
await promisifiedTreeKill(p.pid, 'SIGKILL'); | ||
await killPorts(port); | ||
} | ||
}, 120_000); | ||
|
||
it('should be able to generate an hono application', async () => { | ||
const workerapp = uniq('workerapp'); | ||
const port = 8787; | ||
|
||
runNxCommand( | ||
`generate @naxodev/nx-cloudflare:app ${workerapp} --template="hono"` | ||
); | ||
|
||
const lintResults = runNxCommand(`lint ${workerapp}`); | ||
expect(lintResults).toContain( | ||
`NX Successfully ran target lint for project ${workerapp}` | ||
); | ||
|
||
expect( | ||
fileExists(join(tmpProjPath(), `apps/${workerapp}/src/index.ts`)) | ||
).toBeTruthy(); | ||
|
||
const p = await runCommandUntil(`serve ${workerapp}`, (output: string) => | ||
output.includes(`wrangler dev now uses local mode by default`) | ||
); | ||
|
||
if (p.pid) { | ||
await promisifiedTreeKill(p.pid, 'SIGKILL'); | ||
await killPorts(port); | ||
} | ||
}, 120_000); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
e2e/plugins-nx-cloudflare-e2e/utils/create-test-project.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { dirname, join } from 'path'; | ||
import { removeSync, mkdirSync } from 'fs-extra'; | ||
import { execSync } from 'child_process'; | ||
import { tmpProjPath } from '@nx/plugin/testing'; | ||
|
||
/** | ||
* Deletes the e2e directory | ||
*/ | ||
export function cleanup(): void { | ||
removeSync(dirname(tmpProjPath())); | ||
} | ||
|
||
/** | ||
* Creates a new nx project in the e2e directory | ||
* | ||
*/ | ||
export function newNxProject(): void { | ||
cleanup(); | ||
mkdirSync(dirname(tmpProjPath())); | ||
runNxNewCommand('', false); | ||
} | ||
|
||
export function installPlugin(pluginName: string) { | ||
const localTmpDir = join(tmpProjPath()); | ||
// The plugin has been built and published to a local registry in the jest globalSetup | ||
// Install the plugin built with the latest source code into the test repo | ||
execSync(`pnpm add @naxodev/${pluginName}@e2e`, { | ||
cwd: localTmpDir, | ||
stdio: 'inherit', | ||
env: process.env, | ||
}); | ||
} | ||
|
||
function runNxNewCommand(args?: string, silent?: boolean) { | ||
const localTmpDir = dirname(tmpProjPath()); | ||
|
||
execSync( | ||
`npx --yes create-nx-workspace@latest proj --preset empty --no-nxCloud --no-interactive`, | ||
{ | ||
cwd: localTmpDir, | ||
stdio: 'inherit', | ||
env: process.env, | ||
} | ||
); | ||
console.log(`Created test project in "${localTmpDir}"`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export function isVerbose() { | ||
return ( | ||
process.env.NX_VERBOSE_LOGGING === 'true' || | ||
process.argv.includes('--verbose') | ||
); | ||
} | ||
|
||
export function getStrippedEnvironmentVariables() { | ||
return Object.fromEntries( | ||
Object.entries(process.env).filter(([key, value]) => { | ||
if (key.startsWith('NX_E2E_')) { | ||
return true; | ||
} | ||
|
||
if (key.startsWith('NX_')) { | ||
return false; | ||
} | ||
|
||
if (key === 'JEST_WORKER_ID') { | ||
return false; | ||
} | ||
|
||
return true; | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './create-test-project'; | ||
export * from './run-nx-commands'; | ||
export * from './log-utils'; | ||
export * from './process-utils'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as chalk from 'chalk'; | ||
|
||
export const E2E_LOG_PREFIX = `${chalk.reset.inverse.bold.keyword('orange')( | ||
' E2E ' | ||
)}`; | ||
|
||
export function e2eConsoleLogger(message: string, body?: string) { | ||
process.stdout.write('\n'); | ||
process.stdout.write(`${E2E_LOG_PREFIX} ${message}\n`); | ||
if (body) { | ||
process.stdout.write(`${body}\n`); | ||
} | ||
process.stdout.write('\n'); | ||
} | ||
|
||
export function logInfo(title: string, body?: string) { | ||
const message = `${chalk.reset.inverse.bold.white( | ||
' INFO ' | ||
)} ${chalk.bold.white(title)}`; | ||
return e2eConsoleLogger(message, body); | ||
} | ||
|
||
export function logError(title: string, body?: string) { | ||
const message = `${chalk.reset.inverse.bold.red(' ERROR ')} ${chalk.bold.red( | ||
title | ||
)}`; | ||
return e2eConsoleLogger(message, body); | ||
} | ||
|
||
export function logSuccess(title: string, body?: string) { | ||
const message = `${chalk.reset.inverse.bold.green( | ||
' SUCCESS ' | ||
)} ${chalk.bold.green(title)}`; | ||
return e2eConsoleLogger(message, body); | ||
} | ||
|
||
/** | ||
* Remove log colors for fail proof string search | ||
* @param log | ||
* @returns | ||
*/ | ||
export function stripConsoleColors(log: string): string { | ||
return log?.replace( | ||
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, | ||
'' | ||
); | ||
} |
Oops, something went wrong.