Ts-jest: Function arrow name property.

Created on 14 Aug 2018  路  7Comments  路  Source: kulshekhar/ts-jest

Right now I'm using:

node.js: 9.8.0
Jest: 23.4.2
ts-jest: 23.1.3
typescript: 2.9.2

And when trying to doing the following in my *.test.ts files:

const foo = () => 'bar';
console.log(foo.name); // '' -- empty string, not undefined or null

foo has the property name in it but is empty. I've run the same code in a separate *.ts file, just to know whether or not was some .tsconfig setting, but the console prints out 'foo' as expected.

My jest.config.json:

{
    "bail": true,
    "expand": true,
    "verbose": true,
    "useStderr": true,
    "forceExit": true,
    "logHeapUsage": true,
    "collectCoverage": true,
    "testRegex": "(./ci/__tests__/.*| (\\.|/)(test|spec))\\.(jsx?|tsx?)$",
    "transform": {
        "^.+\\.tsx?$": "ts-jest"
    },
    "moduleFileExtensions": [
        "ts",
        "tsx",
        "js",
        "jsx",
        "json",
        "node"
    ],
    "coveragePathIgnorePatterns": [
        "./node_modules/",
        "./dist/"
    ],
    "coverageReporters": [
        "json",
        "lcov",
        "text"
    ],
    "globals": {
        "ts-jest": {
            "enableTsDiagnostics": true
        }
    }
}

Could anyone help me out? Any ideas would help a lot.

note: I've already tried StackOverflow and I really think this is a really good question since after looking around in Google I haven't found anything to help me out, and asking in a closed group would mean that others that someday have the same issues with that won't be able to find anything related to it.

note of a note: The Jest folks told me to talk to you guys.

Needs More Info

All 7 comments

@Fazendaaa thanks for reporting the issue. I guess you know that, but just to be sure: console.log are swallowed by jest. If they are within a it(...) ot test(...) block, then they should be printed, else I believe jest aren't outputing anything.

Please confirm this is not the case (that you console.log are inside a test(...) block), and if it is, post a link to a minimal repo so we can reproduce the issue and hopefully fix it.

Oh wait, on SO someone answered with the correct answer. Arrow functions are anonymous, so no name.

try this:

const foo = () => 'bar';
console.log(foo.name);
function bar() { return 'foo' };
console.log(bar.name);

UPDATE: I answered too quickly, TS is giving the name as the constant, sorry... but my first answer is still valid ;-)

@huafu Right now I'm trying a new approach of generating the test functions given.json files that describe the function to be tested. It works passing the function itself and its name as string, but when I tried to remove the string with the function name and use the .name property of the function I ran into this issue.

The problem itself is in line 25, here: https://github.com/Fazendaaa/AnilistBot/blob/master/ci/readMocks.ts, called in line 56 here https://github.com/Fazendaaa/AnilistBot/blob/master/ci/doTesting.ts and this is my first test https://github.com/Fazendaaa/AnilistBot/blob/master/ci/__tests__/utils/parse.test.ts

@Fazendaaa FYI you can link to exact line number by clicking in the margin on github ;-)

Ok so I think it's because transpilation is removing the name. Look at how your source is transpiled to javascript, the function becomes anonymous.

https://www.typescriptlang.org/play/#src=export%20const%20sanitize%20%3D%20(%7B%20message%20%7D%3A%20%7B%20message%3A%20string%20%7D)%3A%20string%20%3D%3E%20message.replace(%2F(%3F%3A%3D%5C(%7C%3A0%7C%3Ao%7C%3A%20o%7C%3A%200)%2F%2C%20'%3A%20o')%3B%0D%0A

screen shot 2018-08-14 at 16 28 27

So yeah, I'm closing this as it is not ts-jest related.

@Fazendaaa I can't think of any workaround right now, sorry

@Fazendaaa find more info there https://github.com/Microsoft/TypeScript/issues/6433

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mikeyakymenko picture mikeyakymenko  路  3Comments

bySabi picture bySabi  路  4Comments

bruk1977 picture bruk1977  路  3Comments

qm3ster picture qm3ster  路  3Comments

stangerjm picture stangerjm  路  4Comments