diff --git a/e2e/utils/index.ts b/e2e/utils/index.ts index b9ddf2b25fb56..1dd66f196ff51 100644 --- a/e2e/utils/index.ts +++ b/e2e/utils/index.ts @@ -592,7 +592,7 @@ export function runCommandAsync( exec( command, { - cwd: tmpProjPath(), + cwd: opts.cwd || tmpProjPath(), env: { CI: 'true', ...(opts.env || getStrippedEnvironmentVariables()), diff --git a/e2e/vite/src/vite.test.ts b/e2e/vite/src/vite.test.ts index f1832e42cd541..fa4d6ab534899 100644 --- a/e2e/vite/src/vite.test.ts +++ b/e2e/vite/src/vite.test.ts @@ -205,6 +205,14 @@ describe('Vite Plugin', () => { expect(result.combinedOutput).toContain( `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); it('should collect coverage', () => { diff --git a/packages/vite/src/executors/test/vitest.impl.ts b/packages/vite/src/executors/test/vitest.impl.ts index fd496dcb20250..4e0da54c29b18 100644 --- a/packages/vite/src/executors/test/vitest.impl.ts +++ b/packages/vite/src/executors/test/vitest.impl.ts @@ -1,6 +1,7 @@ -import { ExecutorContext } from '@nrwl/devkit'; +import { ExecutorContext, workspaceRoot } from '@nrwl/devkit'; import { File, Reporter } from 'vitest'; import { VitestExecutorOptions } from './schema'; +import { relative } from 'path'; class NxReporter implements Reporter { deferred: { @@ -46,11 +47,15 @@ export default async function* runExecutor( )() as Promise); const projectRoot = context.projectGraph.nodes[context.projectName].data.root; + const offset = relative(workspaceRoot, context.cwd); const nxReporter = new NxReporter(options.watch); const settings = { ...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], // if reportsDirectory is not provides vitest will remove all files in the project root // when coverage is enabled in the vite.config.ts