Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Expose nock via comlink #61

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

[WIP] Expose nock via comlink #61

wants to merge 4 commits into from

Conversation

CvX
Copy link
Collaborator

@CvX CvX commented Jun 21, 2019

Implements the nock proxy originally mentioned in #53:

import { nock } from 'ember-cli-fastboot-testing/test-support';

await nock('http://localhost:7357')
  .intercept('/api/notes/1', 'GET')
  .reply(200, { status: 'ok' });

The code quality is very much at the "proof of concept" level, but it does work.

You can see usage examples in tests: https://github.com/CvX/ember-cli-fastboot-testing/blob/5bc6c47fec9669eff338f6d1098c58ea1b6c9213/tests/fastboot/nock-proxy-test.js

The postMessage()/addEventListener() architecture of comlink doesn't mesh well with the HTTP client/server stuff we have in ECFT. It might be a good idea to replace that with Web Sockets, though that might have issues of its own. We'll see!

Closes #53
Closes #50

@ryanto
Copy link
Member

ryanto commented Jun 24, 2019

This looks awesome and I'm super excited for getting the whole nock interface.

What happens if a call to nock is made with the wrong arguments, or to an undefined method? Does comlink report back the errors in the browser?

@@ -15,13 +15,15 @@
},
"scripts": {
"build": "ember build",
"install": "cd node_modules/comlink && npm install && npm run build",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does comlink need to be compiled? Does this have any negatives or gotchas?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the compile step is necessary when using the git repo source. Comlink’s written in TypeScript, and gets compiled to JS when published to npm, so here we need to do it on our own.

It’s just a temporary solution for sharing this work-in-progress. I wouldn’t even try publishing ECFT with it. 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah cool, good to know

"lint:js": "eslint .",
"start": "ember serve",
"test": "ember test",
"test:all": "ember try:each"
},
"dependencies": {
"body-parser": "^1.18.3",
"comlink": "CvX/comlink#wrap-chain",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you added some stuff here to get the chaning working. Should we wait for them to merge it or depend on this branch?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Continuing from the previous comment: I wouldn’t use this setup. I’d either wait for a comlink release or, in case that gets stuck, I’d push a fork of comlink with chain method calls to npm.

});

test('it can mock an array of models', async function(assert) {
await nock('http://localhost:7357')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to auto detect the local hostname and not require it here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could set the hostname in the application adapter, and use that here. It wouldn’t then change when, for example, using a different testing port.

@CvX
Copy link
Collaborator Author

CvX commented Jun 25, 2019

What happens if a call to nock is made with the wrong arguments, or to an undefined method? Does comlink report back the errors in the browser?

The error message quality may vary 😉 but yeah, the errors get proxied back to the browser. For example:

await nock('http://localhost:7357').intercept().reply(200, { ok: true });
not ok 41 Chrome 75.0 - Fastboot | nock proxy: it can mock a single model
    ---
        actual: >
            null
        stack: >
            Error: The "method" parameter is required for an intercept call.
                at new Interceptor (/Users/cvx/dev/ember-cli-fastboot-testing/node_modules/nock/lib/interceptor.js:26:15)
                at Scope.intercept (/Users/cvx/dev/ember-cli-fastboot-testing/node_modules/nock/lib/scope.js:85:12)
                at /Users/cvx/dev/ember-cli-fastboot-testing/node_modules/comlink/src/comlink.ts:142:65
                at processTicksAndRejections (internal/process/task_queues.js:89:5)
        message: >
            Promise rejected during "it can mock a single model": The "method" parameter is required for an intercept call.
        negative: >
            false
        Log: |
    ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow more customization of network mocking urls Allow to specify request body on serverMock
2 participants