简体   繁体   中英

How do I mock externally injected libraries that use import (ES6 typescript) exported in a node module for unit testing

In my main app, I am bringing in the node package "ibm_db" using: import * as ibmdb from "ibm_db";

In my unit tests I want to be able to override this, then I: import * as ibmdb from "ibm_db";

in my unit test and then:

beforeEach(() => {
   ibmdb.open = jasmine.createSpy("open");
});

I get error:

Cannot assign to 'open' because it is a readOnly property.

I need to know with typescript (being compiled into js using tsc, then tested using jasmine command), the right way to mock these functions so I can tell if there being called, I don't want the calls to actually fire.

For ES6 imports use:

import * as name from "library_name"

and in the unit tests use:

const name = require("library_name");

Requires will let you overwrite parts of the library where imports will not

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM