Skip to content

Commit c46b24c

Browse files
barbados-clemensFrozenPandaz
authored andcommitted
fix(testing): do not set vitest root when not in workspaceRoot (#14362)
(cherry picked from commit 7857ae0)
1 parent 39b5500 commit c46b24c

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

e2e/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ export function runCommandAsync(
586586
exec(
587587
command,
588588
{
589-
cwd: tmpProjPath(),
589+
cwd: opts.cwd || tmpProjPath(),
590590
env: {
591591
CI: 'true',
592592
...(opts.env || getStrippedEnvironmentVariables()),

e2e/vite/src/vite.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ describe('Vite Plugin', () => {
205205
expect(result.combinedOutput).toContain(
206206
`Successfully ran target test for project ${lib}`
207207
);
208+
209+
// TODO(caleb): run tests from project root and make sure they still work
210+
const nestedResults = await runCLIAsync(`test ${lib} --skip-nx-cache`, {
211+
cwd: `${tmpProjPath()}/libs/${lib}`,
212+
});
213+
expect(nestedResults.combinedOutput).toContain(
214+
`Successfully ran target test for project ${lib}`
215+
);
208216
}, 100_000);
209217

210218
it('should collect coverage', () => {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { ExecutorContext } from '@nrwl/devkit';
1+
import { ExecutorContext, workspaceRoot } from '@nrwl/devkit';
22
import { File, Reporter } from 'vitest';
33
import { VitestExecutorOptions } from './schema';
4+
import { relative } from 'path';
45

56
class NxReporter implements Reporter {
67
deferred: {
@@ -46,11 +47,15 @@ export default async function* runExecutor(
4647
)() as Promise<typeof import('vitest/node')>);
4748

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

5052
const nxReporter = new NxReporter(options.watch);
5153
const settings = {
5254
...options,
53-
root: projectRoot,
55+
// when running nx from the project root, the root will get appended to the cwd.
56+
// creating an invalid path and no tests will be found.
57+
// instead if we are not at the root, let the cwd be root.
58+
root: offset === '' ? projectRoot : '',
5459
reporters: [...(options.reporters ?? []), 'default', nxReporter],
5560
// if reportsDirectory is not provides vitest will remove all files in the project root
5661
// when coverage is enabled in the vite.config.ts

0 commit comments

Comments
 (0)