Skip to content

Commit

Permalink
Merge pull request #5 from kreuzerk/feature/migrateToJest
Browse files Browse the repository at this point in the history
Feature/migrate to jest
  • Loading branch information
nivekcode authored Apr 30, 2020
2 parents 60c3df0 + 0407e5f commit 09ffe8b
Show file tree
Hide file tree
Showing 7 changed files with 6,042 additions and 1,846 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ yarn-error.log*
# Mac OSX Finder files.
**/.DS_Store
.DS_Store

# Coverage
coverage/
8 changes: 8 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
roots: ['<rootDir>/src'],
transform: {
'^.+\\.tsx?$': 'ts-jest'
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
};
7,779 changes: 5,990 additions & 1,789 deletions package-lock.json

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"build:watch": "tsc -p tsconfig.json --watch",
"format": "prettier src/**/*.{ts,json,md} --write",
"format:test": "prettier src/**/*.{ts,json,md} --list-different",
"test": "mocha src/**/*.spec.ts --require ts-node/register --async-only",
"test:watch": "mocha src/**/*.spec.ts --require ts-node/register --watch-extensions ts --watch"
"test": "jest --collectCoverage",
"test:watch": "jest --watch"
},
"husky": {
"hooks": {
Expand All @@ -30,16 +30,15 @@
"typescript": "^3.8.3"
},
"devDependencies": {
"@types/chai": "^4.2.11",
"@types/mocha": "^5.2.7",
"@types/jest": "^25.2.1",
"@types/node": "^8.10.59",
"chai": "^4.2.0",
"husky": "^4.2.5",
"mocha": "^7.0.0",
"jest": "^25.5.2",
"prettier": "^1.19.1",
"pretty-quick": "^2.0.1",
"ts-node": "^8.6.2",
"semantic-release": "^17.0.7"
"semantic-release": "^17.0.7",
"ts-jest": "^25.4.0",
"ts-node": "^8.6.2"
},
"repository": {
"type": "git",
Expand Down
45 changes: 20 additions & 25 deletions src/ng-samurai/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as path from 'path';
import { expect } from 'chai';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { Schema as WorkspaceOptions } from '@schematics/angular/workspace/schema';
import { Schema as LibraryOptions } from '@schematics/angular/library/schema';
Expand Down Expand Up @@ -83,10 +82,6 @@ describe('ng-samurai', () => {
.toPromise();
}

function addModelFile(path: string, content: string) {
appTree.create(path, content);
}

function removeDefaultLibraryModule() {
appTree.delete('/projects/some-lib/src/lib/some-lib.module.ts');
appTree.delete('/projects/some-lib/src/lib/some-lib.component.spec.ts');
Expand Down Expand Up @@ -115,7 +110,7 @@ describe('ng-samurai', () => {
'some-lib/src/lib/bar'
]);

expect(topLevelPublicAPIContent).to.equal(expectedTopLevelPublicAPIContent);
expect(topLevelPublicAPIContent).toEqual(expectedTopLevelPublicAPIContent);
});
});

Expand All @@ -130,17 +125,17 @@ describe('ng-samurai', () => {

it('should add a public_api to foo module', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
expect(tree.exists('/projects/some-lib/src/lib/foo/public-api.ts')).to.be.true;
expect(tree.exists('/projects/some-lib/src/lib/foo/public-api.ts')).toBe(true);
});

it('should add a public_api to bar module', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
expect(tree.exists('/projects/some-lib/src/lib/bar/public-api.ts')).to.be.true;
expect(tree.exists('/projects/some-lib/src/lib/bar/public-api.ts')).toBe(true);
});

it('should not add a public_api to baz module', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
expect(tree.exists('/projects/some-lib/src/lib/bar/baz/public-api.ts')).not.to.be.true;
expect(tree.exists('/projects/some-lib/src/lib/bar/baz/public-api.ts')).not.toBe(true);
});

