Skip to content

Commit

Permalink
feat(vite): add vitest.workspace.ts at root
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Feb 21, 2024
1 parent e7826cd commit f18a723
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export default defineConfig({
"
`;

exports[`vitest generator tsconfig should add vitest.workspace.ts at the root 1`] = `
"export default ['**/*/vite.config.ts', '**/*/vitest.config.ts'];
"
`;

exports[`vitest generator vite.config should create correct vite.config.ts file for apps 1`] = `
"/// <reference types='vitest' />
import { defineConfig } from 'vite';
Expand Down
6 changes: 6 additions & 0 deletions packages/vite/src/generators/vitest/vitest-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ export async function vitestGeneratorInternal(
);
tasks.push(installCoverageProviderTask);

// Setup workspace config file (https://vitest.dev/guide/workspace.html)
tree.write(
'vitest.workspace.ts',
`export default ['**/*/vite.config.ts', '**/*/vitest.config.ts'];`
);

if (!schema.skipFormat) {
await formatFiles(tree);
}
Expand Down
43 changes: 27 additions & 16 deletions packages/vite/src/generators/vitest/vitest.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import {
Tree,
readProjectConfiguration,
updateProjectConfiguration,
} from '@nx/devkit';
import { Tree } from '@nx/devkit';

import generator from './vitest-generator';
import { VitestGeneratorSchema } from './schema';
Expand All @@ -21,14 +17,18 @@ describe('vitest generator', () => {
addPlugin: true,
};

beforeEach(() => {
appTree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
});

describe('tsconfig', () => {
it('should add a tsconfig.spec.json file', async () => {
beforeAll(async () => {
appTree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
mockReactAppGenerator(appTree);
await generator(appTree, options);
});

it('should add vitest.workspace.ts at the root', async () => {
expect(appTree.read('vitest.workspace.ts', 'utf-8')).toMatchSnapshot();
});

it('should add a tsconfig.spec.json file', async () => {
const tsconfig = JSON.parse(
appTree.read('apps/my-test-react-app/tsconfig.json')?.toString() ?? '{}'
);
Expand Down Expand Up @@ -78,11 +78,16 @@ describe('vitest generator', () => {
});

it('should add vitest/importMeta when inSourceTests is true', async () => {
mockReactAppGenerator(appTree);
await generator(appTree, { ...options, inSourceTests: true });
mockReactAppGenerator(appTree, 'my-test-react-app-2');
await generator(appTree, {
...options,
inSourceTests: true,
project: 'my-test-react-app-2',
});
const tsconfig = JSON.parse(
appTree.read('apps/my-test-react-app/tsconfig.app.json')?.toString() ??
'{}'
appTree
.read('apps/my-test-react-app-2/tsconfig.app.json')
?.toString() ?? '{}'
);
expect(tsconfig.compilerOptions.types).toMatchInlineSnapshot(`
[
Expand All @@ -93,9 +98,12 @@ describe('vitest generator', () => {
});

describe('vite.config', () => {
it('should create correct vite.config.ts file for apps', async () => {
beforeAll(async () => {
appTree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
mockReactAppGenerator(appTree);
await generator(appTree, options);
});
it('should create correct vite.config.ts file for apps', async () => {
expect(
appTree.read('apps/my-test-react-app/vite.config.ts', 'utf-8')
).toMatchSnapshot();
Expand All @@ -111,9 +119,12 @@ describe('vitest generator', () => {
});

describe('insourceTests', () => {
it('should add the insourceSource option in the vite config', async () => {
beforeAll(async () => {
appTree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
mockReactAppGenerator(appTree);
await generator(appTree, { ...options, inSourceTests: true });
});
it('should add the insourceSource option in the vite config', async () => {
expect(
appTree.read('apps/my-test-react-app/vite.config.ts', 'utf-8')
).toMatchSnapshot();
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/utils/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ export function mockViteReactAppGenerator(tree: Tree): Tree {
return tree;
}

export function mockReactAppGenerator(tree: Tree): Tree {
const appName = 'my-test-react-app';
export function mockReactAppGenerator(tree: Tree, userAppName?: string): Tree {
const appName = userAppName ?? 'my-test-react-app';

tree.write(
`apps/${appName}/src/main.tsx`,
Expand Down

0 comments on commit f18a723

Please sign in to comment.