test(zone.js): add zone.js externs test (#39108)

Add back the zone.js externs file test for google closure compiler.
The test compiles a test program with and without `zone_externs`.

1. With `zone_externs`, the code should keep the APIs defined in the `zone_externs`.
2. Without `zone_externs`, the code will not keep these APIs.

PR Close #39108
This commit is contained in:
JiaLiPassion 2020-10-04 12:31:14 +09:00 committed by Alex Rickabaugh
parent e44e10bb81
commit 6085d2acc9
7 changed files with 607 additions and 587 deletions

View File

@ -728,6 +728,7 @@ jobs:
steps:
- custom_attach_workspace
- init_environment
- install_java
# Install
- run: yarn --cwd packages/zone.js install --frozen-lockfile --non-interactive
# Run zone.js tools tests
@ -736,9 +737,11 @@ jobs:
- run: yarn bazel build //packages/zone.js:npm_package &&
cp dist/bin/packages/zone.js/npm_package/bundles/zone-mix.umd.js ./packages/zone.js/test/extra/ &&
cp dist/bin/packages/zone.js/npm_package/bundles/zone-patch-electron.umd.js ./packages/zone.js/test/extra/ &&
yarn --cwd packages/zone.js electrontest
cp dist/bin/packages/zone.js/npm_package/bundles/zone.umd.js ./packages/zone.js/build/test/closure/zone.js
- run: yarn --cwd packages/zone.js jest:test
- run: yarn --cwd packages/zone.js jest:nodetest
- run: yarn --cwd packages/zone.js electrontest
- run: yarn --cwd packages/zone.js closuretest
- run: yarn --cwd packages/zone.js/test/typings install --frozen-lockfile --non-interactive
- run: yarn --cwd packages/zone.js/test/typings test

1
packages/zone.js/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
build

View File

@ -11,15 +11,18 @@
"tslib": "^2.0.0"
},
"devDependencies": {
"@externs/nodejs": "^1.5.0",
"@types/node": "^10.9.4",
"domino": "2.1.2",
"jest": "^26.4",
"google-closure-compiler": "^20200927.0.0",
"mocha": "^3.1.2",
"mock-require": "3.0.3",
"promises-aplus-tests": "^2.1.2",
"typescript": "4.0.2"
},
"scripts": {
"closuretest": "./scripts/closure/closure_compiler.sh",
"electrontest": "cd test/extra && node electron.js",
"jest:test": "jest --config ./test/jest/jest.config.js ./test/jest/jest.spec.js",
"jest:nodetest": "jest --config ./test/jest/jest.node.config.js ./test/jest/jest.spec.js",

View File

@ -1,10 +1,10 @@
# compile closure test source file
$(npm bin)/tsc -p .
# Run the Google Closure compiler java runnable with zone externs
java -jar node_modules/google-closure-compiler/compiler.jar --flagfile 'scripts/closure/closure_flagfile' --externs 'lib/closure/zone_externs.js'
java -jar ./node_modules/google-closure-compiler-java/compiler.jar --flagfile './scripts/closure/closure_flagfile' --externs './lib/closure/zone_externs.js' --externs './node_modules/@externs/nodejs/v8/global.js' --process_common_js_modules
# the names of Zone exposed API should be kept correctly with zone externs, test program should exit with 0.
node build/closure/closure-bundle.js
node build/closure/zone-closure-bundle.js
if [ $? -eq 0 ]
then
@ -15,10 +15,9 @@ else
fi
# Run the Google Closure compiler java runnable without zone externs.
java -jar node_modules/google-closure-compiler/compiler.jar --flagfile 'scripts/closure/closure_flagfile'
java -jar node_modules/google-closure-compiler-java/compiler.jar --flagfile 'scripts/closure/closure_flagfile' --externs './node_modules/@externs/nodejs/v8/global.js' --process_common_js_modules
# the names of Zone exposed API should be renamed and fail to be executed, test program should exit with 1.
node build/closure/closure-bundle.js
node build/closure/zone-closure-bundle.js
if [ $? -eq 1 ]
then

View File

@ -1,5 +1,6 @@
--compilation_level ADVANCED_OPTIMIZATIONS
--js_output_file "build/closure/closure-bundle.js"
--js_output_file "build/closure/zone-closure-bundle.js"
--rewrite_polyfills false
--js "build/test/closure/zone.js"
--js "build/test/closure/zone.closure.js"
--formatting PRETTY_PRINT
--formatting PRETTY_PRINT

View File

@ -5,7 +5,7 @@
* 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 '../../dist/zone-node';
import './zone.js';
const testClosureFunction = () => {
const logs: string[] = [];
// call all Zone exposed functions
@ -72,16 +72,54 @@ const testClosureFunction = () => {
logs.push('getZoneWith' + keyZone!.name);
logs.push('get' + keyZone!.get('key'));
logs.push('root' + Zone.root.name);
Object.keys((Zone as any).prototype).forEach(key => {
logs.push(key);
});
Object.keys(testZoneSpec).forEach(key => {
logs.push(key);
const zonePrototypeKeys = [
'get',
'getZoneWith',
'fork',
'wrap',
'run',
'runGuarded',
'runTask',
'scheduleTask',
'scheduleMicroTask',
'scheduleMacroTask',
'scheduleEventTask',
'cancelTask',
];
zonePrototypeKeys.forEach(key => {
if ((Zone as any).prototype.hasOwnProperty(key)) {
logs.push(key);
}
});
const zoneSpecKeys = [
'name',
'properties',
'onFork',
'onIntercept',
'onInvoke',
'onHandleError',
'onScheduleTask',
'onInvokeTask',
'onCancelTask',
'onHasTask',
];
zoneSpecKeys.forEach(key => {
if (testZoneSpec.hasOwnProperty(key)) {
logs.push(key);
}
});
const zoneTaskKeys = [
'onHasTask', 'runCount', 'type', 'source', 'data', 'scheduleFn', 'cancelFn', 'callback',
'invoke'
];
const task = Zone.current.scheduleMicroTask('testTask', () => {}, undefined, () => {});
Object.keys(task).forEach(key => {
logs.push(key);
zoneTaskKeys.forEach(key => {
if (task.hasOwnProperty(key)) {
logs.push(key);
}
});
});
});
@ -92,8 +130,6 @@ const testClosureFunction = () => {
'getZoneWithclosure',
'getvalue',
'root<root>',
'parent',
'name',
'get',
'getZoneWith',
'fork',
@ -106,7 +142,6 @@ const testClosureFunction = () => {
'scheduleMacroTask',
'scheduleEventTask',
'cancelTask',
'_updateTaskCount',
'name',
'properties',
'onFork',
@ -117,10 +152,7 @@ const testClosureFunction = () => {
'onInvokeTask',
'onCancelTask',
'onHasTask',
'_zone',
'runCount',
'_zoneDelegates',
'_state',
'type',
'source',
'data',
@ -137,6 +169,11 @@ const testClosureFunction = () => {
result = false;
}
}
if (result) {
console.log('All tests passed.');
} else {
console.error('Test failed, some public APIs cannot be found after closure compiler.');
}
process['exit'](result ? 0 : 1);
};
process['on']('uncaughtException', (err: any) => {

File diff suppressed because it is too large Load Diff