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

Add an example using Jest with TypeScript #2135

Closed
alexeagle opened this issue Aug 24, 2020 · 7 comments · Fixed by #2245
Closed

Add an example using Jest with TypeScript #2135

alexeagle opened this issue Aug 24, 2020 · 7 comments · Fixed by #2245

Comments

@alexeagle
Copy link
Collaborator

Probably show tsc running as a separate action - although there are speed benefits to having a Babel plugin in Jest do the transpilation instead.
Related #1761 and #1454

@vdeantoni
Copy link
Contributor

I took at stab at having Jest execute the output of ts_library. You can see/clone my repo.
It seems the *.test.js file is not accessible at runtime, see the README.me for more details.

@fredrikredflag
Copy link
Contributor

I managed to get this to fly @vdeantoni .

Some prerequisites:

The jest config needed to contain moduleNameMapper in order to resolve mappings correctly (jest_typescript is my workspace name):

module.exports = {
    testEnvironment: 'node',
    reporters: ['default'],
    testMatch: ['**/*.spec.js'],
    moduleNameMapper: {
        'jest_typescript/(.*)': '<rootDir>/$1',
    },
};

After this is pretty straight forward, doesn't even need a custom macro.

load("@npm//@bazel/typescript:index.bzl", "ts_library")
load("@npm//jest-cli:index.bzl", "jest_test")

ts_library(
    name = "test_lib",
    srcs = glob([
        "**/*.ts",
    ]),
    deps = [
        "//src:src",
        "@npm//@types",
        "@npm//chai"
    ],
)

jest_test(
    name = "unit_test",
    data = [
        "//:jest.config.js",
        ":test_lib",
        "@npm//chai"
    ],
)

@alexeagle
Copy link
Collaborator Author

@fredrikredflag that's cool, do you have a suggestion where we could add this to our examples so other ppl can find the solution?

@fredrikredflag
Copy link
Contributor

@alexeagle In order to make easy to find, maybe just add another example called jest_typescript?

The moduleNameMapper isn't the idea solution. But it works.

I could do a PR for it.

@alexeagle
Copy link
Collaborator Author

Ideally we'd keep a smallish number of examples so maybe add to the existing Jest one? A PR would be awesome, I don't have time ...

@vdeantoni
Copy link
Contributor

@fredrikredflag awesome, it worked great! thanks a lot

@mrmeku
Copy link
Collaborator

mrmeku commented Nov 3, 2020

I'll try to land an example in the near future now that jestjs/jest#9351 is finalized

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

Successfully merging a pull request may close this issue.

4 participants