score:0

Accepted answer

from this doc: https://developer.mozilla.org/en-us/docs/web/javascript/reference/statements/import

the name parameter is the name of the "module object" which will be used as a kind of namespace to refer to the exports. the export parameters specify individual named exports, while the import * as name syntax imports all of them.

that means if you use import * as io from 'socket.io-client', the io is a kind of namespace.

in your mock file, the io is an object and export default io;

if you use import * as io from 'socket.io-client'. the value of io variable will be:

{ // <- the outer object is io namespace
  io: { // <- this is the io object you defined in your mock file
    connect() {
      return socket;
    }
  }
}

use import io from 'socket.io-client', it will import the default object from it which is:

const io = {
  connect() {
    return socket;
  },
};

if you insist using import * as io from 'socket.io-client' statement. change

export const io = {
  connect() {
    return socket;
  },
};

to:

export function connect() {
  return socket;
}

and remove export default io; statement.


Related Query

More Query from same tag