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 running Jest tests #453

Open
bigirishlion opened this issue Jun 11, 2022 · 4 comments
Open

Error when running Jest tests #453

bigirishlion opened this issue Jun 11, 2022 · 4 comments
Assignees

Comments

@bigirishlion
Copy link

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

@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 vsubhuman reopened this Jun 13, 2022
@vsubhuman vsubhuman self-assigned this Jun 13, 2022
@vsubhuman
Copy link
Contributor

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

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'

@vsubhuman vsubhuman assigned ozgrakkurt and unassigned vsubhuman Jun 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants