|
| 1 | +import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; |
| 2 | +import { |
| 3 | + Tree, |
| 4 | + readProjectConfiguration, |
| 5 | + updateProjectConfiguration, |
| 6 | +} from '@nx/devkit'; |
| 7 | + |
| 8 | +import { denoIntegrationGenerator } from './generator'; |
| 9 | +import applicationGenerator from './../../application/generator'; |
| 10 | +import { DenoIntegrationGeneratorSchema } from './schema'; |
| 11 | +import { Linter } from '@nx/linter'; |
| 12 | + |
| 13 | +describe('deno-integration generator', () => { |
| 14 | + let appTree: Tree; |
| 15 | + const projectName = 'test-project'; |
| 16 | + const options: DenoIntegrationGeneratorSchema = { |
| 17 | + project: projectName, |
| 18 | + denoProjectName: 'cute-frog-420', |
| 19 | + }; |
| 20 | + |
| 21 | + beforeEach(async () => { |
| 22 | + appTree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); |
| 23 | + |
| 24 | + await applicationGenerator(appTree, { |
| 25 | + name: projectName, |
| 26 | + e2eTestRunner: 'none', |
| 27 | + linter: Linter.None, |
| 28 | + skipFormat: false, |
| 29 | + strict: true, |
| 30 | + style: 'css', |
| 31 | + unitTestRunner: 'none', |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should add required targets', async () => { |
| 36 | + await denoIntegrationGenerator(appTree, options); |
| 37 | + const config = readProjectConfiguration(appTree, projectName); |
| 38 | + |
| 39 | + expect(config.targets!['build.ssr'].configurations!['production']).toEqual({ |
| 40 | + configFile: `apps/${projectName}/adapters/deno/vite.config.ts`, |
| 41 | + }); |
| 42 | + |
| 43 | + expect(config.targets!['deploy']).toEqual({ |
| 44 | + executor: 'nx:run-commands', |
| 45 | + options: { |
| 46 | + cwd: `dist/apps/${projectName}/client`, |
| 47 | + command: `deployctl deploy --project=${options.denoProjectName} https://deno.land/std/http/file_server.ts --dry-run`, |
| 48 | + }, |
| 49 | + configurations: { |
| 50 | + preview: { |
| 51 | + command: `deployctl deploy --project=${options.denoProjectName} https://deno.land/std/http/file_server.ts`, |
| 52 | + }, |
| 53 | + production: { |
| 54 | + command: `deployctl deploy --project=${options.denoProjectName} --prod https://deno.land/std/http/file_server.ts`, |
| 55 | + }, |
| 56 | + }, |
| 57 | + dependsOn: ['build-deno'], |
| 58 | + }); |
| 59 | + |
| 60 | + expect(config.targets!['preview-deno']).toEqual({ |
| 61 | + executor: 'nx:run-commands', |
| 62 | + options: { |
| 63 | + command: `deno run --allow-net --allow-read --allow-env dist/apps/${projectName}/server/entry.deno.js`, |
| 64 | + }, |
| 65 | + dependsOn: ['build-deno'], |
| 66 | + }); |
| 67 | + |
| 68 | + expect(config.targets!['build-deno']).toEqual({ |
| 69 | + executor: 'nx:run-commands', |
| 70 | + options: { |
| 71 | + command: `npx nx run ${options.project}:build:production`, |
| 72 | + }, |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should use other target name if deploy target is already defined', async () => { |
| 77 | + const configBefore = readProjectConfiguration(appTree, projectName); |
| 78 | + configBefore.targets!['deploy'] = { executor: 'nx:noop' }; |
| 79 | + updateProjectConfiguration(appTree, projectName, configBefore); |
| 80 | + |
| 81 | + await denoIntegrationGenerator(appTree, options); |
| 82 | + |
| 83 | + const config = readProjectConfiguration(appTree, projectName); |
| 84 | + expect(config.targets!['build.ssr'].configurations!['production']).toEqual({ |
| 85 | + configFile: `apps/${projectName}/adapters/deno/vite.config.ts`, |
| 86 | + }); |
| 87 | + expect(config.targets!['deploy']).toEqual({ executor: 'nx:noop' }); |
| 88 | + expect(config.targets!['deploy.deno']).toEqual({ |
| 89 | + executor: 'nx:run-commands', |
| 90 | + options: { |
| 91 | + cwd: `dist/apps/${projectName}/client`, |
| 92 | + command: `deployctl deploy --project=${options.denoProjectName} https://deno.land/std/http/file_server.ts --dry-run`, |
| 93 | + }, |
| 94 | + configurations: { |
| 95 | + preview: { |
| 96 | + command: `deployctl deploy --project=${options.denoProjectName} https://deno.land/std/http/file_server.ts`, |
| 97 | + }, |
| 98 | + production: { |
| 99 | + command: `deployctl deploy --project=${options.denoProjectName} --prod https://deno.land/std/http/file_server.ts`, |
| 100 | + }, |
| 101 | + }, |
| 102 | + dependsOn: ['build-deno'], |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should use the name of the integration if configuration name "production" is already defined', async () => { |
| 107 | + const configBefore = readProjectConfiguration(appTree, projectName); |
| 108 | + configBefore.targets!['build.ssr'].configurations!['production'] = {}; |
| 109 | + updateProjectConfiguration(appTree, projectName, configBefore); |
| 110 | + |
| 111 | + await denoIntegrationGenerator(appTree, options); |
| 112 | + |
| 113 | + const config = readProjectConfiguration(appTree, projectName); |
| 114 | + expect( |
| 115 | + config.targets!['build'].configurations!['production'] |
| 116 | + ).toBeUndefined(); |
| 117 | + expect(config.targets!['build.ssr'].configurations!['production']).toEqual( |
| 118 | + {} |
| 119 | + ); |
| 120 | + expect(config.targets!['build.ssr'].configurations!['deno']).toEqual({ |
| 121 | + configFile: `apps/${projectName}/adapters/deno/vite.config.ts`, |
| 122 | + }); |
| 123 | + |
| 124 | + expect(config.targets!['deploy']).toEqual({ |
| 125 | + executor: 'nx:run-commands', |
| 126 | + options: { |
| 127 | + cwd: `dist/apps/${projectName}/client`, |
| 128 | + command: `deployctl deploy --project=${options.denoProjectName} https://deno.land/std/http/file_server.ts --dry-run`, |
| 129 | + }, |
| 130 | + configurations: { |
| 131 | + preview: { |
| 132 | + command: `deployctl deploy --project=${options.denoProjectName} https://deno.land/std/http/file_server.ts`, |
| 133 | + }, |
| 134 | + production: { |
| 135 | + command: `deployctl deploy --project=${options.denoProjectName} --prod https://deno.land/std/http/file_server.ts`, |
| 136 | + }, |
| 137 | + }, |
| 138 | + dependsOn: ['build-deno'], |
| 139 | + }); |
| 140 | + expect(config.targets!['build-deno']).toEqual({ |
| 141 | + executor: 'nx:run-commands', |
| 142 | + options: { |
| 143 | + command: `npx nx run ${options.project}:build:deno`, |
| 144 | + }, |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should generate deploy.yml file for github actions', async () => { |
| 149 | + await denoIntegrationGenerator(appTree, { |
| 150 | + ...options, |
| 151 | + generateGithubHook: true, |
| 152 | + }); |
| 153 | + const hasDeployYaml = appTree.exists('.github/workflows/deploy.yml'); |
| 154 | + expect(hasDeployYaml).toBeTruthy(); |
| 155 | + }); |
| 156 | + |
| 157 | + describe('should throw if project configuration does not meet the expectations', () => { |
| 158 | + it('project is not an application', async () => { |
| 159 | + const config = readProjectConfiguration(appTree, projectName); |
| 160 | + config.projectType = 'library'; |
| 161 | + updateProjectConfiguration(appTree, projectName, config); |
| 162 | + |
| 163 | + await expect(denoIntegrationGenerator(appTree, options)).rejects.toThrow( |
| 164 | + 'Cannot setup deno integration for the given project.' |
| 165 | + ); |
| 166 | + }); |
| 167 | + |
| 168 | + it('project does not have Qwik\'s "build-ssr" target', async () => { |
| 169 | + const config = readProjectConfiguration(appTree, projectName); |
| 170 | + config.targets!.build.executor = 'nx:run-commands'; |
| 171 | + updateProjectConfiguration(appTree, projectName, config); |
| 172 | + |
| 173 | + await expect(denoIntegrationGenerator(appTree, options)).rejects.toThrow( |
| 174 | + 'Project contains invalid configuration.' |
| 175 | + ); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
0 commit comments