ThomZz / ts-jest-mock-import-meta

A simple Typescript AST transformer for ts-jest, that mock import.meta expressions.
MIT License
33 stars 2 forks source link

SyntaxError: Identifier '__filename' has already been declared #5

Open EphraimB opened 1 year ago

ThomZz commented 1 year ago

Hello,

Please provide a little bit more context on your problem, mentioning which version of typescript, ts-jest and nodeJS you are using, and also your current jest config file.

Thanks,

EphraimB commented 1 year ago

I'm using the latest lts version of everything. I'm not near a computer now to post the jest config file.

ThomZz commented 11 months ago

Please post the jest.config.(ts/js)

EphraimB commented 11 months ago

import type { JestConfigWithTsJest } from 'ts-jest' import { defaults } from 'jest-config';

const jestConfig: JestConfigWithTsJest = { ...defaults, preset: 'ts-jest/presets/default-esm', testEnvironment: 'node', transform: { '^.+\.(t|j)s$': ['ts-jest', { tsconfig: './tsconfig.test.json', useESM: true }] }, moduleNameMapper: { '^(\.\.?\/.+)\.js$': '$1' } };

export default jestConfig;

Get Outlook for iOShttps://aka.ms/o0ukef


From: ThomZz @.> Sent: Monday, July 24, 2023 11:58:49 AM To: ThomZz/ts-jest-mock-import-meta @.> Cc: Ephraim Becker @.>; Author @.> Subject: Re: [ThomZz/ts-jest-mock-import-meta] SyntaxError: Identifier '__filename' has already been declared (Issue #5)

Please post the jest.config.(ts/js)

— Reply to this email directly, view it on GitHubhttps://github.com/ThomZz/ts-jest-mock-import-meta/issues/5#issuecomment-1648189103, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACQA6J5DLASOCLCVFMROOC3XR2LTTANCNFSM6AAAAAA2DQQZQM. You are receiving this because you authored the thread.Message ID: @.***>

ThomZz commented 11 months ago

import type { JestConfigWithTsJest } from 'ts-jest' import { defaults } from 'jest-config'; const jestConfig: JestConfigWithTsJest = { ...defaults, preset: 'ts-jest/presets/default-esm', testEnvironment: 'node', transform: { '^.+\.(t|j)s$': ['ts-jest', { tsconfig: './tsconfig.test.json', useESM: true }] }, moduleNameMapper: { '^(\.\.?\/.+)\.js$': '$1' } }; export default jestConfig; Get Outlook for iOShttps://aka.ms/o0ukef ____ From: ThomZz @.> Sent: Monday, July 24, 2023 11:58:49 AM To: ThomZz/ts-jest-mock-import-meta @.> Cc: Ephraim Becker @.>; Author @.> Subject: Re: [ThomZz/ts-jest-mock-import-meta] SyntaxError: Identifier '__filename' has already been declared (Issue #5) Please post the jest.config.(ts/js) — Reply to this email directly, view it on GitHub<#5 (comment)>, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACQA6J5DLASOCLCVFMROOC3XR2LTTANCNFSM6AAAAAA2DQQZQM. You are receiving this because you authored the thread.Message ID: @.***>

And if you add the ast transformer, it fails with the aforementioned error ? I would like to see the complete jest.config, including the ast-transformer configuration, for wich you can replicate the error.

EphraimB commented 11 months ago

https://github.com/EphraimB/budgeting/commit/69986d0558eaf09ac140b9d0a4b96078bf094107

Get Outlook for iOShttps://aka.ms/o0ukef


From: ThomZz @.> Sent: Monday, July 24, 2023 12:06:26 PM To: ThomZz/ts-jest-mock-import-meta @.> Cc: Ephraim Becker @.>; Author @.> Subject: Re: [ThomZz/ts-jest-mock-import-meta] SyntaxError: Identifier '__filename' has already been declared (Issue #5)

import type { JestConfigWithTsJest } from 'ts-jest' import { defaults } from 'jest-config'; const jestConfig: JestConfigWithTsJest = { ...defaults, preset: 'ts-jest/presets/default-esm', testEnvironment: 'node', transform: { '^.+.(t|j)s$': ['ts-jest', { tsconfig: './tsconfig.test.json', useESM: true }] }, moduleNameMapper: { '^(..?\/.+).js$': '$1' } }; export default jestConfig; Get Outlook for iOShttps://aka.ms/o0ukef … ____ From: ThomZz @.> Sent: Monday, July 24, 2023 11:58:49 AM To: ThomZz/ts-jest-mock-import-meta @.> Cc: Ephraim Becker @.>; Author @.> Subject: Re: [ThomZz/ts-jest-mock-import-meta] SyntaxError: Identifier '__filename' has already been declared (Issue #5https://github.com/ThomZz/ts-jest-mock-import-meta/issues/5) Please post the jest.config.(ts/js) — Reply to this email directly, view it on GitHub<#5 (comment)https://github.com/ThomZz/ts-jest-mock-import-meta/issues/5#issuecomment-1648189103>, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACQA6J5DLASOCLCVFMROOC3XR2LTTANCNFSM6AAAAAA2DQQZQM. You are receiving this because you authored the thread.Message ID: @.***>

And if you add the ast transformer, it fails with the aforementioned error ? I would like to see the complete jest.config, including the ast-transformer configuration, for wich you can replicate the error.

— Reply to this email directly, view it on GitHubhttps://github.com/ThomZz/ts-jest-mock-import-meta/issues/5#issuecomment-1648201163, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACQA6J2JHNXS4A7MNCCDVBTXR2MQFANCNFSM6AAAAAA2DQQZQM. You are receiving this because you authored the thread.Message ID: @.***>

EphraimB commented 11 months ago

import type { JestConfigWithTsJest } from 'ts-jest' import { defaults } from 'jest-config';

const jestConfig: JestConfigWithTsJest = { ...defaults, preset: 'ts-jest/presets/default-esm', testEnvironment: 'node', transform: { '^.+\.(t|j)s$': ['ts-jest', { tsconfig: './tsconfig.json', useESM: true, 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: './' } } } ] } }] }, moduleNameMapper: { '^(\.\.?\/.+)\.js$': '$1' } };

