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 c2f4cf9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ exports[`app generated files content - as-provided general application should co
"executor": "@nx/vite:test",
"outputs": ["{options.reportsDirectory}"],
"options": {
"passWithNoTests": true,
"reportsDirectory": "../coverage/my-app",
"config": "my-app/vitest.config.ts"
}
Expand Down Expand Up @@ -113,8 +112,12 @@ export default defineVitestConfig({
cache: {
dir: '../node_modules/.vitest',
},
include: ['my-app/src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
environment: 'nuxt',
include: ['my-app/src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
includeSource: ['src/**/*.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
coverage: {
reportsDirectory: '../coverage/my-test-react-app',
},
},
});
"
Expand Down
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
27 changes: 13 additions & 14 deletions packages/nuxt/src/generators/application/lib/add-vitest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import {
nxVersion,
vitestVersion,
} from '../../../utils/versions';
import { addVitestTargetDefaults } from '../../init/lib/utils';

export function addVitest(
tree: Tree,
options: NormalizedSchema,
projectRoot: string,
projectOffsetFromRoot: string
projectOffsetFromRoot: string,
hasPlugin: boolean
) {
addDependenciesToPackageJson(
tree,
Expand All @@ -34,21 +36,14 @@ 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
);
addVitestTargetDefaults(tree);

if (!hasPlugin) {
const projectConfig = readProjectConfiguration(tree, options.name);
projectConfig.targets['test'] = {
executor: '@nx/vite:test',
outputs: ['{options.reportsDirectory}'],
options: {
passWithNoTests: true,
reportsDirectory: `${projectOffsetFromRoot}coverage/${projectRoot}`,
config: `${projectRoot}/vitest.config.ts`,
},
Expand All @@ -63,15 +58,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
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 addVitestTargetDefaults(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 c2f4cf9

Please sign in to comment.