Skip to content

Add an example using Jest with TypeScript #2135

Closed

Description

@alexeagle

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

Activity

  1. vdeantoni commented on Sep 7, 2020

    @vdeantoni
    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.

  2. fredrikredflag commented on Oct 19, 2020

    @fredrikredflag
    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"
        ],
    )
    
  3. alexeagle commented on Oct 20, 2020

    @alexeagle
    CollaboratorAuthor

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

  4. fredrikredflag commented on Oct 20, 2020

    @fredrikredflag
    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.

  5. alexeagle commented on Oct 20, 2020

    @alexeagle
    CollaboratorAuthor

    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 ...

  6. vdeantoni commented on Oct 20, 2020

    @vdeantoni
    Contributor

    @fredrikredflag awesome, it worked great! thanks a lot

  7. self-assigned this
    on Nov 3, 2020
  8. mrmeku commented on Nov 3, 2020

    @mrmeku
    Collaborator

    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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions