diff --git a/.prettierignore b/.prettierignore index f97be0874e..ee4804524f 100644 --- a/.prettierignore +++ b/.prettierignore @@ -4,6 +4,8 @@ tmp node_modules /package.json packages/schematics/src/collection/**/files/**/*.json +packages/react/src/schematics/**/files/**/*.json +packages/jest/src/schematics/**/files/**/*.json /.vscode /.idea /.github diff --git a/docs/api-builders/jest.md b/docs/api-jest/builders/jest.md similarity index 100% rename from docs/api-builders/jest.md rename to docs/api-jest/builders/jest.md diff --git a/docs/api-schematics/jest-project.md b/docs/api-jest/schematics/jest-project.md similarity index 92% rename from docs/api-schematics/jest-project.md rename to docs/api-jest/schematics/jest-project.md index fa9b502527..6ba9fe3daf 100644 --- a/docs/api-schematics/jest-project.md +++ b/docs/api-jest/schematics/jest-project.md @@ -1,4 +1,4 @@ -# jest-project [hidden] +# jest-project Add Jest configuration to a project @@ -19,7 +19,7 @@ The name of the project. ### setupFile -Default: `angular` +Default: `none` Type: `string` diff --git a/docs/api-schematics/jest.md b/docs/api-jest/schematics/ng-add.md similarity index 61% rename from docs/api-schematics/jest.md rename to docs/api-jest/schematics/ng-add.md index b8f6960491..c494bd8c98 100644 --- a/docs/api-schematics/jest.md +++ b/docs/api-jest/schematics/ng-add.md @@ -1,10 +1,10 @@ -# jest [hidden] +# ng-add [hidden] Add Jest configuration to the workspace ## Usage ```bash -ng generate jest ... +ng generate ng-add ... ``` diff --git a/e2e/schematics/jest.test.ts b/e2e/schematics/jest.test.ts index 3e61e3eced..f3dbad469c 100644 --- a/e2e/schematics/jest.test.ts +++ b/e2e/schematics/jest.test.ts @@ -1,39 +1,23 @@ -import { - newProject, - runCLI, - newLib, - runCLIAsync, - newApp, - copyMissingPackages, - ensureProject, - uniq -} from '../utils'; +import { newLib, runCLIAsync, newApp, ensureProject, uniq } from '../utils'; describe('Jest', () => { - it('should be able to generate a testable library using jest', async done => { + it('should be able test projects using jest', async done => { ensureProject(); const mylib = uniq('mylib'); + const myapp = uniq('myapp'); + newApp(`${myapp} --unit-test-runner jest --framework=angular`); newLib(`${mylib} --unit-test-runner jest --framework=angular`); await Promise.all([ + runCLIAsync(`generate service test --project ${myapp}`), + runCLIAsync(`generate component test --project ${myapp}`), runCLIAsync(`generate service test --project ${mylib}`), runCLIAsync(`generate component test --project ${mylib}`) ]); - const jestResult = await runCLIAsync(`test ${mylib}`); - expect(jestResult.stderr).toContain('Test Suites: 3 passed, 3 total'); + const appResult = await runCLIAsync(`test ${myapp}`); + expect(appResult.stderr).toContain('Test Suites: 3 passed, 3 total'); + const libResult = await runCLIAsync(`test ${mylib}`); + expect(libResult.stderr).toContain('Test Suites: 3 passed, 3 total'); done(); }, 45000); - - it('should be able to generate a testable application using jest', async () => { - ensureProject(); - const myapp = uniq('myapp'); - newApp(`${myapp} --unit-test-runner jest --framework=angular`); - - await Promise.all([ - runCLIAsync(`generate service test --project ${myapp}`), - runCLIAsync(`generate component test --project ${myapp}`) - ]); - const jestResult = await runCLIAsync(`test ${myapp}`); - expect(jestResult.stderr).toContain('Test Suites: 3 passed, 3 total'); - }, 450000); }); diff --git a/e2e/schematics/ng-new.test.ts b/e2e/schematics/ng-new.test.ts index f540669f93..c2d9ff5c4c 100644 --- a/e2e/schematics/ng-new.test.ts +++ b/e2e/schematics/ng-new.test.ts @@ -1,14 +1,9 @@ import { newApp, newLib, - newProject, - readJson, runCLI, updateFile, exists, - runNgNew, - cleanup, - copyMissingPackages, getSize, expectTestsPass, runCLIAsync, @@ -19,8 +14,11 @@ import { import { toClassName } from '@nrwl/schematics/src/utils/name-utils'; describe('Nrwl Workspace', () => { - it('should work', async () => { + beforeEach(() => { ensureProject(); + }); + + it('should work', async () => { const myapp = uniq('myapp'); const mylib = uniq('mylib'); newApp(`${myapp} --directory=myDir`); @@ -73,7 +71,6 @@ describe('Nrwl Workspace', () => { }, 1000000); it('should support router config generation (lazy)', async () => { - ensureProject(); const myapp = uniq('myapp'); const mylib = uniq('mylib'); newApp(`${myapp} --directory=myDir --routing`); @@ -88,7 +85,6 @@ describe('Nrwl Workspace', () => { }, 1000000); it('should support router config generation (eager)', async () => { - ensureProject(); const myapp = uniq('myapp'); newApp(`${myapp} --directory=myDir --routing`); const mylib = uniq('mylib'); diff --git a/e2e/utils.ts b/e2e/utils.ts index 1b020be192..f097747f36 100644 --- a/e2e/utils.ts +++ b/e2e/utils.ts @@ -25,6 +25,8 @@ export function newProject(): void { if (!directoryExists('./tmp/proj_backup')) { runNgNew('--collection=@nrwl/schematics --npmScope=proj', true); copyMissingPackages(); + runCLI('add @nrwl/jest'); + runCLI('add @nrwl/react'); execSync('mv ./tmp/proj ./tmp/proj_backup'); } execSync('cp -a ./tmp/proj_backup ./tmp/proj'); diff --git a/package.json b/package.json index 5afa9fb1cf..b90389f87e 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "main": "index.js", "private": true, "scripts": { - "build": "./scripts/build.sh", + "build": "./scripts/package.sh --local", "commit": "git-cz", "checkcommit": "node ./scripts/commit-lint.js", "e2e": "./scripts/e2e.sh", diff --git a/packages/builders/src/builders.json b/packages/builders/src/builders.json index fc78845314..dc909649e1 100644 --- a/packages/builders/src/builders.json +++ b/packages/builders/src/builders.json @@ -21,11 +21,6 @@ "schema": "./web/dev-server/schema.json", "description": "Serve a web application" }, - "jest": { - "class": "./jest/jest.builder", - "schema": "./jest/schema.json", - "description": "Run Jest unit tests" - }, "cypress": { "class": "./cypress/cypress.builder", "schema": "./cypress/schema.json", diff --git a/packages/jest/builders.json b/packages/jest/builders.json new file mode 100644 index 0000000000..37a3e35791 --- /dev/null +++ b/packages/jest/builders.json @@ -0,0 +1,10 @@ +{ + "$schema": "@angular-devkit/architect/src/builders-schema.json", + "builders": { + "jest": { + "class": "./src/builders/jest/jest.builder", + "schema": "./src/builders/jest/schema.json", + "description": "Run Jest unit tests" + } + } +} diff --git a/packages/jest/collection.json b/packages/jest/collection.json new file mode 100644 index 0000000000..6d4232d732 --- /dev/null +++ b/packages/jest/collection.json @@ -0,0 +1,17 @@ +{ + "name": "Nx Jest", + "version": "0.1", + "schematics": { + "ng-add": { + "factory": "./src/schematics/ng-add/ng-add", + "schema": "./src/schematics/ng-add/schema.json", + "description": "Add Jest configuration to the workspace", + "hidden": true + }, + "jest-project": { + "factory": "./src/schematics/jest-project/jest-project", + "schema": "./src/schematics/jest-project/schema.json", + "description": "Add Jest configuration to a project" + } + } +} diff --git a/packages/jest/index.ts b/packages/jest/index.ts new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/jest/migrations.json b/packages/jest/migrations.json new file mode 100644 index 0000000000..63001b4458 --- /dev/null +++ b/packages/jest/migrations.json @@ -0,0 +1,3 @@ +{ + "schematics": {} +} diff --git a/packages/jest/package.json b/packages/jest/package.json new file mode 100644 index 0000000000..00bd610835 --- /dev/null +++ b/packages/jest/package.json @@ -0,0 +1,41 @@ +{ + "name": "@nrwl/jest", + "version": "0.0.1", + "description": "Jest plugin for Nx", + "repository": { + "type": "git", + "url": "git+https://github.com/nrwl/nx.git" + }, + "keywords": [ + "Jest", + "Unit Testing", + "Workspace", + "Monorepo", + "Schematics", + "Nx", + "Angular CLI" + ], + "main": "index.js", + "types": "index.d.ts", + "author": "Victor Savkin", + "license": "MIT", + "bugs": { + "url": "https://github.com/nrwl/nx/issues" + }, + "homepage": "https://nx.dev", + "schematics": "./collection.json", + "builders": "./builders.json", + "ng-update": { + "requirements": {}, + "migrations": "./migrations.json" + }, + "peerDependencies": { + "@nrwl/schematics": "*" + }, + "dependencies": { + "@angular-devkit/architect": "~0.13.1", + "@angular-devkit/core": "~7.3.1", + "@angular-devkit/schematics": "~7.3.1", + "rxjs": "6.3.3" + } +} diff --git a/packages/builders/plugins/jest/resolver.ts b/packages/jest/plugins/resolver.ts similarity index 100% rename from packages/builders/plugins/jest/resolver.ts rename to packages/jest/plugins/resolver.ts diff --git a/packages/builders/src/jest/jest.builder.spec.ts b/packages/jest/src/builders/jest/jest.builder.spec.ts similarity index 100% rename from packages/builders/src/jest/jest.builder.spec.ts rename to packages/jest/src/builders/jest/jest.builder.spec.ts diff --git a/packages/builders/src/jest/jest.builder.ts b/packages/jest/src/builders/jest/jest.builder.ts similarity index 100% rename from packages/builders/src/jest/jest.builder.ts rename to packages/jest/src/builders/jest/jest.builder.ts diff --git a/packages/builders/src/jest/schema.json b/packages/jest/src/builders/jest/schema.json similarity index 99% rename from packages/builders/src/jest/schema.json rename to packages/jest/src/builders/jest/schema.json index a046c39af4..d838ddfc29 100644 --- a/packages/builders/src/jest/schema.json +++ b/packages/jest/src/builders/jest/schema.json @@ -1,5 +1,5 @@ { - "title": "Jest Target", + "title": "Jest Builder", "description": "Jest target options for Build Facade", "type": "object", "properties": { diff --git a/packages/schematics/src/collection/jest-project/files/jest.config.js__tmpl__ b/packages/jest/src/schematics/jest-project/files/jest.config.js__tmpl__ similarity index 100% rename from packages/schematics/src/collection/jest-project/files/jest.config.js__tmpl__ rename to packages/jest/src/schematics/jest-project/files/jest.config.js__tmpl__ diff --git a/packages/schematics/src/collection/jest-project/files/src/test-setup.ts__tmpl__ b/packages/jest/src/schematics/jest-project/files/src/test-setup.ts__tmpl__ similarity index 100% rename from packages/schematics/src/collection/jest-project/files/src/test-setup.ts__tmpl__ rename to packages/jest/src/schematics/jest-project/files/src/test-setup.ts__tmpl__ diff --git a/packages/schematics/src/collection/jest-project/files/tsconfig.spec.json b/packages/jest/src/schematics/jest-project/files/tsconfig.spec.json similarity index 100% rename from packages/schematics/src/collection/jest-project/files/tsconfig.spec.json rename to packages/jest/src/schematics/jest-project/files/tsconfig.spec.json diff --git a/packages/schematics/src/collection/jest-project/jest-project.spec.ts b/packages/jest/src/schematics/jest-project/jest-project.spec.ts similarity index 86% rename from packages/schematics/src/collection/jest-project/jest-project.spec.ts rename to packages/jest/src/schematics/jest-project/jest-project.spec.ts index cc9f8a761a..86c1df5b5b 100644 --- a/packages/schematics/src/collection/jest-project/jest-project.spec.ts +++ b/packages/jest/src/schematics/jest-project/jest-project.spec.ts @@ -1,6 +1,7 @@ import { Tree, VirtualTree } from '@angular-devkit/schematics'; -import { createEmptyWorkspace, runSchematic } from '../../utils/testing-utils'; -import { readJsonInTree } from '@nrwl/schematics/src/utils/ast-utils'; +import { createEmptyWorkspace } from '@nrwl/schematics/testing'; +import { readJsonInTree, updateJsonInTree } from '@nrwl/schematics'; +import { runSchematic, callRule } from '../../utils/testing'; describe('jestProject', () => { let appTree: Tree; @@ -8,12 +9,31 @@ describe('jestProject', () => { beforeEach(async () => { appTree = new VirtualTree(); appTree = createEmptyWorkspace(appTree); - appTree = await runSchematic( - 'lib', - { - name: 'lib1', - unitTestRunner: 'none' - }, + appTree = await callRule( + updateJsonInTree('angular.json', json => { + json.projects.lib1 = { + root: 'libs/lib1', + architect: { + lint: { + builder: '@angular-devkit/build-angular:tslint', + options: { + tsConfig: [] + } + } + } + }; + return json; + }), + appTree + ); + appTree = await callRule( + updateJsonInTree('libs/lib1/tsconfig.json', json => { + return { + compilerOptions: { + types: [] + } + }; + }), appTree ); }); @@ -22,7 +42,8 @@ describe('jestProject', () => { const resultTree = await runSchematic( 'jest-project', { - project: 'lib1' + project: 'lib1', + setupFile: 'angular' }, appTree ); @@ -35,13 +56,14 @@ describe('jestProject', () => { const resultTree = await runSchematic( 'jest-project', { - project: 'lib1' + project: 'lib1', + setupFile: 'angular' }, appTree ); const angularJson = readJsonInTree(resultTree, 'angular.json'); expect(angularJson.projects.lib1.architect.test).toEqual({ - builder: '@nrwl/builders:jest', + builder: '@nrwl/jest:jest', options: { jestConfig: 'libs/lib1/jest.config.js', setupFile: 'libs/lib1/src/test-setup.ts', @@ -91,7 +113,8 @@ describe('jestProject', () => { const resultTree = await runSchematic( 'jest-project', { - project: 'lib1' + project: 'lib1', + setupFile: 'angular' }, appTree ); @@ -168,21 +191,6 @@ describe('jestProject', () => { ).toBeDefined(); }); }); - - describe('not angular', () => { - it('should add ts-jest dependency', async () => { - const resultTree = await runSchematic( - 'jest-project', - { - project: 'lib1', - setupFile: 'web-components' - }, - appTree - ); - const packageJson = readJsonInTree(resultTree, 'package.json'); - expect(packageJson.devDependencies['ts-jest']).toBeDefined(); - }); - }); }); describe('--skip-setup-file', () => { diff --git a/packages/schematics/src/collection/jest-project/index.ts b/packages/jest/src/schematics/jest-project/jest-project.ts similarity index 83% rename from packages/schematics/src/collection/jest-project/index.ts rename to packages/jest/src/schematics/jest-project/jest-project.ts index c318736829..a4aad3f680 100644 --- a/packages/schematics/src/collection/jest-project/index.ts +++ b/packages/jest/src/schematics/jest-project/jest-project.ts @@ -9,18 +9,16 @@ import { move, template, noop, - filter, - schematic + filter } from '@angular-devkit/schematics'; +import { readJsonInTree, updateJsonInTree } from '@nrwl/schematics'; import { getProjectConfig, - readJsonInTree, - updateJsonInTree, addDepsToPackageJson -} from '../../utils/ast-utils'; -import { offsetFromRoot } from '../../utils/common'; +} from '@nrwl/schematics/src/utils/ast-utils'; +import { offsetFromRoot } from '@nrwl/schematics/src/utils/common'; import { join, normalize } from '@angular-devkit/core'; -import { jestPresetAngularVersion, tsJestversion } from '../../lib-versions'; +import { jestPresetAngularVersion } from '../../utils/versions'; export interface JestProjectSchema { project: string; @@ -51,8 +49,16 @@ function generateFiles(options: JestProjectSchema): Rule { } function updateTsConfig(options: JestProjectSchema): Rule { - return (host: Tree, context: SchematicContext) => { + return (host: Tree) => { const projectConfig = getProjectConfig(host, options.project); + if (!host.exists(join(projectConfig.root, 'tsconfig.json'))) { + throw new Error( + `Expected ${join( + projectConfig.root, + 'tsconfig.json' + )} to exist. Please create one.` + ); + } return updateJsonInTree(join(projectConfig.root, 'tsconfig.json'), json => { return { ...json, @@ -71,7 +77,7 @@ function updateAngularJson(options: JestProjectSchema): Rule { return updateJsonInTree('angular.json', json => { const projectConfig = json.projects[options.project]; projectConfig.architect.test = { - builder: '@nrwl/builders:jest', + builder: '@nrwl/jest:jest', options: { jestConfig: join(normalize(projectConfig.root), 'jest.config.js'), tsConfig: join(normalize(projectConfig.root), 'tsconfig.spec.json') @@ -97,8 +103,6 @@ function addDependencies(options: JestProjectSchema): Rule { const devDeps = {}; if (options.setupFile === 'angular') { devDeps['jest-preset-angular'] = jestPresetAngularVersion; - } else { - devDeps['ts-jest'] = tsJestversion; } return addDepsToPackageJson({}, devDeps); } @@ -113,8 +117,10 @@ function check(options: JestProjectSchema): Rule { } const packageJson = readJsonInTree(host, 'package.json'); if (!packageJson.devDependencies.jest) { - return schematic('jest', {}); + context.logger.warn(`"jest" is not installed as a dependency.`); + context.logger.info(`Add "jest" via "ng add @nrwl/jest"`); } + return host; }; } diff --git a/packages/schematics/src/collection/jest-project/schema.json b/packages/jest/src/schematics/jest-project/schema.json similarity index 88% rename from packages/schematics/src/collection/jest-project/schema.json rename to packages/jest/src/schematics/jest-project/schema.json index 3de2358595..0051b6ad6c 100644 --- a/packages/schematics/src/collection/jest-project/schema.json +++ b/packages/jest/src/schematics/jest-project/schema.json @@ -1,7 +1,7 @@ { "$schema": "http://json-schema.org/schema", - "id": "SchematicsNxJestProject", - "title": "Create Jest Configuration for a project", + "id": "NxJestProject", + "title": "Add Jest Configuration to a project", "type": "object", "properties": { "project": { @@ -21,7 +21,7 @@ "type": "string", "enum": ["none", "angular", "web-components"], "description": "The setup file to be generated", - "default": "angular" + "default": "none" }, "skipSerializers": { "type": "boolean", diff --git a/packages/schematics/src/collection/jest/files/jest.config.js b/packages/jest/src/schematics/ng-add/files/jest.config.js similarity index 81% rename from packages/schematics/src/collection/jest/files/jest.config.js rename to packages/jest/src/schematics/ng-add/files/jest.config.js index 8f0b3a0ead..caba6c4f7c 100644 --- a/packages/schematics/src/collection/jest/files/jest.config.js +++ b/packages/jest/src/schematics/ng-add/files/jest.config.js @@ -3,7 +3,7 @@ module.exports = { transform: { '^.+\\.(ts|js|html)$': 'ts-jest' }, - resolver: '@nrwl/builders/plugins/jest/resolver', + resolver: '@nrwl/jest/plugins/resolver', moduleFileExtensions: ['ts', 'js', 'html'], collectCoverage: true, coverageReporters: ['html'] diff --git a/packages/schematics/src/collection/jest/jest.spec.ts b/packages/jest/src/schematics/ng-add/ng-add.spec.ts similarity index 62% rename from packages/schematics/src/collection/jest/jest.spec.ts rename to packages/jest/src/schematics/ng-add/ng-add.spec.ts index 571a999f9f..dcc4cf2c59 100644 --- a/packages/schematics/src/collection/jest/jest.spec.ts +++ b/packages/jest/src/schematics/ng-add/ng-add.spec.ts @@ -1,12 +1,7 @@ -import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; -import * as path from 'path'; import { Tree, VirtualTree } from '@angular-devkit/schematics'; -import { - createEmptyWorkspace, - createLib, - runSchematic -} from '../../utils/testing-utils'; +import { createEmptyWorkspace } from '@nrwl/schematics/testing'; import { readJsonInTree } from '@nrwl/schematics/src/utils/ast-utils'; +import { runSchematic } from '../../utils/testing'; describe('jest', () => { let appTree: Tree; @@ -17,15 +12,16 @@ describe('jest', () => { }); it('should generate files', async () => { - const resultTree = await runSchematic('jest', {}, appTree); + const resultTree = await runSchematic('ng-add', {}, appTree); expect(resultTree.exists('jest.config.js')).toBeTruthy(); }); it('should add dependencies', async () => { - const resultTree = await runSchematic('jest', {}, appTree); + const resultTree = await runSchematic('ng-add', {}, appTree); const packageJson = readJsonInTree(resultTree, 'package.json'); expect(packageJson.devDependencies.jest).toBeDefined(); - expect(packageJson.devDependencies['@nrwl/builders']).toBeDefined(); + expect(packageJson.devDependencies['@nrwl/jest']).toBeDefined(); expect(packageJson.devDependencies['@types/jest']).toBeDefined(); + expect(packageJson.devDependencies['ts-jest']).toBeDefined(); }); }); diff --git a/packages/jest/src/schematics/ng-add/ng-add.ts b/packages/jest/src/schematics/ng-add/ng-add.ts new file mode 100644 index 0000000000..34e7b17854 --- /dev/null +++ b/packages/jest/src/schematics/ng-add/ng-add.ts @@ -0,0 +1,33 @@ +import { mergeWith, chain, url } from '@angular-devkit/schematics'; +import { + addDepsToPackageJson, + updateJsonInTree +} from '@nrwl/schematics/src/utils/ast-utils'; +import { + jestVersion, + jestTypesVersion, + tsJestVersion, + nxVersion +} from '../../utils/versions'; +import { Rule } from '@angular-devkit/schematics'; + +const updatePackageJson = chain([ + addDepsToPackageJson( + {}, + { + '@nrwl/jest': nxVersion, + jest: jestVersion, + '@types/jest': jestTypesVersion, + 'ts-jest': tsJestVersion + } + ), + updateJsonInTree('package.json', json => { + json.dependencies = json.dependencies || {}; + delete json.dependencies['@nrwl/jest']; + return json; + }) +]); + +export default function(): Rule { + return chain([mergeWith(url('./files')), updatePackageJson]); +} diff --git a/packages/schematics/src/collection/jest/schema.json b/packages/jest/src/schematics/ng-add/schema.json similarity index 55% rename from packages/schematics/src/collection/jest/schema.json rename to packages/jest/src/schematics/ng-add/schema.json index 629d008baf..1bc9f0fe82 100644 --- a/packages/schematics/src/collection/jest/schema.json +++ b/packages/jest/src/schematics/ng-add/schema.json @@ -1,7 +1,7 @@ { "$schema": "http://json-schema.org/schema", - "id": "SchematicsNxJest", - "title": "Create Jest Configuration for the workspace", + "id": "NxJestNgAdd", + "title": "Add Jest Configuration to a workspace", "type": "object", "properties": {}, "required": [] diff --git a/packages/jest/src/utils/testing.ts b/packages/jest/src/utils/testing.ts new file mode 100644 index 0000000000..e94d842ba1 --- /dev/null +++ b/packages/jest/src/utils/testing.ts @@ -0,0 +1,16 @@ +import { join } from 'path'; +import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; +import { Tree, Rule } from '@angular-devkit/schematics'; + +const testRunner = new SchematicTestRunner( + '@nrwl/jest', + join(__dirname, '../../collection.json') +); + +export function runSchematic(schematicName: string, options: any, tree: Tree) { + return testRunner.runSchematicAsync(schematicName, options, tree).toPromise(); +} + +export function callRule(rule: Rule, tree: Tree) { + return testRunner.callRule(rule, tree).toPromise(); +} diff --git a/packages/jest/src/utils/versions.ts b/packages/jest/src/utils/versions.ts new file mode 100644 index 0000000000..cd513f5752 --- /dev/null +++ b/packages/jest/src/utils/versions.ts @@ -0,0 +1,5 @@ +export const nxVersion = '*'; +export const jestVersion = '24.1.0'; +export const jestTypesVersion = '24.0.9'; +export const jestPresetAngularVersion = '7.0.0'; +export const tsJestVersion = '24.0.0'; diff --git a/packages/react/package.json b/packages/react/package.json index 6636917cd2..c4548e666a 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -32,6 +32,7 @@ }, "dependencies": { "@angular-devkit/core": "~7.3.1", - "@angular-devkit/schematics": "~7.3.1" + "@angular-devkit/schematics": "~7.3.1", + "@nrwl/jest": "*" } } diff --git a/packages/react/src/schematics/application/application.ts b/packages/react/src/schematics/application/application.ts index b48da548a2..f9bbca2288 100644 --- a/packages/react/src/schematics/application/application.ts +++ b/packages/react/src/schematics/application/application.ts @@ -189,7 +189,7 @@ export default function(schema: Schema): Rule { }) : noop(), options.unitTestRunner === 'jest' - ? externalSchematic('@nrwl/schematics', 'jest-project', { + ? externalSchematic('@nrwl/jest', 'jest-project', { project: options.name, supportTsx: true, skipSerializers: true, diff --git a/packages/react/src/schematics/library/library.ts b/packages/react/src/schematics/library/library.ts index 22414f94ec..0553a1ccf4 100644 --- a/packages/react/src/schematics/library/library.ts +++ b/packages/react/src/schematics/library/library.ts @@ -101,7 +101,7 @@ export default function(schema: Schema): Rule { addProject(options), updateNxJson(options), options.unitTestRunner !== 'none' - ? externalSchematic('@nrwl/schematics', 'jest-project', { + ? externalSchematic('@nrwl/jest', 'jest-project', { project: options.name, setupFile: 'none', supportTsx: true, diff --git a/packages/react/src/schematics/ng-add/ng-add.ts b/packages/react/src/schematics/ng-add/ng-add.ts index 6a8d5fc47d..3d4bf7dd1a 100644 --- a/packages/react/src/schematics/ng-add/ng-add.ts +++ b/packages/react/src/schematics/ng-add/ng-add.ts @@ -1,7 +1,14 @@ -import { Rule, chain } from '@angular-devkit/schematics'; +import { + Rule, + chain, + externalSchematic, + noop, + Tree +} from '@angular-devkit/schematics'; import { addDepsToPackageJson, - updateJsonInTree + updateJsonInTree, + readJsonInTree } from '@nrwl/schematics/src/utils/ast-utils'; import { frameworkVersion, @@ -35,6 +42,22 @@ function moveDependency(): Rule { }); } -export default function() { - return chain([addDependencies(), moveDependency()]); +function addJest(): Rule { + return (host: Tree) => { + const packageJson = readJsonInTree(host, 'package.json'); + return !packageJson.devDependencies['@nrwl/jest'] + ? externalSchematic( + '@nrwl/jest', + 'ng-add', + {}, + { + interactive: false + } + ) + : noop(); + }; +} + +export default function() { + return chain([addJest(), addDependencies(), moveDependency()]); } diff --git a/packages/schematics/src/collection.json b/packages/schematics/src/collection.json index 26d6d9a78f..02a8d24977 100644 --- a/packages/schematics/src/collection.json +++ b/packages/schematics/src/collection.json @@ -50,20 +50,6 @@ "description": "Add NgRx support to a module" }, - "jest": { - "factory": "./collection/jest", - "schema": "./collection/jest/schema.json", - "description": "Add Jest configuration to the workspace", - "hidden": true - }, - - "jest-project": { - "factory": "./collection/jest-project", - "schema": "./collection/jest-project/schema.json", - "description": "Add Jest configuration to a project", - "hidden": true - }, - "karma": { "factory": "./collection/karma", "schema": "./collection/karma/schema.json", diff --git a/packages/schematics/src/collection/application/index.ts b/packages/schematics/src/collection/application/index.ts index a1964ae29e..c7d53cca21 100644 --- a/packages/schematics/src/collection/application/index.ts +++ b/packages/schematics/src/collection/application/index.ts @@ -515,7 +515,7 @@ export default function(schema: Schema): Rule { updateDependencies(options), updateLinting(options), options.unitTestRunner === 'jest' - ? schematic('jest-project', { + ? externalSchematic('@nrwl/jest', 'jest-project', { project: options.name, supportTsx: false, skipSerializers: options.framework !== Framework.Angular, diff --git a/packages/schematics/src/collection/cypress-project/files/cypress.json b/packages/schematics/src/collection/cypress-project/files/cypress.json index ffd321dd84..f9c49a3d04 100644 --- a/packages/schematics/src/collection/cypress-project/files/cypress.json +++ b/packages/schematics/src/collection/cypress-project/files/cypress.json @@ -3,7 +3,7 @@ "fixturesFolder": "<%= offsetFromRoot %>dist/out-tsc/<%= projectRoot %>/src/fixtures", "integrationFolder": "<%= offsetFromRoot %>dist/out-tsc/<%= projectRoot %>/src/integration", "pluginsFile": "<%= offsetFromRoot %>dist/out-tsc/<%= projectRoot %>/src/plugins/index.js", - "supportFile": false, + "supportFile": false, "video": true, "videosFolder": "<%= offsetFromRoot %>dist/out-tsc/<%= projectRoot %>/videos", "screenshotsFolder": "<%= offsetFromRoot %>dist/out-tsc/<%= projectRoot %>/screenshots", diff --git a/packages/schematics/src/collection/cypress-project/files/tsconfig.e2e.json b/packages/schematics/src/collection/cypress-project/files/tsconfig.e2e.json index 4ae8a320ff..2496547c0e 100644 --- a/packages/schematics/src/collection/cypress-project/files/tsconfig.e2e.json +++ b/packages/schematics/src/collection/cypress-project/files/tsconfig.e2e.json @@ -4,7 +4,5 @@ "sourceMap": false, "outDir": "<%= offsetFromRoot %>dist/out-tsc/<%= projectRoot %>/src" }, - "include": [ - "src/**/*.ts" - ] + "include": ["src/**/*.ts"] } diff --git a/packages/schematics/src/collection/jest/index.ts b/packages/schematics/src/collection/jest/index.ts deleted file mode 100644 index f559a4b97c..0000000000 --- a/packages/schematics/src/collection/jest/index.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { mergeWith, chain, url } from '@angular-devkit/schematics'; -import { addDepsToPackageJson } from '../../utils/ast-utils'; -import { jestVersion, jestTypesVersion, nxVersion } from '../../lib-versions'; -import { Rule } from '@angular-devkit/schematics'; - -const updatePackageJson = addDepsToPackageJson( - {}, - { - '@nrwl/builders': nxVersion, - jest: jestVersion, - '@types/jest': jestTypesVersion - } -); - -export default function(): Rule { - return chain([mergeWith(url('./files')), updatePackageJson]); -} diff --git a/packages/schematics/src/collection/library/index.ts b/packages/schematics/src/collection/library/index.ts index 8f912a308d..703c2df867 100644 --- a/packages/schematics/src/collection/library/index.ts +++ b/packages/schematics/src/collection/library/index.ts @@ -450,7 +450,7 @@ export default function(schema: Schema): Rule { updateProject(options), updateTsConfig(options), options.unitTestRunner === 'jest' - ? schematic('jest-project', { + ? externalSchematic('@nrwl/jest', 'jest-project', { project: options.name, setupFile: options.framework === Framework.Angular ? 'angular' : 'none', diff --git a/packages/schematics/src/collection/node-application/index.ts b/packages/schematics/src/collection/node-application/index.ts index 7e490612e3..dc57b969eb 100644 --- a/packages/schematics/src/collection/node-application/index.ts +++ b/packages/schematics/src/collection/node-application/index.ts @@ -9,7 +9,8 @@ import { mergeWith, apply, url, - template + template, + externalSchematic } from '@angular-devkit/schematics'; import { join, normalize, Path } from '@angular-devkit/core'; import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks'; @@ -222,7 +223,7 @@ export default function(schema: Schema): Rule { updateNxJson(options), options.framework !== 'none' ? createSourceCode(options) : noop(), options.unitTestRunner === 'jest' - ? schematic('jest-project', { + ? externalSchematic('@nrwl/jest', 'jest-project', { project: options.name, setupFile: 'none', skipSerializers: true diff --git a/packages/schematics/src/collection/workspace/files/package.json b/packages/schematics/src/collection/workspace/files/package.json index 673065ff4d..b331b39477 100644 --- a/packages/schematics/src/collection/workspace/files/package.json +++ b/packages/schematics/src/collection/workspace/files/package.json @@ -27,8 +27,7 @@ "help": "./node_modules/.bin/nx help" }, "private": true, - "dependencies": { - }, + "dependencies": {}, "devDependencies": { "@angular/cli": "<%= angularCliVersion %>", "@nrwl/schematics": "<%= schematicsVersion %>", diff --git a/packages/schematics/src/collection/workspace/files/tslint.json b/packages/schematics/src/collection/workspace/files/tslint.json index 5406a76a30..7079558ab5 100644 --- a/packages/schematics/src/collection/workspace/files/tslint.json +++ b/packages/schematics/src/collection/workspace/files/tslint.json @@ -1,7 +1,5 @@ { - "rulesDirectory": [ - "node_modules/@nrwl/schematics/src/tslint" - ], + "rulesDirectory": ["node_modules/@nrwl/schematics/src/tslint"], "rules": { "arrow-return-shorthand": true, "callable-types": true, @@ -10,10 +8,7 @@ "severity": "warn" }, "forin": true, - "import-blacklist": [ - true, - "rxjs/Rx" - ], + "import-blacklist": [true, "rxjs/Rx"], "interface-over-type-literal": true, "member-access": false, "member-ordering": [ @@ -29,24 +24,14 @@ ], "no-arg": true, "no-bitwise": true, - "no-console": [ - true, - "debug", - "info", - "time", - "timeEnd", - "trace" - ], + "no-console": [true, "debug", "info", "time", "timeEnd", "trace"], "no-construct": true, "no-debugger": true, "no-duplicate-super": true, "no-empty": false, "no-empty-interface": true, "no-eval": true, - "no-inferrable-types": [ - true, - "ignore-params" - ], + "no-inferrable-types": [true, "ignore-params"], "no-misused-new": true, "no-non-null-assertion": true, "no-shadowed-variable": true, @@ -60,10 +45,7 @@ "object-literal-sort-keys": false, "prefer-const": true, "radix": true, - "triple-equals": [ - true, - "allow-null-check" - ], + "triple-equals": [true, "allow-null-check"], "unified-signatures": true, "variable-name": false, diff --git a/packages/schematics/src/lib-versions.ts b/packages/schematics/src/lib-versions.ts index 306de74b10..9861620035 100644 --- a/packages/schematics/src/lib-versions.ts +++ b/packages/schematics/src/lib-versions.ts @@ -10,10 +10,6 @@ export const latestMigration = '20180507-create-nx-json'; export const prettierVersion = '1.16.4'; export const typescriptVersion = '3.2.2'; export const rxjsVersion = '6.3.3'; -export const jestVersion = '24.1.0'; -export const jestTypesVersion = '24.0.9'; -export const jestPresetAngularVersion = '7.0.0'; -export const tsJestversion = '24.0.0'; export const jasmineMarblesVersion = '^0.4.0'; export const cypressVersion = '3.1.0'; diff --git a/scripts/documentation/builders.ts b/scripts/documentation/builders.ts index 35579e5ba3..6a1247b121 100644 --- a/scripts/documentation/builders.ts +++ b/scripts/documentation/builders.ts @@ -17,34 +17,36 @@ import { sortAlphabeticallyFunction } from './utils'; * parsable in order to be used in a rendering process using template. This * in order to generate a markdown file for each available schematic. */ -const buildersSourceDirectory = path.join( - __dirname, - '../../packages/builders/src' -); -const buildersOutputDirectory = path.join(__dirname, '../../docs/api-builders'); -const builderCollectionFile = path.join( - buildersSourceDirectory, - 'builders.json' -); -fs.removeSync(buildersOutputDirectory); -const builderCollection = fs.readJsonSync(builderCollectionFile).builders; +interface DocConfig { + source: string; + output: string; +} +const buildersConfig = { + source: path.join(__dirname, '../../packages/builders/src'), + output: path.join(__dirname, '../../docs/api-builders') +}; +const jestConfig = { + source: path.join(__dirname, '../../packages/jest'), + output: path.join(__dirname, '../../docs/api-jest/builders') +}; +const docConfigs: DocConfig[] = [buildersConfig, jestConfig]; const registry = new CoreSchemaRegistry(); registry.addFormat(pathFormat); registry.addFormat(htmlSelectorFormat); function generateSchematicList( - builderCollection: Schematic, + config: DocConfig, registry: CoreSchemaRegistry ): Promise[] { + const builderCollectionFile = path.join(config.source, 'builders.json'); + fs.removeSync(config.output); + const builderCollection = fs.readJsonSync(builderCollectionFile).builders; return Object.keys(builderCollection).map(builderName => { const builder = { name: builderName, ...builderCollection[builderName], rawSchema: fs.readJsonSync( - path.join( - buildersSourceDirectory, - builderCollection[builderName]['schema'] - ) + path.join(config.source, builderCollection[builderName]['schema']) ) }; @@ -105,10 +107,19 @@ function generateFile( ); } -Promise.all(generateSchematicList(builderCollection, registry)) - .then(builderList => builderList.map(generateTemplate)) - .then(markdownList => - markdownList.forEach(template => - generateFile(buildersOutputDirectory, template) - ) - ); +Promise.all( + docConfigs.map(config => { + Promise.all(generateSchematicList(config, registry)) + .then(builderList => builderList.map(generateTemplate)) + .then(markdownList => + markdownList.forEach(template => generateFile(config.output, template)) + ) + .then(() => + console.log( + `Generated documentation for ${config.source} to ${config.output}` + ) + ); + }) +).then(() => { + console.log('Done generating Builders Documentation'); +}); diff --git a/scripts/documentation/schematics.ts b/scripts/documentation/schematics.ts index 7678d6698b..a807e50d84 100644 --- a/scripts/documentation/schematics.ts +++ b/scripts/documentation/schematics.ts @@ -26,11 +26,15 @@ const reactConfig = { source: path.join(__dirname, '../../packages/react'), output: path.join(__dirname, '../../docs/api-react') }; +const jestConfig = { + source: path.join(__dirname, '../../packages/jest'), + output: path.join(__dirname, '../../docs/api-jest/schematics') +}; interface DocConfig { source: string; output: string; } -const docSections: DocConfig[] = [schematicsConfig, reactConfig]; +const docSections: DocConfig[] = [schematicsConfig, reactConfig, jestConfig]; const registry = new CoreSchemaRegistry(); registry.addFormat(pathFormat); registry.addFormat(htmlSelectorFormat); diff --git a/scripts/nx-release.js b/scripts/nx-release.js index 736c46dc34..0bac64c799 100755 --- a/scripts/nx-release.js +++ b/scripts/nx-release.js @@ -147,7 +147,9 @@ const options = { 'build/npm/builders/package.json', 'build/npm/nx/package.json', 'build/npm/schematics/package.json', - 'build/npm/create-nx-workspace/package.json' + 'build/npm/create-nx-workspace/package.json', + 'build/npm/jest/package.json', + 'build/npm/react/package.json' ], increment: parsedVersion.version, requireUpstream: false, diff --git a/scripts/package.sh b/scripts/package.sh index d214002baa..82fc162700 100755 --- a/scripts/package.sh +++ b/scripts/package.sh @@ -6,6 +6,10 @@ NX_VERSION=$1 ANGULAR_CLI_VERSION=$2 +if [[ $NX_VERSION == "--local" ]]; then + NX_VERSION="*" +fi + ./scripts/build.sh cd build/packages @@ -13,9 +17,8 @@ cd build/packages if [[ "$OSTYPE" == "darwin"* ]]; then sed -i "" "s|exports.nxVersion = '\*';|exports.nxVersion = '$NX_VERSION';|g" schematics/src/lib-versions.js sed -i "" "s|exports.schematicsVersion = '\*';|exports.schematicsVersion = '$NX_VERSION';|g" schematics/src/lib-versions.js - sed -i "" "s|exports.nxVersion = '\*';|exports.nxVersion = '$NX_VERSION';|g" react/src/utils/versions.js - sed -i "" "s|\*|$NX_VERSION|g" react/package.json - sed -i "" "s|\*|$NX_VERSION|g" schematics/package.json + sed -i "" "s|exports.nxVersion = '\*';|exports.nxVersion = '$NX_VERSION';|g" {react,jest}/src/utils/versions.js + sed -i "" "s|\*|$NX_VERSION|g" {schematics,react,jest}/package.json sed -i "" "s|NX_VERSION|$NX_VERSION|g" create-nx-workspace/bin/create-nx-workspace.js sed -i "" "s|ANGULAR_CLI_VERSION|$ANGULAR_CLI_VERSION|g" create-nx-workspace/bin/create-nx-workspace.js sed -i "" "s|NX_VERSION|$NX_VERSION|g" schematics/bin/create-nx-workspace.js @@ -23,15 +26,21 @@ if [[ "$OSTYPE" == "darwin"* ]]; then else sed -i "s|exports.nxVersion = '\*';|exports.nxVersion = '$NX_VERSION';|g" schematics/src/lib-versions.js sed -i "s|exports.schematicsVersion = '\*';|exports.schematicsVersion = '$NX_VERSION';|g" schematics/src/lib-versions.js - sed -i "s|exports.nxVersion = '\*';|exports.nxVersion = '$NX_VERSION';|g" react/src/utils/versions.js - sed -i "s|\*|$NX_VERSION|g" react/package.json - sed -i "s|\*|$NX_VERSION|g" schematics/package.json + sed -i "s|exports.nxVersion = '\*';|exports.nxVersion = '$NX_VERSION';|g" {react,jest}/src/utils/versions.js + sed -i "s|\*|$NX_VERSION|g" {schematics,react,jest}/package.json sed -i "s|NX_VERSION|$NX_VERSION|g" create-nx-workspace/bin/create-nx-workspace.js sed -i "s|ANGULAR_CLI_VERSION|$ANGULAR_CLI_VERSION|g" create-nx-workspace/bin/create-nx-workspace.js sed -i "s|NX_VERSION|$NX_VERSION|g" schematics/bin/create-nx-workspace.js sed -i "s|ANGULAR_CLI_VERSION|$ANGULAR_CLI_VERSION|g" schematics/bin/create-nx-workspace.js fi +if [[ $NX_VERSION == "*" ]]; then + if [[ "$OSTYPE" == "darwin"* ]]; then + sed -E -i "" "s/\"@nrwl\/([^\"]+)\": \"\\*\"/\"@nrwl\/\1\": \"file:..\/\1\"/" {schematics,jest,react}/package.json + else + sed -E -i "s/\"@nrwl\/([^\"]+)\": \"\\*\"/\"@nrwl\/\1\": \"file:..\/\1\"/" {schematics,jest,react}/package.json + fi +fi tar -czf nx.tgz nx tar -czf schematics.tgz schematics diff --git a/scripts/test_schematics.sh b/scripts/test_schematics.sh index 2ab7c0ed27..9a6944c09d 100755 --- a/scripts/test_schematics.sh +++ b/scripts/test_schematics.sh @@ -3,5 +3,5 @@ if [ -n "$1" ]; then jest --maxWorkers=1 ./build/packages/$1.spec.js else - jest --maxWorkers=1 ./build/packages/{schematics,bazel,builders,react} + jest --maxWorkers=1 ./build/packages/{schematics,bazel,builders,jest,react} fi