Emurgo / cardano-serialization-lib

This is a library, written in Rust, for serialization & deserialization of data structures used in Cardano's Haskell implementation of Alonzo along with useful utility functions.
Other
231 stars 125 forks source link

Error when running Jest tests #453

Open bigirishlion opened 2 years ago

bigirishlion commented 2 years ago

I'm currently working on a project that uses Typescript and Jest to test out my library. When I run tests, I'm getting this error:

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import * as wasm from "./cardano_serialization_lib.asm.js";
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

       6 |     }
       7 |
    >  8 |     cardanoWasm = await import('@emurgo/cardano-serialization-lib-asmjs/cardano_serialization_lib');

Here is my tsconfig.js

{
    "compilerOptions": {
        "target": "es2016",
        "module": "commonjs",
        "declaration": true,
        "outDir": "./lib",
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true,
        "skipLibCheck": true
    },
    "exclude": ["node_modules", "lib"]
}

And jest.config.js

module.exports = {
  testEnvironment: 'node',
  preset: 'ts-jest/presets/js-with-ts',
  testMatch: ['**/*.test.ts'],
  moduleFileExtensions: ["js", "ts"],
  moduleDirectories: ["node_modules", "src"],
  transform: {
    '^.+\\.(ts|tsx)$': 'ts-jest',
  }
};

Any idea how to fix this error?

vsubhuman commented 2 years ago

This is unrelated to our library, plz google running jest with typescript

https://bobbyhadz.com/blog/typescript-jest-cannot-use-import-statement-outside-module#:~:text=my%20test%20passes.-,The%20TypeScript%20jest%20error%20%22Cannot%20use%20import%20statement%20outside%20module,its%20own%20cannot%20understand%20TypeScript.

bigirishlion commented 2 years ago

@vsubhuman Thanks for the reply.

I've definitely isolated it to the library because when I remove the package, all the tests work properly. I'm curious if you know of any projects out there that are using Jest to run tests against the asmjs version of the library?

vsubhuman commented 2 years ago

because when I remove the package, all the tests work properly

Hi, @bigirishlion! Plz show how you load the asm package when running the tests

bigirishlion commented 2 years ago

because when I remove the package, all the tests work properly

Hi, @bigirishlion! Plz show how you load the asm package when running the tests

I've tried it multiple ways:

Standard import:

import * as serializationLib from '@emurgo/cardano-serialization-lib-asmjs/cardano_serialization_lib';

I've also tried dynamically loading the package via:

export const loadCardanoWasm = async () => {
    if (cardanoWasm) {
        return cardanoWasm;
    }

    cardanoWasm = await import('@emurgo/cardano-serialization-lib-asmjs/cardano_serialization_lib');
    return cardanoWasm;
};

Also, if I try without cardano_serialization_lib I get this error:

import * as serializationLib from '@emurgo/cardano-serialization-lib-asmjs';

Cannot find module '@emurgo/cardano-serialization-lib-asmjs' from 'src/index.ts'