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
This commit is contained in:
Misko Hevery 2017-12-16 11:35:47 -08:00 committed by Igor Minar
parent f3fc74ab67
commit 47bcb5bc35
19 changed files with 149 additions and 28 deletions

View File

@ -38,3 +38,17 @@ filegroup(
"*.d.ts", "*.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",
],
)

View File

@ -16,12 +16,12 @@ node_repositories(package_json = ["//:package.json"])
git_repository( git_repository(
name = "build_bazel_rules_typescript", name = "build_bazel_rules_typescript",
remote = "https://github.com/bazelbuild/rules_typescript.git", 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( local_repository(
name = "angular", name = "angular",

View File

@ -53,6 +53,7 @@ module.exports = function(config) {
], ],
exclude: [ exclude: [
'dist/all/@angular/_testing_init/**',
'dist/all/@angular/**/e2e_test/**', 'dist/all/@angular/**/e2e_test/**',
'dist/all/@angular/**/*node_only_spec.js', 'dist/all/@angular/**/*node_only_spec.js',
'dist/all/@angular/benchpress/**', 'dist/all/@angular/benchpress/**',

View File

@ -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",
],
)

View File

@ -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());

View File

@ -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();

View File

@ -1,5 +1,3 @@
package(default_visibility = ["//visibility:public"])
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library") load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
ts_library( ts_library(

View File

@ -1,5 +1,3 @@
package(default_visibility = ["//visibility:public"])
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library") load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
ts_library( ts_library(

View File

@ -76,4 +76,5 @@ export * from './style_compiler';
export * from './template_parser/template_parser'; export * from './template_parser/template_parser';
export {ViewCompiler} from './view_compiler/view_compiler'; export {ViewCompiler} from './view_compiler/view_compiler';
export {getParseErrors, isSyntaxError, syntaxError, Version} from './util'; 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. // This file only reexports content of the `src` folder. Keep it that way.

View File

@ -1,5 +1,3 @@
package(default_visibility = ["//visibility:public"])
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library") load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
ts_library( ts_library(

View File

@ -9,11 +9,10 @@
import {AotSummaryResolver, GeneratedFile, StaticSymbolCache, StaticSymbolResolver, toTypeScript} from '@angular/compiler'; import {AotSummaryResolver, GeneratedFile, StaticSymbolCache, StaticSymbolResolver, toTypeScript} from '@angular/compiler';
import {MetadataBundler} from '@angular/compiler-cli/src/metadata/bundler'; import {MetadataBundler} from '@angular/compiler-cli/src/metadata/bundler';
import {privateEntriesToIndex} from '@angular/compiler-cli/src/metadata/index_writer'; 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 {NodeFlags} from '@angular/core/src/view/index';
import * as ts from 'typescript'; 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'; import {EmittingCompilerHost, MockAotCompilerHost, MockCompilerHost, MockDirectory, MockMetadataBundlerHost, arrayToMockDir, compile, expectNoDiagnostics, settings, setup, toMockFileArray} from './test_util';
describe('compiler (unbundled Angular)', () => { describe('compiler (unbundled Angular)', () => {

View File

@ -9,7 +9,7 @@
import {ParseLocation, ParseSourceFile, ParseSourceSpan} from '@angular/compiler'; import {ParseLocation, ParseSourceFile, ParseSourceSpan} from '@angular/compiler';
import {EmitterVisitorContext} from '@angular/compiler/src/output/abstract_emitter'; import {EmitterVisitorContext} from '@angular/compiler/src/output/abstract_emitter';
import {SourceMap} from '@angular/compiler/src/output/source_map'; 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() { export function main() {
describe('AbstractEmitter', () => { describe('AbstractEmitter', () => {

View File

@ -13,7 +13,7 @@ import * as o from '@angular/compiler/src/output/output_ast';
import {SourceMap} from '@angular/compiler/src/output/source_map'; import {SourceMap} from '@angular/compiler/src/output/source_map';
import {ParseLocation, ParseSourceFile, ParseSourceSpan} from '@angular/compiler/src/parse_util'; 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'; const someGenFilePath = 'somePackage/someGenFile';

View File

@ -13,7 +13,7 @@ import {SourceMap} from '@angular/compiler/src/output/source_map';
import {TypeScriptEmitter} from '@angular/compiler/src/output/ts_emitter'; import {TypeScriptEmitter} from '@angular/compiler/src/output/ts_emitter';
import {ParseSourceSpan} from '@angular/compiler/src/parse_util'; 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'; const someGenFilePath = 'somePackage/someGenFile';

View File

@ -8,5 +8,8 @@ ts_library(
srcs = glob(["**/*.ts"]), srcs = glob(["**/*.ts"]),
module_name = "@angular/compiler/testing", module_name = "@angular/compiler/testing",
tsconfig = "//packages:tsconfig", tsconfig = "//packages:tsconfig",
deps = ["//packages/compiler"], deps = [
"//packages:types",
"//packages/compiler",
],
) )

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license * 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 b64 = require('base64-js');
const SourceMapConsumer = require('source-map').SourceMapConsumer; const SourceMapConsumer = require('source-map').SourceMapConsumer;

View File

@ -1,6 +1,6 @@
package(default_visibility = ["//visibility:public"]) 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") load("@build_bazel_rules_nodejs//:defs.bzl", "jasmine_node_test")
ts_library( ts_library(
@ -8,7 +8,10 @@ ts_library(
testonly = 1, testonly = 1,
srcs = glob( srcs = glob(
["**/*.ts"], ["**/*.ts"],
exclude = ["render3/**/*.ts"], exclude = [
"**/*_node_only_spec.ts",
"render3/**/*.ts",
],
), ),
tsconfig = "//packages:tsconfig", tsconfig = "//packages:tsconfig",
deps = [ deps = [
@ -17,7 +20,6 @@ ts_library(
"//packages/animations/browser/testing", "//packages/animations/browser/testing",
"//packages/common", "//packages/common",
"//packages/compiler", "//packages/compiler",
"//packages/compiler/test",
"//packages/compiler/testing", "//packages/compiler/testing",
"//packages/core", "//packages/core",
"//packages/core/testing", "//packages/core/testing",
@ -25,22 +27,48 @@ ts_library(
"//packages/platform-browser-dynamic", "//packages/platform-browser-dynamic",
"//packages/platform-browser/animations", "//packages/platform-browser/animations",
"//packages/platform-browser/testing", "//packages/platform-browser/testing",
"//packages/platform-server",
"//packages/router", "//packages/router",
"//packages/router/testing", "//packages/router/testing",
"@rxjs", "@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( jasmine_node_test(
name = "test", name = "test",
srcs = [], bootstrap = ["angular_src/packages/_testing_init/init_node_spec.js"],
bootstrap = [
"zone.js/dist/zone-node.js",
],
tags = ["manual"],
deps = [ deps = [
":test_lib", ":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",
], ],
) )

View File

@ -1,5 +1,3 @@
package(default_visibility = ["//visibility:public"])
load("@build_bazel_rules_typescript//:defs.bzl", "ts_library") load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
load("@build_bazel_rules_nodejs//:defs.bzl", "jasmine_node_test") load("@build_bazel_rules_nodejs//:defs.bzl", "jasmine_node_test")

View File

@ -52,6 +52,7 @@ var specFiles: any =
cwd: distAll, cwd: distAll,
ignore: [ ignore: [
// the following code and tests are not compatible with CJS/node environment // the following code and tests are not compatible with CJS/node environment
'@angular/_testing_init/**',
'@angular/examples/**', '@angular/examples/**',
'@angular/platform-browser/**', '@angular/platform-browser/**',
'@angular/platform-browser-dynamic/**', '@angular/platform-browser-dynamic/**',