Skip to content
This repository has been archived by the owner on Jun 21, 2020. It is now read-only.

test async functions #19

Closed
FrontMage opened this issue Aug 17, 2017 · 3 comments
Closed

test async functions #19

FrontMage opened this issue Aug 17, 2017 · 3 comments
Labels

Comments

@FrontMage
Copy link
Contributor

Hi, I've been using saul to test my code, it's really fantastic.
However when testing with asyn functions, I wrote an test engine like this:

const expect = require('chai').expect;

module.exports = (
  testDescription,
  func,
  argsArray,
  expected,
  test
) => {
  test(testDescription, () => {
    func.apply(null, argsArray)
      .then(actual => {
        expect(actual).to.eql(eval(`var trouble = ${expected}; trouble`));
      });
  });
};

It suppose to act like deep-equals with function returns promise, but it's not working and it did not throw any error.
Could you help me with this? Thanks :)

@nadeesha
Copy link
Owner

Hi @FrontMage, thanks for the question!

I think you need to export.default your engine. Like this:

const expect = require('chai').expect;

exports.default = (
  testDescription,
  func,
  argsArray,
  expected,
  test
) => {
  test(testDescription, () => {
    return func.apply(null, argsArray)
      .then(actual => {
        expect(actual).to.eql(eval(`var trouble = ${expected}; trouble`));
      });
  });
};

I've created a sample repo here with a working test: https://github.com/nadeesha/saul-async-test

Does that answer your question?

@FrontMage
Copy link
Contributor Author

FrontMage commented Aug 17, 2017

This did answer my question.
In fact using module.exports will throw an error, mine did not because the // @t "desc" was written as // @t 'desc'.
After I change ' to " it works great.
Can I make a pull request to add this to the default engines? I think people might find it handy.@nadeesha


I've encountered another problem, the async function I tried to test to returns a considerably large json, written them into one single line makes it very hard to read.
Maybe there is a support for this problem?

@nadeesha
Copy link
Owner

nadeesha commented Aug 17, 2017

Can I make a pull request to add this to the default engines? I think people might find it handy

Of course! Please.

I've encountered another problem, the async function I tried to test to returns a considerably large json, written them into one single line makes it very hard to read.

Yes, if the payload is serializable, and large, you could do a snapshot test: https://github.com/nadeesha/saul#matches-snapshot

// @t "returns correct json" myLargeJSONFunc() ~matches-snapshot

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

No branches or pull requests

2 participants