ts-jest-mock-import-meta
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

ts-jest-mock-import-meta AST transformer Npm package version

Install

npm install -D ts-jest-mock-import-meta

📗 This transformer is aimed to be used with ts-jest

That moment finally came, where you want to test your esm code that use "import.meta" expressions. (if your are like me, and you do not embrace the TDD approach 😏). You type "jest" in the console, and this infamous error pops up :

> jest

 FAIL  src/anyfile.spec.ts
  ● Test suite failed to run

    src/anyfile.spec.ts:2:24 - error TS1343: The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'esnext', or 'system'.

    2   const url = new URL('./', import.meta.url) !== undefined;
                             ~~~~~~~~~~~

Worry not !

Here comes this simple typescript AST transformer to the rescue. By using it "before" typescript transpilation, it will simply replace any "import.meta" expressions in typescript files by a mocked object. "import.meta" expressions are not compatible with jest, that by default, works in commonjs, so they need to be stripped down and replaced by a mocked object before typescript transpilation is done by ts-jest.

Configuration examples (jest.config) :

For ts-jest >= 29.0.0

{
  // [...]
  transform: {
    '^.+\\.tsx?$': [
      'ts-jest',
      {
        diagnostics: {
          ignoreCodes: [1343]
        },
        astTransformers: {
          before: [
            {
              path: 'node_modules/ts-jest-mock-import-meta',  // or, alternatively, 'ts-jest-mock-import-meta' directly, without node_modules.
              options: { metaObjectReplacement: { url: 'https://www.url.com' } }
            }
          ]
        }
      }
    ]
  }
}

For ts-jest < 29.0.0

{
  // [...]
  globals: {
    'ts-jest': {
      diagnostics: {
        ignoreCodes: [1343]
      },
      astTransformers: {
        before: [
          {
            path: 'node_modules/ts-jest-mock-import-meta',  // or, alternatively, 'ts-jest-mock-import-meta' directly, without node_modules.
            options: { metaObjectReplacement: { url: 'https://www.url.com' } }
          }
        ]
      }
    }
  }
}

⚠️ IMPORTANT: error code 1343 MUST be ignored for the transformer to work. https://github.com/Microsoft/TypeScript/blob/main/src/compiler/diagnosticMessages.json#L1035

See ts-jest options documentation for more details about configuration : https://kulshekhar.github.io/ts-jest/docs/getting-started/options

Options

  • [metaObjectReplacement]:

    [Optional] The mocked object that will substitute every "import.meta" expressions. It can contains any properties. example :

    {
      url: 'https://www.url.com',
      env: {
        PROD: false,
        DEV: true
      },
      status: 2
    }

    [Default Value]

    { url: __dirname }

Install

npm i ts-jest-mock-import-meta

DownloadsWeekly Downloads

10,726

Version

1.0.0

License

MIT

Unpacked Size

8.89 kB

Total Files

5

Last publish

Collaborators

  • thomzz