LeaVerou / htest

Declarative, boilerplate-free unit testing
https://htest.dev/
MIT License
19 stars 3 forks source link

Allow to replace files with other (mock) ones #25

Open DmitrySharabin opened 8 months ago

DmitrySharabin commented 8 months ago

In Madata, we have a natively supported Firebase backend. However, we face an issue when we need to test it (or any other Madata feature depending on this backend, e.g., the Backend.from() method) in Node. The Firebase backend imports the modules it depends on (provided by Google) via URLs with the https scheme, which Node's default ESM loader does not support.

It would be nice if hTest could provide a way to replace one file (e.g., by intercepting the corresponding request) with another one on the fly. For example, we could provide hTest a mock Firebase backend (with the functionality we need to test) instead of the real one. This feature will allow hTest to avoid choking over features unsupported by Node (or any other environment the tests run in).

This enhancement might also be helpful in other cases, such as when we need to test features requiring some user actions, like authentication.

As another option, hTest might support something similar to Angular's Dependency Injection, where the author can use some existing class instead of the injected one. If I'm correct, this feature is heavily used exactly when some class (component, directive, etc.) needs to be tested, and it depends on a (injected) class that is replaced by another (mock) class.

In other words, it would be helpful if hTest supports something like (unsure about the API, just an example):

export default {
    replace: {
        "./src/foo.js": "./test/foo.js",
    },
    tests: [...],
};

OR

export default {
    use: {
        "./src/foo.js": "./test/foo.js",
    },
    tests: [...],
};