Skip to content

Commit 0788a81

Browse files
FrozenPandazvsavkin
authored andcommittedAug 22, 2018
feat(builders): introduce builders package and jest builder
1 parent 78b1ea1 commit 0788a81

File tree

13 files changed

+290
-2
lines changed

13 files changed

+290
-2
lines changed
 

Diff for: ‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"@schematics/angular": "^0.7.2",
4040
"@types/jasmine": "~2.8.6",
4141
"@types/jasminewd2": "~2.0.3",
42+
"@types/jest": "^23.3.1",
4243
"@types/node": "~8.9.4",
4344
"@types/prettier": "^1.10.0",
4445
"@types/yargs": "^11.0.0",

Diff for: ‎packages/builders/index.ts

Whitespace-only changes.

Diff for: ‎packages/builders/package.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "@nrwl/builders",
3+
"version": "0.0.1",
4+
"description": "Nrwl Extensions for Angular: Builders",
5+
"repository": {
6+
"type": "git",
7+
"url": "git+https://github.com/nrwl/nx.git"
8+
},
9+
"keywords": [
10+
"Test",
11+
"Jest",
12+
"Angular",
13+
"Workspace",
14+
"Builders",
15+
"Angular CLI"
16+
],
17+
"main": "index.js",
18+
"types": "index.d.js",
19+
"author": "Victor Savkin",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/nrwl/nx/issues"
23+
},
24+
"homepage": "https://github.com/nrwl/nx#readme",
25+
"builders": "./src/builders.json",
26+
"dependencies": {
27+
"@angular-devkit/architect": "~0.7.0",
28+
"rxjs": "6.2.2"
29+
}
30+
}

Diff for: ‎packages/builders/plugins/jest/resolver.ts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { dirname } from 'path';
2+
import * as ts from 'typescript';
3+
import defaultResolver from 'jest-resolve/build/default_resolver';
4+
5+
interface ResolveOptions {
6+
rootDir: string;
7+
basedir: string;
8+
paths: string[];
9+
moduleDirectory: string[];
10+
browser: boolean;
11+
extensions: string[];
12+
}
13+
14+
function getCompilerSetup(rootDir: string) {
15+
const tsConfigPath =
16+
ts.findConfigFile(rootDir, ts.sys.fileExists, 'tsconfig.spec.json') ||
17+
ts.findConfigFile(rootDir, ts.sys.fileExists, 'tsconfig.test.json') ||
18+
ts.findConfigFile(rootDir, ts.sys.fileExists, 'tsconfig.jest.json');
19+
20+
if (!tsConfigPath) {
21+
console.error(
22+
`Cannot locate a tsconfig.spec.json. Please create one at ${rootDir}/tsconfig.spec.json`
23+
);
24+
}
25+
26+
const readResult = ts.readConfigFile(tsConfigPath, ts.sys.readFile);
27+
const config = ts.parseJsonConfigFileContent(
28+
readResult.config,
29+
ts.sys,
30+
dirname(tsConfigPath)
31+
);
32+
const compilerOptions = config.options;
33+
const host = ts.createCompilerHost(compilerOptions, true);
34+
return { compilerOptions, host };
35+
}
36+
37+
let compilerSetup;
38+
39+
module.exports = function(path: string, options: ResolveOptions) {
40+
// Try to use the defaultResolver
41+
try {
42+
return defaultResolver(path, options);
43+
} catch (e) {
44+
// Fallback to using typescript
45+
compilerSetup = compilerSetup || getCompilerSetup(options.rootDir);
46+
const { compilerOptions, host } = compilerSetup;
47+
return ts.resolveModuleName(path, options.basedir, compilerOptions, host)
48+
.resolvedModule.resolvedFileName;
49+
}
50+
};

Diff for: ‎packages/builders/src/builders.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"$schema": "../architect/src/builders-schema.json",
3+
"builders": {
4+
"jest": {
5+
"class": "./jest/jest.builder",
6+
"schema": "./jest/schema.json",
7+
"description": "Run Jest unit tests"
8+
}
9+
}
10+
}

