Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(testing): add babel-jest transform options for react libraries #13175

Merged
merged 1 commit into from Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -311,6 +311,14 @@ describe('app', () => {
);
});

it('should setup jest with babel-jest support', async () => {
await applicationGenerator(appTree, { ...schema, name: 'my-app' });

expect(appTree.read('apps/my-app/jest.config.ts').toString()).toContain(
"['babel-jest', { presets: ['@nrwl/react/babel'] }]"
);
});

it('should setup jest without serializers', async () => {
await applicationGenerator(appTree, { ...schema, name: 'my-app' });

Expand Down
16 changes: 16 additions & 0 deletions packages/react/src/generators/library/library.spec.ts
Expand Up @@ -224,6 +224,12 @@ describe('lib', () => {
}
`);
});
it('should update jest.config.ts for babel', async () => {
await libraryGenerator(appTree, { ...defaultSchema, compiler: 'babel' });
expect(appTree.read('libs/my-lib/jest.config.ts', 'utf-8')).toContain(
"['babel-jest', { presets: ['@nrwl/react/babel'] }]"
);
});
});

describe('nested', () => {
Expand Down Expand Up @@ -270,6 +276,16 @@ describe('lib', () => {
).toBeTruthy();
});

it('should update jest.config.ts for babel', async () => {
await libraryGenerator(appTree, {
...defaultSchema,
directory: 'myDir',
compiler: 'babel',
});
expect(
appTree.read('libs/my-dir/my-lib/jest.config.ts', 'utf-8')
).toContain("['babel-jest', { presets: ['@nrwl/react/babel'] }]");
});
it('should update workspace.json', async () => {
await libraryGenerator(appTree, { ...defaultSchema, directory: 'myDir' });
const workspaceJson = readJson(appTree, '/workspace.json');
Expand Down
12 changes: 11 additions & 1 deletion packages/react/src/generators/library/library.ts
Expand Up @@ -46,7 +46,7 @@ import {
import componentGenerator from '../component/component';
import init from '../init/init';
import { Schema } from './schema';

import { updateJestConfigContent } from '../../utils/jest-utils';
export interface NormalizedSchema extends Schema {
name: string;
fileName: string;
Expand Down Expand Up @@ -99,6 +99,16 @@ export async function libraryGenerator(host: Tree, schema: Schema) {
compiler: options.compiler,
});
tasks.push(jestTask);
const jestConfigPath = joinPathFragments(
options.projectRoot,
options.js ? 'jest.config.js' : 'jest.config.ts'
);
if (options.compiler === 'babel' && host.exists(jestConfigPath)) {
const updatedContent = updateJestConfigContent(
host.read(jestConfigPath, 'utf-8')
);
host.write(jestConfigPath, updatedContent);
}
}

if (options.component) {
Expand Down