automerge / automerge-repo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't run sync server tests on Windows

HerbCaudill opened this issue · comments

I haven't been able to get the sync server tests to run on windows.

First, there's a line-endings issue with tests.sh. I can fix this manually by switching my local file from CRLF to LF. There's probably something we can do with the git config so that it doesn't do its normal magic with this one file's line endings on Windows.

Once that is fixed, it complains about imports in the js file.

Error message
↯ yarn workspace automerge-repo-sync-server test
yarn workspace v1.22.10
yarn run v1.22.10
$ bash ./scripts/tests.sh
yarn run v1.22.10
yarn run v1.22.10
$ mocha --no-warnings --experimental-specifier-resolution=node --exit
$ node ./src/index.js
/mnt/c/git/pvh/automerge-repo/packages/automerge-repo-sync-server/src/index.js:2
import fs from "fs"
    ^^

SyntaxError: Unexpected identifier
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Error: Not supported
    at Object.exports.doImport (/mnt/c/git/pvh/automerge-repo/node_modules/mocha/lib/nodejs/esm-utils.js:35:41)
    at formattedImport (/mnt/c/git/pvh/automerge-repo/node_modules/mocha/lib/nodejs/esm-utils.js:9:28)
    at Object.exports.requireOrImport (/mnt/c/git/pvh/automerge-repo/node_modules/mocha/lib/nodejs/esm-utils.js:42:34)   
    at Object.exports.loadFilesAsync (/mnt/c/git/pvh/automerge-repo/node_modules/mocha/lib/nodejs/esm-utils.js:100:34)   
    at Mocha.loadFilesAsync (/mnt/c/git/pvh/automerge-repo/node_modules/mocha/lib/mocha.js:447:19)
    at singleRun (/mnt/c/git/pvh/automerge-repo/node_modules/mocha/lib/cli/run-helpers.js:125:15)
    at exports.runMocha (/mnt/c/git/pvh/automerge-repo/node_modules/mocha/lib/cli/run-helpers.js:190:10)
    at Object.exports.handler (/mnt/c/git/pvh/automerge-repo/node_modules/mocha/lib/cli/run.js:370:11)
    at innerArgv.then.argv (/mnt/c/git/pvh/automerge-repo/node_modules/yargs/build/index.cjs:443:71)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
./scripts/tests.sh: line 7: kill: (15) - No such process
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed.
Exit code: 1
Command: C:\Program Files\nodejs\node.exe
Arguments: C:\Users\herb\AppData\Roaming\npm\node_modules\yarn\lib\cli.js test
Directory: C:\git\pvh\automerge-repo\packages\automerge-repo-sync-server
Output:

info Visit https://yarnpkg.com/en/docs/cli/workspace for documentation about this command.

In localfirst/relay, the server is written as a typescript class; and there's a separate repo (relay-deployable) that just contains a small js file to instantiate the class and run the server. That setup makes it easy to test the server's functionality without having to spin up separate processes:

import { Server } from './Server'

 // ...

describe('Server', () => {
  let url: string
  let server: Server

  beforeAll(async () => {
    const port = await getAvailablePort({ port: 3100 })
    url = `ws://localhost:${port}`
    server = new Server({ port })
    await server.listen({ silent: true })
  })

  afterAll(() => {
    server.close()
  })

  // ...

  it('should make a connection', done => {
    const { aliceId } = setup()

    server.on('introductionConnection', userName => {
      expect(userName).toEqual(aliceId)
      expect(server.peers).toHaveProperty(aliceId)
      expect(server.documentIds).toEqual({})
      done()
    })

    // make a connection
    const alice = new WebSocket(`${url}/introduction/${aliceId}`)
  })

// ...

})

If that seems like a reasonable approach I'll just refactor the server and tests along those lines, rather than trying to get that script to work cross-platform.