export default jestConfig;

null-prophet commented 7 months ago

same issues with esModule project.

if I remove the dirname or filename var in my code it works. Then obviously my code is broken when I run it normally in ts-node etc.

ts-jest seems to have a different environment than whats setup in the ts-node when I run the code.

bulkbeings-admin commented 3 months ago

@null-prophet were you able to figure out a way for this?

EphraimB commented 3 months ago

I chose a different solution in the end

Get Outlook for iOShttps://aka.ms/o0ukef


From: bulkbeings-admin @.> Sent: Thursday, April 4, 2024 9:01:12 AM To: ThomZz/ts-jest-mock-import-meta @.> Cc: Ephraim Becker @.>; Author @.> Subject: Re: [ThomZz/ts-jest-mock-import-meta] SyntaxError: Identifier '__filename' has already been declared (Issue #5)

@null-prophethttps://github.com/null-prophet were you able to figure out a way for this?

— Reply to this email directly, view it on GitHubhttps://github.com/ThomZz/ts-jest-mock-import-meta/issues/5#issuecomment-2037153469, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACQA6J2DEUPKOR654C2GP3LY3VFJRAVCNFSM6AAAAAA2DQQZQOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMZXGE2TGNBWHE. You are receiving this because you authored the thread.Message ID: @.***>

bulkbeings-admin commented 3 months ago

what's that?

EphraimB commented 3 months ago

I found a way to not rely on import meta

Get Outlook for iOShttps://aka.ms/o0ukef


From: bulkbeings-admin @.> Sent: Thursday, April 4, 2024 9:03:39 AM To: ThomZz/ts-jest-mock-import-meta @.> Cc: Ephraim Becker @.>; Author @.> Subject: Re: [ThomZz/ts-jest-mock-import-meta] SyntaxError: Identifier '__filename' has already been declared (Issue #5)

what's that?

— Reply to this email directly, view it on GitHubhttps://github.com/ThomZz/ts-jest-mock-import-meta/issues/5#issuecomment-2037158587, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ACQA6J3FQOZHTBNCQLJW6SLY3VFSXAVCNFSM6AAAAAA2DQQZQOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMZXGE2TQNJYG4. You are receiving this because you authored the thread.Message ID: @.***>