Skip to content

Commit

Permalink
fix(testing): web and react application fixes for vitest's insourceTe…
Browse files Browse the repository at this point in the history
…sts (#13447)
  • Loading branch information
Cammisuli committed Nov 28, 2022
1 parent 5af4ae0 commit f0f60fb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
18 changes: 18 additions & 0 deletions packages/react/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1002,5 +1002,23 @@ describe('app', () => {
expect(viteAppTree.exists('/apps/my-app/index.html')).toBe(true);
expect(viteAppTree.exists('/apps/my-app/vite.config.ts')).toBe(true);
});

it('should not include a spec file when the bundler or unitTestRunner is vite and insourceTests is false', async () => {
// check to make sure that the other spec file exists
expect(viteAppTree.exists('/apps/my-app/src/app/app.spec.tsx')).toBe(
true
);

await applicationGenerator(viteAppTree, {
...schema,
name: 'insourceTests',
bundler: 'vite',
inSourceTests: true,
});

expect(
viteAppTree.exists('/apps/insourceTests/src/app/app.spec.tsx')
).toBe(false);
});
});
});
16 changes: 15 additions & 1 deletion packages/react/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ export async function applicationGenerator(host: Tree, schema: Schema) {
tasks.push(vitestTask);
}

if (
(options.bundler === 'vite' || options.unitTestRunner === 'vitest') &&
options.inSourceTests
) {
host.delete(
joinPathFragments(
options.appProjectRoot,
`src/app/${options.fileName}.spec.tsx`
)
);
}

const lintTask = await addLinting(host, options);
tasks.push(lintTask);

Expand All @@ -120,8 +132,10 @@ export async function applicationGenerator(host: Tree, schema: Schema) {
if (options.unitTestRunner === 'jest') {
const jestTask = await addJest(host, options);
tasks.push(jestTask);
updateSpecConfig(host, options);
}

// Handle tsconfig.spec.json for jest or vitest
updateSpecConfig(host, options);
const styledTask = addStyledModuleDependencies(host, options.styledModule);
tasks.push(styledTask);
const routingTask = addRouting(host, options);
Expand Down
16 changes: 16 additions & 0 deletions packages/web/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,5 +573,21 @@ describe('app', () => {
expect(viteAppTree.exists('/apps/my-app/index.html')).toBe(true);
expect(viteAppTree.exists('/apps/my-app/vite.config.ts')).toBe(true);
});

it('should not include a spec file when the bundler or unitTestRunner is vite and insourceTests is false', async () => {
expect(
viteAppTree.exists('/apps/my-app/src/app/app.element.spec.ts')
).toBe(true);

await applicationGenerator(viteAppTree, {
name: 'insourceTests',
bundler: 'vite',
inSourceTests: true,
});

expect(
viteAppTree.exists('/apps/insource-tests/src/app/app.element.spec.ts')
).toBe(false);
});
});
});
12 changes: 11 additions & 1 deletion packages/web/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,11 @@ export async function applicationGenerator(host: Tree, schema: Schema) {
host.delete(joinPathFragments(options.appProjectRoot, 'src/environments'));

const viteTask = await viteConfigurationGenerator(host, {
uiFramework: 'react',
uiFramework: 'none',
project: options.projectName,
newProject: true,
includeVitest: true,
inSourceTests: options.inSourceTests,
});
tasks.push(viteTask);
}
Expand All @@ -223,6 +224,15 @@ export async function applicationGenerator(host: Tree, schema: Schema) {
tasks.push(vitestTask);
}

if (
(options.bundler === 'vite' || options.unitTestRunner === 'vitest') &&
options.inSourceTests
) {
host.delete(
joinPathFragments(options.appProjectRoot, `src/app/app.element.spec.ts`)
);
}

const lintTask = await lintProjectGenerator(host, {
linter: options.linter,
project: options.projectName,
Expand Down

0 comments on commit f0f60fb

Please sign in to comment.