Skip to content

Commit 0492be6

Browse files
committed
chore(nx-plugin): fixup code
1 parent d38b390 commit 0492be6

13 files changed

Lines changed: 755 additions & 31 deletions

File tree

e2e/plugin/src/nx-plugin.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ProjectConfiguration } from '@nx/devkit';
22
import {
33
checkFilesExist,
4+
checkFilesMatchingPatternExist,
45
cleanupProject,
56
createFile,
67
expectTestsPass,
@@ -71,13 +72,12 @@ describe('Nx Plugin', () => {
7172
);
7273

7374
// Verify vitest config was created
74-
const vitestConfigExists =
75-
checkFilesExist(`${plugin}-e2e/vitest.config.ts`, false) ||
76-
checkFilesExist(`${plugin}-e2e/vitest.config.mts`, false);
77-
expect(vitestConfigExists).toBeTruthy();
75+
checkFilesMatchingPatternExist(`${plugin}-e2e/vitest.config.(ts|mts)`);
7876

7977
// Run the e2e tests with vitest
80-
runCLI(`e2e ${plugin}-e2e`);
78+
expect(() => {
79+
runCLI(`e2e ${plugin}-e2e`);
80+
}).not.toThrow();
8181
}, 120000);
8282

8383
it('should be able to generate a migration', async () => {

packages/plugin/src/generators/e2e-project/e2e.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import 'nx/src/internal-testing-utils/mock-project-graph';
33
import {
44
Tree,
55
addProjectConfiguration,
6-
readProjectConfiguration,
7-
readJson,
86
getProjects,
9-
writeJson,
7+
readJson,
8+
readProjectConfiguration,
109
updateJson,
10+
writeJson,
1111
} from '@nx/devkit';
1212
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
1313
import { e2eProjectGenerator } from './e2e';
@@ -217,7 +217,7 @@ describe('NxPlugin e2e-project Generator', () => {
217217
pluginOutputPath: `dist/libs/my-plugin`,
218218
npmPackageName: '@proj/my-plugin',
219219
testRunner: 'vitest',
220-
addPlugin: true,
220+
addPlugin: false,
221221
});
222222

223223
const project = readProjectConfiguration(tree, 'my-plugin-e2e');

packages/plugin/src/generators/e2e-project/e2e.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
addProjectToTsSolutionWorkspace,
3434
isUsingTsSolutionSetup,
3535
} from '@nx/js/src/utils/typescript/ts-solution-setup';
36-
import type { VitestGeneratorSchema } from '@nx/vitest';
36+
import type { VitestGeneratorSchema } from '@nx/vitest/generators';
3737
import type { PackageJson } from 'nx/src/utils/package-json';
3838
import { join } from 'path';
3939
import type { Schema } from './schema';
@@ -238,17 +238,22 @@ async function addVitest(host: Tree, options: NormalizedSchema) {
238238
skipFormat: true,
239239
addPlugin: options.addPlugin,
240240
testEnvironment: 'node',
241+
coverageProvider: 'none',
241242
} satisfies Partial<VitestGeneratorSchema>);
242243

243-
addLocalRegistryScripts(host);
244+
const { startLocalRegistryPath, stopLocalRegistryPath } =
245+
addLocalRegistryScripts(host);
244246

245247
// Add globalSetup and globalTeardown to vitest config
246248
// Check for both .mts and .ts extensions (mts is checked first as it's the default created by @nx/vitest)
247249
const vitestConfigExtensions = ['mts', 'ts'];
248250
let vitestConfigPath: string | undefined;
249251

250252
for (const ext of vitestConfigExtensions) {
251-
const configPath = joinPathFragments(options.projectRoot);
253+
const configPath = joinPathFragments(
254+
options.projectRoot,
255+
`vitest.config.${ext}`
256+
);
252257
if (host.exists(configPath)) {
253258
vitestConfigPath = configPath;
254259
break;
@@ -261,11 +266,15 @@ async function addVitest(host: Tree, options: NormalizedSchema) {
261266
offsetFromRoot(options.projectRoot),
262267
startLocalRegistryPath
263268
);
264-
const globalTeardownPath = join(offsetFromRoot(options.projectRoot));
269+
const globalTeardownPath = join(
270+
offsetFromRoot(options.projectRoot),
271+
stopLocalRegistryPath
272+
);
265273

266274
// Insert globalSetup and globalTeardown in the test config
267275
// Look for 'test: {' and insert our properties right after the opening brace
268276
const testConfigRegex = /(test:\s*\{\s*)/;
277+
const match = testConfigRegex.exec(vitestConfig);
269278

270279
if (match) {
271280
// Extract the indentation from the next line to maintain consistent formatting

packages/plugin/src/generators/e2e-project/files/src/__simplePluginName__.spec.ts__tmpl__

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('<%= pluginName %>', () => {
1515
stdio: 'inherit',
1616
env: process.env,
1717
});
18-
});
18+
}, 30_000);
1919

