Zaid-Ajaj / Fable.Mocha

Fable library for a proper testing story using different runners such as mocha, standalone browsers and dotnet
MIT License
54 stars 17 forks source link

ES module error even with type: module #73

Open playfulThinking opened 2 months ago

playfulThinking commented 2 months ago

I followed your installation directions faithfully (I hope). when I run yarn test I get this error:

$ dotnet fable -o dist/tests
Fable 4.19.3: F# to JavaScript compiler
Minimum @fable-org/fable-library-js version (when installed from npm): 1.4.2

Thanks to the contributor! @kerams
Stand with Ukraine! https://standwithukraine.com.ua/

Parsing Tests.fsproj...
Retrieving project options from cache, in case of issues run `dotnet fable clean` or try `--noCache` option.
Project and references (58 source files) parsed in 118ms

Skipped compilation because all generated files are up-to-date!
$ mocha dist/tests
node:internal/modules/cjs/loader:1432
      throw err;
      ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/michaelfremont/Documents/playthink/code/zebra-shmebra/packages/zebra-shmebra-app/tests/dist/tests/Tests.js from /usr/local/lib/node_modules/mocha/lib/mocha.js not supported.
Instead change the require of Tests.js in /usr/local/lib/node_modules/mocha/lib/mocha.js to a dynamic import() which is available in all CommonJS modules.
    at /usr/local/lib/node_modules/mocha/lib/mocha.js:184:27
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/usr/local/lib/node_modules/mocha/lib/mocha.js:181:14)
    at Mocha.run (/usr/local/lib/node_modules/mocha/lib/mocha.js:393:31)
    at Object.<anonymous> (/usr/local/lib/node_modules/mocha/bin/_mocha

package.json:

{
  "type": "module",
  "scripts": {
    "pretest": "dotnet fable -o dist/tests",
    "test": "mocha dist/tests"
  },
  "devDependencies": {
    "mocha": "^10.6.0"
  }
}

Tests.fsproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Tests.fs" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\fsharp\App.fsproj" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Fable.Core" Version="4.3.0" />
    <PackageReference Include="Fable.Mocha" Version="2.17.0" />
  </ItemGroup>

</Project>

Tests.fs:

module Tests

open Fable.Mocha

let arithmeticTests =
    testList "Arithmetic tests" [
        test "plus works" {
            Expect.equal (1 + 1) 2 "plus"
        }

        test "Test for falsehood" {
            Expect.isFalse (1 = 2) "false"
        }

        testAsync "Test async code" {
            let! x = async { return 21 }
            let answer = x * 2
            Expect.equal 42 answer "async"
        }
    ]

Mocha.runTests arithmeticTests

Thanks!

playfulThinking commented 2 months ago

So I uninstalled the current version of Mocha and installed 9.2.0 is in your example, and it now works - there's apparently something in newer versions causing the problem...