Skip to content

Commit

Permalink
feat(misc): add babelJest support to node,web,workspace,express plugi…
Browse files Browse the repository at this point in the history
…ns (nrwl#3636)

ISSUES CLOSED: nrwl#3041
  • Loading branch information
Hotell authored Sep 8, 2020
1 parent e10aa45 commit 6b8592d
Show file tree
Hide file tree
Showing 50 changed files with 621 additions and 85 deletions.
8 changes: 8 additions & 0 deletions docs/angular/api-express/schematics/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ nx g application ... --dry-run

## Options

### babelJest

Default: `false`

Type: `boolean`

Use babel instead ts-jest

### directory

Type: `string`
Expand Down
8 changes: 8 additions & 0 deletions docs/angular/api-node/schematics/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ nx g application ... --dry-run

## Options

### babelJest

Default: `false`

Type: `boolean`

Use babel instead ts-jest

### directory

Type: `string`
Expand Down
8 changes: 8 additions & 0 deletions docs/angular/api-node/schematics/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ nx g lib mylib --directory=myapp

## Options

### babelJest

Default: `false`

Type: `boolean`

Use babel instead ts-jest

### buildable

Default: `false`
Expand Down
8 changes: 8 additions & 0 deletions docs/angular/api-web/schematics/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ nx g application ... --dry-run

## Options

### babelJest

Default: `false`

Type: `boolean`

Use babel instead ts-jest

### directory

Type: `string`
Expand Down
8 changes: 8 additions & 0 deletions docs/angular/api-workspace/schematics/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ nx g lib mylib --directory=myapp

## Options

### babelJest

Default: `false`

Type: `boolean`

Use babel instead ts-jest

### directory

Type: `string`
Expand Down
8 changes: 8 additions & 0 deletions docs/node/api-express/schematics/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ nx g application ... --dry-run

## Options

### babelJest

Default: `false`

Type: `boolean`

Use babel instead ts-jest

### directory

Type: `string`
Expand Down
8 changes: 8 additions & 0 deletions docs/node/api-node/schematics/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ nx g application ... --dry-run

## Options

### babelJest

Default: `false`

Type: `boolean`

Use babel instead ts-jest

### directory

Type: `string`
Expand Down
8 changes: 8 additions & 0 deletions docs/node/api-node/schematics/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ nx g lib mylib --directory=myapp

## Options

### babelJest

Default: `false`

Type: `boolean`

Use babel instead ts-jest

### buildable

Default: `false`
Expand Down
8 changes: 8 additions & 0 deletions docs/node/api-web/schematics/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ nx g application ... --dry-run

## Options

### babelJest

Default: `false`

Type: `boolean`

Use babel instead ts-jest

### directory

Type: `string`
Expand Down
8 changes: 8 additions & 0 deletions docs/node/api-workspace/schematics/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ nx g lib mylib --directory=myapp

## Options

### babelJest

Default: `false`

Type: `boolean`

Use babel instead ts-jest

### directory

Type: `string`
Expand Down
8 changes: 8 additions & 0 deletions docs/react/api-express/schematics/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ nx g application ... --dry-run

## Options

### babelJest

Default: `false`

Type: `boolean`

Use babel instead ts-jest

### directory

Type: `string`
Expand Down
8 changes: 8 additions & 0 deletions docs/react/api-node/schematics/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ nx g application ... --dry-run

## Options

### babelJest

Default: `false`

Type: `boolean`

Use babel instead ts-jest

### directory

Type: `string`
Expand Down
8 changes: 8 additions & 0 deletions docs/react/api-node/schematics/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ nx g lib mylib --directory=myapp

## Options

### babelJest

Default: `false`

Type: `boolean`

Use babel instead ts-jest

### buildable

Default: `false`
Expand Down
8 changes: 8 additions & 0 deletions docs/react/api-web/schematics/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ nx g application ... --dry-run

## Options

### babelJest

Default: `false`

Type: `boolean`

Use babel instead ts-jest

### directory

Type: `string`
Expand Down
8 changes: 8 additions & 0 deletions docs/react/api-workspace/schematics/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ nx g lib mylib --directory=myapp

## Options

### babelJest

Default: `false`

Type: `boolean`

Use babel instead ts-jest

### directory

Type: `string`
Expand Down
2 changes: 1 addition & 1 deletion e2e/node/src/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ forEachCli((currentCLIName) => {
transform: {
'^.+\\.[tj]sx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/${nestlib}',
};
`
Expand Down
1 change: 1 addition & 0 deletions packages/express/src/schematics/application/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface Schema {
tags?: string;
linter: Linter;
frontendProject?: string;
babelJest?: boolean;
}
5 changes: 5 additions & 0 deletions packages/express/src/schematics/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
"frontendProject": {
"type": "string",
"description": "Frontend project that needs to access this application. This sets up proxy configuration."
},
"babelJest": {
"type": "boolean",
"description": "Use babel instead ts-jest",
"default": false
}
},
"required": []
Expand Down
44 changes: 32 additions & 12 deletions packages/jest/src/schematics/init/init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { readJsonInTree } from '@nrwl/workspace';
import { createEmptyWorkspace } from '@nrwl/workspace/testing';
import { runSchematic } from '../../utils/testing';

import { JestInitSchema } from './schema.d';

describe('jest', () => {
let appTree: Tree;

Expand All @@ -12,8 +14,20 @@ describe('jest', () => {
});

it('should generate files', async () => {
const resultTree = await runSchematic('init', {}, appTree);
const resultTree = await runSchematic<JestInitSchema>('init', {}, appTree);

expect(resultTree.exists('jest.config.js')).toBeTruthy();
expect(resultTree.readContent('jest.config.js')).toMatchInlineSnapshot(`
"module.exports = {
testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'],
transform: {
'^.+\\\\\\\\.(ts|js|html)$': 'ts-jest'
},
resolver: '@nrwl/jest/plugins/resolver',
moduleFileExtensions: ['ts', 'js', 'html'],
coverageReporters: ['html']
};"
`);
});

it('should not override existing files', async () => {
Expand All @@ -23,23 +37,29 @@ describe('jest', () => {
});

it('should add dependencies', async () => {
const resultTree = await runSchematic('init', {}, appTree);
const resultTree = await runSchematic<JestInitSchema>('init', {}, appTree);
const packageJson = readJsonInTree(resultTree, 'package.json');
expect(packageJson.devDependencies.jest).toBeDefined();
expect(packageJson.devDependencies['@nrwl/jest']).toBeDefined();
expect(packageJson.devDependencies['@types/jest']).toBeDefined();
expect(packageJson.devDependencies['ts-jest']).toBeDefined();
});

it('should add babel dependencies', async () => {
const resultTree = await runSchematic('init', { babelJest: true }, appTree);
const packageJson = readJsonInTree(resultTree, 'package.json');
expect(packageJson.devDependencies['@babel/core']).toBeDefined();
expect(packageJson.devDependencies['@babel/preset-env']).toBeDefined();
expect(
packageJson.devDependencies['@babel/preset-typescript']
).toBeDefined();
expect(packageJson.devDependencies['@babel/preset-react']).toBeDefined();
expect(packageJson.devDependencies['babel-jest']).toBeDefined();
describe('--babelJest', () => {
it('should add babel dependencies', async () => {
const resultTree = await runSchematic<JestInitSchema>(
'init',
{ babelJest: true },
appTree
);
const packageJson = readJsonInTree(resultTree, 'package.json');
expect(packageJson.devDependencies['@babel/core']).toBeDefined();
expect(packageJson.devDependencies['@babel/preset-env']).toBeDefined();
expect(
packageJson.devDependencies['@babel/preset-typescript']
).toBeDefined();
expect(packageJson.devDependencies['@babel/preset-react']).toBeDefined();
expect(packageJson.devDependencies['babel-jest']).toBeDefined();
});
});
});
35 changes: 26 additions & 9 deletions packages/jest/src/schematics/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import {
nxVersion,
tsJestVersion,
} from '../../utils/versions';
import { JestInitOptions } from './schema';
import { JestInitSchema } from './schema';

interface NormalizedSchema extends ReturnType<typeof normalizeOptions> {}

const schemaDefaults = {
babelJest: false,
} as const;

const removeNrwlJestFromDeps = (host: Tree) => {
// check whether updating the package.json is necessary
Expand All @@ -38,10 +44,13 @@ const removeNrwlJestFromDeps = (host: Tree) => {
};

const createJestConfig = (host: Tree) => {
if (!host.exists('jest.config.js')) {
host.create(
'jest.config.js',
stripIndents`
if (host.exists('jest.config.js')) {
return;
}

host.create(
'jest.config.js',
stripIndents`
module.exports = {
testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'],
transform: {
Expand All @@ -51,11 +60,10 @@ const createJestConfig = (host: Tree) => {
moduleFileExtensions: ['ts', 'js', 'html'],
coverageReporters: ['html']
};`
);
}
);
};

function updateDependencies(options: JestInitOptions): Rule {
function updateDependencies(options: NormalizedSchema): Rule {
const devDeps = {
'@nrwl/jest': nxVersion,
jest: jestVersion,
Expand All @@ -74,10 +82,19 @@ function updateDependencies(options: JestInitOptions): Rule {
return addDepsToPackageJson({}, devDeps);
}

export default function (options: JestInitOptions): Rule {
export default function (schema: JestInitSchema): Rule {
const options = normalizeOptions(schema);

return chain([
createJestConfig,
updateDependencies(options),
removeNrwlJestFromDeps,
]);
}

function normalizeOptions(options: JestInitSchema) {
return {
...schemaDefaults,
...options,
};
}
4 changes: 2 additions & 2 deletions packages/jest/src/schematics/init/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export interface JestInitOptions {
babelJest: boolean;
export interface JestInitSchema {
babelJest?: boolean;
}
Loading

0 comments on commit 6b8592d

Please sign in to comment.