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

Error when using finish(fail) on master #99

Open
carere opened this issue Nov 29, 2021 · 3 comments
Open

Error when using finish(fail) on master #99

carere opened this issue Nov 29, 2021 · 3 comments

Comments

@carere
Copy link

carere commented Nov 29, 2021

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
Copy link
Author

carere commented Nov 30, 2021

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 carere closed this as completed Nov 30, 2021
@glennsl
Copy link
Owner

glennsl commented Nov 30, 2021

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 glennsl reopened this Nov 30, 2021
@carere
Copy link
Author

carere commented Dec 1, 2021

Of course 😄 glad to help !!

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

No branches or pull requests

2 participants