fix(testing): do not set vitest root when not in workspaceRoot (#14362)

This commit is contained in:
Caleb Ukle 2023-01-20 15:58:03 -06:00 committed by GitHub
parent f1f69df470
commit 7857ae00a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 3 deletions

View File

@ -592,7 +592,7 @@ export function runCommandAsync(
exec( exec(
command, command,
{ {
cwd: tmpProjPath(), cwd: opts.cwd || tmpProjPath(),
env: { env: {
CI: 'true', CI: 'true',
...(opts.env || getStrippedEnvironmentVariables()), ...(opts.env || getStrippedEnvironmentVariables()),

View File

@ -205,6 +205,14 @@ describe('Vite Plugin', () => {
expect(result.combinedOutput).toContain( expect(result.combinedOutput).toContain(
`Successfully ran target test for project ${lib}` `Successfully ran target test for project ${lib}`
); );
// TODO(caleb): run tests from project root and make sure they still work
const nestedResults = await runCLIAsync(`test ${lib} --skip-nx-cache`, {
cwd: `${tmpProjPath()}/libs/${lib}`,
});
expect(nestedResults.combinedOutput).toContain(
`Successfully ran target test for project ${lib}`
);
}, 100_000); }, 100_000);
it('should collect coverage', () => { it('should collect coverage', () => {

View File

@ -1,6 +1,7 @@
import { ExecutorContext } from '@nrwl/devkit'; import { ExecutorContext, workspaceRoot } from '@nrwl/devkit';
import { File, Reporter } from 'vitest'; import { File, Reporter } from 'vitest';
import { VitestExecutorOptions } from './schema'; import { VitestExecutorOptions } from './schema';
import { relative } from 'path';
class NxReporter implements Reporter { class NxReporter implements Reporter {
deferred: { deferred: {
@ -46,11 +47,15 @@ export default async function* runExecutor(
)() as Promise<typeof import('vitest/node')>); )() as Promise<typeof import('vitest/node')>);
const projectRoot = context.projectGraph.nodes[context.projectName].data.root; const projectRoot = context.projectGraph.nodes[context.projectName].data.root;
const offset = relative(workspaceRoot, context.cwd);
const nxReporter = new NxReporter(options.watch); const nxReporter = new NxReporter(options.watch);
const settings = { const settings = {
...options, ...options,
root: projectRoot, // 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], reporters: [...(options.reporters ?? []), 'default', nxReporter],
// if reportsDirectory is not provides vitest will remove all files in the project root // if reportsDirectory is not provides vitest will remove all files in the project root
// when coverage is enabled in the vite.config.ts // when coverage is enabled in the vite.config.ts