Sinon is not designed to handle the specific issue of substituting a module for a fake one at the module loader level. This task is typically addressed by third-party libraries like proxyquire and rewire. These libraries specialize in module substitution, commonly referred to as a "link seam" in testing literature.
For CommonJS environments, the Sinon team has provided a helpful guide on how to address this issue. You can find more information at https://sinonjs.org/how-to/link-seams-commonjs/.
If you are encountering the @lib/...
string, it may indicate a webpack-specific problem. In such cases, it is recommended to explore module replacement libraries tailored for webpack, such as inject-loader.
Although Sinon can sometimes be used to replace exports within a module, this is highly dependent on the environment. ES Modules, for example, export an immutable namespace, making it challenging to override exports in a standard manner.
If you wish to stub exports, you may need to deviate from ES Module standards by producing writable exports or adjusting the read-only nature of your environment.
Alternative Testing Approaches
In a separate answer, I discuss two methods of employing plain dependency injection to address this issue without external tools. While straightforward, these approaches may require minor changes to your production code for testing purposes. It's important to weigh the pros and cons of introducing code changes versus additional dependencies.
For a more detailed example of this technique, you can refer to a discussion on the Sinon issue tracker where I demonstrate how to optionally inject dependencies: a more elaborate example of this technique on the Sinon issue.