Diff for: ‎packages/builders/src/jest/jest.builder.spec.ts

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import JestBuilder from './jest.builder';
2+
import { normalize } from '@angular-devkit/core';
3+
import * as jestCLI from 'jest-cli';
4+
import * as path from 'path';
5+
6+
describe('Jest Builder', () => {
7+
let builder: JestBuilder;
8+
9+
beforeEach(() => {
10+
builder = new JestBuilder();
11+
});
12+
13+
it('should send appropriate options to jestCLI', () => {
14+
const runCLI = spyOn(jestCLI, 'runCLI').and.returnValue(
15+
Promise.resolve({
16+
results: {
17+
success: true
18+
}
19+
})
20+
);
21+
const root = normalize('/root');
22+
builder
23+
.run({
24+
root,
25+
builder: '',
26+
projectType: 'application',
27+
options: {
28+
jestConfig: './jest.config.js',
29+
tsConfig: './tsconfig.test.json',
30+
watch: false
31+
}
32+
})
33+
.toPromise();
34+
expect(runCLI).toHaveBeenCalledWith(
35+
{
36+
globals: JSON.stringify({
37+
'ts-jest': {
38+
tsConfigFile: path.relative(root, './tsconfig.test.json')
39+
},
40+
__TRANSFORM_HTML__: true
41+
}),
42+
watch: false
43+
},
44+
['./jest.config.js']
45+
);
46+
});
47+
48+
it('should send the setupFile to jestCLI', () => {
49+
const runCLI = spyOn(jestCLI, 'runCLI').and.returnValue(
50+
Promise.resolve({
51+
results: {
52+
success: true
53+
}
54+
})
55+
);
56+
const root = normalize('/root');
57+
builder
58+
.run({
59+
root,
60+
builder: '',
61+
projectType: 'application',
62+
options: {
63+
jestConfig: './jest.config.js',
64+
tsConfig: './tsconfig.test.json',
65+
setupFile: './test.ts',
66+
watch: false
67+
}
68+
})
69+
.toPromise();
70+
expect(runCLI).toHaveBeenCalledWith(
71+
{
72+
globals: JSON.stringify({
73+
'ts-jest': {
74+
tsConfigFile: path.relative(root, './tsconfig.test.json')
75+
},
76+
__TRANSFORM_HTML__: true
77+
}),
78+
setupTestFrameworkScriptFile: path.join(
79+
'<rootDir>',
80+
path.relative(root, './test.ts')
81+
),
82+
watch: false
83+
},
84+
['./jest.config.js']
85+
);
86+
});
87+
88+
it('should return the proper result', async done => {
89+
spyOn(jestCLI, 'runCLI').and.returnValue(
90+
Promise.resolve({
91+
results: {
92+
success: true
93+
}
94+
})
95+
);
96+
const root = normalize('/root');
97+
const result = await builder
98+
.run({
99+
root,
100+
builder: '',
101+
projectType: 'application',
102+
options: {
103+
jestConfig: './jest.config.js',
104+
tsConfig: './tsconfig.test.json',
105+
watch: false
106+
}
107+
})
108+
.toPromise();
109+
expect(result).toEqual({
110+
success: true
111+
});
112+
done();
113+
});
114+
});

