to-run-test-with.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Module dependencies
  3. */
  4. var util = require('util');
  5. var _ = require('@sailshq/lodash');
  6. var infer = require('../../lib/infer');
  7. var getDisplayType = require('../../lib/get-display-type');
  8. var isEqual = require('../../lib/is-equal');
  9. var getAbbreviatedDisplayVal = require('../../lib/helpers/get-abbreviated-display-val');
  10. module.exports = function toRunTestWith(transformationFn) {
  11. return function _runTest(expectations, cb){
  12. // Determine type schema of the value.
  13. // (using inference to pull it from the `example`, if provided)
  14. var typeSchema;
  15. if (!_.isUndefined(expectations.type)) {
  16. typeSchema = expectations.type;
  17. }
  18. else {
  19. typeSchema = infer(expectations.example);
  20. }
  21. // Create backup copies of our type schema and example
  22. // (we validate whether they were inadvertently changed below)
  23. var originalExampleBackup = _.cloneDeep(expectations.example);
  24. var originalTypeSchemaBackup = _.cloneDeep(typeSchema);
  25. // Now validate and/or coerce the actual value against the type schema.
  26. var actualResult;
  27. var gotError;
  28. try {
  29. actualResult = transformationFn(typeSchema, expectations.actual);
  30. }
  31. catch (e) {
  32. gotError = e;
  33. }
  34. // Finally, make sure the right thing happened and that we
  35. // got the appropriate result.
  36. //
  37. //
  38. // Ensure that if we got an error, we were expecting it.
  39. if (gotError){
  40. if (expectations.error) {return cb();}
  41. return cb(new Error('did not expect error, but got one:\n' + util.inspect(gotError) + '\n\nHere is the stack from the error:\n'+gotError.stack+'\n' ));
  42. }
  43. // Handle case where we were expecting an error, but we didn't get one.
  44. if (expectations.error) {
  45. return cb(new Error('expected a error, but did not get one. Instead, returned '+util.inspect(actualResult, false, null)+'.'));
  46. }
  47. // If an expected `result` is provided, compare the actual result against that.
  48. // Otherwise compare it against the original value (`actual`)
  49. var compareTo = expectations.hasOwnProperty('result') ? expectations.result : expectations.actual;
  50. if (!isEqual(actualResult, compareTo, typeSchema)) {
  51. return cb(new Error('returned incorrect value: '+getAbbreviatedDisplayVal(actualResult)+' (a '+getDisplayType(actualResult)+')'));
  52. }
  53. // Test using strict equality (===) if explicitly requested
  54. if (expectations.strictEq) {
  55. if (actualResult !== compareTo) {
  56. return cb(new Error('returned value is equivalent (but not ===)'));
  57. }
  58. }
  59. // Test AGAINST strict equality using `isNew` if requested
  60. // (i.e. guarantees this is a new value and is !== what was passed in)
  61. if (expectations.isNew) {
  62. // Check both the expected result and the actual value, just to be safe.
  63. // (should never even be possible for it to be a direct reference to the expected result)
  64. if (actualResult === compareTo || actualResult === expectations.actual) {
  65. return cb(new Error('returned value === value that was passed in -- but should have been a new value!'));
  66. }
  67. }
  68. // Test that the `example` originally passed in, as well as the
  69. // inferred `typeSchema`, have not been altered.
  70. //
  71. // (The `typeSchema` should NEVER change as a result of running this test.
  72. // And neither should `expectations.example`, if it was provided.)
  73. if (!isEqual(typeSchema, originalTypeSchemaBackup)) {
  74. return cb(new Error('inferred type schema was modified as a result of running test!! it became: '+util.inspect(typeSchema, {depth: null})+' (a '+getDisplayType(typeSchema)+')'));
  75. }
  76. if (!_.isUndefined(originalExampleBackup)) {
  77. if (!isEqual(expectations.example, originalExampleBackup)) {
  78. return cb(new Error('`example` was modified as a result of running test!! it became: '+util.inspect(expectations.example, {depth: null})+' (a '+getDisplayType(expectations.example)+')'));
  79. }
  80. }
  81. // If we made it here, everything's good!
  82. return cb();
  83. };
  84. };