build: update jasmine to 3.5 (#34625)

1. update jasmine to 3.5
2. update @types/jasmine to 3.5
3. update @types/jasminewd2 to 2.0.8

Also fix several cases, the new jasmine 3 will help to create test cases correctly,
such as in the `jasmine 2.x` version, the following case will pass

```
expect(1 == 2);
```

But in jsamine 3, the case will need to be

```
expect(1 == 2).toBeTrue();
```

PR Close #34625
This commit is contained in:
JiaLiPassion
2020-01-03 14:28:06 +09:00
committed by Kara Erickson
parent 52ab9397a0
commit b28a5f6eef
29 changed files with 124 additions and 85 deletions

View File

@ -95,7 +95,7 @@ runInEachFileSystem(() => {
});
// The "test" compilation result is just the name of the decorator being compiled
// (suffixed with `(compiled)`)
handler.compile.and.callFake((decl: ts.Declaration, analysis: any) => {
(handler.compile as any).and.callFake((decl: ts.Declaration, analysis: any) => {
logs.push(`compile: ${(decl as any).name.text}@${analysis.decoratorName} (resolved: ${
analysis.resolved})`);
return `@${analysis.decoratorName} (compiled)`;
@ -183,7 +183,7 @@ runInEachFileSystem(() => {
it('should call detect on the decorator handlers with each class from the parsed file',
() => {
expect(testHandler.detect).toHaveBeenCalledTimes(5);
expect(testHandler.detect.calls.allArgs().map(args => args[1])).toEqual([
expect(testHandler.detect.calls.allArgs().map((args: any[]) => args[1])).toEqual([
null,
jasmine.arrayContaining([jasmine.objectContaining({name: 'Component'})]),
jasmine.arrayContaining([jasmine.objectContaining({name: 'Directive'})]),

View File

@ -34,9 +34,9 @@ describe('ClusterExecutor', () => {
beforeEach(() => {
masterRunSpy = spyOn(ClusterMaster.prototype, 'run')
.and.returnValue(Promise.resolve('CusterMaster#run()'));
.and.returnValue(Promise.resolve('CusterMaster#run()' as any));
workerRunSpy = spyOn(ClusterWorker.prototype, 'run')
.and.returnValue(Promise.resolve('CusterWorker#run()'));
.and.returnValue(Promise.resolve('CusterWorker#run()' as any));
createTaskCompletedCallback = jasmine.createSpy('createTaskCompletedCallback');
mockLogger = new MockLogger();

View File

@ -38,7 +38,7 @@ runInEachFileSystem(() => {
initMockFileSystem(fs, testFiles);
// Force single-process execution in unit tests by mocking available CPUs to 1.
spyOn(os, 'cpus').and.returnValue([{model: 'Mock CPU'}]);
spyOn(os, 'cpus').and.returnValue([{ model: 'Mock CPU' } as any]);
});
it('should run ngcc without errors for esm2015', () => {
@ -962,7 +962,8 @@ runInEachFileSystem(() => {
.toMatch(ANGULAR_CORE_IMPORT_REGEX);
// Copies over files (unchanged) that did not need compiling
expect(fs.exists(_(`/node_modules/@angular/common/__ivy_ngcc__/esm5/src/version.js`)));
expect(fs.exists(_(`/node_modules/@angular/common/__ivy_ngcc__/esm5/src/version.js`)))
.toBeTrue();
expect(fs.readFile(_(`/node_modules/@angular/common/__ivy_ngcc__/esm5/src/version.js`)))
.toEqual(fs.readFile(_(`/node_modules/@angular/common/esm5/src/version.js`)));

View File

@ -66,6 +66,9 @@ runInEachFileSystem(() => {
});
spyOn(lockFile, 'read').and.callFake(() => {
log.push('read() => ' + lockFileContents);
if (lockFileContents === null) {
throw {code: 'ENOENT'};
}
return lockFileContents;
});
@ -99,6 +102,9 @@ runInEachFileSystem(() => {
});
spyOn(lockFile, 'read').and.callFake(() => {
log.push('read() => ' + lockFileContents);
if (lockFileContents === null) {
throw {code: 'ENOENT'};
}
return lockFileContents;
});
@ -148,6 +154,9 @@ runInEachFileSystem(() => {
});
spyOn(lockFile, 'read').and.callFake(() => {
log.push('read() => ' + lockFileContents);
if (lockFileContents === null) {
throw {code: 'ENOENT'};
}
return lockFileContents;
});
@ -167,4 +176,4 @@ runInEachFileSystem(() => {
});
});
});
});
});

View File

@ -12,6 +12,7 @@ describe('unlocker', () => {
it('should attach a handler to the `disconnect` event', () => {
spyOn(process, 'on');
require('../../../src/locking/lock_file_with_child_process/unlocker');
expect(process.on).toHaveBeenCalledWith('disconnect', jasmine.any(Function));
// TODO: @JiaLiPassion, need to wait for @types/jasmine to handle the override case
expect(process.on).toHaveBeenCalledWith('disconnect' as any, jasmine.any(Function));
});
});

View File

@ -70,7 +70,7 @@ runInEachFileSystem(() => {
const config = new NgccConfiguration(fs, _('/project'));
spyOn(config, 'getConfig').and.returnValue({
entryPoints: {[_('/project/node_modules/some_package/valid_entry_point')]: {ignore: true}}
});
} as any);
const entryPoint = getEntryPointInfo(
fs, config, new MockLogger(), SOME_PACKAGE,
_('/project/node_modules/some_package/valid_entry_point'));
@ -95,7 +95,8 @@ runInEachFileSystem(() => {
esm2015: './some_other.js',
};
spyOn(config, 'getConfig').and.returnValue({
entryPoints: {[_('/project/node_modules/some_package/valid_entry_point')]: {override}}
entryPoints: {[_('/project/node_modules/some_package/valid_entry_point')]: {override}},
versionRange: '*'
});
const entryPoint = getEntryPointInfo(
fs, config, new MockLogger(), SOME_PACKAGE,
@ -145,7 +146,9 @@ runInEachFileSystem(() => {
const override =
JSON.parse(createPackageJson('missing_package_json', {excludes: ['name']}));
spyOn(config, 'getConfig').and.returnValue({
entryPoints: {[_('/project/node_modules/some_package/missing_package_json')]: {override}}
entryPoints:
{[_('/project/node_modules/some_package/missing_package_json')]: {override}},
versionRange: '*'
});
const entryPoint = getEntryPointInfo(
fs, config, new MockLogger(), SOME_PACKAGE,
@ -279,7 +282,8 @@ runInEachFileSystem(() => {
]);
const config = new NgccConfiguration(fs, _('/project'));
spyOn(config, 'getConfig').and.returnValue({
entryPoints: {[_('/project/node_modules/some_package/missing_metadata')]: {}}
entryPoints: {[_('/project/node_modules/some_package/missing_metadata')]: {}},
versionRange: '*'
});
const entryPoint = getEntryPointInfo(
fs, config, new MockLogger(), SOME_PACKAGE,
@ -398,7 +402,7 @@ runInEachFileSystem(() => {
if (result === NO_ENTRY_POINT || result === INCOMPATIBLE_ENTRY_POINT) {
return fail(`Expected an entry point but got ${result}`);
}
entryPoint = result;
entryPoint = result as any;
});
it('should return `esm2015` format for `fesm2015` property', () => {

View File

@ -47,7 +47,7 @@ describe('CachedFileSystem', () => {
let lstatSpy: jasmine.Spy;
beforeEach(() => {
// For most of the tests the files are not symbolic links.
lstatSpy = spyOn(delegate, 'lstat').and.returnValue({isSymbolicLink: () => false});
lstatSpy = spyOn(delegate, 'lstat').and.returnValue({ isSymbolicLink: () => false } as any);
});
it('should call delegate if not in cache', () => {
@ -93,7 +93,7 @@ describe('CachedFileSystem', () => {
describe('invalidateCaches()', () => {
it('should call the delegate `readFile()` if the path for the cached file has been invalidated',
() => {
spyOn(delegate, 'lstat').and.returnValue({isSymbolicLink: () => false});
spyOn(delegate, 'lstat').and.returnValue({ isSymbolicLink: () => false } as any);
const spy = spyOn(delegate, 'readFile').and.returnValue('Some contents');
fs.readFile(abcPath); // Call once to fill the cache
spy.calls.reset();
@ -230,7 +230,7 @@ describe('CachedFileSystem', () => {
describe('moveFile()', () => {
beforeEach(() => {
// `moveFile()` relies upon `readFile` which calls through to `lstat()`, so stub it out.
spyOn(delegate, 'lstat').and.returnValue({isSymbolicLink: () => false});
spyOn(delegate, 'lstat').and.returnValue({ isSymbolicLink: () => false } as any);
});
it('should call delegate', () => {

View File

@ -65,10 +65,11 @@ describe('NodeJSFileSystem', () => {
describe('readdir()', () => {
it('should delegate to fs.readdirSync()', () => {
const spy = spyOn(realFs, 'readdirSync').and.returnValue(['x', 'y/z']);
const spy = spyOn(realFs, 'readdirSync').and.returnValue(['x', 'y/z'] as any);
const result = fs.readdir(abcPath);
expect(result).toEqual([relativeFrom('x'), relativeFrom('y/z')]);
expect(spy).toHaveBeenCalledWith(abcPath);
// TODO: @JiaLiPassion need to wait for @types/jasmine update to handle optional parameters.
expect(spy as any).toHaveBeenCalledWith(abcPath);
});
});
@ -88,7 +89,8 @@ describe('NodeJSFileSystem', () => {
const spy = spyOn(realFs, 'statSync').and.returnValue(stats);
const result = fs.stat(abcPath);
expect(result).toBe(stats);
expect(spy).toHaveBeenCalledWith(abcPath);
// TODO: @JiaLiPassion need to wait for @types/jasmine update to handle optional parameters.
expect(spy as any).toHaveBeenCalledWith(abcPath);
});
});
@ -125,7 +127,7 @@ describe('NodeJSFileSystem', () => {
const xyPath = absoluteFrom('/x/y');
const mkdirCalls: string[] = [];
const existsCalls: string[] = [];
spyOn(realFs, 'mkdirSync').and.callFake((path: string) => mkdirCalls.push(path));
spyOn(realFs, 'mkdirSync').and.callFake(((path: string) => mkdirCalls.push(path)) as any);
spyOn(fs, 'exists').and.callFake((path: AbsoluteFsPath) => {
existsCalls.push(path);
switch (path) {
@ -171,12 +173,12 @@ describe('NodeJSFileSystem', () => {
}
return false;
});
spyOn(fs, 'stat').and.returnValue({isDirectory: () => true});
const mkdirSyncSpy = spyOn(realFs, 'mkdirSync').and.callFake((path: string) => {
spyOn(fs, 'stat').and.returnValue({ isDirectory: () => true } as any);
const mkdirSyncSpy = spyOn(realFs, 'mkdirSync').and.callFake(((path: string) => {
if (path === abcPath) {
throw new Error('It exists already. Supposedly.');
}
});
}) as any);
fs.ensureDir(abcPath);
expect(mkdirSyncSpy).toHaveBeenCalledTimes(3);
@ -186,11 +188,11 @@ describe('NodeJSFileSystem', () => {
it('should fail if creating the directory throws and the directory does not exist', () => {
spyOn(fs, 'exists').and.returnValue(false);
spyOn(realFs, 'mkdirSync').and.callFake((path: string) => {
spyOn(realFs, 'mkdirSync').and.callFake(((path: string) => {
if (path === abcPath) {
throw new Error('Unable to create directory (for whatever reason).');
}
});
}) as any);
expect(() => fs.ensureDir(abcPath))
.toThrowError('Unable to create directory (for whatever reason).');
@ -210,12 +212,12 @@ describe('NodeJSFileSystem', () => {
}
return false;
});
spyOn(fs, 'stat').and.returnValue({isDirectory: isDirectorySpy});
spyOn(realFs, 'mkdirSync').and.callFake((path: string) => {
spyOn(fs, 'stat').and.returnValue({ isDirectory: isDirectorySpy } as any);
spyOn(realFs, 'mkdirSync').and.callFake(((path: string) => {
if (path === abcPath) {
throw new Error('It exists already. Supposedly.');
}
});
}) as any);
expect(() => fs.ensureDir(abcPath)).toThrowError('It exists already. Supposedly.');
expect(isDirectorySpy).toHaveBeenCalledTimes(1);

View File

@ -775,18 +775,22 @@ runInEachFileSystem(() => {
describe('(visited file tracking)', () => {
it('should track each time a source file is visited', () => {
const addDependency = jasmine.createSpy('DependencyTracker');
const addDependency =
jasmine.createSpy<DependencyTracker['addDependency']>('DependencyTracker');
const {expression, checker} = makeExpression(
`class A { static foo = 42; } function bar() { return A.foo; }`, 'bar()');
const evaluator = makeEvaluator(checker, {...fakeDepTracker, addDependency});
evaluator.evaluate(expression);
expect(addDependency).toHaveBeenCalledTimes(2); // two declaration visited
expect(addDependency.calls.allArgs().map(args => [args[0].fileName, args[1].fileName]))
expect(
addDependency.calls.allArgs().map(
(args: Parameters<typeof addDependency>) => [args[0].fileName, args[1].fileName]))
.toEqual([[_('/entry.ts'), _('/entry.ts')], [_('/entry.ts'), _('/entry.ts')]]);
});
it('should track imported source files', () => {
const addDependency = jasmine.createSpy('DependencyTracker');
const addDependency =
jasmine.createSpy<DependencyTracker['addDependency']>('DependencyTracker');
const {expression, checker} =
makeExpression(`import {Y} from './other'; const A = Y;`, 'A', [
{name: _('/other.ts'), contents: `export const Y = 'test';`},
@ -795,7 +799,9 @@ runInEachFileSystem(() => {
const evaluator = makeEvaluator(checker, {...fakeDepTracker, addDependency});
evaluator.evaluate(expression);
expect(addDependency).toHaveBeenCalledTimes(2);
expect(addDependency.calls.allArgs().map(args => [args[0].fileName, args[1].fileName]))
expect(
addDependency.calls.allArgs().map(
(args: Parameters<typeof addDependency>) => [args[0].fileName, args[1].fileName]))
.toEqual([
[_('/entry.ts'), _('/entry.ts')],
[_('/entry.ts'), _('/other.ts')],
@ -803,7 +809,8 @@ runInEachFileSystem(() => {
});
it('should track files passed through during re-exports', () => {
const addDependency = jasmine.createSpy('DependencyTracker');
const addDependency =
jasmine.createSpy<DependencyTracker['addDependency']>('DependencyTracker');
const {expression, checker} =
makeExpression(`import * as mod from './direct-reexport';`, 'mod.value.property', [
{name: _('/const.ts'), contents: 'export const value = {property: "test"};'},
@ -823,7 +830,9 @@ runInEachFileSystem(() => {
const evaluator = makeEvaluator(checker, {...fakeDepTracker, addDependency});
evaluator.evaluate(expression);
expect(addDependency).toHaveBeenCalledTimes(2);
expect(addDependency.calls.allArgs().map(args => [args[0].fileName, args[1].fileName]))
expect(
addDependency.calls.allArgs().map(
(args: Parameters<typeof addDependency>) => [args[0].fileName, args[1].fileName]))
.toEqual([
[_('/entry.ts'), _('/direct-reexport.ts')],
// Not '/indirect-reexport.ts' or '/def.ts'.

View File

@ -1559,11 +1559,13 @@ describe('ngc transformer command-line', () => {
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
const timerToken = 100;
spyOn(ts.sys, 'setTimeout').and.callFake((callback: () => void) => {
// TODO: @JiaLiPassion, need to wait @types/jasmine to handle optional method case
spyOn(ts.sys as any, 'setTimeout').and.callFake((callback: () => void) => {
timer = callback;
return timerToken;
});
spyOn(ts.sys, 'clearTimeout').and.callFake((token: number) => {
// TODO: @JiaLiPassion, need to wait @types/jasmine to handle optional method case
spyOn(ts.sys as any, 'clearTimeout').and.callFake((token: number) => {
if (token == timerToken) {
timer = undefined;
}
@ -2323,17 +2325,17 @@ describe('ngc transformer command-line', () => {
}));
write('lib1/index.ts', `
import {Directive} from '@angular/core';
@Directive()
export class BaseClass {}
`);
write('index.ts', `
import {NgModule, Directive} from '@angular/core';
import {BaseClass} from 'lib1_built';
@Directive({selector: 'my-dir'})
export class MyDirective extends BaseClass {}
@NgModule({declarations: [MyDirective]})
export class AppModule {}
`);