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

Test failed to run when importing typescript package #1236

Closed
MiroslavPetrik opened this issue Sep 23, 2019 · 2 comments
Closed

Test failed to run when importing typescript package #1236

MiroslavPetrik opened this issue Sep 23, 2019 · 2 comments

Comments

@MiroslavPetrik
Copy link

Issue :

I have a monorepo of 2 typescript packages.

{
    "name": "@repo/package-a",
    "main": "out/index.js",
    "types": "out/index.d.ts"
}

The second package depends on the first one.

{
    "name": "@repo/package-b",
    "main": "out/index.js",
    "types": "out/index.d.ts",
    "dependencies": {
        "@repo/package-a": "1.0.2"
    }
}

I have a test for the package-b which imports the package-a. The import fails, because the runtime tries to process javascript code instead of typescript:

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

 Details:

    /Users/user/repo/packages/package-a/out/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import * as Events from "./events";

Currently I'm able to fix this in 2 ways

  1. Point package.jsons main to the typescript code
    "main": "src/index.ts",

This is unacceptable, as this is not the correct way how to build TS npm package. It would be unusable from javascript, but this makes the test pass as the typescript is loaded.

  1. Add transform option to the jest config

If I add a transform option to my jest config, to transform the js file from previous error

{
    "jest": {
        "preset": "ts-jest",
        "testEnvironment": "node",
        "roots": [
            "./src/"
        ],
        "transform": {
            "\\.js$": "babel-jest"
        }
    }
}

and supply a babel.config.js at the same time with

module.exports = {
    presets: ["@babel/preset-env"],
};

then the test passes, as it can now handle the javascript source.
This is my current solution, but this should be NOT required. the ts-jest should be able to work out of the box in this case.

Expected behavior :

I expect the ts-jest to load the monorepo package as a typescript package, not a javascript one. There is the types key ("types": "out/index.d.ts",) defined, so it should be used.

I don't want to add babel config, @babel/preset-env devDependency and transform option to each of my packages using typescript packages in the tests. This should work out of the box.

Debug log:

log file content
# content of ts-jest.log :

Minimal repo :

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 8, 2020

would you please provide a repo to clone/download for this issue ?

@ahnpnl
Copy link
Collaborator

ahnpnl commented Jan 12, 2020

Close as related to monorepo support in #1336

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