it('should export foo.component.ts and foo.module.ts from foos public-api', async () => {
Expand All @@ -151,7 +146,7 @@ describe('ng-samurai', () => {
expectedFilesIncludedInPublicAPI
);

expect(publicAPI).to.equal(expectedFileContent);
expect(publicAPI).toEqual(expectedFileContent);
});

it('should export bar.component.ts, bar.module.ts, bar.model and baz.component.ts from bars public-api', async () => {
Expand All @@ -167,46 +162,46 @@ describe('ng-samurai', () => {
expectedFilesIncludedInPublicAPI
);

expect(publicAPI).to.equal(expectedFileContent);
expect(publicAPI).toEqual(expectedFileContent);
});
});
});

describe('index.ts', () => {
it('should add an index.ts to foo module', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
expect(tree.exists('/projects/some-lib/src/lib/foo/index.ts')).to.be.true;
expect(tree.exists('/projects/some-lib/src/lib/foo/index.ts')).toBe(true);
});

it('should add export everything from public-api inside the index.ts of foo', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
expect(tree.read('/projects/some-lib/src/lib/foo/index.ts').toString()).to.equal(
expect(tree.read('/projects/some-lib/src/lib/foo/index.ts').toString()).toEqual(
"export * from './public-api';\n"
);
});

it('should add an index.ts bar module', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
expect(tree.exists('/projects/some-lib/src/lib/bar/index.ts')).to.be.true;
expect(tree.exists('/projects/some-lib/src/lib/bar/index.ts')).toBe(true);
});

it('should add export everything from public-api inside the index.ts of bar', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
expect(tree.read('/projects/some-lib/src/lib/bar/index.ts').toString()).to.equal(
expect(tree.read('/projects/some-lib/src/lib/bar/index.ts').toString()).toEqual(
"export * from './public-api';\n"
);
});

it('should not add an index.ts to baz module', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
expect(tree.exists('/projects/some-lib/src/lib/bar/baz/index.ts')).not.to.be.true;
expect(tree.exists('/projects/some-lib/src/lib/bar/baz/index.ts')).not.toBe(true);
});
});

describe('package.json', () => {
it('should add an index.ts to foo module', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
expect(tree.exists('/projects/some-lib/src/lib/foo/package.json')).to.be.true;
expect(tree.exists('/projects/some-lib/src/lib/foo/package.json')).toBe(true);
});

it('should add the correct config to the package.json of foo subentry', async () => {
Expand All @@ -221,12 +216,12 @@ describe('ng-samurai', () => {
const subEntryConfig = JSON.parse(
tree.read('/projects/some-lib/src/lib/foo/package.json').toString()
);
expect(subEntryConfig).to.eql(expectedSubentryConfig);
expect(subEntryConfig).toEqual(expectedSubentryConfig);
});

it('should add an packag.json to bar module', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
expect(tree.exists('/projects/some-lib/src/lib/bar/package.json')).to.be.true;
expect(tree.exists('/projects/some-lib/src/lib/bar/package.json')).toBe(true);
});

it('should add the correct config to the package.json of bar subentry', async () => {
Expand All @@ -241,12 +236,12 @@ describe('ng-samurai', () => {
const subEntryConfig = JSON.parse(
tree.read('/projects/some-lib/src/lib/bar/package.json').toString()
);
expect(subEntryConfig).to.eql(expectedSubentryConfig);
expect(subEntryConfig).toEqual(expectedSubentryConfig);
});

it('should not add a package.json to baz module', async () => {
const tree = await runner.runSchematicAsync('ng-samurai', {}, appTree).toPromise();
expect(tree.exists('/projects/some-lib/src/lib/bar/baz/package.json')).not.to.be.true;
expect(tree.exists('/projects/some-lib/src/lib/bar/baz/package.json')).not.toBe(true);
});
});

Expand Down Expand Up @@ -293,7 +288,7 @@ describe('ng-samurai', () => {
'/projects/some-lib/src/lib/bar/bar.module.ts'
);

expect(moduleContentAfterSchematics).to.equal(expectedModuleContent);
expect(moduleContentAfterSchematics).toEqual(expectedModuleContent);
});

it('should not update the baz components content since the import paths do not need to be updated', async () => {
Expand All @@ -306,7 +301,7 @@ describe('ng-samurai', () => {
'/projects/some-lib/src/lib/bar/baz/baz.component.ts'
);

expect(componentContentAfterSchematics).to.equal(expectedComponentContent);
expect(componentContentAfterSchematics).toEqual(expectedComponentContent);
});
});

