Skip to content

Commit

Permalink
fix(vite): update nx.json for vitest (#13606)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicholas Cunningham <ndcunningham>
  • Loading branch information
ndcunningham committed Dec 2, 2022
1 parent 67c7822 commit 32f60f1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
34 changes: 33 additions & 1 deletion packages/vite/src/generators/init/init.spec.ts
@@ -1,4 +1,10 @@
import { addDependenciesToPackageJson, readJson, Tree } from '@nrwl/devkit';
import {
addDependenciesToPackageJson,
NxJsonConfiguration,
readJson,
Tree,
updateJson,
} from '@nrwl/devkit';
import { createTreeWithEmptyV1Workspace } from '@nrwl/devkit/testing';
import { nxVersion } from '../../utils/versions';

Expand Down Expand Up @@ -28,4 +34,30 @@ describe('@nrwl/vite:init', () => {
expect(packageJson).toMatchSnapshot();
});
});

describe('vitest targets', () => {
it('should add target defaults for test', async () => {
updateJson<NxJsonConfiguration>(tree, 'nx.json', (json) => {
json.namedInputs ??= {};
json.namedInputs.production = ['default'];
return json;
});

initGenerator(tree, { uiFramework: 'react' });

const productionNamedInputs = readJson(tree, 'nx.json').namedInputs
.production;
const testDefaults = readJson(tree, 'nx.json').targetDefaults.test;

expect(productionNamedInputs).toContain(
'!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?(.snap)'
);
expect(productionNamedInputs).toContain(
'!{projectRoot}/tsconfig.spec.json'
);
expect(testDefaults).toEqual({
inputs: ['default', '^production'],
});
});
});
});
28 changes: 28 additions & 0 deletions packages/vite/src/generators/init/init.ts
Expand Up @@ -2,8 +2,10 @@ import {
addDependenciesToPackageJson,
convertNxGenerator,
readJson,
readWorkspaceConfiguration,
Tree,
updateJson,
updateWorkspaceConfiguration,
} from '@nrwl/devkit';

import {
Expand Down Expand Up @@ -60,8 +62,34 @@ function moveToDevDependencies(tree: Tree) {
});
}

export function createVitestConfig(tree: Tree) {
const workspaceConfiguration = readWorkspaceConfiguration(tree);

const productionFileSet = workspaceConfiguration.namedInputs?.production;
if (productionFileSet) {
productionFileSet.push(
'!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?(.snap)',
'!{projectRoot}/tsconfig.spec.json'
);

workspaceConfiguration.namedInputs.production = Array.from(
new Set(productionFileSet)
);
}

workspaceConfiguration.targetDefaults ??= {};
workspaceConfiguration.targetDefaults.test ??= {};
workspaceConfiguration.targetDefaults.test.inputs ??= [
'default',
productionFileSet ? '^production' : '^default',
];

updateWorkspaceConfiguration(tree, workspaceConfiguration);
}

export function initGenerator(tree: Tree, schema: Schema) {
moveToDevDependencies(tree);
createVitestConfig(tree);
const installTask = checkDependenciesInstalled(tree, schema);
return installTask;
}
Expand Down

0 comments on commit 32f60f1

Please sign in to comment.