From 47bcb5bc356eaed6922fa5a72be56d6915d48ebd Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Sat, 16 Dec 2017 11:35:47 -0800 Subject: [PATCH] build(core): add bazel test targets for core (#21053) - Add tests target for `test`, `test_node_only` and `test_web` in `core` package. - Created a `_testing_init` pseudo package where bootstrap code for tests is kept. - Moved `source_map_util` from `test` to `testing` so to prevent circular dependency. - Removed `visibility:public` for testing `BUILD` packages. PR Close #21053 --- BUILD.bazel | 14 ++++++ WORKSPACE | 6 +-- karma-js.conf.js | 1 + packages/_testing_init/BUILD.bazel | 27 +++++++++++ packages/_testing_init/init_browser_spec.ts | 14 ++++++ packages/_testing_init/init_node_spec.ts | 41 ++++++++++++++++ packages/animations/browser/test/BUILD.bazel | 2 - packages/animations/test/BUILD.bazel | 2 - packages/compiler/src/compiler.ts | 1 + packages/compiler/test/BUILD.bazel | 2 - packages/compiler/test/aot/compiler_spec.ts | 3 +- .../output/abstract_emitter_node_only_spec.ts | 2 +- .../test/output/js_emitter_node_only_spec.ts | 2 +- .../test/output/ts_emitter_node_only_spec.ts | 2 +- packages/compiler/testing/BUILD.bazel | 5 +- .../src}/output/source_map_util.ts | 2 +- packages/core/test/BUILD.bazel | 48 +++++++++++++++---- .../service-worker/worker/test/BUILD.bazel | 2 - tools/cjs-jasmine/index.ts | 1 + 19 files changed, 149 insertions(+), 28 deletions(-) create mode 100644 packages/_testing_init/BUILD.bazel create mode 100644 packages/_testing_init/init_browser_spec.ts create mode 100644 packages/_testing_init/init_node_spec.ts rename packages/compiler/{test => testing/src}/output/source_map_util.ts (95%) diff --git a/BUILD.bazel b/BUILD.bazel index 64b5f05179..a86bcabe3c 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -38,3 +38,17 @@ filegroup( "*.d.ts", ]]), ) + +filegroup( + name = "angular_bootstrap_scripts", + # do not sort + srcs = [ + "//:node_modules/reflect-metadata/Reflect.js", + "//:node_modules/zone.js/dist/zone.js", + "//:node_modules/zone.js/dist/async-test.js", + "//:node_modules/zone.js/dist/sync-test.js", + "//:node_modules/zone.js/dist/fake-async-test.js", + "//:node_modules/zone.js/dist/proxy.js", + "//:node_modules/zone.js/dist/jasmine-patch.js", + ], +) diff --git a/WORKSPACE b/WORKSPACE index b858a9e414..e94abab6f1 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -16,12 +16,12 @@ node_repositories(package_json = ["//:package.json"]) git_repository( name = "build_bazel_rules_typescript", remote = "https://github.com/bazelbuild/rules_typescript.git", - tag = "0.6.0", + tag = "0.7.1", ) -load("@build_bazel_rules_typescript//:defs.bzl", "ts_repositories") +load("@build_bazel_rules_typescript//:setup.bzl", "ts_setup_workspace") -ts_repositories() +ts_setup_workspace() local_repository( name = "angular", diff --git a/karma-js.conf.js b/karma-js.conf.js index c7c559a491..50b812b9d6 100644 --- a/karma-js.conf.js +++ b/karma-js.conf.js @@ -53,6 +53,7 @@ module.exports = function(config) { ], exclude: [ + 'dist/all/@angular/_testing_init/**', 'dist/all/@angular/**/e2e_test/**', 'dist/all/@angular/**/*node_only_spec.js', 'dist/all/@angular/benchpress/**', diff --git a/packages/_testing_init/BUILD.bazel b/packages/_testing_init/BUILD.bazel new file mode 100644 index 0000000000..5d2f741e52 --- /dev/null +++ b/packages/_testing_init/BUILD.bazel @@ -0,0 +1,27 @@ +package(default_visibility = ["//visibility:public"]) + +load("@build_bazel_rules_typescript//:defs.bzl", "ts_library") + +ts_library( + name = "browser", + testonly = 1, + srcs = ["init_browser_spec.ts"], + tsconfig = "//packages:tsconfig", + deps = [ + "//packages/core/testing", + "//packages/platform-browser-dynamic/testing", + "//packages/platform-browser/animations", + ], +) + +ts_library( + name = "node", + testonly = 1, + srcs = ["init_node_spec.ts"], + tsconfig = "//packages:tsconfig", + deps = [ + "//packages/core/testing", + "//packages/platform-server", + "//packages/platform-server/testing", + ], +) diff --git a/packages/_testing_init/init_browser_spec.ts b/packages/_testing_init/init_browser_spec.ts new file mode 100644 index 0000000000..c12a0bebad --- /dev/null +++ b/packages/_testing_init/init_browser_spec.ts @@ -0,0 +1,14 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {TestBed} from '@angular/core/testing'; +import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} from '@angular/platform-browser-dynamic/testing'; +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; + +TestBed.initTestEnvironment( + [BrowserDynamicTestingModule, NoopAnimationsModule], platformBrowserDynamicTesting()); diff --git a/packages/_testing_init/init_node_spec.ts b/packages/_testing_init/init_node_spec.ts new file mode 100644 index 0000000000..9f5b086346 --- /dev/null +++ b/packages/_testing_init/init_node_spec.ts @@ -0,0 +1,41 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import 'zone.js/dist/zone-node.js'; +import 'zone.js/dist/long-stack-trace-zone.js'; +import 'zone.js/dist/proxy.js'; +import 'zone.js/dist/sync-test.js'; +import 'zone.js/dist/async-test.js'; +import 'zone.js/dist/fake-async-test.js'; +import 'reflect-metadata/Reflect'; + +// This hack is needed to get jasmine, node and zone working inside bazel. +// 1) we load `jasmine-core` which contains the ENV: it, describe etc... +const jasmineCore: any = require('jasmine-core'); +// 2) We create an instance of `jasmine` ENV. +const patchedJasmine = jasmineCore.boot(jasmineCore); +// 3) Save the `jasmine` into global so that `zone.js/dist/jasmine-patch.js` can get a hold of it to +// patch it. +(global as any)['jasmine'] = patchedJasmine; +// 4) Change the `jasmine-core` to make sure that all subsequent jasmine's have the same ENV, +// otherwise the patch will not work. +// This is needed since Bazel creates a new instance of jasmine and it's ENV and we want to make +// sure it gets the same one. +jasmineCore.boot = function() { + return patchedJasmine; +}; +// 5) Patch jasmine ENV with code which understands ProxyZone. +import 'zone.js/dist/jasmine-patch.js'; + +// Init TestBed +import {TestBed} from '@angular/core/testing'; +import {ServerTestingModule, platformServerTesting} from '@angular/platform-server/testing/src/server'; +import {DominoAdapter} from '@angular/platform-server/src/domino_adapter'; + +TestBed.initTestEnvironment(ServerTestingModule, platformServerTesting()); +DominoAdapter.makeCurrent(); diff --git a/packages/animations/browser/test/BUILD.bazel b/packages/animations/browser/test/BUILD.bazel index f8cb1743d1..a735555485 100644 --- a/packages/animations/browser/test/BUILD.bazel +++ b/packages/animations/browser/test/BUILD.bazel @@ -1,5 +1,3 @@ -package(default_visibility = ["//visibility:public"]) - load("@build_bazel_rules_typescript//:defs.bzl", "ts_library") ts_library( diff --git a/packages/animations/test/BUILD.bazel b/packages/animations/test/BUILD.bazel index e172b127c3..fa2cc4142e 100644 --- a/packages/animations/test/BUILD.bazel +++ b/packages/animations/test/BUILD.bazel @@ -1,5 +1,3 @@ -package(default_visibility = ["//visibility:public"]) - load("@build_bazel_rules_typescript//:defs.bzl", "ts_library") ts_library( diff --git a/packages/compiler/src/compiler.ts b/packages/compiler/src/compiler.ts index 8f8bf4a973..1cf8496156 100644 --- a/packages/compiler/src/compiler.ts +++ b/packages/compiler/src/compiler.ts @@ -76,4 +76,5 @@ export * from './style_compiler'; export * from './template_parser/template_parser'; export {ViewCompiler} from './view_compiler/view_compiler'; export {getParseErrors, isSyntaxError, syntaxError, Version} from './util'; +export {SourceMap} from './output/source_map'; // This file only reexports content of the `src` folder. Keep it that way. diff --git a/packages/compiler/test/BUILD.bazel b/packages/compiler/test/BUILD.bazel index ca89efd784..08afaa898a 100644 --- a/packages/compiler/test/BUILD.bazel +++ b/packages/compiler/test/BUILD.bazel @@ -1,5 +1,3 @@ -package(default_visibility = ["//visibility:public"]) - load("@build_bazel_rules_typescript//:defs.bzl", "ts_library") ts_library( diff --git a/packages/compiler/test/aot/compiler_spec.ts b/packages/compiler/test/aot/compiler_spec.ts index 788d01a4e3..d8aea934c0 100644 --- a/packages/compiler/test/aot/compiler_spec.ts +++ b/packages/compiler/test/aot/compiler_spec.ts @@ -9,11 +9,10 @@ import {AotSummaryResolver, GeneratedFile, StaticSymbolCache, StaticSymbolResolver, toTypeScript} from '@angular/compiler'; import {MetadataBundler} from '@angular/compiler-cli/src/metadata/bundler'; import {privateEntriesToIndex} from '@angular/compiler-cli/src/metadata/index_writer'; +import {extractSourceMap, originalPositionFor} from '@angular/compiler/testing/src/output/source_map_util'; import {NodeFlags} from '@angular/core/src/view/index'; import * as ts from 'typescript'; -import {extractSourceMap, originalPositionFor} from '../output/source_map_util'; - import {EmittingCompilerHost, MockAotCompilerHost, MockCompilerHost, MockDirectory, MockMetadataBundlerHost, arrayToMockDir, compile, expectNoDiagnostics, settings, setup, toMockFileArray} from './test_util'; describe('compiler (unbundled Angular)', () => { diff --git a/packages/compiler/test/output/abstract_emitter_node_only_spec.ts b/packages/compiler/test/output/abstract_emitter_node_only_spec.ts index a735ce9dcf..d0f976e632 100644 --- a/packages/compiler/test/output/abstract_emitter_node_only_spec.ts +++ b/packages/compiler/test/output/abstract_emitter_node_only_spec.ts @@ -9,7 +9,7 @@ import {ParseLocation, ParseSourceFile, ParseSourceSpan} from '@angular/compiler'; import {EmitterVisitorContext} from '@angular/compiler/src/output/abstract_emitter'; import {SourceMap} from '@angular/compiler/src/output/source_map'; -import {extractSourceMap, originalPositionFor} from './source_map_util'; +import {extractSourceMap, originalPositionFor} from '@angular/compiler/testing/src/output/source_map_util'; export function main() { describe('AbstractEmitter', () => { diff --git a/packages/compiler/test/output/js_emitter_node_only_spec.ts b/packages/compiler/test/output/js_emitter_node_only_spec.ts index b210dbc66a..03a689fb72 100644 --- a/packages/compiler/test/output/js_emitter_node_only_spec.ts +++ b/packages/compiler/test/output/js_emitter_node_only_spec.ts @@ -13,7 +13,7 @@ import * as o from '@angular/compiler/src/output/output_ast'; import {SourceMap} from '@angular/compiler/src/output/source_map'; import {ParseLocation, ParseSourceFile, ParseSourceSpan} from '@angular/compiler/src/parse_util'; -import {extractSourceMap, originalPositionFor} from './source_map_util'; +import {extractSourceMap, originalPositionFor} from '@angular/compiler/testing/src/output/source_map_util'; const someGenFilePath = 'somePackage/someGenFile'; diff --git a/packages/compiler/test/output/ts_emitter_node_only_spec.ts b/packages/compiler/test/output/ts_emitter_node_only_spec.ts index 54810d9122..b1dbabc82b 100644 --- a/packages/compiler/test/output/ts_emitter_node_only_spec.ts +++ b/packages/compiler/test/output/ts_emitter_node_only_spec.ts @@ -13,7 +13,7 @@ import {SourceMap} from '@angular/compiler/src/output/source_map'; import {TypeScriptEmitter} from '@angular/compiler/src/output/ts_emitter'; import {ParseSourceSpan} from '@angular/compiler/src/parse_util'; -import {extractSourceMap, originalPositionFor} from './source_map_util'; +import {extractSourceMap, originalPositionFor} from '@angular/compiler/testing/src/output/source_map_util'; const someGenFilePath = 'somePackage/someGenFile'; diff --git a/packages/compiler/testing/BUILD.bazel b/packages/compiler/testing/BUILD.bazel index 1660a42671..465c0d6a97 100644 --- a/packages/compiler/testing/BUILD.bazel +++ b/packages/compiler/testing/BUILD.bazel @@ -8,5 +8,8 @@ ts_library( srcs = glob(["**/*.ts"]), module_name = "@angular/compiler/testing", tsconfig = "//packages:tsconfig", - deps = ["//packages/compiler"], + deps = [ + "//packages:types", + "//packages/compiler", + ], ) diff --git a/packages/compiler/test/output/source_map_util.ts b/packages/compiler/testing/src/output/source_map_util.ts similarity index 95% rename from packages/compiler/test/output/source_map_util.ts rename to packages/compiler/testing/src/output/source_map_util.ts index 4e92329d48..24abecbb2a 100644 --- a/packages/compiler/test/output/source_map_util.ts +++ b/packages/compiler/testing/src/output/source_map_util.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {SourceMap} from '@angular/compiler/src/output/source_map'; +import {SourceMap} from '@angular/compiler'; const b64 = require('base64-js'); const SourceMapConsumer = require('source-map').SourceMapConsumer; diff --git a/packages/core/test/BUILD.bazel b/packages/core/test/BUILD.bazel index de17bc6aed..9ca36e14f8 100644 --- a/packages/core/test/BUILD.bazel +++ b/packages/core/test/BUILD.bazel @@ -1,6 +1,6 @@ package(default_visibility = ["//visibility:public"]) -load("@build_bazel_rules_typescript//:defs.bzl", "ts_library") +load("@build_bazel_rules_typescript//:defs.bzl", "ts_library", "ts_web_test") load("@build_bazel_rules_nodejs//:defs.bzl", "jasmine_node_test") ts_library( @@ -8,7 +8,10 @@ ts_library( testonly = 1, srcs = glob( ["**/*.ts"], - exclude = ["render3/**/*.ts"], + exclude = [ + "**/*_node_only_spec.ts", + "render3/**/*.ts", + ], ), tsconfig = "//packages:tsconfig", deps = [ @@ -17,7 +20,6 @@ ts_library( "//packages/animations/browser/testing", "//packages/common", "//packages/compiler", - "//packages/compiler/test", "//packages/compiler/testing", "//packages/core", "//packages/core/testing", @@ -25,22 +27,48 @@ ts_library( "//packages/platform-browser-dynamic", "//packages/platform-browser/animations", "//packages/platform-browser/testing", - "//packages/platform-server", "//packages/router", "//packages/router/testing", "@rxjs", ], ) -# TODO(mhevery): re-enable when passing +ts_library( + name = "test_node_only_lib", + testonly = 1, + srcs = glob(["**/*_node_only_spec.ts"]), + tsconfig = "//packages:tsconfig", + deps = [ + ":test_lib", + "//packages/compiler", + "//packages/compiler/testing", + "//packages/core", + "//packages/core/testing", + "//packages/platform-server", + "//packages/platform-server/testing", + ], +) + jasmine_node_test( name = "test", - srcs = [], - bootstrap = [ - "zone.js/dist/zone-node.js", - ], - tags = ["manual"], + bootstrap = ["angular_src/packages/_testing_init/init_node_spec.js"], deps = [ ":test_lib", + ":test_node_only_lib", + "//packages/_testing_init:node", + "//packages/platform-server", + "//packages/platform-server/testing", + ], +) + +ts_web_test( + name = "test_web", + bootstrap = [ + "//:angular_bootstrap_scripts", + ], + # do not sort + deps = [ + "//packages/_testing_init:browser", + ":test_lib", ], ) diff --git a/packages/service-worker/worker/test/BUILD.bazel b/packages/service-worker/worker/test/BUILD.bazel index 76c74bc3f8..3b9560b5b2 100644 --- a/packages/service-worker/worker/test/BUILD.bazel +++ b/packages/service-worker/worker/test/BUILD.bazel @@ -1,5 +1,3 @@ -package(default_visibility = ["//visibility:public"]) - load("@build_bazel_rules_typescript//:defs.bzl", "ts_library") load("@build_bazel_rules_nodejs//:defs.bzl", "jasmine_node_test") diff --git a/tools/cjs-jasmine/index.ts b/tools/cjs-jasmine/index.ts index 0b72880492..9527d0b843 100644 --- a/tools/cjs-jasmine/index.ts +++ b/tools/cjs-jasmine/index.ts @@ -52,6 +52,7 @@ var specFiles: any = cwd: distAll, ignore: [ // the following code and tests are not compatible with CJS/node environment + '@angular/_testing_init/**', '@angular/examples/**', '@angular/platform-browser/**', '@angular/platform-browser-dynamic/**',