cardano-serialization-lib icon indicating copy to clipboard operation
cardano-serialization-lib copied to clipboard

Error when running Jest tests

Open bigirishlion opened this issue 2 years ago • 4 comments

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?

bigirishlion avatar Jun 11 '22 18:06 bigirishlion

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.

vsubhuman avatar Jun 12 '22 10:06 vsubhuman

@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?

bigirishlion avatar Jun 13 '22 17:06 bigirishlion

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

vsubhuman avatar Jun 13 '22 17:06 vsubhuman

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'

bigirishlion avatar Jun 13 '22 17:06 bigirishlion