Skip to content

Commit d6ff724

Browse files
FrozenPandazbcabanes
authored andcommittedSep 14, 2018
feat(schematics): allow generation of app with jest unit testing (#764)
This PR requires #758 ## Current Behavior The option to generate an application with a unit-test-runner of jest is not available ## Expected Behavior User is able to generate an application which uses jest to run unit tests via: ```sh ng g jest ng g app jest-app --unit-test-runner jest ng test jest-app ```
1 parent 9fd3993 commit d6ff724

File tree

6 files changed

+113
-31
lines changed

6 files changed

+113
-31
lines changed
 

Diff for: ‎e2e/schematics/jest.test.ts

+18-14
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,15 @@ import {
22
newProject,
33
runCLI,
44
newLib,
5-
copyMissingPackages,
6-
updateFile,
7-
readJson,
8-
runCommand,
9-
runCLIAsync
5+
runCLIAsync,
6+
newApp,
7+
copyMissingPackages
108
} from '../utils';
119

1210
describe('Jest', () => {
1311
beforeAll(() => {
1412
newProject();
15-
runCLI('generate jest', {
16-
silenceError: true
17-
});
18-
// TODO: remove this hack after there's a version of @nrwl/builders published
19-
const packageJson = readJson('package.json');
20-
packageJson.devDependencies['@nrwl/builders'] =
21-
'../../build/packages/builders';
22-
updateFile('package.json', JSON.stringify(packageJson));
23-
runCommand('npm install');
13+
runCLI('generate jest');
2414
copyMissingPackages();
2515
});
2616

@@ -38,4 +28,18 @@ describe('Jest', () => {
3828
},
3929
10000
4030
);
31+
32+
it(
33+
'should be able to generate a testable application using jest',
34+
async () => {
35+
newApp('jestapp --unit-test-runner jest');
36+
await Promise.all([
37+
runCLIAsync('generate service test --project jestapp'),
38+
runCLIAsync('generate component test --project jestapp')
39+
]);
40+
const jestResult = await runCLIAsync('test jestapp');
41+
expect(jestResult.stderr).toContain('Test Suites: 3 passed, 3 total');
42+
},
43+
10000
44+
);
4145
});

Diff for: ‎packages/schematics/src/collection/application/application.spec.ts

+38
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,42 @@ describe('app', () => {
278278
).toContain('This is an Angular CLI app built with Nrwl Nx!');
279279
});
280280
});
281+
282+
describe('--unit-test-runner jest', () => {
283+
beforeEach(() => {
284+
appTree = schematicRunner.runSchematic('jest', {}, appTree);
285+
});
286+
it('should generate a jest config', () => {
287+
const tree = schematicRunner.runSchematic(
288+
'app',
289+
{ name: 'myApp', unitTestRunner: 'jest' },
290+
appTree
291+
);
292+
expect(tree.exists('apps/my-app/src/test.ts')).toBeFalsy();
293+
expect(tree.exists('apps/my-app/src/test-setup.ts')).toBeTruthy();
294+
expect(tree.exists('apps/my-app/tsconfig.spec.json')).toBeTruthy();
295+
expect(tree.exists('apps/my-app/jest.config.js')).toBeTruthy();
296+
const angularJson = readJsonInTree(tree, 'angular.json');
297+
expect(angularJson.projects['my-app'].architect.test.builder).toEqual(
298+
'@nrwl/builders:jest'
299+
);
300+
});
301+
});
302+
303+
describe('--unit-test-runner none', () => {
304+
it('should not generate test configuration', () => {
305+
const tree = schematicRunner.runSchematic(
306+
'app',
307+
{ name: 'myApp', unitTestRunner: 'none' },
308+
appTree
309+
);
310+
expect(tree.exists('apps/my-app/src/test-setup.ts')).toBeFalsy();
311+
expect(tree.exists('apps/my-app/src/test.ts')).toBeFalsy();
312+
expect(tree.exists('apps/my-app/tsconfig.spec.json')).toBeFalsy();
313+
expect(tree.exists('apps/my-app/jest.config.js')).toBeFalsy();
314+
expect(tree.exists('apps/my-app/karma.config.js')).toBeFalsy();
315+
const angularJson = readJsonInTree(tree, 'angular.json');
316+
expect(angularJson.projects['my-app'].architect.test).toBeUndefined();
317+
});
318+
});
281319
});

Diff for: ‎packages/schematics/src/collection/application/index.ts

