Coder Social home page Coder Social logo

ionic-team / cap-plugin-mock-jasmine Goto Github PK

View Code? Open in Web Editor NEW
4.0 8.0 2.0 751 KB

Show how to mock a Capacitor plugin in a Jasmine based unit test

Shell 0.34% TypeScript 49.12% JavaScript 6.47% HTML 5.32% SCSS 23.22% Java 3.53% Swift 9.48% Ruby 2.51%

cap-plugin-mock-jasmine's Introduction

Capacitor Plugin Mocks with Jasmine

Mocking a plugin in a unit test can be a challenge. Most mocking libraries mock an object by wrapping a JavaScript proxy around the object. Capacitor plugins, however, are already set up as JavaScript proxies, and you cannot create a proxy of a proxy. To get around this when using Jest as your testing framework, manual mocks would typically be used. Angular projects, however, use Jasmine by default and the manual mocking is not available. This project shows how to set up a similar structure within a projet that is using the Jasmine unit testing framework.

The Manual Mocks (Jest)

For this project, I am using two Capacitor plugins: @capacitor/storage and @capacitor/toast. With an @ionic/vue or @ionic/react project, both of which use Jest as the default unit testing framework, I create the following file structure at the root of the project:

.
|
+- __mocks__
| |
| +- @capacitor
|   |
|   +- storeage.ts
|   +- toast.ts
...
+- src

The code in these files create some simple stubs that I use within the tests. For example:

storeage.ts

export const Storage = {
  async get(data: { key: string }): Promise<{ value: string | undefined }> {
    return { value: undefined };
  },

  async set(data: { key: string; value: string }): Promise<void> {},
  async clear(): Promise<void> {},
};

toast.ts

export const Toast = {
  async show(data: {
    text: string;
    duration?: 'short' | 'long';
    position?: 'bottom' | 'center' | 'top';
  }): Promise<void> {},
};

As you can see, they do almost nothing, which is exactly what you want stubs to do.

When I want to have fine-grained control in my tests, I can create mocks on the stubs and have complete control over my tests. For example, here is a test for an @ionic/vue application that controls the return value for Storage.get():

  it("gets the first and last name", async () => {
    Storage.get = jest.fn().mockImplementation(
      async (data: { key: string }): Promise<{ value: string }> => {
        return data.key === "firstName"
          ? { value: "Jimmy" }
          : data.key === "lastName"
          ? { value: "Simms" }
          : { value: "unknown" };
      }
    );
    const w = mount(Home);
    await flushPromises();
    expect(w.vm.firstName).toEqual("Jimmy");
    expect(w.vm.lastName).toEqual("Simms");
  });

The combination of the Jest manual mocks and the standard Jest mocks provides this level of control.

The Manual Mocks (Jasmine)

For my Angular project, I would like to write a very similar test:

  it("gets the first and last name", async () => {
    spyOn(Storage, 'get');
    (Storage.get as any)
      .withArgs({ key: 'firstName' })
      .and.returnValue(Promise.resolve({ value: 'Jimmy' }));
    (Storage.get as any)
      .withArgs({ key: 'lastName' })
      .and.returnValue(Promise.resolve({ value: 'Simms' }));
    fixture.detectChanges();
    await fixture.whenRenderingDone();

    expect(component.firstName).toEqual('Jimmy');
    expect(component.lastName).toEqual('Simms');
  });

When I try this, I get odd errors because the spyOn is trying to wrap Storage.get() in a proxy. However, Storage already is a proxy so this doesn't work (we would have the same issue in our Jest based test above without the manual mock in place).

Ideally, I would create a manual mock to stub @capacitor/storage just like I did for my @ionic/vue project, but unlike Jest, Jasmine does not have this capability. TypeScript allows me to easily fake it, though.

First, set up a __mocks__ directory exactly the same as was done with the Jest based projects:

.
|
+- __mocks__
| |
| +- @capacitor
|   |
|   +- storeage.ts
|   +- toast.ts
...
+- src

storeage.ts

export const Storage = {
  async get(data: { key: string }): Promise<{ value: string | undefined }> {
    return { value: undefined };
  },

  async set(data: { key: string; value: string }): Promise<void> {},
  async clear(): Promise<void> {},
};

toast.ts

export const Toast = {
  async show(data: {
    text: string;
    duration?: 'short' | 'long';
    position?: 'bottom' | 'center' | 'top';
  }): Promise<void> {},
};

With Jest these will automatically be picked up by the tests. With Jasmine this will not happen automatically, but we can use TypeScript's file mapping features in order to do the same thing.

Typically, within the tsconfig.json file, I do a little TypeScript path mapping in order to avoid the use of relative paths. For example, I typically have a mapping such as the following:

    "paths": {
      "@app/*": ["src/app/*"],
      "@env/*": ["src/environments/*"],
      "@test/*": ["test/*"]
    },

Extend this in the tsconfig.spec.json file as such:

    "paths": {
      "@app/*": ["src/app/*"],
      "@env/*": ["src/environments/*"],
      "@test/*": ["test/*"],
      "@capacitor/*": ["__mocks__/@capacitor/*"]
    }

Now, when your code is being compiled for testing, and the compiler comes across code such as import { Stroage } from @capacitor/storage, TypeScript will look in __mocks__/@capacitor for the code rather than node_modules.

There are a couple of things to keep in mind:

  • Replacing the paths object in tsconfig.spec.json replaces the whole object, so you need to copy any paths that are set up in tsconfig.json so everything will continue to work properly.
  • When running tests, TypeScript will no longer look in node_modules for any @capacitor file you are importing, so you need to create stubs for everything you are using.

Conclusion

With a little bit of TypeScript path mapping, we can facilitate the same kind of manual mocking that Jest has within our Jasmine based tests, making it very easy to mock the @capacitor plugins that are used within our code.

Happy Testing, Everybody!! ๐Ÿค“

cap-plugin-mock-jasmine's People

Contributors

kensodemann avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cap-plugin-mock-jasmine's Issues

How to Unit Test App.addListener in Jasmine

Hi,

I am trying to write Jasmine unit tests for events passed from App.
Here is my code .ts file:

public _disableBackButton(): void { App.addListener('backButton', () => { return; }); }

Here is .spec file:

 it('should disable back button', fakeAsync(() => { 
   spyOn(App, 'addListener').and.callFake((type: string, fn: BackButtonListener) => { 
      return { type, fn };
   });

   component._disableBackButton();
   App.addListener('backButton', () => ({}));
   expect(App.addListener).toBeDefined();
 }));

But it can't run into callFake.
The docs on testing seem limited, and I cannot see any issues on this. Am I missing a simple thing?

Thanks in advance!

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.