Skip to content

Commit

Permalink
fix(testing): vitest deleting project root with coverage enabled (#14116
Browse files Browse the repository at this point in the history
)
  • Loading branch information
barbados-clemens committed Jan 3, 2023
1 parent 0fbf50e commit 843bad1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
50 changes: 50 additions & 0 deletions e2e/vite/src/vite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,56 @@ export default defineConfig({
expect(directoryExists(coverageDir)).toBeTruthy();
}, 100_000);

it('should not delete the project directory when coverage is enabled', () => {
// when coverage is enabled in the vite.config.ts but reportsDirectory is removed
// from the @nrwl/vite:test executor options, vite will delete the project root directory
runCLI(`generate @nrwl/react:lib ${lib} --unitTestRunner=vitest`);
updateFile(`libs/${lib}/vite.config.ts`, () => {
return `import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';
export default defineConfig({
server: {
port: 4200,
host: 'localhost',
},
plugins: [
react(),
viteTsConfigPaths({
root: './',
}),
],
test: {
globals: true,
cache: {
dir: './node_modules/.vitest',
},
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
coverage: {
enabled: true,
reportsDirectory: 'coverage',
}
},
});
`;
});
updateProjectConfig(lib, (config) => {
delete config.targets.test.options.reportsDirectory;
return config;
});

const projectRoot = `${tmpProjPath()}/libs/${lib}`;

const results = runCLI(`test ${lib}`);

expect(directoryExists(projectRoot)).toBeTruthy();
expect(results).toContain(
`Successfully ran target test for project ${lib}`
);
}, 100_000);

it('should be able to run tests with inSourceTests set to true', async () => {
runCLI(
`generate @nrwl/react:lib ${lib} --unitTestRunner=vitest --inSourceTests`
Expand Down
18 changes: 13 additions & 5 deletions packages/vite/src/executors/test/vitest.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,22 @@ export default async function* runExecutor(
const projectRoot = context.projectGraph.nodes[context.projectName].data.root;

const nxReporter = new NxReporter(options.watch);
const ctx = await startVitest(options.mode, [], {
const settings = {
...options,
root: projectRoot,
reporters: [...(options.reporters ?? []), 'default', nxReporter],
coverage: {
reportsDirectory: options.reportsDirectory,
},
});
// if reportsDirectory is not provides vitest will remove all files in the project root
// when coverage is enabled in the vite.config.ts
...(options.reportsDirectory
? {
coverage: {
reportsDirectory: options.reportsDirectory,
},
}
: {}),
};

const ctx = await startVitest(options.mode, [], settings);

let hasErrors = false;

Expand Down

0 comments on commit 843bad1

Please sign in to comment.