fix(vite): update nx.json for vitest (#13606)

Co-authored-by: Nicholas Cunningham <ndcunningham>
This commit is contained in:
Nicholas Cunningham 2022-12-02 15:35:01 -07:00 committed by GitHub
parent 67c7822ad3
commit 32f60f11f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 1 deletions

View File

@ -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 { createTreeWithEmptyV1Workspace } from '@nrwl/devkit/testing';
import { nxVersion } from '../../utils/versions'; import { nxVersion } from '../../utils/versions';
@ -28,4 +34,30 @@ describe('@nrwl/vite:init', () => {
expect(packageJson).toMatchSnapshot(); 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'],
});
});
});
}); });

View File

@ -2,8 +2,10 @@ import {
addDependenciesToPackageJson, addDependenciesToPackageJson,
convertNxGenerator, convertNxGenerator,
readJson, readJson,
readWorkspaceConfiguration,
Tree, Tree,
updateJson, updateJson,
updateWorkspaceConfiguration,
} from '@nrwl/devkit'; } from '@nrwl/devkit';
import { import {
@ -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) { export function initGenerator(tree: Tree, schema: Schema) {
moveToDevDependencies(tree); moveToDevDependencies(tree);
createVitestConfig(tree);
const installTask = checkDependenciesInstalled(tree, schema); const installTask = checkDependenciesInstalled(tree, schema);
return installTask; return installTask;
} }