-
-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nx-plugin): add schematic to setup Vitest for existing Angular p…
…rojects (#787)
- Loading branch information
1 parent
dc9c37f
commit 5deadfc
Showing
15 changed files
with
399 additions
and
26 deletions.
There are no files selected for viewing
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
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
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,5 @@ | ||
import { convertNxGenerator } from '@nx/devkit'; | ||
|
||
import setupVitestGenerator from './generator'; | ||
|
||
export default convertNxGenerator(setupVitestGenerator); |
12 changes: 12 additions & 0 deletions
12
packages/nx-plugin/src/generators/setup-vitest/files/src/test-setup.ts__template__
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,12 @@ | ||
import '@analogjs/vite-plugin-angular/setup-vitest'; | ||
|
||
import { | ||
BrowserDynamicTestingModule, | ||
platformBrowserDynamicTesting, | ||
} from '@angular/platform-browser-dynamic/testing'; | ||
import { getTestBed } from '@angular/core/testing'; | ||
|
||
getTestBed().initTestEnvironment( | ||
BrowserDynamicTestingModule, | ||
platformBrowserDynamicTesting() | ||
); |
20 changes: 20 additions & 0 deletions
20
packages/nx-plugin/src/generators/setup-vitest/files/vite.config.ts__template__
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,20 @@ | ||
/// <reference types="vitest" /> | ||
|
||
import analog from '@analogjs/platform'; | ||
import { defineConfig } from 'vite'; | ||
|
||
// https://vitejs.dev/config/ | ||
export default defineConfig(({ mode }) => { | ||
return { | ||
plugins: [analog()], | ||
test: { | ||
globals: true, | ||
environment: 'jsdom', | ||
setupFiles: ['src/test-setup.ts'], | ||
include: ['**/*.spec.ts'], | ||
}, | ||
define: { | ||
'import.meta.vitest': mode !== 'production', | ||
}, | ||
}; | ||
}); |
53 changes: 53 additions & 0 deletions
53
packages/nx-plugin/src/generators/setup-vitest/generator.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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { | ||
formatFiles, | ||
generateFiles, | ||
getProjects, | ||
installPackagesTask, | ||
Tree, | ||
} from '@nx/devkit'; | ||
import * as path from 'path'; | ||
|
||
import { getInstalledPackageVersion } from '../../utils/version-utils'; | ||
import { addAnalogDependencies } from './lib/add-analog-dependencies'; | ||
import { updateTsConfig } from './lib/update-tsconfig'; | ||
import { updateTestTarget } from './lib/update-test-target'; | ||
import { SetupVitestGeneratorSchema } from './schema'; | ||
|
||
function addFiles(tree: Tree, options: SetupVitestGeneratorSchema) { | ||
const projects = getProjects(tree); | ||
|
||
const projectConfig = projects.get(options.project); | ||
|
||
const templateOptions = { | ||
...options, | ||
template: '', | ||
}; | ||
|
||
generateFiles( | ||
tree, | ||
path.join(__dirname, 'files'), | ||
projectConfig.root || '.', | ||
templateOptions | ||
); | ||
} | ||
|
||
export async function setupVitestGenerator( | ||
tree: Tree, | ||
options: SetupVitestGeneratorSchema | ||
) { | ||
const angularVersion = getInstalledPackageVersion(tree, '@angular/core'); | ||
|
||
addAnalogDependencies(tree, angularVersion); | ||
updateTsConfig(tree, options); | ||
updateTestTarget(tree, options); | ||
|
||
addFiles(tree, options); | ||
|
||
await formatFiles(tree); | ||
|
||
return () => { | ||
installPackagesTask(tree); | ||
}; | ||
} | ||
|
||
export default setupVitestGenerator; |
9 changes: 9 additions & 0 deletions
9
packages/nx-plugin/src/generators/setup-vitest/lib/add-analog-dependencies.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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { addDependenciesToPackageJson, Tree } from '@nx/devkit'; | ||
|
||
import { getAnalogDevDependencies } from '../versions/dev-dependencies'; | ||
|
||
export function addAnalogDependencies(tree: Tree, angularVersion: string) { | ||
const devDependencies = getAnalogDevDependencies(angularVersion); | ||
|
||
addDependenciesToPackageJson(tree, {}, devDependencies); | ||
} |
40 changes: 40 additions & 0 deletions
40
packages/nx-plugin/src/generators/setup-vitest/lib/update-test-target.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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
Tree, | ||
getProjects, | ||
updateJson, | ||
updateProjectConfiguration, | ||
} from '@nx/devkit'; | ||
|
||
import { SetupVitestGeneratorSchema } from '../schema'; | ||
|
||
export function updateTestTarget( | ||
tree: Tree, | ||
schema: SetupVitestGeneratorSchema | ||
) { | ||
const angularJsonPath = '/angular.json'; | ||
|
||
if (tree.exists(angularJsonPath)) { | ||
updateJson( | ||
tree, | ||
angularJsonPath, | ||
(json) => { | ||
json.projects[schema.project].architect.test = { | ||
builder: '@analogjs/platform:vitest', | ||
}; | ||
|
||
return json; | ||
}, | ||
{ expectComments: true, allowTrailingComma: true } | ||
); | ||
} else { | ||
const projects = getProjects(tree); | ||
|
||
const projectConfig = projects.get(schema.project); | ||
|
||
projectConfig.targets.test = { | ||
executor: '@analogjs/platform:vitest', | ||
}; | ||
|
||
updateProjectConfiguration(tree, schema.project, projectConfig); | ||
} | ||
} |
Oops, something went wrong.