kulshekhar / ts-jest

A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript.
https://kulshekhar.github.io/ts-jest
MIT License
6.91k stars 452 forks source link

[Bug]: Import issue when using "Satisfies" keyword #4100

Open ajubin opened 1 year ago

ajubin commented 1 year ago

Version

29.1.0

Steps to reproduce

My repository is private, I tried to reproduce on an example repo but I couldn't.

My bug is the following, I have 3 files

aFunction.spec.ts // testing aFunction.ts
aFunction.ts // importing model.ts
model.ts 

With the following content

// in aFunction.spec.ts
import { aFunction } from './aFunction';

test('bug class undefined when using satisifies', () => {
  const method = aFunction(['a']);
  expect(() => method).toThrow();
});

// in aFunction.ts
import { create, ReturnType } from './model';

export function aFunction(names: string[] | undefined) {
  return {
    experiences: names?.map((name) =>
      create({
        name: name,
      }),
    ),
  } satisfies ReturnType;
}

// in model.ts
interface Props {
  name: string;
}
export type ReturnType = {
  experiences?: Props[];
};

export function create(props: Props) {
  return { name: props.name };
}

When I run yarn jest I've got the following error ReferenceError: model_1 is not defined

But the error disappear in one of the following cases:

Expected behavior

imports from 'model' are indeed imported

Actual behavior

ReferenceError

Debug log

ts-jest.log

Additional context

// Jest.config.ts

import type { Config } from '@jest/types';

export default async (): Promise<Config.InitialOptions> => {
  return {
    moduleFileExtensions: ['js', 'json', 'ts'],
    rootDir: 'src',
    testRegex: '(?<!e2e|matching).spec.ts$',
    transform: {
      '^.+\\.(t|j)s$': [
        'ts-jest',
        {
          isolatedModules: true, // The following line improves perf : https://huafu.github.io/ts-jest/user/config/isolatedModules
        },
      ],
    },
 }
}

maybe useful packages version

Environment

System:
    OS: macOS 13.2.1
    CPU: (8) arm64 Apple M1 Pro
  Binaries:
    Node: 18.9.0 - /var/folders/rb/7gpds4dj163b0g71gyr42brh0000gn/T/fnm_multishells/20546_1681215510586/bin/node
    Yarn: 1.22.19 - /var/folders/rb/7gpds4dj163b0g71gyr42brh0000gn/T/fnm_multishells/20546_1681215510586/bin/yarn
    npm: 8.19.2 - /var/folders/rb/7gpds4dj163b0g71gyr42brh0000gn/T/fnm_multishells/20546_1681215510586/bin/npm
  npmPackages:
    jest: ^29.3.1 => 29.3.1
tfmertz-aw commented 1 year ago

Having the same issue. Jest explicitly calls out not recognizing satisfies from typescript. Removing satisfies runs the test correctly.

} satisfies Color);
      ^^^^^^^^^

    SyntaxError: Unexpected identifier
bvaughn commented 1 year ago

Hitting this same issue 😄 Need any help moving this along?

alainfonhof commented 1 year ago

Having the same issue. Any updates on this? It mentions in the changelog that v29.1.0 supports Typescript 5.x

onivue commented 1 year ago

same issue with jest" 29.5.0, any updates?

Slate245 commented 1 year ago

Same here. Jest 29.5.0, typescript 4.9.5

jordanmnunez commented 11 months ago

Same here. Bummer that this has been an issue for 5 months.

Typescript: 5.0.4 ts-jest: 29.1.1 jest: 29.6.2

KubaZachacz commented 11 months ago

It's quite impressive how it can't go along with TypeScript changes.

Any ideas how to handle that? My code is more safe with satisfies than with unit tests, but still need to make coverage target :skull:

JohnBerd commented 4 months ago

Same here it's been around one year now with no update from the team. satisfies is widely used by now, and still no support from ts-jest

hjumeau commented 2 months ago

I don't know what the difficulties are in integrating the management of the satisfies operator into ts-jest. But it's a bit complicated not to be able to use a widely used typescript operator because the tests don't support it… Could we know why this is not integrated yet? What technical difficulties are you encountering?

ahnpnl commented 1 month ago

I don't see the Reference Error anymore. Does anyone have reproduce repo for this?

jordanmnunez commented 1 month ago

@ahnpnl ya, I have no idea what changed when but I am able to use satisfies now.