diff --git a/README.md b/README.md index e91fb10..7547e60 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ Dynamic page title & meta tags generator for **Angular** [![Linux build](https://travis-ci.org/fulls1z3/ngx-meta.svg?branch=v0.2.x)](https://travis-ci.org/fulls1z3/ngx-meta) [![Windows build](https://ci.appveyor.com/api/projects/status/github/fulls1z3/ngx-meta?branch=v0.2.x&svg=true)](https://ci.appveyor.com/project/fulls1z3/ngx-meta) [![coverage](https://codecov.io/github/fulls1z3/ngx-meta/coverage.svg?branch=v0.2.x)](https://codecov.io/gh/fulls1z3/ngx-meta) +[![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest) [![Angular Style Guide](https://mgechev.github.io/angular2-style-guide/images/badge.svg)](https://angular.io/styleguide) > Please support this project by simply putting a Github star. Share this library with friends on Twitter and everywhere else you can. diff --git a/config/webpack.test.js b/config/webpack.test.js deleted file mode 100644 index 9bb0ed4..0000000 --- a/config/webpack.test.js +++ /dev/null @@ -1,88 +0,0 @@ -const helpers = require('./helpers'); - -const contextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin'), - loaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin'); - -module.exports = (group, item, settings) => { - return { - devtool: 'inline-source-map', - resolve: { - extensions: ['.ts', '.js'], - modules: [ - helpers.root(`packages/${group}/${item}/src`), - helpers.root(`packages/${group}/${item}/node_modules`), - helpers.root('node_modules') - ] - }, - module: { - rules: [ - { - enforce: 'pre', - test: /\.js$/, - use: 'source-map-loader', - exclude: [ - // these packages have problems with their sourcemaps - helpers.root(`packages/${group}/${item}/node_modules/rxjs`), - helpers.root('node_modules/rxjs'), - helpers.root(`packages/${group}/${item}/node_modules/@angular`), - helpers.root('node_modules/@angular'), - ] - }, - { - test: /\.ts$/, - use: [ - { - loader: 'awesome-typescript-loader', - query: { - // use inline sourcemaps for "karma-remap-coverage" reporter - sourceMap: false, - inlineSourceMap: true, - compilerOptions: { - // remove TypeScript helpers to be injected - // below by DefinePlugin - removeComments: true - }, - configFileName: helpers.root(`packages/${group}/${item}/tests/tsconfig.json`) - } - } - ], - exclude: [/\.e2e\.ts$/] - }, - { - enforce: 'post', - test: /\.(js|ts)$/, - use: 'istanbul-instrumenter-loader?esModules', - include: helpers.root(`packages/${group}/${item}/src`), - exclude: [ - /\.(e2e|spec)\.ts$/, - /node_modules/ - ] - } - ] - }, - plugins: [ - new contextReplacementPlugin( - // fix the warning in ./~/@angular/core/src/linker/system_js_ng_module_factory_loader.js - settings.angularVersion === 2 - ? /angular([\\\/])core([\\\/])(esm([\\\/])src|src)([\\\/])linker/ - : /angular([\\\/])core([\\\/])@angular/, - helpers.root(`packages/${group}/${item}/src`) - ), - new loaderOptionsPlugin({ - debug: true, - options: {} - }) - ], - performance: { - hints: false - }, - node: { - global: true, - crypto: 'empty', - process: false, - module: false, - clearImmediate: false, - setImmediate: false - } - }; -}; diff --git a/package.json b/package.json index dd2d8c9..848e4e2 100644 --- a/package.json +++ b/package.json @@ -20,11 +20,12 @@ "clean": "gulp clean --color", "lint": "gulp tslint --color", "make": "gulp make --color", - "test": "gulp test --color" + "test": "jest --runInBand --colors" }, "devDependencies": { "@angular/common": "~2.4.0", "@angular/compiler": "~2.4.0", + "@angular/compiler-cli": "~2.4.0", "@angular/core": "~2.4.0", "@angular/platform-browser": "~2.4.0", "@angular/platform-browser-dynamic": "~2.4.0", @@ -32,7 +33,9 @@ "core-js": "^2.4.1", "rxjs": "^5.0.1", "zone.js": "^0.7.2", - "@types/jasmine": "^2.5.54", + "lodash": "^4.17.4", + "@types/lodash": "4.14.55", + "@types/jest": "^20.0.8", "gulp": "gulpjs/gulp#4.0", "gulp-load-plugins": "^1.5.0", "rimraf": "^2.6.1", @@ -41,20 +44,28 @@ "webpack": "^3.5.5", "tslint-loader": "^3.5.3", "awesome-typescript-loader": "^3.2.3", - "jasmine-core": "^2.8.0", - "karma": "^1.7.1", - "karma-jasmine": "^1.1.0", - "karma-coverage": "^1.1.1", - "karma-mocha-reporter": "^2.2.4", - "karma-coverage-istanbul-reporter": "^1.3.0", - "karma-sourcemap-loader": "^0.3.7", - "karma-webpack": "^2.0.4", - "karma-chrome-launcher": "^2.2.0", - "source-map-loader": "^0.2.1", - "istanbul-instrumenter-loader": "^3.0.0", + "jest": "^20.0.4", + "jest-preset-angular": "^2.0.5", "codelyzer": "^3.1.2", "tslint": "~5.7.0", "angular-tslint-rules": "^1.0.4", "typescript": "~2.1.6" + }, + "jest": { + "preset": "jest-preset-angular", + "setupTestFrameworkScriptFile": "./config/jest.setup.ts", + "globals": { + "ts-jest": { + "tsConfigFile": "./tsconfig.json" + }, + "__TRANSFORM_HTML__": true + }, + "cache": false, + "noStackTrace": true, + "silent": true, + "collectCoverage": true, + "collectCoverageFrom": [ + "packages/@ngx-meta/core/src/**.ts" + ] } } diff --git a/packages/@ngx-meta/core/config/karma.conf.js b/packages/@ngx-meta/core/config/karma.conf.js deleted file mode 100644 index a5ffa3c..0000000 --- a/packages/@ngx-meta/core/config/karma.conf.js +++ /dev/null @@ -1,51 +0,0 @@ -module.exports = function(config) { - const configuration = { - basePath: '', - frameworks: ['jasmine'], - exclude: [], - client: {captureConsole: false}, - files: [ - { - pattern: './spec-bundle.js', - watched: false - } - ], - preprocessors: { - './spec-bundle.js': [ - 'coverage', - 'webpack', - 'sourcemap' - ] - }, - webpackMiddleware: { - noInfo: true, - stats: {chunks: false} - }, - reporters: [ - 'mocha', - 'coverage', - 'coverage-istanbul' - ], - coverageReporter: {type: 'in-memory'}, - port: 9876, - colors: true, - logLevel: config.LOG_WARN, - autoWatch: false, - browsers: ['Chrome'], - customLaunchers: { - ChromeTravisCi: { - base: 'Chrome', - flags: ['--no-sandbox'] - } - }, - singleRun: true - }; - - if (process.env.TRAVIS) { - configuration.browsers = [ - 'ChromeTravisCi' - ]; - } - - config.set(configuration); -}; diff --git a/packages/@ngx-meta/core/config/spec-bundle.js b/packages/@ngx-meta/core/config/spec-bundle.js deleted file mode 100644 index 1f36095..0000000 --- a/packages/@ngx-meta/core/config/spec-bundle.js +++ /dev/null @@ -1,28 +0,0 @@ -Error.stackTraceLimit = Infinity; - -require('core-js/es6'); -require('core-js/es7/reflect'); -require('zone.js/dist/zone'); -require('zone.js/dist/long-stack-trace-zone'); -require('zone.js/dist/proxy'); -require('zone.js/dist/sync-test'); -require('zone.js/dist/jasmine-patch'); -require('zone.js/dist/async-test'); -require('zone.js/dist/fake-async-test'); -require('rxjs/Rx'); - -const testing = require('@angular/core/testing'), - browser = require('@angular/platform-browser-dynamic/testing'); - -testing.TestBed.initTestEnvironment( - browser.BrowserDynamicTestingModule, - browser.platformBrowserDynamicTesting() -); - -const testContext = require.context('../tests', true, /\.spec\.ts/); - -function requireAll(requireContext) { - return requireContext.keys().map(requireContext); -} - -requireAll(testContext); diff --git a/packages/@ngx-meta/core/tests/index.spec.ts b/packages/@ngx-meta/core/tests/common.ts similarity index 100% rename from packages/@ngx-meta/core/tests/index.spec.ts rename to packages/@ngx-meta/core/tests/common.ts diff --git a/packages/@ngx-meta/core/tests/meta.loader.spec.ts b/packages/@ngx-meta/core/tests/meta.loader.spec.ts index f9ea3a5..dadab6c 100644 --- a/packages/@ngx-meta/core/tests/meta.loader.spec.ts +++ b/packages/@ngx-meta/core/tests/meta.loader.spec.ts @@ -1,9 +1,9 @@ // angular -import { getTestBed } from '@angular/core/testing'; +import { TestBed } from '@angular/core/testing'; // module import { MetaLoader, MetaService, MetaSettings, MetaStaticLoader } from '../index'; -import { defaultSettings, emptySettings, testModuleConfig, testSettings } from './index.spec'; +import { defaultSettings, emptySettings, testModuleConfig, testSettings } from './common'; describe('@ngx-meta/core:', () => { @@ -35,8 +35,7 @@ describe('@ngx-meta/core:', useFactory: (metaFactory) }); - const injector = getTestBed(); - const meta = injector.get(MetaService); + const meta = TestBed.get(MetaService); expect(MetaStaticLoader).toBeDefined(); expect(meta.loader).toBeDefined(); @@ -56,8 +55,7 @@ describe('@ngx-meta/core:', useClass: CustomLoader }); - const injector = getTestBed(); - const meta = injector.get(MetaService); + const meta = TestBed.get(MetaService); expect(CustomLoader).toBeDefined(); expect(meta.loader).toBeDefined(); diff --git a/packages/@ngx-meta/core/tests/meta.service.spec.ts b/packages/@ngx-meta/core/tests/meta.service.spec.ts index 3b9749e..8b3e61e 100644 --- a/packages/@ngx-meta/core/tests/meta.service.spec.ts +++ b/packages/@ngx-meta/core/tests/meta.service.spec.ts @@ -1,5 +1,5 @@ // angular -import { fakeAsync, getTestBed, inject, TestBed } from '@angular/core/testing'; +import { fakeAsync, inject, TestBed } from '@angular/core/testing'; import { Title } from '@angular/platform-browser'; import { Router } from '@angular/router'; @@ -11,7 +11,7 @@ import * as _ from 'lodash'; // module import { MetaLoader, MetaService, MetaStaticLoader, PageTitlePositioning } from '../index'; import { MetaHelper } from '../src/meta.helper'; -import { defaultSettings, emptySettings, TestBootstrapComponent, testModuleConfig, testSettings } from './index.spec'; +import { defaultSettings, emptySettings, TestBootstrapComponent, testModuleConfig, testSettings } from './common'; describe('@ngx-meta/core:', () => { @@ -51,8 +51,7 @@ describe('@ngx-meta/core:', it('should be able to set meta tags using routes', fakeAsync(inject([MetaHelper, Title], (meta: MetaHelper, title: Title) => { - const injector = getTestBed(); - const router = injector.get(Router); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -100,8 +99,7 @@ describe('@ngx-meta/core:', it('should be able to set meta tags using routes w/o `meta` property', fakeAsync(inject([MetaHelper, Title], (meta: MetaHelper, title: Title) => { - const injector = getTestBed(); - const router = injector.get(Router); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -126,8 +124,7 @@ describe('@ngx-meta/core:', useFactory: (metaFactory) }); - const injector = getTestBed(); - const router = injector.get(Router); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -156,8 +153,7 @@ describe('@ngx-meta/core:', useFactory: (metaFactory) }); - const injector = getTestBed(); - const router = injector.get(Router); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -182,8 +178,7 @@ describe('@ngx-meta/core:', useFactory: (metaFactory) }); - const injector = getTestBed(); - const router = injector.get(Router); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -198,8 +193,7 @@ describe('@ngx-meta/core:', it('should be able to set the `title`', fakeAsync(inject([MetaService, Title], (metaService: MetaService, title: Title) => { - const injector = getTestBed(); - const router = injector.get(Router); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -236,9 +230,8 @@ describe('@ngx-meta/core:', useFactory: (metaFactory) }); - const injector = getTestBed(); - const metaService = injector.get(MetaService); - const router = injector.get(Router); + const metaService = TestBed.get(MetaService); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -273,9 +266,8 @@ describe('@ngx-meta/core:', useFactory: (metaFactory) }); - const injector = getTestBed(); - const metaService = injector.get(MetaService); - const router = injector.get(Router); + const metaService = TestBed.get(MetaService); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -300,9 +292,8 @@ describe('@ngx-meta/core:', useFactory: (metaFactory) }); - const injector = getTestBed(); - const metaService = injector.get(MetaService); - const router = injector.get(Router); + const metaService = TestBed.get(MetaService); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -326,8 +317,7 @@ describe('@ngx-meta/core:', useFactory: (metaFactory) }); - const injector = getTestBed(); - const metaService = injector.get(MetaService); + const metaService = TestBed.get(MetaService); expect(() => metaService.setTitle('')).toThrowError('Invalid pageTitlePositioning specified [undefined]!'); }); @@ -343,8 +333,7 @@ describe('@ngx-meta/core:', it('should be able to set meta `description`', fakeAsync(inject([MetaService, MetaHelper], (metaService: MetaService, meta: MetaHelper) => { - const injector = getTestBed(); - const router = injector.get(Router); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -374,9 +363,8 @@ describe('@ngx-meta/core:', useFactory: (metaFactory) }); - const injector = getTestBed(); - const metaService = injector.get(MetaService); - const router = injector.get(Router); + const metaService = TestBed.get(MetaService); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -391,8 +379,7 @@ describe('@ngx-meta/core:', it('should be able to set meta `author`', fakeAsync(inject([MetaService, MetaHelper], (metaService: MetaService, meta: MetaHelper) => { - const injector = getTestBed(); - const router = injector.get(Router); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -413,8 +400,7 @@ describe('@ngx-meta/core:', it('should be able to set meta `publisher`', fakeAsync(inject([MetaService, MetaHelper], (metaService: MetaService, meta: MetaHelper) => { - const injector = getTestBed(); - const router = injector.get(Router); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -435,8 +421,7 @@ describe('@ngx-meta/core:', it('should be able to set `og:locale`', fakeAsync(inject([MetaService, MetaHelper], (metaService: MetaService, meta: MetaHelper) => { - const injector = getTestBed(); - const router = injector.get(Router); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -469,8 +454,7 @@ describe('@ngx-meta/core:', it('should be able to set `og:locale:alternate` w/ `og:locale:alternate`', fakeAsync(inject([MetaService, MetaHelper], (metaService: MetaService, meta: MetaHelper) => { - const injector = getTestBed(); - const router = injector.get(Router); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -503,9 +487,8 @@ describe('@ngx-meta/core:', useFactory: (metaFactory) }); - const injector = getTestBed(); - const router = injector.get(Router); - const metaService = injector.get(MetaService); + const router = TestBed.get(Router); + const metaService = TestBed.get(MetaService); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -536,8 +519,7 @@ describe('@ngx-meta/core:', useFactory: (metaFactory) }); - const injector = getTestBed(); - const metaService = injector.get(MetaService); + const metaService = TestBed.get(MetaService); expect(meta.getMetaElement('property="og:locale"').content).toEqual('tr_TR'); @@ -558,8 +540,7 @@ describe('@ngx-meta/core:', useFactory: (metaFactory) }); - const injector = getTestBed(); - const router = injector.get(Router); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -576,8 +557,7 @@ describe('@ngx-meta/core:', it('should be able to set any other meta tag', fakeAsync(inject([MetaService, MetaHelper], (metaService: MetaService, meta: MetaHelper) => { - const injector = getTestBed(); - const router = injector.get(Router); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -612,9 +592,8 @@ describe('@ngx-meta/core:', useFactory: (metaFactory) }); - const injector = getTestBed(); - const metaService = injector.get(MetaService); - const router = injector.get(Router); + const metaService = TestBed.get(MetaService); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -643,9 +622,8 @@ describe('@ngx-meta/core:', useFactory: (metaFactory) }); - const injector = getTestBed(); - const metaService = injector.get(MetaService); - const router = injector.get(Router); + const metaService = TestBed.get(MetaService); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -671,8 +649,7 @@ describe('@ngx-meta/core:', useFactory: (metaFactory) }); - const injector = getTestBed(); - const router = injector.get(Router); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); @@ -695,8 +672,7 @@ describe('@ngx-meta/core:', useFactory: (metaFactory) }); - const injector = getTestBed(); - const router = injector.get(Router); + const router = TestBed.get(Router); const fixture = TestBed.createComponent(TestBootstrapComponent); fixture.detectChanges(); diff --git a/packages/@ngx-meta/core/tests/util.spec.ts b/packages/@ngx-meta/core/tests/util.spec.ts index 6d1cf56..80ead9b 100644 --- a/packages/@ngx-meta/core/tests/util.spec.ts +++ b/packages/@ngx-meta/core/tests/util.spec.ts @@ -1,5 +1,3 @@ -/* tslint:disable */ - // libs import { Observable } from 'rxjs/Observable'; import 'rxjs/add/observable/of'; @@ -18,6 +16,7 @@ describe('@ngx-meta/core:', () => { () => { expect(isPromise({ then: () => { + // NOTE: go on } })).toEqual(true); }); @@ -35,6 +34,7 @@ describe('@ngx-meta/core:', () => { it('should be false if the argument is undefined or null', () => { expect(isPromise(undefined)).toEqual(false); + // tslint:disable-next-line expect(isPromise(null)).toEqual(false); }); }); @@ -52,6 +52,7 @@ describe('@ngx-meta/core:', () => { it('should be false if the argument is null', () => { + // tslint:disable-next-line expect(isObservable(null)).toEqual(false); }); @@ -63,6 +64,7 @@ describe('@ngx-meta/core:', () => { it('should be false if the argument is a function', () => { expect(isObservable(() => { + // NOTE: go on })).toEqual(false); }); diff --git a/packages/@ngx-meta/core/tsconfig.json b/packages/@ngx-meta/core/tsconfig.json deleted file mode 100644 index 720d03e..0000000 --- a/packages/@ngx-meta/core/tsconfig.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "compilerOptions": { - "moduleResolution": "node", - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "module": "es2015", - "target": "es5", - "noImplicitAny": true, - "suppressImplicitAnyIndexErrors": true, - "importHelpers": true, - "declaration": true, - "removeComments": true, - "typeRoots": [ - "../../../node_modules/@types" - ], - "lib": [ - "es2017", - "dom" - ] - }, - "files": [ - "./index.ts", - "./src/models/meta-definition.ts", - "./src/models/meta-settings.ts", - "./src/models/page-title-positioning.ts", - "./src/meta.helper.ts", - "./src/meta.loader.ts", - "./src/meta.service.ts", - "./src/util.ts" - ], - "exclude": [ - "node_modules" - ], - "angularCompilerOptions": { - "strictMetadataEmit": true, - "skipTemplateCodegen": true - }, - "compileOnSave": false, - "buildOnSave": false -} diff --git a/tools/test/jest.setup.ts b/tools/test/jest.setup.ts new file mode 100644 index 0000000..d16a9f7 --- /dev/null +++ b/tools/test/jest.setup.ts @@ -0,0 +1,29 @@ +import 'jest-preset-angular'; + +const mock = () => { + let storage = {}; + + return { + getItem: (key: string) => key in storage ? (storage as any)[key] : undefined, + setItem: (key: string, value: any) => (storage as any)[key] = value || '', + removeItem: (key: string) => delete (storage as any)[key], + clear: () => storage = {} + }; +}; + +Object.defineProperty(window, 'CSS', {value: mock()}); +Object.defineProperty(window, 'localStorage', {value: mock()}); +Object.defineProperty(window, 'sessionStorage', {value: mock()}); + +Object.defineProperty(document, 'doctype', { + value: '' +}); + +Object.defineProperty(window, 'getComputedStyle', { + value: () => { + return { + display: 'none', + appearance: ['-webkit-appearance'] + }; + } +}); diff --git a/packages/@ngx-meta/core/tests/tsconfig.json b/tsconfig.json similarity index 71% rename from packages/@ngx-meta/core/tests/tsconfig.json rename to tsconfig.json index f8a2283..acc36b0 100644 --- a/packages/@ngx-meta/core/tests/tsconfig.json +++ b/tsconfig.json @@ -1,25 +1,23 @@ { "compilerOptions": { + "target": "es5", + "module": "commonjs", "moduleResolution": "node", "emitDecoratorMetadata": true, "experimentalDecorators": true, - "module": "commonjs", - "target": "es5", "noImplicitAny": true, "suppressImplicitAnyIndexErrors": true, "importHelpers": true, - "removeComments": true, - "typeRoots": [ - "../../../../node_modules/@types" - ], "lib": [ "es2017", "dom" ] }, + "include": [ + "scripts/*.ts", + "packages/**/*.ts" + ], "exclude": [ "node_modules" - ], - "compileOnSave": false, - "buildOnSave": false + ] }