Skip to content

Commit

Permalink
fix(testing): do not set vitest root when not in workspaceRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
barbados-clemens committed Jan 20, 2023
1 parent 1c3ebfc commit 29d003d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
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

0 comments on commit 29d003d

Please sign in to comment.