2020
afterAll(() => {
2121
if (projectDirectory) {

packages/plugin/tsconfig.lib.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424
{
2525
"path": "../jest/tsconfig.lib.json"
2626
},
27+
{
28+
"path": "../devkit/tsconfig.lib.json"
29+
},
2730
{
2831
"path": "../nx/tsconfig.lib.json"
2932
},
3033
{
31-
"path": "../devkit/tsconfig.lib.json"
34+
"path": "../vitest/tsconfig.lib.json"
3235
}
3336
]
3437
}

packages/react-native/src/utils/add-jest.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { Tree, ensurePackage, offsetFromRoot } from '@nx/devkit';
1+
import {
2+
Tree,
3+
ensurePackage,
4+
offsetFromRoot,
5+
type GeneratorCallback,
6+
} from '@nx/devkit';
27
import { nxVersion } from './versions';
38

49
export async function addJest(
@@ -10,7 +15,7 @@ export async function addJest(
1015
skipPackageJson: boolean,
1116
addPlugin: boolean,
1217
runtimeTsconfigFileName: string
13-
) {
18+
): Promise<GeneratorCallback> {
1419
if (unitTestRunner !== 'jest') {
1520
return () => {};
1621
}

packages/vitest/src/generators/configuration/configuration.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ import {
2222
} from '@nx/js/src/utils/typescript/ts-solution-setup';
2323
import { typesNodeVersion } from '@nx/js/src/utils/versions';
2424
import { join } from 'path';
25+
import { clean, coerce, major } from 'semver';
26+
import { detectUiFramework } from '../../utils/detect-ui-framework';
2527
import { ensureDependencies } from '../../utils/ensure-dependencies';
2628
import {
2729
addOrChangeTestTarget,
2830
createOrEditViteConfig,
2931
} from '../../utils/generator-utils';
30-
import initGenerator from '../init/init';
31-
import { VitestGeneratorSchema } from './schema';
32-
import { detectUiFramework } from '../../utils/detect-ui-framework';
3332
import {
3433
getInstalledViteMajorVersion,
3534
getVitestDependenciesVersionsToInstall,
3635
} from '../../utils/version-utils';
37-
import { clean, coerce, major } from 'semver';
36+
import initGenerator from '../init/init';
37+
import { VitestGeneratorSchema } from './schema';
3838

3939
/**
4040
* Determines whether to use vitest.config.mts instead of vite.config.mts.
@@ -210,7 +210,10 @@ getTestBed().initTestEnvironment(
210210
: `import react from '@vitejs/plugin-react'`,
211211
],
212212
plugins: ['react()'],
213-
coverageProvider: schema.coverageProvider,
213+
coverageProvider:
214+
schema.coverageProvider === 'none'
215+
? undefined
216+
: schema.coverageProvider,
214217
useEsmExtension: true,
215218
},
216219
true,
@@ -448,6 +451,8 @@ async function getCoverageProviderDependency(
448451
return {
449452
'@vitest/coverage-istanbul': vitestCoverageIstanbul,
450453
};
454+
case 'none':
455+
return {};
451456
default:
452457
return {
453458
'@vitest/coverage-v8': vitestCoverageV8,

packages/vitest/src/generators/configuration/schema.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export interface VitestGeneratorSchema {
22
project: string;
33
uiFramework?: 'angular' | 'react' | 'vue' | 'none';
4-
coverageProvider: 'v8' | 'istanbul' | 'custom';
4+
coverageProvider: 'v8' | 'istanbul' | 'custom' | 'none';
55
inSourceTests?: boolean;
66
skipViteConfig?: boolean;
77
testTarget?: string;

packages/vitest/src/generators/configuration/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
},
3131
"coverageProvider": {
3232
"type": "string",
33-
"enum": ["v8", "istanbul", "custom"],
33+
"enum": ["v8", "istanbul", "custom", "none"],
3434
"default": "v8",
3535
"description": "Coverage provider to use."
3636
},

0 commit comments

Comments
 (0)