Skip to content

Commit

Permalink
fix(testing): add babel-jest transform options for react libraries (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
barbados-clemens authored Nov 16, 2022
1 parent 40d37b3 commit 620f6a6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/react/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 620f6a6

Please sign in to comment.