feat(remix): generate vitest file instead vite.config (#21100)

This commit is contained in:
Colum Ferry 2024-01-12 18:27:21 +00:00 committed by GitHub
parent 1f308bf736
commit 08bc36691f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 24 deletions

View File

@ -60,7 +60,7 @@ describe('remix e2e', () => {
const plugin = uniq('remix'); const plugin = uniq('remix');
const appName = `sub-${plugin}`; const appName = `sub-${plugin}`;
runCLI( runCLI(
`generate @nx/remix:app ${plugin} --directory=sub --projectNameAndRootFormat=derived --rootProject=false` `generate @nx/remix:app ${plugin} --directory=sub --projectNameAndRootFormat=derived --rootProject=false --no-interactive`
); );
const project = readJson(`sub/${plugin}/project.json`); const project = readJson(`sub/${plugin}/project.json`);
expect(project.targets.build.options.outputPath).toEqual( expect(project.targets.build.options.outputPath).toEqual(
@ -74,7 +74,7 @@ describe('remix e2e', () => {
it('should create src in the specified directory --projectNameAndRootFormat=as-provided', async () => { it('should create src in the specified directory --projectNameAndRootFormat=as-provided', async () => {
const plugin = uniq('remix'); const plugin = uniq('remix');
runCLI( runCLI(
`generate @nx/remix:app ${plugin} --directory=subdir --projectNameAndRootFormat=as-provided --rootProject=false` `generate @nx/remix:app ${plugin} --directory=subdir --projectNameAndRootFormat=as-provided --rootProject=false --no-interactive`
); );
const project = readJson(`subdir/project.json`); const project = readJson(`subdir/project.json`);
expect(project.targets.build.options.outputPath).toEqual(`dist/subdir`); expect(project.targets.build.options.outputPath).toEqual(`dist/subdir`);

View File

@ -66,7 +66,7 @@ describe('Remix Application', () => {
expectTargetsToBeCorrect(tree, '.'); expectTargetsToBeCorrect(tree, '.');
expect(tree.read('remix.config.cjs', 'utf-8')).toMatchSnapshot(); expect(tree.read('remix.config.cjs', 'utf-8')).toMatchSnapshot();
expect(tree.read('vite.config.ts', 'utf-8')).toMatchSnapshot(); expect(tree.read('vitest.config.ts', 'utf-8')).toMatchSnapshot();
expect( expect(
tree.read('tests/routes/_index.spec.tsx', 'utf-8') tree.read('tests/routes/_index.spec.tsx', 'utf-8')
).toMatchSnapshot(); ).toMatchSnapshot();
@ -248,7 +248,7 @@ describe('Remix Application', () => {
tree.read(`${appDir}/remix.config.cjs`, 'utf-8') tree.read(`${appDir}/remix.config.cjs`, 'utf-8')
).toMatchSnapshot(); ).toMatchSnapshot();
expect( expect(
tree.read(`${appDir}/vite.config.ts`, 'utf-8') tree.read(`${appDir}/vitest.config.ts`, 'utf-8')
).toMatchSnapshot(); ).toMatchSnapshot();
expect( expect(
tree.read(`${appDir}/test-setup.ts`, 'utf-8') tree.read(`${appDir}/test-setup.ts`, 'utf-8')

View File

@ -137,10 +137,9 @@ export default async function (tree: Tree, _options: NxRemixGeneratorSchema) {
if (options.unitTestRunner !== 'none') { if (options.unitTestRunner !== 'none') {
if (options.unitTestRunner === 'vitest') { if (options.unitTestRunner === 'vitest') {
const { vitestGenerator } = ensurePackage<typeof import('@nx/vite')>( const { vitestGenerator, createOrEditViteConfig } = ensurePackage<
'@nx/vite', typeof import('@nx/vite')
getPackageVersion(tree, 'nx') >('@nx/vite', getPackageVersion(tree, 'nx'));
);
const vitestTask = await vitestGenerator(tree, { const vitestTask = await vitestGenerator(tree, {
uiFramework: 'react', uiFramework: 'react',
project: options.projectName, project: options.projectName,
@ -148,7 +147,22 @@ export default async function (tree: Tree, _options: NxRemixGeneratorSchema) {
inSourceTests: false, inSourceTests: false,
skipFormat: true, skipFormat: true,
testEnvironment: 'jsdom', testEnvironment: 'jsdom',
skipViteConfig: true,
}); });
createOrEditViteConfig(
tree,
{
project: options.projectName,
includeLib: false,
includeVitest: true,
testEnvironment: 'jsdom',
imports: [`import react from '@vitejs/plugin-react';`],
plugins: [`react()`],
},
true,
undefined,
true
);
tasks.push(vitestTask); tasks.push(vitestTask);
} else { } else {
const { configurationGenerator: jestConfigurationGenerator } = const { configurationGenerator: jestConfigurationGenerator } =

View File

@ -7,8 +7,8 @@ import {
} from '@nx/devkit'; } from '@nx/devkit';
import { import {
updateJestTestSetup, updateJestTestSetup,
updateViteTestIncludes, updateVitestTestIncludes,
updateViteTestSetup, updateVitestTestSetup,
} from '../../../utils/testing-config-utils'; } from '../../../utils/testing-config-utils';
import { import {
getRemixVersion, getRemixVersion,
@ -32,18 +32,18 @@ export function updateUnitTestConfig(
); );
if (unitTestRunner === 'vitest') { if (unitTestRunner === 'vitest') {
const pathToViteConfig = joinPathFragments(pathToRoot, 'vite.config.ts'); const pathToViteConfig = joinPathFragments(pathToRoot, 'vitest.config.ts');
updateViteTestIncludes( updateVitestTestIncludes(
tree, tree,
pathToViteConfig, pathToViteConfig,
'./app/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}' './app/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'
); );
updateViteTestIncludes( updateVitestTestIncludes(
tree, tree,
pathToViteConfig, pathToViteConfig,
'./tests/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}' './tests/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'
); );
updateViteTestSetup(tree, pathToViteConfig, 'test-setup.ts'); updateVitestTestSetup(tree, pathToViteConfig, 'test-setup.ts');
} else if (unitTestRunner === 'jest') { } else if (unitTestRunner === 'jest') {
const pathToJestConfig = joinPathFragments(pathToRoot, 'jest.config.ts'); const pathToJestConfig = joinPathFragments(pathToRoot, 'jest.config.ts');
tree.rename('jest.preset.js', 'jest.preset.cjs'); tree.rename('jest.preset.js', 'jest.preset.cjs');

View File

@ -6,7 +6,7 @@ import {
} from '@nx/devkit'; } from '@nx/devkit';
import { import {
updateJestTestSetup, updateJestTestSetup,
updateViteTestSetup, updateVitestTestSetup,
} from '../../../utils/testing-config-utils'; } from '../../../utils/testing-config-utils';
import { import {
getRemixVersion, getRemixVersion,
@ -40,7 +40,7 @@ export function addUnitTestingSetup(tree: Tree, options: RemixLibraryOptions) {
options.projectRoot, options.projectRoot,
`vite.config.ts` `vite.config.ts`
); );
updateViteTestSetup(tree, pathToVitestConfig, './src/test-setup.ts'); updateVitestTestSetup(tree, pathToVitestConfig, './src/test-setup.ts');
} else if (options.unitTestRunner === 'jest') { } else if (options.unitTestRunner === 'jest') {
const pathToJestConfig = joinPathFragments( const pathToJestConfig = joinPathFragments(
options.projectRoot, options.projectRoot,

View File

@ -3,16 +3,16 @@ import { ensureTypescript } from '@nx/js/src/utils/typescript/ensure-typescript'
let tsModule: typeof import('typescript'); let tsModule: typeof import('typescript');
export function updateViteTestSetup( export function updateVitestTestSetup(
tree: Tree, tree: Tree,
pathToViteConfig: string, pathToVitestConfig: string,
pathToTestSetup: string pathToTestSetup: string
) { ) {
if (!tsModule) { if (!tsModule) {
tsModule = ensureTypescript(); tsModule = ensureTypescript();
} }
const { tsquery } = require('@phenomnomnominal/tsquery'); const { tsquery } = require('@phenomnomnominal/tsquery');
const fileContents = tree.read(pathToViteConfig, 'utf-8'); const fileContents = tree.read(pathToVitestConfig, 'utf-8');
const ast = tsquery.ast(fileContents); const ast = tsquery.ast(fileContents);
@ -50,7 +50,7 @@ export function updateViteTestSetup(
} }
} }
tree.write(pathToViteConfig, updatedFileContents); tree.write(pathToVitestConfig, updatedFileContents);
} }
export function updateJestTestSetup( export function updateJestTestSetup(
@ -100,16 +100,16 @@ export function updateJestTestSetup(
} }
} }
export function updateViteTestIncludes( export function updateVitestTestIncludes(
tree: Tree, tree: Tree,
pathToViteConfig: string, pathToVitestConfig: string,
includesString: string includesString: string
) { ) {
if (!tsModule) { if (!tsModule) {
tsModule = ensureTypescript(); tsModule = ensureTypescript();
} }
const { tsquery } = require('@phenomnomnominal/tsquery'); const { tsquery } = require('@phenomnomnominal/tsquery');
const fileContents = tree.read(pathToViteConfig, 'utf-8'); const fileContents = tree.read(pathToVitestConfig, 'utf-8');
const ast = tsquery.ast(fileContents); const ast = tsquery.ast(fileContents);
@ -123,6 +123,6 @@ export function updateViteTestIncludes(
nodes[0].getStart() nodes[0].getStart()
)}include: ["${includesString}"]${fileContents.slice(nodes[0].getEnd())}`; )}include: ["${includesString}"]${fileContents.slice(nodes[0].getEnd())}`;
tree.write(pathToViteConfig, updatedFileContents); tree.write(pathToVitestConfig, updatedFileContents);
} }
} }