|
1 |
| -import { ExecutorContext, workspaceRoot } from '@nrwl/devkit'; |
| 1 | +import { |
| 2 | + ExecutorContext, |
| 3 | + logger, |
| 4 | + stripIndents, |
| 5 | + workspaceRoot, |
| 6 | +} from '@nrwl/devkit'; |
2 | 7 | import { CoverageOptions, File, Reporter } from 'vitest';
|
| 8 | +import { loadConfigFromFile } from 'vite'; |
3 | 9 | import { VitestExecutorOptions } from './schema';
|
4 |
| -import { relative } from 'path'; |
| 10 | +import { join, relative } from 'path'; |
| 11 | +import { existsSync } from 'fs'; |
5 | 12 |
|
6 | 13 | class NxReporter implements Reporter {
|
7 | 14 | deferred: {
|
@@ -46,27 +53,9 @@ export async function* vitestExecutor(
|
46 | 53 | 'return import("vitest/node")'
|
47 | 54 | )() as Promise<typeof import('vitest/node')>);
|
48 | 55 |
|
49 |
| - const projectRoot = context.projectGraph.nodes[context.projectName].data.root; |
50 |
| - const offset = relative(workspaceRoot, context.cwd); |
51 |
| - |
52 | 56 | const nxReporter = new NxReporter(options.watch);
|
53 |
| - // if reportsDirectory is not provides vitest will remove all files in the project root |
54 |
| - // when coverage is enabled in the vite.config.ts |
55 |
| - const coverage: CoverageOptions = options.reportsDirectory |
56 |
| - ? { |
57 |
| - enabled: options.coverage, |
58 |
| - reportsDirectory: options.reportsDirectory, |
59 |
| - } |
60 |
| - : {}; |
61 |
| - const settings = { |
62 |
| - ...options, |
63 |
| - // when running nx from the project root, the root will get appended to the cwd. |
64 |
| - // creating an invalid path and no tests will be found. |
65 |
| - // instead if we are not at the root, let the cwd be root. |
66 |
| - root: offset === '' ? projectRoot : '', |
67 |
| - reporters: [...(options.reporters ?? []), 'default', nxReporter], |
68 |
| - coverage, |
69 |
| - }; |
| 57 | + const settings = await getSettings(options, context); |
| 58 | + settings.reporters.push(nxReporter); |
70 | 59 |
|
71 | 60 | const ctx = await startVitest(options.mode, [], settings);
|
72 | 61 |
|
@@ -98,4 +87,69 @@ export async function* vitestExecutor(
|
98 | 87 | };
|
99 | 88 | }
|
100 | 89 |
|
| 90 | +async function getSettings( |
| 91 | + options: VitestExecutorOptions, |
| 92 | + context: ExecutorContext |
| 93 | +) { |
| 94 | + const projectRoot = context.projectGraph.nodes[context.projectName].data.root; |
| 95 | + const offset = relative(workspaceRoot, context.cwd); |
| 96 | + // if reportsDirectory is not provides vitest will remove all files in the project root |
| 97 | + // when coverage is enabled in the vite.config.ts |
| 98 | + const coverage: CoverageOptions = options.reportsDirectory |
| 99 | + ? { |
| 100 | + enabled: options.coverage, |
| 101 | + reportsDirectory: options.reportsDirectory, |
| 102 | + } |
| 103 | + : {}; |
| 104 | + |
| 105 | + const viteConfigPath = options.config |
| 106 | + ? join(context.root, options.config) |
| 107 | + : findViteConfig(join(context.root, projectRoot)); |
| 108 | + |
| 109 | + const resolved = await loadConfigFromFile( |
| 110 | + { |
| 111 | + mode: options.mode, |
| 112 | + command: 'serve', |
| 113 | + }, |
| 114 | + viteConfigPath |
| 115 | + ); |
| 116 | + |
| 117 | + if (!viteConfigPath || !resolved?.config?.test) { |
| 118 | + logger.warn(stripIndents`Unable to load test config from config file ${ |
| 119 | + resolved.path ?? viteConfigPath |
| 120 | + } |
| 121 | +Some settings may not be applied as expected. |
| 122 | +You can manually set the config in the project, ${ |
| 123 | + context.projectName |
| 124 | + }, configuration. |
| 125 | + `); |
| 126 | + } |
| 127 | + |
| 128 | + const settings = { |
| 129 | + ...options, |
| 130 | + // when running nx from the project root, the root will get appended to the cwd. |
| 131 | + // creating an invalid path and no tests will be found. |
| 132 | + // instead if we are not at the root, let the cwd be root. |
| 133 | + root: offset === '' ? projectRoot : '', |
| 134 | + reporters: [ |
| 135 | + ...(options.reporters ?? []), |
| 136 | + ...((resolved?.config?.test?.reporters as string[]) ?? []), |
| 137 | + 'default', |
| 138 | + ] as (string | Reporter)[], |
| 139 | + coverage: { ...resolved?.config?.test?.coverage, ...coverage }, |
| 140 | + }; |
| 141 | + |
| 142 | + return settings; |
| 143 | +} |
| 144 | + |
| 145 | +function findViteConfig(projectRootFullPath: string): string { |
| 146 | + const allowsExt = ['js', 'mjs', 'ts', 'cjs', 'mts', 'cts']; |
| 147 | + |
| 148 | + for (const ext of allowsExt) { |
| 149 | + if (existsSync(join(projectRootFullPath, `vite.config.${ext}`))) { |
| 150 | + return join(projectRootFullPath, `vite.config.${ext}`); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
101 | 155 | export default vitestExecutor;
|
0 commit comments