Coder Social home page Coder Social logo

Comments (8)

gparlakov avatar gparlakov commented on May 26, 2024

Are you using jest for testing?

If not see the documentation for autospy generation

Specifically the commands for different setups and chose what you need.

angular jest jasmine command
5   V schematics scuri:autospy --legacy
5 V   schematics scuri:autospy --for jest --legacy
6   V ng g scuri:autospy
6 V   ng g scuri:autospy --for jest
7   V ng g scuri:autospy
7 V   ng g scuri:autospy --for jest
8   V ng g scuri:autospy
8 V   ng g scuri:autospy --for jest

Basically for angular 5 and below use --legacy and for jasmine do not add --for jest

from scuri.

marimendez88 avatar marimendez88 commented on May 26, 2024

yes @gparlakov , I am using jest for testing and the file is generated correctly, but inside of it, seems like the type SpyOf is not working and using a private name:

Screen Shot 2020-12-09 at 10 38 53

from scuri.

gparlakov avatar gparlakov commented on May 26, 2024

It looks like the typing for jest is missing.
How do you include the @types/jest?

from scuri.

gparlakov avatar gparlakov commented on May 26, 2024

@marimendez88 hi, did you manage to work out your issue? If so let's close this issue.

from scuri.

venkatp57 avatar venkatp57 commented on May 26, 2024

Hi, i am trying to use scuri in my project and i am getting some compilation issue on auto-spy.ts file

image

image

can you please help on this.

from scuri.

gparlakov avatar gparlakov commented on May 26, 2024

from scuri.

venkatp57 avatar venkatp57 commented on May 26, 2024

Here are the details

"typescript": "~4.1.5",

image

from scuri.

gparlakov avatar gparlakov commented on May 26, 2024

Hey @venkatp57 I was able to reproduce the issue here -> stackblitz link
(it's a fully working example with the whole setup)

It boils down to telling typescript that we know what we are doing :)

Here's a version of autospy that works as expected on the environment you specified

/** Create an object with methods that are autoSpy-ed to use as mock dependency */
export function autoSpy<T>(obj: new (...args: any[]) => T): SpyOf<T> {
  const res: SpyOf<T> = {} as any;

  // turns out that in target:es2015 the methods attached to the prototype are not enumerable so Object.keys returns []. So to workaround that and keep some backwards compatibility - merge with ownPropertyNames - that disregards the enumerable property.
  // the Set remove duplicate entries
  const keys = new Set([
    ...(Object.keys(obj.prototype) as Array<keyof T>),
    ...(Object.getOwnPropertyNames(obj.prototype) as Array<keyof T>),
  ]);

  keys.forEach((key) => {
    if (typeof key === 'string') {
      (res[key] as any) = jasmine.createSpy(key);
    }
  });

  return res;
}

/** Keeps the types of properties of a type but assigns type of Spy to the methods */
export type SpyOf<T> = T &
  Partial<{
    [k in keyof T]: T[k] extends (...args: any[]) => any ? jasmine.Spy : T[k];
  }>;

from scuri.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.