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

Jest example #10

Open
vladmelnikov opened this issue Dec 16, 2020 · 1 comment
Open

Jest example #10

vladmelnikov opened this issue Dec 16, 2020 · 1 comment

Comments

@vladmelnikov
Copy link

Thank you for library its awesome. But i ran into issue cant configure with jest. Can you please provide example how to setup with jest ts-jest

@ZettZet
Copy link

ZettZet commented Sep 8, 2023

Three years later, this question will be answered:)
It may no longer be relevant to you, I guess, but somebody will find it helpful.

There are two main points to connect di-compiler to ts-jest:

  1. let ts-jest know about transformer
  2. implement a transformer

let's start with the second one

import type { DiOptions } from '@wessberg/di-compiler'
import { di } from '@wessber/di-compiler'
import type { TsCompilerInstance } from 'ts-jest/dist/types'

export const version = 1
export const name = '@wessberg/di-compiler'

type Options = {
  isAfter?: boolean
}

export function factory(compilerInstance: TsCompilerInstance, options?: Options) {
  const typescript = compilerInstance.configSet.compilerModule

  const config: DiOptions = {
    program: compilerInstance.program,
    typescript: ts
  }

  const { before, after } = di(config)
  if (options?.isAfter) {
    return after?.[0]
  }

  return before?.[0]
}

After that we can add this transformer to jest.config.js:

/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
  transform: {
    '^.+\\.[tj]sx?$': [
      'ts-jest',
      {
        astTransformers: {
          before: [{ path: 'relative path to transformer file' }],
          after: [{ path: 'relative path to transformer file', options: { isAfter: true } }],
        }
      }
    ]
  }
  /* Rest of your config */
}

Maybe there is a better way to deal with it, but it worked for me. I hope it helps someone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants