Open MichalLytek opened 5 years ago
Hi, I see you are using path mapping in tsconfig but I couldn't find your jest config moduleNameMapper accordingly. That will usually throw the error "cannot find module". Would you please add moduleNameMapper for your jest config and check if still seeing the same error.
Regarding to collect coverage, i'm not sure what caused the issue.
I use custom compiler with a plugin that rewrites the paths. And it works, test are executed correctly, no "cannot find module" or type errors. They fail only on collecting coverage, like the compiler is not used on coverage things.
Hmm, so to break down your described problem, I see 2 things:
The first issue could be that jest doesn't pick up the correct config. This one I think it's more related to jest itself. Ts-jest doesn't interfere with how jest picks up config
The 2nd issue is definitely related to ts and custom compiler. Based on your above comment that it failed at generating coverage report ? (Sorry I haven't checked your repo yet because my internet is down)
This one I think it's more related to jest itself.
Yes, I will report that to the Jest repo. I mentioned it only as a background of what I was doing and trying.
Based on your above comment that it failed at generating coverage report?
Yes, the tests step is executed without any problem but then it throws TS types error on collecting coverage step:
[...]\typegraphql-next\packages\core> npx jest --verbose --runInBand --coverage
PASS tests/functional/foo.ts
foo
β should return 'foo' (3ms)
PASS tests/functional/bar.ts
bar
β should return 'bar' (1ms)
Running coverage on untested files...Failed to collect coverage from [...]\typegraphql-next\packages\core\src\index.ts
ERROR: TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
src/index.ts:1:17 - error TS2307: Cannot find module '@src/foo'.
1 import foo from "@src/foo";
~~~~~~~~~~
src/index.ts:2:17 - error TS2307: Cannot find module '@src/bar'.
2 import bar from "@src/bar";
~~~~~~~~~~
src/index.ts:3:66 - error TS2307: Cannot find module '@src/utils'.
3 import { ensureInstalledCorrectTypeGraphQLPackagesVersion } from "@src/utils";
~~~~~~~~~~~~
STACK:
Failed to collect coverage from [...]\typegraphql-next\packages\core\src\utils\index.ts
ERROR: TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
src/utils/index.ts:3:8 - error TS2307: Cannot find module '@src/utils/check-deps-version'.
3 } from "@src/utils/check-deps-version";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Hi, I do have a few difficulties when cloning your repo and run your scenarios. Do you have some clear steps after cloning/downloading your repo to produce your scenarios ?
@ahnpnl
It should be just npm install
in root folder and then npm run bootstrap
after cloning to install all deps for the packages.
Let me know if that doesn't work for you π
hi, I managed to make your repo work by doing these:
then I could run the tests. I have a question regarding to your jest config. From what I saw from your repo, there are 2 files jest.base.js
and jest.config.js
. How are these 2 files relate to each other ? When I put console log in ts-jest
codes, your repo runs tests with this jest
config which is collected by jest
itself and pass to ts-jest
process
automock: false,
browser: false,
cache: true,
cacheDirectory: '/private/var/folders/cf/mkbmyv653tb__lcrmgd2zp700000gn/T/jest_dx',
clearMocks: false,
coveragePathIgnorePatterns: [ '/node_modules/' ],
cwd: '/Users/ahn/issues-ts-jest/issue-1227',
detectLeaks: false,
detectOpenHandles: false,
displayName: { name: 'CLASS-VALIDATOR', color: 'white' },
errorOnDeprecated: false,
extraGlobals: [],
forceCoverageMatch: [],
globals: {
'ts-jest': {
tsConfig: '<rootDir>/tests/tsconfig.json',
compiler: 'ttypescript'
}
},
haste: {
computeSha1: false,
providesModuleNodeModules: [],
throwOnModuleCollision: false
},
moduleDirectories: [ 'node_modules' ],
moduleFileExtensions: [ 'js', 'json', 'jsx', 'ts', 'tsx', 'node' ],
moduleNameMapper: [],
modulePathIgnorePatterns: [],
name: 'a96a8a5c596e3eb8f5289a45b2975243',
prettierPath: 'prettier',
resetMocks: false,
resetModules: false,
restoreMocks: false,
rootDir: '/Users/ahn/issues-ts-jest/issue-1227/packages/class-validator',
roots: [ '/Users/ahn/issues-ts-jest/issue-1227/packages/class-validator' ],
runner: 'jest-runner',
setupFiles: [],
setupFilesAfterEnv: [
'/Users/ahn/issues-ts-jest/issue-1227/node_modules/jest-extended/dist/index.js'
],
skipFilter: false,
snapshotSerializers: [],
testEnvironment: '/Users/ahn/issues-ts-jest/issue-1227/node_modules/jest-environment-node/build/index.js',
testEnvironmentOptions: {},
testLocationInResults: false,
testMatch: [
'/Users/ahn/issues-ts-jest/issue-1227/packages/class-validator/tests/functional/**/*.ts',
'/Users/ahn/issues-ts-jest/issue-1227/packages/class-validator/tests/unit/**/*.ts'
],
testPathIgnorePatterns: [ '/node_modules/' ],
testRegex: [],
testRunner: '/Users/ahn/issues-ts-jest/issue-1227/node_modules/jest-jasmine2/build/index.js',
testURL: 'http://localhost',
timers: 'real',
transform: [
[
'^.+\\.tsx?$',
'/Users/ahn/issues-ts-jest/issue-1227/node_modules/ts-jest/dist/index.js',
{}
]
],
transformIgnorePatterns: [ '/node_modules/' ],
watchPathIgnorePatterns: []
by looking at this jest
config, the option collectCoverageFrom
doesn't exist so that's why you saw the coverage report isn't like your expectation.
ts-jest
doesn't take care of collect base jest
config but only use base jest
config to enrich global
property. Since the base jest
config doesn't contain collectCoverageFrom
so it's not ts-jest
issue.
@ahnpnl
Since the base jest config doesn't contain collectCoverageFrom so it's not ts-jest issue.
How not calling a custom compiler (thus not applying custom paths transformer) is not a ts-jest issue?
Running coverage on untested files...Failed to collect coverage from [...]\typegraphql-next\packages\core\src\index.ts
ERROR: TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
src/index.ts:1:17 - error TS2307: Cannot find module '@src/foo'.
1 import foo from "@src/foo";
~~~~~~~~~~
src/index.ts:2:17 - error TS2307: Cannot find module '@src/bar'.
The issue is about that "when I run this command in selected dir, the coverage from is ok but ts-jest brokes compilation on applying transformers.".
So on root dir it works but collects coverage from lib
(other story) but it doesn't work when I just want to test single project.
Detailed steps to reproduce:
STACK:
Failed to collect coverage from F:\#Projekty\#Github\typegraphql-next\packages\core\src\index.ts
ERROR: TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
src/index.ts:1:32 - error TS2307: Cannot find module '@src/foo'.
1 export { default as foo } from "@src/foo";
hi, I only looked at the 1st issue regarding to
The problem is that when I run the jest --coverage on root dir, it collects the coverage ignoring collectCoverageFrom settings
I haven't looked into the 2nd issue yet related to custom compiler. I hope the debugging information above solved your 1st question. Will keep you updated related to 2nd issue.
when I run this command in selected dir, the coverage from is ok but ts-jest brokes compilation on applying transformers.
Updated: I think I saw the issue. The compileOptions only contains the paths of current level tsconfig.json
.
"paths": {
"@typegraphql/core": ["../lib"],
"@tests/*": ["./*"]
},
Since test files are in tests
folder and tests
folder has a tsconfig.json
, ts-jest
only takes into account of this tsconfig.json
+ tsconfig.settings.json
and it doesn't know about tsconfig.json
on core
folder level. I tried to make tests/tsconfig.json
to extend core/tsconfig.json
but I still couldn't get the correct config (typescript seems to ignore extends
option in tests/tsconfig.json
).
A workaround for now is declaring path mapping in tests/tsconfig.json
, for example
{
"extends": "../../../tsconfig.settings.json",
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@typegraphql/core": ["../lib"],
"@tests/*": ["./*"],
"@src/*": ["../src/*"] <---- this is added extra
},
"plugins": [
{
"transform": "@zerollup/ts-transform-paths",
"exclude": ["*"]
}
]
}
}
Another suggestion is: it's hard to debug when test files' extension is the same as normal files' extension even though there is a folder called tests
, imo it's better to follow test file extension, either .spec.ts
or .test.ts
.
hi @MichalLytek , for some reasons I can't reproduce the error anymore related to
when I run this command in selected dir, the coverage from is ok but ts-jest brokes compilation on applying transformers.
on your master branch. Did you change something to make it work ?
@ahnpnl
I have unpublished that vNext
branch but I still can reproduce the issue there with the newest deps.
I gonna try to create a bare repository with my TS transformers setup so you will be able to checkout and reproduce that issue π
thanks, that would be much better with a small repo π
@MichalLytek - I came across this issue b/c I'm experiencing a similar issue. All of the suggestions listed here were already in place, but I was still seeing errors when collecting coverage data, like:
STACK:
Failed to collect coverage from /<snip>/libs/data-access/src/lib/change-store/subscription-db-store-map.ts
ERROR: libs/data-access/src/lib/change-store/subscription-db-store-map.ts:2:30 - error TS2307: Cannot find module '@mycompany/util'.
The tests pass fine, but I ended up with these errors while collecting coverage. After trying a bunch of things, and reviewing the docs, I found that switching to using the v8
coverage provider makes the issue go away.
Docs here: https://jestjs.io/docs/en/configuration#coverageprovider-string
Change jest.config.js to:
testEnvironment: "jest-environment-jsdom-sixteen",
coverageProvider: 'v8',
Then add jest-environment-jsdom-sixteen to your project.
Hope that helps...
@MichalLytek @johncrim Is it easy to reproduce this issue with a very simple project like this repo https://github.com/ahnpnl/ts-jest-only-example ?
@johncrim If I set up coverageProvider: 'v8'
, I don't get the errors but also I have red 0 coverage π
I guess it's because trying to map src files which fails when using path mapping and linking to builded js code.
As the Jest team has fixed the coverage for projects
setups, I can get coverage from the whole project, so for now I don't have time to make a separate reproduction repo for the issue "run jest --coverage in selected dir and run into issue with cannot find module." π
Issue :
I have a configured lerna monorepo that uses jest
projects
config to run all the tests at once and a customcompiler: "ttypescript",
setting to apply some transformers for the paths mapping: https://github.com/MichalLytek/type-graphql/tree/vNextThe problem is that when I run the
jest --coverage
on root dir, it collects the coverage ignoringcollectCoverageFrom
settings. And when I run this command in selected dir, the coverage from is ok but ts-jest brokes compilation on applying transformers.Normal tests
jest --verbose
works ok without any problem, both for folder-scoper and on the root folder with projects.I've tried with Node 10, Node 12, TS 3.6.3, TS 3.7.1-rc - nothing works.
Expected behavior :
It should properly run custom compiler on collecting coverage.
Also it should collect coverage only from all projects
collectCoverageFrom
settings.Debug log:
Minimal repo :
https://github.com/MichalLytek/type-graphql/tree/vNext