+44-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
noop,
66
Rule,
77
Tree,
8-
SchematicContext
8+
SchematicContext,
9+
schematic
910
} from '@angular-devkit/schematics';
1011
import { Schema } from './schema';
1112
import * as ts from 'typescript';
@@ -155,11 +156,14 @@ function updateProject(options: NormalizedSchema): Rule {
155156
return chain([
156157
updateJsonInTree(getWorkspacePath(host), json => {
157158
const project = json.projects[options.name];
158-
const fixedProject = replaceAppNameWithPath(
159+
let fixedProject = replaceAppNameWithPath(
159160
project,
160161
options.name,
161162
options.appProjectRoot
162163
);
164+
if (options.unitTestRunner !== 'karma') {
165+
delete fixedProject.architect.test;
166+
}
163167
json.projects[options.name] = fixedProject;
164168
return json;
165169
}),
@@ -177,18 +181,28 @@ function updateProject(options: NormalizedSchema): Rule {
177181
include: ['**/*.ts']
178182
};
179183
}),
180-
updateJsonInTree(`${options.appProjectRoot}/tsconfig.spec.json`, json => {
181-
return {
182-
...json,
183-
extends: `${offsetFromRoot(options.appProjectRoot)}tsconfig.json`,
184-
compilerOptions: {
185-
...json.compilerOptions,
186-
outDir: `${offsetFromRoot(options.appProjectRoot)}dist/out-tsc/${
187-
options.appProjectRoot
188-
}`
189-
}
190-
};
191-
}),
184+
options.unitTestRunner === 'karma'
185+
? updateJsonInTree(
186+
`${options.appProjectRoot}/tsconfig.spec.json`,
187+
json => {
188+
return {
189+
...json,
190+
extends: `${offsetFromRoot(
191+
options.appProjectRoot
192+
)}tsconfig.json`,
193+
compilerOptions: {
194+
...json.compilerOptions,
195+
outDir: `${offsetFromRoot(
196+
options.appProjectRoot
197+
)}dist/out-tsc/${options.appProjectRoot}`
198+
}
199+
};
200+
}
201+
)
202+
: host => {
203+
host.delete(`${options.appProjectRoot}/tsconfig.spec.json`);
204+
return host;
205+
},
192206
updateJsonInTree(`${options.appProjectRoot}/tslint.json`, json => {
193207
return {
194208
...json,
@@ -206,6 +220,12 @@ function updateProject(options: NormalizedSchema): Rule {
206220
};
207221
}),
208222
host => {
223+
if (options.unitTestRunner !== 'karma') {
224+
host.delete(`${options.appProjectRoot}/karma.conf.js`);
225+
host.delete(`${options.appProjectRoot}/src/test.ts`);
226+
return host;
227+
}
228+
209229
const karma = host
210230
.read(`${options.appProjectRoot}/karma.conf.js`)
211231
.toString();
@@ -285,9 +305,16 @@ export default function(schema: Schema): Rule {
285305
updateComponentTemplate(options),
286306
addNxModule(options),
287307
options.routing ? addRouterRootConfiguration(options) : noop(),
288-
updateKarmaConf({
289-
projectName: options.name
290-
}),
308+
options.unitTestRunner === 'karma'
309+
? updateKarmaConf({
310+
projectName: options.name
311+
})
312+
: noop(),
313+
options.unitTestRunner === 'jest'
314+
? schematic('jest-project', {
315+
project: options.name
316+
})
317+
: noop(),
291318
formatFiles(options)
292319
])(host, context);
293320
};

Diff for: ‎packages/schematics/src/collection/application/schema.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { UnitTestRunner } from '../../utils/test-runners';
2+
13
export interface Schema {
24
name: string;
35
skipFormat: boolean;
@@ -10,4 +12,5 @@ export interface Schema {
1012
skipTests?: boolean;
1113
directory?: string;
1214
tags?: string;
15+
unitTestRunner: UnitTestRunner;
1316
}

Diff for: ‎packages/schematics/src/collection/application/schema.json

+6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@
6868
"tags": {
6969
"type": "string",
7070
"description": "Add tags to the application (used for linting)"
71+
},
72+
"unitTestRunner": {
73+
"type": "string",
74+
"enum": ["karma", "jest", "none"],
75+
"description": "Test runner to use for unit tests",
76+
"default": "karma"
7177
}
7278
},
7379
"required": []

Diff for: ‎packages/schematics/src/collection/library/library.spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,9 @@ describe('lib', () => {
404404
{ name: 'myLib', unitTestRunner: 'jest' },
405405
appTree
406406
);
407+
expect(resultTree.exists('libs/my-lib/src/test.ts')).toBeFalsy();
407408
expect(resultTree.exists('libs/my-lib/src/test-setup.ts')).toBeTruthy();
409+
expect(resultTree.exists('libs/my-lib/tsconfig.spec.json')).toBeTruthy();
408410
expect(resultTree.exists('libs/my-lib/jest.config.js')).toBeTruthy();
409411
const angularJson = readJsonInTree(resultTree, 'angular.json');
410412
expect(angularJson.projects['my-lib'].architect.test.builder).toEqual(
@@ -423,6 +425,8 @@ describe('lib', () => {
423425
expect(
424426
resultTree.exists('libs/my-lib/src/lib/my-lib.module.spec.ts')
425427
).toBeFalsy();
428+
expect(resultTree.exists('libs/my-lib/src/test.ts')).toBeFalsy();
429+
expect(resultTree.exists('libs/my-lib/src/test.ts')).toBeFalsy();
426430
expect(resultTree.exists('libs/my-lib/tsconfig.spec.json')).toBeFalsy();
427431
expect(resultTree.exists('libs/my-lib/jest.config.js')).toBeFalsy();
428432
expect(resultTree.exists('libs/my-lib/karma.config.js')).toBeFalsy();

0 commit comments

Comments
 (0)
Please sign in to comment.