Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cb5f4fd

Browse files
barbados-clemensFrozenPandaz
authored andcommittedApr 11, 2023
fix(testing): merge reporter/coverage values from vite config (#16165)
(cherry picked from commit 9ba8444)
1 parent 86e9c03 commit cb5f4fd

File tree

2 files changed

+79
-22
lines changed

2 files changed

+79
-22
lines changed
 

‎e2e/vite/src/vite.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ export default defineConfig({
292292
},
293293
environment: 'jsdom',
294294
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
295+
reporters: ['junit'],
296+
outputFile: 'junit.xml',
295297
coverage: {
296298
enabled: true,
297299
reportsDirectory: 'coverage',
@@ -313,6 +315,7 @@ export default defineConfig({
313315
expect(results).toContain(
314316
`Successfully ran target test for project ${lib}`
315317
);
318+
expect(results).toContain(`JUNIT report written`);
316319
}, 100_000);
317320

318321
it('should be able to run tests with inSourceTests set to true', async () => {

‎packages/vite/src/executors/test/vitest.impl.ts

+76-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
import { ExecutorContext, workspaceRoot } from '@nrwl/devkit';
1+
import {
2+
ExecutorContext,
3+
logger,
4+
stripIndents,
5+
workspaceRoot,
6+
} from '@nrwl/devkit';
27
import { CoverageOptions, File, Reporter } from 'vitest';
8+
import { loadConfigFromFile } from 'vite';
39
import { VitestExecutorOptions } from './schema';
4-
import { relative } from 'path';
10+
import { join, relative } from 'path';
11+
import { existsSync } from 'fs';
512

613
class NxReporter implements Reporter {
714
deferred: {
@@ -46,27 +53,9 @@ export async function* vitestExecutor(
4653
'return import("vitest/node")'
4754
)() as Promise<typeof import('vitest/node')>);
4855

49-
const projectRoot = context.projectGraph.nodes[context.projectName].data.root;
50-
const offset = relative(workspaceRoot, context.cwd);
51-
5256
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);
7059

7160
const ctx = await startVitest(options.mode, [], settings);
7261

@@ -98,4 +87,69 @@ export async function* vitestExecutor(
9887
};
9988
}
10089

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+
101155
export default vitestExecutor;

0 commit comments

Comments
 (0)
Please sign in to comment.