firsttris / vscode-jest-runner

Simple way to run or debug one or more tests from context menu, codelens or command plalette
https://marketplace.visualstudio.com/items?itemName=firsttris.vscode-jest-runner
MIT License
265 stars 124 forks source link

Changing the test file name before running Jest #190

Closed gtbono closed 3 years ago

gtbono commented 3 years ago

Hello,

I don't know if my use case is possible, but I will try to describe it here.

I'm using Node.js and TypeScript, but I don't run ts-node or ts-jest, I'm running tsc before testing.

I have my tests in

myproject/tests/unit/unit_test.ts

But they compile using tsc to:

myproject/dist/tests/unit/unit_test.js

So my jest config is as follows:

  "jest": {
    "moduleFileExtensions": [
      "js",
      "json"
    ],
    "rootDir": "dist",
    "testRegex": "dist/test/unit.*\\.spec\\.js$",
    "collectCoverageFrom": [
      "src/**/*.js"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node",
    "coveragePathIgnorePatterns": [
      ".module.js$",
      "main.js$"
    ]
  },

And to run, I run npm run test, with the following configuration:

"pretest": "tsc",
"test": "jest",

So when I run npm run test, it compiles my test to dist folder, and jest picks up the tests from there.

But here's my problem, when I run the tests from the extension, I could change my Jest executable, so they run the following command:

npm run test -- '/home/giovanni/Code/myproject/test/unit/controllers/HelloWorldController.spec.ts' -t 'HelloWorldController should do hello world' 

But because of my Jest config, it can't find my tests, because I would need to strip out the .ts extension from the test (or change to .js) and run them from the Jest root dir folder, like:

npm run test -- '/home/giovanni/Code/myproject/dist/test/unit/controllers/HelloWorldController.spec' -t 'HelloWorldController should do hello world' 

But I don't know if the extension provides something for me to configure this, or if I can achieve what I need by changing my Jest config, do you have any idea how I could do it? if you need any more info please ask me

firsttris commented 3 years ago

Jest ships with one transformer out of the box - babel-jest It will automatically load your project's Babel configuration and transform any file matching the following RegEx: /.[jt]sx?$/ meaning any .js, .jsx, .ts and .tsx file.

means you can just run your test-fiels with jest without thinking about transpilation

https://jestjs.io/docs/next/code-transformation#defaults

firsttris commented 3 years ago

if you have a custom jest config, you probably need to add the transformer option back

transform: { '\.(js|ts|jsx|tsx)$': 'babel-jest', }

gtbono commented 3 years ago

hi @firsttris !

this was exactly what I needed, I used the ts-jest transformer

thanks! :)