1

I am trying to express something like the following:

it("should use the 'text' mode", () => {
    let usedMockMode = false;

    let env = new Environment();
    let mockMode = { parse: () => usedMockMode = true };
    env.modes.set("text", mockMode);

    return env.parseContent("foo", "text")
        .should.eventually.be.true(() => usedMockMode);
                       // ^--------------------------^
                       //        Pseudo-code
})

Is there a way to achieve this?

Lea Hayes
  • 62,536
  • 16
  • 62
  • 111
  • What ist your testing framework (aka where does your `it` come from)? mocha? Jasmine? something else? Most of them support an parameter to the function starting in your first line. – Patrick J. S. Jan 05 '16 at 00:17
  • @Aᴍɪʀ: that's an assertion framework. – Patrick J. S. Jan 05 '16 at 00:20
  • 1
    @PatrickJ.S. I am using the mocha.js testing framework and in some cases I'm using the 'mocha-testdata' module which allows me to pass data for many test iterations through the arguments of my `it` test function. – Lea Hayes Jan 05 '16 at 00:22
  • Have you tried using [match](https://shouldjs.github.io/#assertion-match)? `.should.eventually.match(() => usedMockMode)` – Chris Anderson Jan 05 '16 at 00:27
  • @ChrisAnderson-MSFT that doesn't seem to be working for me. – Lea Hayes Jan 05 '16 at 00:33

2 Answers2

0

I didn't have a new node and mocha at hand. but it should work with arrows as well.

describe('promise fn', function(){
  it('should fullfill a promise', function(done){
    …
    return env.parseContent('foo', 'bar').then(function(){
      (usedMockMode).should.be.true})
      .finally(done);
  });
})
Patrick J. S.
  • 2,885
  • 19
  • 26
  • This doesn't seem to be working for me. The test hangs for 2000ms and then it throws a timed out error specifying that the `done` callback should have been invoked by the test. Even if I just invoke `done();` directly after the assertion; it still hangs. Even when I remove the `return` keyword from the promise. I am probably making a silly mistake... but it certainly wasn't working for me :/ – Lea Hayes Jan 05 '16 at 09:50
0

After some trial-and-error I eventually found that the following works:

it("should use the 'text' mode", () => {
    let usedMockMode = false;

    let env = new Environment();
    let mockMode = { parse: () => usedMockMode = true };
    env.modes.set("text", mockMode);

    return env.parseContent("foo", "text")
        .then(() => {
            usedMockMode.should.be.true();
        });
})

This didn't seem very intuitive to me since there is obviously some magic behind the scenes to bind the assertion to the context of the test.

Lea Hayes
  • 62,536
  • 16
  • 62
  • 111
  • You are using normal promise syntax. it has nothing to do with the assertion. With `.then` you are creating a new promise that gets executed _after_ the `parseContent` promise is resolved. – Patrick J. S. Jan 05 '16 at 17:20