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

[Meteor] global server variable not global to mocha files #29

Open
samhatoum opened this issue Jul 21, 2018 · 6 comments
Open

[Meteor] global server variable not global to mocha files #29

samhatoum opened this issue Jul 21, 2018 · 6 comments
Labels
help wanted Extra attention is needed imported

Comments

@samhatoum
Copy link
Contributor

Issue by brucejo75
Friday Apr 29, 2016 at 04:56 GMT
Originally opened as xolvio/chimp#355


I am trying to use the server global to initialize data for all of my tests outside of an it block. This becomes quite useful if you use a data pattern to create the tests. My example is to get the users DB and create a test to verify each user.

Is there some reason why server is only available in an it block? It took me 3-4 hrs to realize this limitation when I first started to use chimp.

Plus I am unable to create any sort of workaround. Frustrating.

Thanks!

Expected behaviour

I expect to be able to reference the global server variable in any scope within a mocha file. It is global after all.

Actual behaviour

The global servervariable is only defined within a test (it block).

Steps to reproduce the problem

try to use the global server variable outside of an it block and you will find it is undefined.

Used version
  • Chimp: 0.33.1
  • Node.js: v5.8.0
  • Operation system: Windows 10
  • Meteor: 1.3.2.4
Console / Log Output

N/A

@samhatoum samhatoum added help wanted Extra attention is needed imported labels Jul 21, 2018
@samhatoum
Copy link
Contributor Author

Comment by Sanjo
Friday Apr 29, 2016 at 06:33 GMT


You can use the before hook to do stuff before the tests run: https://mochajs.org/#hooks.

@samhatoum
Copy link
Contributor Author

Comment by brucejo75
Friday Apr 29, 2016 at 18:02 GMT


Thanks for responding @sanjo.
Yes, I am aware of this hook. Essentially, I am trying to apply this pattern using the data from a server call. Here is test showing what I was trying to do:

debugger;
describe.only("test description", function() {
  let users = server.call("getUsers");
  users.forEach(function(user) {
    it("run test for " + user.emails[0].address, function(done) {
      // test stuff
      console.log(user.emails[0].address);
      done();
    });
  });
});

  • I tried before hooks prior to the describe block, thinking it would hook the describe block, That did not work because all hooks are for the test functions.
  • I tried nesting it functions and declaring in the outer function but that never executed the code in the inner it. I do not think that nested it blocks are allowed.
  • I tried creating a test in a preceding describe block, but all the describe blocks are executed prior to any tests running.
  • I tried importing a file that does the initialization of the users variable. But again all describe blocks are evaluated before any it blocks.

The problem is I need access to the users variable within the describe block. When I step through the code, not matter what I tried the describe block is executed before the it blocks anywhere.

Is it a hard constraint that server is only defined in it blocks?

Thanks!

@samhatoum
Copy link
Contributor Author

Comment by samhatoum
Thursday May 05, 2016 at 14:18 GMT


According to this line, it should be available to you globally

https://github.com/xolvio/chimp/blob/master/src/lib/chimp-helper.js#L66

However for the mocha helper, we only call the helpers and such in a before block:

https://github.com/xolvio/chimp/blob/master/src/lib/mocha/mocha-helper.js#L9

which means it's simply not ready by the time you try.

Perhaps you might get away with removing the before block here:
https://github.com/xolvio/chimp/blob/master/src/lib/mocha/mocha-helper.js#L5

Try forking the repo and removing that line. If it works, let us know :)

@samhatoum
Copy link
Contributor Author

Comment by brucejo75
Friday May 06, 2016 at 19:14 GMT


Thanks for the pointer. I tried pulling the code out of the before wrapper. I get an error (see console output below).

In essence, this line:
https://github.com/xolvio/chimp/blob/master/src/lib/chimp-helper.js#L110
is a wrapasync call which returns a future.wait(). There is no Fiber.current so the wait throws.

I am a little out of my depth here, but I think there may be a couple of options:

  1. Does setting the global.browser variable really need to be synchronous? Or could the setting of the global.browser be a normal async call, setting the global.browser and chaiAsPromised.transferPromiseness in the callback?
  2. Maybe this function could have a modified wrapasync? This thread says that you can wait on a future without a fiber by using future.resolve.
  3. Create a fiber?

Thanks for any input.

Console Output

[chimp][helper] setupBrowserAndDDP had error
[Error: Can't wait without a fiber]
Error: Can't wait without a fiber
    at Function.wait (F:\chimp\node_modules\fibers\future.js:159:9)
    at Object.Future.wait (F:\chimp\node_modules\fibers\future.js:448:10)
    at F:\chimp\node_modules\xolvio-fiber-utils\index.js:48:19
    at setupBrowser (chimp-helper.js:117:24)
    at Object.setupBrowserAndDDP (chimp-helper.js:199:7)
    at Object.<anonymous> (mocha-helper.js:10:13)
    at Module._compile (module.js:413:34)
    at loader (F:\chimp\node_modules\babel-register\lib\node.js:126:5)
    at Object.require.extensions.(anonymous function) [as .js] (F:\chimp\node_modules\babel-register\lib\node.js:136:7)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at F:\chimp\node_modules\mocha\lib\mocha.js:216:27
    at Array.forEach (native)
    at Mocha.loadFiles (F:\chimp\node_modules\mocha\lib\mocha.js:213:14)
Mocha failed

@samhatoum
Copy link
Contributor Author

Comment by brucejo75
Monday May 09, 2016 at 01:31 GMT


Well, I could not leave it alone :-).

I put all the init steps into a function and fiberize'd it. Now setupBrowserAndDDP seems to call the future.wait() inside the wrapAsync and never come back.

I tried to find any mocha event hooks that would be called after Mocha is initialized and ready to start running. I could not find anything. I did find this thread kind of addressing the issue. But the problem for us is that server is still not defined.

If you can point me in some more useful directions, I will take a look.

@samhatoum
Copy link
Contributor Author

Comment by samhatoum
Thursday May 12, 2016 at 19:18 GMT


This is not a trivial one I'm afraid and without investing a lot of time it'll be hard for us to help much.

You can try to use the before hook to remove any existing data and then set it up again.

We can offer paid support if you guys desperately need this. I've also marked this as help-wanted in case anyone out there would like to invest the time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed imported
Projects
None yet
Development

No branches or pull requests

1 participant