Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ export function runCommandAsync(
exec(
command,
{
cwd: tmpProjPath(),
cwd: opts.cwd || tmpProjPath(),
env: {
CI: 'true',
...(opts.env || getStrippedEnvironmentVariables()),
Expand Down
8 changes: 8 additions & 0 deletions e2e/vite/src/vite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
9 changes: 7 additions & 2 deletions packages/vite/src/executors/test/vitest.impl.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -46,11 +47,15 @@ export default async function* runExecutor(
)() 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);
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
Expand Down