羽毛连接客户端-服务器测试时套接字连接超时

时间:2019-04-03 09:57:31

标签: socket.io feathersjs

我正在通过羽毛首页上的“聊天应用程序”进行浏览,然后我正在编写测试的舞台上。 https://docs.feathersjs.com/guides/chat/testing.html

我已经将client-users.test.js设置为与文章中指出的代码完全相同

/* eslint-disable indent */
/* eslint-disable no-console */
const assert = require('assert');
const feathersClient = require('@feathersjs/client');
const io = require('socket.io-client');
const app = require('../../src/app');

const host = app.get('host');
const port = app.get('port');
const email = 'login@example.com';
const password = 'login';

describe('\'users\' service - client', function () {
  this.timeout(20000);
  let server;
  let client;

  before(async () => {
    await app.service('users').create({
      email,
      password
    });

    server = app.listen(port);
    server.on('listening', async () => {
      // eslint-disable-next-line no-console
      console.log(
        'CLIENT - Feathers application started on http://%s:%d',
        host,
        port
      );
    });
    client = await makeClient(host, port, email, password);
  });

  after((done) => {
    client.logout();
    server.close(done);
  });

  describe('Run tests using client and server', () => {
    it('registered the service', () => {
      const service = client.service('users');

      assert.ok(service, 'Registered the service');
    });

    it('creates a user, encrypts password and adds gravatar', async () => {
      const user = await client.service('users').create({
        email: 'testclient@example.com',
        password: 'secret'
      });

      // Verify Gravatar has been set to what we'd expect
      assert.equal(
        user.avatar,
        'https://s.gravatar.com/avatar/1b9c869fa7a93e59463c31a377fe0cf6?s=60'
      );
      // Makes sure the password got encrypted
      assert.ok(user.password !== 'secret');
    });

    it('removes password for external requests', async () => {
      // Setting `provider` indicates an external request
      const params = {
        provider: 'rest'
      };

      const user = await client.service('users').create({
          email: 'testclient2@example.com',
          password: 'secret'
        },
        params
      );

      // Make sure password has been removed
      assert.ok(!user.password);
    });
  });
});

async function makeClient(host, port, email, password) {
  const client = feathersClient();
  const socket = io(`http://${host}:${port}`, {
    transports: ['websocket'],
    forceNew: true,
    reconnection: false,
    extraHeaders: {}
  });
  client.configure(feathersClient.socketio(socket));
  client.configure(feathersClient.authentication({
    storage: localStorage()
  }));

  await client.authenticate({
    strategy: 'local',
    email,
    password,
  });

  return client;
}

function localStorage() {
  const store = {};

  return {
    setItem(key, value) {
      store[key] = value;
    },
    getItem(key) {
      return store[key];
    },
    removeItem(key) {
      delete store[key];
    }
  };
}

我跑步时 npm test 最后两个测试用例失败,如下所示:

 1) 'users' service - client
       "before all" hook:
     Error: Socket connection timed out
      at Timeout._onTimeout (node_modules\@feathersjs\client\dist\feathers.js:2534:18)

  2) 'users' service - client
       "after all" hook:
     TypeError: Cannot read property 'logout' of undefined
      at Context.after (test\services\client-users.test.js:37:12)

我认为logout只是因为客户端无法在1上建立套接字连接

请注意,我已尝试注释掉所有文件app.test.js(这是另一个包含初始化应用程序语句的地方,我认为它可能与此问题有关eddyystop在https://github.com/feathersjs/docs/issues/1220上发表了评论关于Windows 10无法运行 delete require.cache[...] 注释掉所有文件app.test.js之后,对client-users.test.js的测试工作正常。

我的问题是为什么会发生这种魔术,以及如何解决? 谢谢大家。

0 个答案:

没有答案