rescript-jest icon indicating copy to clipboard operation
rescript-jest copied to clipboard

Error when using `finish(fail)` on master

Open carere opened this issue 3 years ago • 3 comments

Hello, first of all, thx for your amazing work 👌🏾

I got some problem when testing async code as follow:

open GlennslRescriptJest.Jest

describe("Executing some code which return errors", _ => {
  testAsync("should properly trigger fail", finish => {
    finish(fail("some error"))
  })
})

which translates in following JS:

// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Curry from "rescript/lib/es6/curry.js";
import * as Jest$GlennslRescriptJest from "@glennsl/rescript-jest/src/jest.mjs";

Jest$GlennslRescriptJest.describe(
  "Executing some code which return errors",
  function (param) {
    return Jest$GlennslRescriptJest.testAsync(
      "should properly trigger fail",
      undefined,
      function (finish) {
        return Curry._1(finish, Jest$GlennslRescriptJest.fail("some error"));
      }
    );
  }
);

export {};
/*  Not a pure module */

Errors with:

 FAIL  test/unit/some_test.test.mjs
  Executing some code which return errors
    ✕ should properly trigger fail (1 ms)

  ● Executing some code which return errors › should properly trigger fail

    ReferenceError: fail is not defined

      11 |       undefined,
      12 |       function (finish) {
    > 13 |         return Curry._1(finish, Jest$GlennslRescriptJest.fail("some error"));
         |                      ^
      14 |       }
      15 |     );
      16 |   }

      at affirm (node_modules/@glennsl/rescript-jest/src/jest.mjs:36:9)
      at node_modules/@glennsl/rescript-jest/src/jest.mjs:232:19
      at Module._1 (node_modules/rescript/lib/es6/curry.js:32:12)
      at test/unit/some_test.test.mjs:13:22
      at Module._1 (node_modules/rescript/lib/es6/curry.js:32:12)
      at Object.<anonymous> (node_modules/@glennsl/rescript-jest/src/jest.mjs:231:17)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.219 s, estimated 1 s

Here you can find my bsconfig.json:

{
  "name": "bijensu",
  "sources": [
    {
      "dir": "src",
      "subdirs": true
    },
    {
      "dir": "test",
      "subdirs": true,
      "type": "dev"
    }
  ],
  "suffix": ".mjs",
  "package-specs": {
    "module": "es6",
    "in-source": true
  },
  "bs-dev-dependencies": ["@glennsl/rescript-jest"],
  "bs-dependencies": ["@ryyppy/rescript-promise"],
  "warnings": {
    "error": "+101"
  }
}

And my package.json:

{
  "name": "test-rescript-jest",
  "type": "module",
  "scripts": {
    "build": "rescript",
    "clean": "rescript clean -with-deps",
    "start": "rescript build -w",
    "test:unit": "jest test/unit"
  },
  "devDependencies": {,
    "jest": "27.*",
    "rescript": "9.*",
    "@glennsl/rescript-jest": "glennsl/rescript-jest#master",
    "@ryyppy/rescript-promise": "2.*"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "mjs"
    ],
    "testMatch": [
      "**/test/**/*.test.mjs"
    ]
  }
}

As you can see, i use the master version of your package since npm can't find any 0.8 version on npm.org And i also use open GlennslRescriptJest.Jest since Rescript cannot find Jest namespace. My environnement is: OS: Elementary OS 6 Odin (Ubuntu 20.04.3 LTS) Node: v16.13.0 (with flag --experimental-vm-modules in order to use ESM module without babel) Yarn: 1.22.17

Im available if you need more information about this issue 😉

carere avatar Nov 29 '21 11:11 carere

After further investigation, it seems that this issue is the problem.

So in order to fix the problem, we can either create a global function fail as follow:

function fail(reason = "fail was called in a test.") {
  throw new Error(reason);
}

global.fail = fail;

Or in order to stay in ReScript world, we can simply use this:

test("fail", () => Js.Exn.raiseError("A description in order to understand the test failure"))

This way, jest displays his nice messages output 😄 I close this one since the problem is not from you wonderful work 😅

carere avatar Nov 30 '21 07:11 carere

Thanks for the report. And the research!

If you don't mind I'd like to keep this open until they've fixed this upstream as we have to update the dependency here as well.

Also, another workaround from that issue:

Can circumvent in 27.x with testRunner: "jest-jasmine2" in jest.config.js

glennsl avatar Nov 30 '21 19:11 glennsl

Of course 😄 glad to help !!

carere avatar Dec 01 '21 12:12 carere