Skip to content

Commit

Permalink
refactor(testing): separate jest to @nrwl/jest
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored and vsavkin committed Apr 16, 2019
1 parent 9ee487d commit 625b283
Show file tree
Hide file tree
Showing 49 changed files with 315 additions and 206 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# jest-project [hidden]
# jest-project

Add Jest configuration to a project

Expand All @@ -19,7 +19,7 @@ The name of the project.

### setupFile

Default: `angular`
Default: `none`

Type: `string`

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# jest [hidden]
# ng-add [hidden]

Add Jest configuration to the workspace

## Usage

```bash
ng generate jest ...
ng generate ng-add ...

```
36 changes: 10 additions & 26 deletions e2e/schematics/jest.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
12 changes: 4 additions & 8 deletions e2e/schematics/ng-new.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import {
newApp,
newLib,
newProject,
readJson,
runCLI,
updateFile,
exists,
runNgNew,
cleanup,
copyMissingPackages,
getSize,
expectTestsPass,
runCLIAsync,
Expand All @@ -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`);
Expand Down Expand Up @@ -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`);
Expand All @@ -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');
Expand Down
2 changes: 2 additions & 0 deletions e2e/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 0 additions & 5 deletions packages/builders/src/builders.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions packages/jest/builders.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
17 changes: 17 additions & 0 deletions packages/jest/collection.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
Empty file added packages/jest/index.ts
Empty file.
3 changes: 3 additions & 0 deletions packages/jest/migrations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"schematics": {}
}
41 changes: 41 additions & 0 deletions packages/jest/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"title": "Jest Target",
"title": "Jest Builder",
"description": "Jest target options for Build Facade",
"type": "object",
"properties": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
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;

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
);
});
Expand All @@ -22,7 +42,8 @@ describe('jestProject', () => {
const resultTree = await runSchematic(
'jest-project',
{
project: 'lib1'
project: 'lib1',
setupFile: 'angular'
},
appTree
);
Expand All @@ -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',
Expand Down Expand Up @@ -91,7 +113,8 @@ describe('jestProject', () => {
const resultTree = await runSchematic(
'jest-project',
{
project: 'lib1'
project: 'lib1',
setupFile: 'angular'
},
appTree
);
Expand Down Expand Up @@ -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', () => {
Expand Down
Loading

0 comments on commit 625b283

Please sign in to comment.