Skip to content

Commit

Permalink
feat(nuxt): updating the way test is handled
Browse files Browse the repository at this point in the history
  • Loading branch information
mandarini committed Nov 10, 2023
1 parent fa509d7 commit 76a1b1f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 62 deletions.
8 changes: 7 additions & 1 deletion packages/nuxt/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ export async function applicationGenerator(tree: Tree, schema: Schema) {
);

if (options.unitTestRunner === 'vitest') {
addVitest(tree, options, options.appProjectRoot, projectOffsetFromRoot);
addVitest(
tree,
options,
options.appProjectRoot,
projectOffsetFromRoot,
hasPlugin
);
}

tasks.push(await addE2e(tree, options));
Expand Down
25 changes: 11 additions & 14 deletions packages/nuxt/src/generators/application/lib/add-vitest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export function addVitest(
tree: Tree,
options: NormalizedSchema,
projectRoot: string,
projectOffsetFromRoot: string
projectOffsetFromRoot: string,
hasPlugin: boolean
) {
addDependenciesToPackageJson(
tree,
Expand All @@ -34,14 +35,6 @@ export function addVitest(
}
);

const nxJson = readNxJson(tree);
const hasPlugin = nxJson.plugins?.some((p) =>
typeof p === 'string'
? p === '@nx/nuxt/plugin'
: p.plugin === '@nx/nuxt/plugin' &&
p?.options?.['testTargetName'] === undefined
);

if (!hasPlugin) {
const projectConfig = readProjectConfiguration(tree, options.name);
projectConfig.targets['test'] = {
Expand All @@ -63,15 +56,19 @@ export function addVitest(
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
export default defineVitestConfig({
plugins: [nxViteTsPaths()],
test: {
plugins: [nxViteTsPaths()],
test: {
globals: true,
cache: {
dir: '${projectOffsetFromRoot}node_modules/.vitest',
dir: '${projectOffsetFromRoot}node_modules/.vitest',
},
include: ['${projectRoot}/src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
environment: 'nuxt',
},
include: ['${projectRoot}/src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
includeSource: ['src/**/*.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
coverage: {
reportsDirectory: '${projectOffsetFromRoot}coverage/my-test-react-app',
},
},
});
`
);
Expand Down
4 changes: 3 additions & 1 deletion packages/nuxt/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { GeneratorCallback, runTasksInSerial, Tree } from '@nx/devkit';
import { initGenerator as jsInitGenerator } from '@nx/js';

import { InitSchema } from './schema';
import { addPlugin, updateDependencies } from './lib/utils';
import { addPlugin, createVitestConfig, updateDependencies } from './lib/utils';

export async function nuxtInitGenerator(host: Tree, schema: InitSchema) {
const tasks: GeneratorCallback[] = [];
Expand All @@ -25,6 +25,8 @@ export async function nuxtInitGenerator(host: Tree, schema: InitSchema) {
addPlugin(host);
}

createVitestConfig(host);

return runTasksInSerial(...tasks);
}

Expand Down
83 changes: 37 additions & 46 deletions packages/nuxt/src/generators/init/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,63 +43,54 @@ export function updateDependencies(host: Tree, schema: InitSchema) {
return addDependenciesToPackageJson(host, {}, devDependencies);
}

export function createVitestConfig(tree: Tree) {
const nxJson = readNxJson(tree);

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

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

nxJson.targetDefaults ??= {};
nxJson.targetDefaults['@nx/vite:test'] ??= {};
nxJson.targetDefaults['@nx/vite:test'].cache ??= true;
nxJson.targetDefaults['@nx/vite:test'].inputs ??= [
'default',
productionFileSet ? '^production' : '^default',
];
nxJson.targetDefaults['@nx/vite:test'].options ??= {
passWithNoTests: true,
};

updateNxJson(tree, nxJson);
}

export function addPlugin(tree: Tree) {
const nxJson = readNxJson(tree);
nxJson.plugins ??= [];

let addNuxt = true;
let addVitest = true;
let vitePlugin: PluginConfiguration | undefined | string;
let vitePluginIndex: number | undefined;
for (const [index, plugin] of nxJson.plugins.entries()) {
for (const plugin of nxJson.plugins) {
if (
typeof plugin === 'string'
? plugin === '@nx/nuxt/plugin'
: plugin.plugin === '@nx/nuxt/plugin'
) {
addNuxt = false;
return;
}

if (
typeof plugin === 'string'
? plugin === '@nx/vite/plugin'
: plugin.plugin === '@nx/vite/plugin'
) {
addVitest = false;
vitePlugin = plugin;
vitePluginIndex = index;
}
}

if (addNuxt) {
nxJson.plugins.push({
plugin: '@nx/nuxt/plugin',
options: {
buildTargetName: 'build',
serveTargetName: 'serve',
},
});
}

// We need to do something like this for vitest:
// TODO(katerina): Enable once `@nx/vite/plugin` is released/merged
// if (addVitest) {
// nxJson.plugins.push({
// plugin: '@nx/vite/plugin',
// options: {
// buildTargetName: 'build',
// serveTargetName: 'serve',
// testTargetName: 'test',
// previewTargetName: 'preview',
// },
// });
// } else if (
// typeof vitePlugin !== 'string' &&
// vitePlugin?.options?.['testTargetName'] === undefined
// ) {
// vitePlugin.options['testTargetName'] = 'test';
// nxJson.plugins[vitePluginIndex!] = vitePlugin;
// }

nxJson.plugins.push({
plugin: '@nx/nuxt/plugin',
options: {
buildTargetName: 'build',
testTargetName: 'test',
serveTargetName: 'serve',
},
});
updateNxJson(tree, nxJson);
}

0 comments on commit 76a1b1f

Please sign in to comment.