nats.ws: Compiling Angular code with nats.ws as dependency fails

Hi there,

first of all, thank you for your work. This repo helped us a lot.

Currently, we are using an older version of nats.ws, and when trying to update to nats.ws 1.0.0-117, we encounter a few compilation errors, such as:

Error: ./node_modules/nats.ws/nats.mjs 1070:22
Module parse failed: Unexpected token (1070:22)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|         this._off = 0;
|     }
>     _tryGrowByReslice = (n)=>{
|         const l = this._buf.byteLength;
|         if (n <= this.capacity - l) {
 @ ./src/app/app.component.ts 2:0-34 40:8-15
 @ ./src/app/app.module.ts
 @ ./src/main.ts
 @ multi ./src/main.ts

This is a log from trying to compile this example repo I set up, but it’s the same log in our main application.

The example repo uses Angular 11, we currently use Angular 10.

If I follow the errors and fix them, the code works, but this is not a good solution because we would need to copy the code from the dependency into our own repository and keep making the same modifications for all upcoming updates.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 5
  • Comments: 16 (12 by maintainers)

Most upvoted comments

Hi @aricart, could you perhaps release a new version of this lib?

It compiles now, but it’s still awkward to consume.

To the Angular users:

If you are using Jest for testing, you will run into another rabbit hole because of *.mjs. For now, I solves this by adding a nats.mock.ts file somewhere, with the content:

// nats.mock.ts

export const connect = jest.fn();

with usage in a test suite:

// socket.service.spec.ts
// Note that, even if you don't use it in tests, any test that indirectly imports `nats.ws` will break.

describe('My Socket Service', () => {
    connectMock = <jest.Mock>nats.connect;
    beforeEach(() => {
        connectMock.mockReset();
    })
})

and the addition to jest.config.js:

module.exports = {
    // ... other config
    transform: {
        // I think this should already be included if you are using jest for Angular
        '^.+\\.(ts|html)$': 'ts-jest',
    },
    moduleNameMapper: {
        // note that the file ending here is `.js`, not `.ts`, probably because of ts-jest
        'nats.ws': 'path/to/nats.mock.js',
    }
}