fix(react): respect unitTestRunner passed to the generator (#23383)

closes: #22276
This commit is contained in:
Nicholas Cunningham 2024-05-16 14:59:57 -06:00 committed by GitHub
parent e4a4121ca4
commit 2e630568eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 26 additions and 74 deletions

View File

@ -163,7 +163,6 @@ describe('nx init (for React - legacy)', () => {
); );
const packageJson = readJson('package.json'); const packageJson = readJson('package.json');
expect(packageJson.devDependencies['@nx/jest']).toBeUndefined();
expect(packageJson.dependencies['redux']).toBeDefined(); expect(packageJson.dependencies['redux']).toBeDefined();
expect(packageJson.name).toEqual(appName); expect(packageJson.name).toEqual(appName);

View File

@ -151,4 +151,20 @@ describe('Build React applications and libraries with Vite', () => {
`dist/libs/${nonBuildableLib}/index.mjs` `dist/libs/${nonBuildableLib}/index.mjs`
); );
}, 300_000); }, 300_000);
it('should support bundling with Vite and Jest', async () => {
const viteApp = uniq('viteapp');
runCLI(
`generate @nx/react:app ${viteApp} --bundler=vite --unitTestRunner=jest --no-interactive`
);
const appTestResults = await runCLIAsync(`test ${viteApp}`);
expect(appTestResults.combinedOutput).toContain(
'Successfully ran target test'
);
await runCLIAsync(`build ${viteApp}`);
checkFilesExist(`dist/apps/${viteApp}/index.html`);
}, 300_000);
}); });

View File

@ -28,7 +28,7 @@ describe('Web Components Applications with bundler set as vite', () => {
const testResults = await runCLIAsync(`test ${appName}`); const testResults = await runCLIAsync(`test ${appName}`);
expect(testResults.combinedOutput).toContain('Tests 2 passed (2)'); expect(testResults.combinedOutput).toContain(`PASS ${appName}`);
const lintE2eResults = runCLI(`lint ${appName}-e2e`); const lintE2eResults = runCLI(`lint ${appName}-e2e`);

View File

@ -38,19 +38,6 @@ nxViteTsPaths()],
}, },
test: {
globals: true,
cache: {
dir: '../node_modules/.vitest/my-vite-app'
},
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
reporters: ['default'],
coverage: {
reportsDirectory: '../coverage/my-vite-app',
provider: 'v8',
}
},
});" });"
`; `;

View File

@ -126,20 +126,7 @@ nxViteTsPaths()],
}, },
test: {
globals: true,
cache: {
dir: '../node_modules/.vitest/my-app'
},
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
reporters: ['default'],
coverage: {
reportsDirectory: '../coverage/my-app',
provider: 'v8',
}
},
});" });"
`; `;
@ -217,20 +204,7 @@ nxViteTsPaths()],
}, },
test: {
globals: true,
cache: {
dir: '../node_modules/.vitest/my-app'
},
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
reporters: ['default'],
coverage: {
reportsDirectory: '../coverage/my-app',
provider: 'v8',
}
},
});" });"
`; `;
@ -355,20 +329,7 @@ nxViteTsPaths()],
}, },
test: {
globals: true,
cache: {
dir: '../node_modules/.vitest/my-app'
},
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
reporters: ['default'],
coverage: {
reportsDirectory: '../coverage/my-app',
provider: 'v8',
}
},
});" });"
`; `;

View File

@ -95,10 +95,6 @@ export async function normalizeOptions<T extends Schema = Schema>(
assertValidStyle(options.style); assertValidStyle(options.style);
if (options.bundler === 'vite' && options.unitTestRunner !== 'none') {
options.unitTestRunner = 'vitest';
}
const normalized = { const normalized = {
...options, ...options,
name: names(options.name).fileName, name: names(options.name).fileName,

View File

@ -202,12 +202,12 @@ describe('web app generator (legacy)', () => {
}, },
}, },
"test": { "test": {
"executor": "@nx/vite:test", "executor": "@nx/jest:jest",
"options": { "options": {
"reportsDirectory": "../coverage/my-vite-app", "jestConfig": "my-vite-app/jest.config.ts",
}, },
"outputs": [ "outputs": [
"{options.reportsDirectory}", "{workspaceRoot}/coverage/{projectRoot}",
], ],
}, },
}, },

View File

@ -425,7 +425,7 @@ describe('app', () => {
// Updated this test to match the way we do this for React // Updated this test to match the way we do this for React
// When user chooses Vite as bundler and they choose to generate unit tests // When user chooses Vite as bundler and they choose to generate unit tests
// then use vitest // then use vitest
it('--bundler=vite --unitTestRunner=jest - still generate with vitest', async () => { it('--bundler=vite --unitTestRunner=jest respects unitTestRunner given', async () => {
await applicationGenerator(tree, { await applicationGenerator(tree, {
name: 'my-vite-app', name: 'my-vite-app',
@ -435,10 +435,7 @@ describe('app', () => {
addPlugin: true, addPlugin: true,
}); });
expect(tree.exists('my-vite-app/vite.config.ts')).toBeTruthy(); expect(tree.exists('my-vite-app/vite.config.ts')).toBeTruthy();
expect(tree.read('my-vite-app/vite.config.ts', 'utf-8')).toContain( expect(tree.exists('my-vite-app/jest.config.ts')).toBeTruthy();
'test: {'
);
expect(tree.exists('my-vite-app/jest.config.ts')).toBeFalsy();
}); });
it('--bundler=vite --unitTestRunner=none', async () => { it('--bundler=vite --unitTestRunner=none', async () => {

View File

@ -522,10 +522,6 @@ async function normalizeOptions(
? options.tags.split(',').map((s) => s.trim()) ? options.tags.split(',').map((s) => s.trim())
: []; : [];
if (options.bundler === 'vite' && options.unitTestRunner !== 'none') {
options.unitTestRunner = 'vitest';
}
options.style = options.style || 'css'; options.style = options.style || 'css';
options.linter = options.linter || ('eslint' as Linter.EsLint); options.linter = options.linter || ('eslint' as Linter.EsLint);
options.unitTestRunner = options.unitTestRunner || 'jest'; options.unitTestRunner = options.unitTestRunner || 'jest';