0

I'm using an external package called oauth4webapi and I can't seem to test it.

In my file where I use it...

import * as o from "oauth4webapi";

When testing I get this error:

    /Users/.../node_modules/oauth4webapi/build/index.js:7
    export const clockSkew = Symbol();
    ^^^^^^

    SyntaxError: Unexpected token 'export'

    > 1 | import * as o from "oauth4webapi";
        | ^

The actual file is a .js file that uses export const.

My jest.config.ts is below. I tried:

  1. transforming js/jsx files with babel-jest
  2. adding all the babel packages in my .babelrc didn't work
  3. adding transformIgnorePattern for the package to no avail. I tried adding moduleDirectories: ['node_modules', 'src'], along with this option to no avail.
  4. Adding "type": "module" to my package.json didn't work
// jest.config.ts
import type {Config} from 'jest';

const config: Config = {
  testEnvironment: 'jsdom',
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/$1',
    '\\.(css|less|sass|scss)$': '<rootDir>/__mocks__/styleMock.js',
    '\\.(gif|ttf|eot|svg)$': '<rootDir>/__mocks__/fileMock.js',
  },
  transform: {
    '^.+\\.tsx?$': 'ts-jest',
    '^.+\\.jsx?$': 'babel-jest',
  },
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  setupFiles: ['./jest.setup.ts'],
  // https://stackoverflow.com/a/49676319
  "transformIgnorePatterns": [
    "/node_modules/(?!(oauth4webapi))"
  ],
};

export default config;

My .babelrc I created to try to solve this issue

{
  "presets": ["@babel/preset-env"],
  "env": {
    "test": {
      "plugins": ["@babel/plugin-transform-modules-commonjs"]
    }
  }
}

My understading is that this external package uses esm in a .js file so it needs to be transformed. How can I do that in my jest.config.ts as all the things I've tried didn't work

wongz
  • 3,255
  • 2
  • 28
  • 55

0 Answers0