Expand All @@ -326,7 +321,7 @@ describe('ng-samurai', () => {
};

const paths = tsconfigContent.compilerOptions.paths;
expect(paths).to.eql(expectedPaths);
expect(paths).toEqual(expectedPaths);
});

it('should add paths to the tsconfig.json even if no path exist', async () => {
Expand All @@ -338,7 +333,7 @@ describe('ng-samurai', () => {
};

const paths = tsconfigContent.compilerOptions.paths;
expect(paths).to.eql(expectedPaths);
expect(paths).toEqual(expectedPaths);
});
});
});
19 changes: 10 additions & 9 deletions src/submodule/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as path from 'path';
import { expect } from 'chai';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { Style, Schema as ApplicationOptions } from '@schematics/angular/application/schema';
import { Schema as LibraryOptions } from '@schematics/angular/library/schema';
Expand Down Expand Up @@ -52,30 +51,32 @@ describe('submodule', () => {

const tree = await runner.runSchematicAsync('submodule', options, appTree).toPromise();

expect(tree.files.includes('/projects/some-lib/src/lib/path/to/customer/customer.component.ts'))
.to.be.true;
expect(
tree.files.includes('/projects/some-lib/src/lib/path/to/customer/customer.component.ts')
).toBe(true);
});

it('should generate a CustomerModule and add a CustomerComponent', async () => {
const options = { ...defaultOptions };

const tree = await runner.runSchematicAsync('submodule', options, appTree).toPromise();

expect(tree.files.includes('/projects/some-lib/src/lib/path/to/customer/customer.module.ts')).to
.be.true;
expect(
tree.files.includes('/projects/some-lib/src/lib/path/to/customer/customer.module.ts')
).toBe(true);
expect(
tree
.readContent('/projects/some-lib/src/lib/path/to/customer/customer.module.ts')
.includes('CustomerComponent')
).to.be.true;
).toBe(true);
});

it('should export everything from public-api inside index.ts', async () => {
const options = { ...defaultOptions };
const expectedContent = "export * from './public-api';\n";

const tree = await runner.runSchematicAsync('submodule', options, appTree).toPromise();
expect(tree.readContent('/projects/some-lib/src/lib/path/to/customer/index.ts')).to.equal(
expect(tree.readContent('/projects/some-lib/src/lib/path/to/customer/index.ts')).toEqual(
expectedContent
);
});
Expand All @@ -86,7 +87,7 @@ describe('submodule', () => {
"export * from './customer.module';\nexport * from './customer.component';\n";

const tree = await runner.runSchematicAsync('submodule', options, appTree).toPromise();
expect(tree.readContent('/projects/some-lib/src/lib/path/to/customer/public-api.ts')).to.equal(
expect(tree.readContent('/projects/some-lib/src/lib/path/to/customer/public-api.ts')).toEqual(
expectedContent
);
});
Expand All @@ -104,6 +105,6 @@ describe('submodule', () => {
const tree = await runner.runSchematicAsync('submodule', options, appTree).toPromise();
expect(
JSON.parse(tree.readContent('/projects/some-lib/src/lib/path/to/customer/package.json'))
).to.eql(expectedContent);
).toEqual(expectedContent);
});
});
19 changes: 4 additions & 15 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{
"compilerOptions": {
"baseUrl": "tsconfig",
"lib": [
"es2018",
"dom"
],
"lib": ["es2018", "dom"],
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
Expand All @@ -20,16 +17,8 @@
"sourceMap": true,
"strictNullChecks": false,
"target": "es6",
"types": [
"mocha",
"node",
"chai"
]
"types": ["jest", "node"]
},
"include": [
"src/**/*"
],
"exclude": [
"src/*/files/**/*"
]
"include": ["src/**/*"],
"exclude": ["src/*/files/**/*"]
}

0 comments on commit 09ffe8b

Please sign in to comment.