paulmillr / noble-ed25519

Fastest 4KB JS implementation of ed25519 signatures
https://paulmillr.com/noble
MIT License
414 stars 50 forks source link

Jest gives an error: "SyntaxError: Unexpected token export" #85

Closed toniton closed 1 year ago

toniton commented 1 year ago

I'm am using @noble/ed25519 in my NestJS application, and using Jest for unit test coverage.

When running my test, I get the following error:

 FAIL  src/<**********>.spec.ts
  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
...

    Details:

    /Users/<*******************************>/node_modules/@noble/ed25519/index.js:373
    export { getPublicKey, getPublicKeyAsync, sign, verify, // Remove the export to easily use in REPL
    ^^^^^^

    SyntaxError: Unexpected token 'export'

Package.json

{
  "name": "project",
  "version": "1.0.0",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/axios": "^2.0.0",
    "@nestjs/common": "^9.3.12",
    "@nestjs/config": "^2.3.1",
    "@nestjs/core": "^9.3.12",
    "@nestjs/mongoose": "^9.2.2",
    "@nestjs/platform-express": "^9.3.12",
    "@noble/ed25519": "^2.0.0",
    "axios": "^1.3.4",
    "class-validator": "^0.14.0",
    "reflect-metadata": "^0.1.13",
    "regtest-client": "^0.2.0",
    "rxjs": "^7.8.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.3.0",
    "@nestjs/schematics": "^9.1.0",
    "@nestjs/testing": "^9.3.12",
    "@types/express": "^4.17.17",
    "@types/jest": "29.5.0",
    "@types/node": "18.15.11",
    "@types/supertest": "^2.0.12",
    "@typescript-eslint/eslint-plugin": "^5.57.0",
    "@typescript-eslint/parser": "^5.57.0",
    "eslint": "^8.37.0",
    "eslint-config-prettier": "^8.8.0",
    "eslint-plugin-prettier": "^4.2.1",
    "jest": "29.5.0",
    "prettier": "^2.8.7",
    "source-map-support": "^0.5.21",
    "supertest": "^6.3.3",
    "ts-jest": "29.0.5",
    "ts-loader": "^9.4.2",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "4.2.0",
    "typescript": "^5.0.3"
  }
}
paulmillr commented 1 year ago

Make sure your Jest setup supports ESM (ecmascript modules), it's not on by default

toniton commented 1 year ago

Thanks Paul, that worked. Adding this comment for future reference.

I resolved this issue using the following config:

jest.config.ts

/** @type {import('jest').Config} */
const config = {
  moduleFileExtensions: ['js', 'json', 'ts'],
  rootDir: 'src',
  preset: 'ts-jest/presets/default-esm',
  extensionsToTreatAsEsm: ['.ts'],
  globals: {
    'ts-jest': {
      useESM: true,
    },
  },
  testRegex: '.*\\.spec\\.ts$',
  transform: {
    '^.+\\.(t|j)s$': 'ts-jest',
  },
  testEnvironment: 'node',
  transformIgnorePatterns: ['/node_modules/(?!@noble)'],
};

module.exports = config;

ts.config.json

{
  "compilerOptions": {
    ...
    "resolveJsonModule": true,
    "allowJs": true,
    "esModuleInterop": true,
    ...
  }
}