2019-05-31 11:56:07 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2019-05-31 11:56:07 -04:00
|
|
|
*
|
|
|
|
* 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 {ifEnvSupports} from './test-util';
|
|
|
|
|
|
|
|
function supportJasmineSpec() {
|
|
|
|
return jasmine && (jasmine as any)['Spec'];
|
|
|
|
}
|
|
|
|
|
|
|
|
(supportJasmineSpec as any).message = 'jasmine spec';
|
|
|
|
|
|
|
|
ifEnvSupports(supportJasmineSpec, () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
// assert that each jasmine run has a task, so that drainMicrotask works properly.
|
|
|
|
expect(Zone.currentTask).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('jasmine', () => {
|
|
|
|
let throwOnAsync = false;
|
|
|
|
let beforeEachZone: Zone|null = null;
|
|
|
|
let beforeAllZone: Zone|null = null;
|
|
|
|
let itZone: Zone|null = null;
|
|
|
|
const syncZone = Zone.current;
|
|
|
|
try {
|
|
|
|
Zone.current.scheduleMicroTask('dontallow', (): any => null);
|
|
|
|
} catch (e) {
|
|
|
|
throwOnAsync = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeAll(() => beforeAllZone = Zone.current);
|
|
|
|
|
|
|
|
beforeEach(() => beforeEachZone = Zone.current);
|
|
|
|
|
|
|
|
it('should throw on async in describe', () => {
|
|
|
|
expect(throwOnAsync).toBe(true);
|
|
|
|
expect(syncZone.name).toEqual('syncTestZone for jasmine.describe');
|
|
|
|
itZone = Zone.current;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should cope with pending tests, which have no test body');
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
let zone = Zone.current;
|
|
|
|
expect(zone.name).toEqual('ProxyZone');
|
2020-04-13 19:40:21 -04:00
|
|
|
expect(beforeEachZone!.name).toEqual(zone.name);
|
2019-05-31 11:56:07 -04:00
|
|
|
expect(itZone).toBe(zone);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
let zone = Zone.current;
|
|
|
|
expect(zone.name).toEqual('ProxyZone');
|
2020-04-13 19:40:21 -04:00
|
|
|
expect(beforeAllZone!.name).toEqual(zone.name);
|
2019-05-31 11:56:07 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('return promise', () => {
|
|
|
|
let log: string[];
|
2020-04-13 19:40:21 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
log = [];
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
it('should wait for promise to resolve', () => {
|
|
|
|
return new Promise((res, _) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
log.push('resolved');
|
|
|
|
res();
|
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
afterEach(() => {
|
|
|
|
expect(log).toEqual(['resolved']);
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
});
|
feat(zone.js): patch jasmine.createSpyObj to make properties enumerable to be true (#34624)
Close #33657
in jasmine 3.5, there is a new feature, user can pass a properties object to `jasmine.createSpyObj`
```
const spy = jasmine.createSpyObj('spy', ['method1'], {prop1: 'foo'});
expect(spy.prop1).toEqual('foo');
```
This case will not work for Angular TestBed, for example,
```
describe('AppComponent', () => {
beforeEach(() => {
//Note the third parameter
// @ts-ignore
const someServiceSpy = jasmine.createSpyObj('SomeService', ['someFunction'], ['aProperty']);
TestBed.configureTestingModule({
declarations: [
AppComponent
],
providers: [
{provide: SomeService, useValue: someServiceSpy},
]
}).compileComponents();
});
it('should create the app', () => {
//spyObj will have someFunction, but will not have aProperty
let spyObj = TestBed.get(SomeService);
});
```
Because `jasmine.createSpyObj` will create the `aProperty` with `enumerable=false`,
and `TestBed.configureTestingModule` will try to copy all the properties from spyObj to
the injected service instance. And because `enumerable` is false, so the property (here is aProperty)
will not be copied.
This PR will monkey patch the `jasmine.createSpyObj` and make sure the new property's
`enumerable=true`.
PR Close #34624
2020-01-03 00:21:17 -05:00
|
|
|
|
|
|
|
describe('jasmine.createSpyObj', () => {
|
|
|
|
it('createSpyObj with properties should be able to be retrieved from the spy', () => {
|
|
|
|
const spy = jasmine.createSpyObj('obj', ['someFunction'], {prop1: 'foo'});
|
|
|
|
expect(spy.prop1).toEqual('foo');
|
|
|
|
const desc: any = Object.getOwnPropertyDescriptor(spy, 'prop1');
|
|
|
|
expect(desc.enumerable).toBe(true);
|
|
|
|
expect(desc.configurable).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
})();
|