Definitelytyped: Jasmine createSpyObj has new array parameter for specifying properties

Created on 23 Jul 2019  路  7Comments  路  Source: DefinitelyTyped/DefinitelyTyped

If you know how to fix the issue, make a pull request instead.

  • [ X] I tried using the @types/xxxx package and had problems.
  • [ ] I tried using the latest stable version of tsc. https://www.npmjs.com/package/typescript
  • [ ] I have a question that is inappropriate for StackOverflow. (Please ask any appropriate questions there).
  • [ x] [Mention](https://github.com/blog/821-mention-somebody-they-re-notified) the authors (see Definitions by: in index.d.ts) so they can respond.

    • Authors: @borisyankov @theodorejb @davidparsson @gmoothart @lukas-zech-software @Engineer2B @cyungmann @Roaders @devoto13 @dim @pe8ter @kolodny

jasmine.createSpyObj takes an array of property names as a parameter following the array of method names.

Merged to master 27 days ago in https://github.com/jasmine/jasmine/pull/1722

Maybe included in the package version bump 15 days ago?

Most helpful comment

FYI: Jasmine 3.5.0 is out
which includes this feature: "Allow users to pass property names to createSpyObj"

All 7 comments

This also accepts an object where each object property value becomes the value of the property defined on the mock.

@rcollette New version of jasmine has not been published yet. Let's wait for the release before updating typings.

FYI: Jasmine 3.5.0 is out
which includes this feature: "Allow users to pass property names to createSpyObj"

Hi @rcollette @zvirja @HonoluluHenk The types for 3.5.0 have been released. Please try them out to see if I've added them correctly.

createSpyObj should be able to take a signature like:

createSpyObj<T>('name',[],{prop1:'value'})

but the type declaration seems to be forcing me to specify an array of property names, unless T is undefined.

I wound up having to do:
authorizationService = createSpyObj('authorizationService', [], { principal$ });

@rcollette Could you change the types in your node_modules so the SpyObjPropertyNames is:

type SpyObjPropertyNames<T = undefined> =
    T extends undefined ?
            (ReadonlyArray<string> | { [propertyName: string]: any }) :
            (ReadonlyArray<keyof T>) | { [P in keyof T]?: any });;

Sorry for the delay... work priorities.

I corrected the declaration to (note there was an extra paren and semicolon):

type SpyObjPropertyNames<T = undefined> =
    T extends undefined ?
            (ReadonlyArray<string> | { [propertyName: string]: any }) :
            (ReadonlyArray<keyof T>) | { [P in keyof T]?: any };

Using:

principal$ = new Subject<IPrincipal>();
authorizationService = createSpyObj<AuthorizationService>('authorizationService', [], { principal$ });

I get the following error.

Error:(22, 93) TS2345: Argument of type '{ principal$: Subject; }' is not assignable to parameter of type 'ReadonlyArray<"authenticated$" | "principal$" | "isAuthenticated$">'.
Object literal may only specify known properties, and 'principal$' does not exist in type 'ReadonlyArray<"authenticated$" | "principal$" | "isAuthenticated$">'.

I see that you've included an object in that declaration but it does not get picked up for some reason.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JudeAlquiza picture JudeAlquiza  路  3Comments

JWT
svipas picture svipas  路  3Comments

ArtemZag picture ArtemZag  路  3Comments

jgoz picture jgoz  路  3Comments

Loghorn picture Loghorn  路  3Comments