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

ts-jest aliases does not work: Cannot find module '@/server'.ts(2307) but code compiles normally #94474

Closed
kuskoman opened this issue Apr 5, 2020 · 5 comments
Assignees
Labels
*question Issue represents a question, should be posted to StackOverflow (VS Code) typescript Typescript support issues

Comments

@kuskoman
Copy link

kuskoman commented Apr 5, 2020

  • VSCode Version: 1.43.2
    0ba0ca5
    x64
  • OS Version: Windows 10 Education

Steps to Reproduce:

  1. Create any TypeScript file
  2. Install typescript, ttypescript, jest, ts-jest
  3. IDE will show Cannot find module <file> (2307) for every file using moduleMappings

Example:
jest.config.js:

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  roots: ['<rootDir>/test'],
  transform: {
    '^.+\\.tsx?$': 'ts-jest'
  },
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1',
    '^@@/(.*)$': '<rootDir>/test/$1'
  }
}

part of the test file:

import { DateTimeInput } from '@/generated/prisma-client'
import { startTestServer } from '@@/helpers/apolloInstance'
import prisma from '@@/helpers/prismaInstance'
import { createUser } from '@@/helpers/userHelper'
import { toPromise } from 'apollo-link'
import gql from 'graphql-tag'

every line using '@' or '@@' has a red underline and error message

error image

tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "outDir": "dist",
    "lib": ["es6", "es7", "esnext", "dom"],
    "sourceMap": true,
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"],
      "@@/*": ["test/*"]
    },
    "esModuleInterop": true,
    "plugins": [{ "transform": "typescript-transform-paths" }]
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules"]
}

Does this issue occur when all extensions are disabled?: Yes

@mjbvz
Copy link
Collaborator

mjbvz commented Apr 6, 2020

In the test file, run TypeScript: Go to project configuration. Does this open the correct tsconfig?

If so, can you please share a complete minimal project that demonstrates this issue?

@mjbvz mjbvz added info-needed Issue requires more information from poster typescript Typescript support issues labels Apr 6, 2020
@kuskoman
Copy link
Author

kuskoman commented Apr 6, 2020

Effect of TypeScript: Go to project configuration:
error message
project screen

@mjbvz I created a repository example-vscode-error

@mjbvz
Copy link
Collaborator

mjbvz commented Apr 6, 2020

Thanks. This helped me understand the problem.

The issue is that the test file is not considered part of the tsconfig project. This is because the project sets:

  "include": ["src/**/*.ts"],

so only files under src are considered to be part of that tsconfig project. If you want the paths to apply to your tests as well, make sure to also include the test directory in your tsconfig

@mjbvz mjbvz closed this as completed Apr 6, 2020
@mjbvz mjbvz added *question Issue represents a question, should be posted to StackOverflow (VS Code) and removed info-needed Issue requires more information from poster labels Apr 6, 2020
@kuskoman
Copy link
Author

kuskoman commented Apr 7, 2020

@mjbvz I understand that you do not consider it as a bug, however your answer is not helpful and may be misleading for other people- adding test directory to include is workaround, not a solution. There is no reason to compile dozens of test files just to make IDE not throwing errors.
Jest (with ts-jest preset) and probably other test framework are able to use tsconfig.json to get path aliases, while their files are not included in tsconfig.
I would also mention thing that VSCode does not properly support Jest is even more annoying because of lack of easy ways to change it (at least I don't know any way to disable path errors for specific directory, without disabling rest of ts server features).

@mjbvz
Copy link
Collaborator

mjbvz commented Apr 7, 2020

If you want that paths setting from a tsconfig to apply in a file, that file needs to be part of a tsconfig project. Either:

  • Create another tsconfig under tests that also configures paths. Make sure the tests files are part of this
  • Or create a tsconfig.src.json at the top level of your project and use that compile. This tsconfig can extend your tsconfig.json but should include files from src

VS Code itself is library agnostics so are not going to special case how we handle ts files that look like they might be jest tests

@github-actions github-actions bot locked and limited conversation to collaborators May 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
*question Issue represents a question, should be posted to StackOverflow (VS Code) typescript Typescript support issues
Projects
None yet
Development

No branches or pull requests

2 participants