Diff for: ‎packages/builders/src/jest/jest.builder.ts

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import {
2+
Builder,
3+
BuildEvent,
4+
BuilderConfiguration
5+
} from '@angular-devkit/architect';
6+
7+
import { Observable, from } from 'rxjs';
8+
import { map } from 'rxjs/operators';
9+
10+
import * as path from 'path';
11+
12+
import { runCLI as runJest } from 'jest-cli';
13+
14+
export interface JestBuilderOptions {
15+
jestConfig: string;
16+
tsConfig: string;
17+
watch: boolean;
18+
setupFile?: string;
19+
}
20+
21+
export default class JestBuilder implements Builder<JestBuilderOptions> {
22+
run(
23+
builderConfig: BuilderConfiguration<JestBuilderOptions>
24+
): Observable<BuildEvent> {
25+
const options = builderConfig.options;
26+
const config: any = {
27+
watch: options.watch,
28+
globals: JSON.stringify({
29+
'ts-jest': {
30+
tsConfigFile: path.relative(builderConfig.root, options.tsConfig)
31+
},
32+
__TRANSFORM_HTML__: true
33+
})
34+
};
35+
36+
if (options.setupFile) {
37+
config.setupTestFrameworkScriptFile = path.join(
38+
'<rootDir>',
39+
path.relative(builderConfig.root, options.setupFile)
40+
);
41+
}
42+
43+
return from(runJest(config, [options.jestConfig])).pipe(
44+
map((results: any) => {
45+
return {
46+
success: results.results.success
47+
};
48+
})
49+
);
50+
}
51+
}

Diff for: ‎packages/builders/src/jest/schema.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"title": "Jest Target",
3+
"description": "Jest target options for Build Facade",
4+
"type": "object",
5+
"properties": {
6+
"jestConfig": {
7+
"type": "string",
8+
"description": "The path of the Jest configuration. (https://jestjs.io/docs/en/configuration.html)"
9+
},
10+
"tsConfig": {
11+
"type": "string",
12+
"description": "The name of the Typescript configuration file."
13+
},
14+
"setupFile": {
15+
"type": "string",
16+
"description": "The name of a setup file used by Jest. (https://jestjs.io/docs/en/configuration.html#setuptestframeworkscriptfile-string)"
17+
},
18+
"watch": {
19+
"type": "boolean",
20+
"description": "Run tests when files change.",
21+
"default": false
22+
}
23+
},
24+
"required": ["jestConfig", "tsConfig"]
25+
}

Diff for: ‎packages/schematics/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"schematics": "./src/collection.json",
3030
"ng-update": {
3131
"requirements": {},
32-
"packageGroup": ["@nrwl/nx", "@nrwl/schematics"],
32+
"packageGroup": ["@nrwl/nx", "@nrwl/schematics", "@nrwl/builders"],
3333
"migrations": "./migrations/migrations.json"
3434
},
3535
"dependencies": {

Diff for: ‎scripts/build.sh

+2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ chmod +x build/packages/schematics/src/command-line/nx.js
2929
rm -rf build/packages/install
3030
rm -rf build/packages/nx/dist
3131
rm -rf build/packages/nx/spec
32+
cp README.md build/packages/builders
3233
cp README.md build/packages/schematics
3334
cp README.md build/packages/nx
3435
cp LICENSE build/packages/bazel
36+
cp LICENSE build/packages/builders
3537
cp LICENSE build/packages/schematics
3638
cp LICENSE build/packages/nx

Diff for: ‎scripts/nx-release.js

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ const options = {
129129
pkgFiles: [
130130
'package.json',
131131
'build/npm/bazel/package.json',
132+
'build/npm/builders/package.json',
132133
'build/npm/nx/package.json',
133134
'build/npm/schematics/package.json'
134135
],

Diff for: ‎scripts/test_schematics.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
if [ -n "$1" ]; then
44
jest --maxWorkers=1 ./build/packages/$1.spec.js
55
else
6-
jest --maxWorkers=1 ./build/packages/{schematics,bazel}
6+
jest --maxWorkers=1 ./build/packages/{schematics,bazel,builders}
77
fi

Diff for: ‎yarn.lock

+4
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@
269269
dependencies:
270270
"@types/jasmine" "*"
271271

272+
"@types/jest@^23.3.1":
273+
version "23.3.1"
274+
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-23.3.1.tgz#a4319aedb071d478e6f407d1c4578ec8156829cf"
275+
272276
"@types/node@*":
273277
version "10.5.3"
274278
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.5.3.tgz#5bcfaf088ad17894232012877669634c06b20cc5"

0 commit comments

Comments
 (0)
Please sign in to comment.