Skip to content

Commit

Permalink
fix(testing): merge reporter/coverage values from vite config (#16165)
Browse files Browse the repository at this point in the history
(cherry picked from commit 9ba8444)
  • Loading branch information
barbados-clemens authored and FrozenPandaz committed Apr 11, 2023
1 parent 86e9c03 commit cb5f4fd
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 22 deletions.
3 changes: 3 additions & 0 deletions e2e/vite/src/vite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,8 @@ export default defineConfig({
},
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
reporters: ['junit'],
outputFile: 'junit.xml',
coverage: {
enabled: true,
reportsDirectory: 'coverage',
Expand All @@ -313,6 +315,7 @@ export default defineConfig({
expect(results).toContain(
`Successfully ran target test for project ${lib}`
);
expect(results).toContain(`JUNIT report written`);
}, 100_000);

it('should be able to run tests with inSourceTests set to true', async () => {
Expand Down
98 changes: 76 additions & 22 deletions packages/vite/src/executors/test/vitest.impl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { ExecutorContext, workspaceRoot } from '@nrwl/devkit';
import {
ExecutorContext,
logger,
stripIndents,
workspaceRoot,
} from '@nrwl/devkit';
import { CoverageOptions, File, Reporter } from 'vitest';
import { loadConfigFromFile } from 'vite';
import { VitestExecutorOptions } from './schema';
import { relative } from 'path';
import { join, relative } from 'path';
import { existsSync } from 'fs';

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

const projectRoot = context.projectGraph.nodes[context.projectName].data.root;
const offset = relative(workspaceRoot, context.cwd);

const nxReporter = new NxReporter(options.watch);
// if reportsDirectory is not provides vitest will remove all files in the project root
// when coverage is enabled in the vite.config.ts
const coverage: CoverageOptions = options.reportsDirectory
? {
enabled: options.coverage,
reportsDirectory: options.reportsDirectory,
}
: {};
const settings = {
...options,
// when running nx from the project root, the root will get appended to the cwd.
// creating an invalid path and no tests will be found.
// instead if we are not at the root, let the cwd be root.
root: offset === '' ? projectRoot : '',
reporters: [...(options.reporters ?? []), 'default', nxReporter],
coverage,
};
const settings = await getSettings(options, context);
settings.reporters.push(nxReporter);

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

Expand Down Expand Up @@ -98,4 +87,69 @@ export async function* vitestExecutor(
};
}

async function getSettings(
options: VitestExecutorOptions,
context: ExecutorContext
) {
const projectRoot = context.projectGraph.nodes[context.projectName].data.root;
const offset = relative(workspaceRoot, context.cwd);
// if reportsDirectory is not provides vitest will remove all files in the project root
// when coverage is enabled in the vite.config.ts
const coverage: CoverageOptions = options.reportsDirectory
? {
enabled: options.coverage,
reportsDirectory: options.reportsDirectory,
}
: {};

const viteConfigPath = options.config
? join(context.root, options.config)
: findViteConfig(join(context.root, projectRoot));

const resolved = await loadConfigFromFile(
{
mode: options.mode,
command: 'serve',
},
viteConfigPath
);

if (!viteConfigPath || !resolved?.config?.test) {
logger.warn(stripIndents`Unable to load test config from config file ${
resolved.path ?? viteConfigPath
}
Some settings may not be applied as expected.
You can manually set the config in the project, ${
context.projectName
}, configuration.
`);
}

const settings = {
...options,
// when running nx from the project root, the root will get appended to the cwd.
// creating an invalid path and no tests will be found.
// instead if we are not at the root, let the cwd be root.
root: offset === '' ? projectRoot : '',
reporters: [
...(options.reporters ?? []),
...((resolved?.config?.test?.reporters as string[]) ?? []),
'default',
] as (string | Reporter)[],
coverage: { ...resolved?.config?.test?.coverage, ...coverage },
};

return settings;
}

function findViteConfig(projectRootFullPath: string): string {
const allowsExt = ['js', 'mjs', 'ts', 'cjs', 'mts', 'cts'];

for (const ext of allowsExt) {
if (existsSync(join(projectRootFullPath, `vite.config.${ext}`))) {
return join(projectRootFullPath, `vite.config.${ext}`);
}
}
}

export default vitestExecutor;

0 comments on commit cb5f4fd

Please sign in to comment.