-
-
Notifications
You must be signed in to change notification settings - Fork 524
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 not configured to match tsc options #59
Comments
Hi @kmturley, thanks for asking. I believe these are outside the scope of this base template, as these are specific problems with jest and ts-jest. These are not “issues” with the template repository itself. As you noticed yourself, simply compiling the modified As for the top-level await, this is one of many known challenges when using ts-jest, jest, ESM and TypeScript. People are using different combinations of settings to work around the challenges, with mixed success. If you really need this behavior, please read this and this. One, example, way to work around this, after reading and understanding the side effects of changed options, is to:
Here's the diff: diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts
index 9aaca6e..5d505a0 100644
--- a/__tests__/main.test.ts
+++ b/__tests__/main.test.ts
@@ -40,3 +40,5 @@ describe('greeter function', () => {
expect(hello).toBe(`Hello, ${name}`);
});
});
+
+console.log(await greeter('Bob'));
\ No newline at end of file
diff --git a/jest.config.js b/jest.config.js
index 10696f4..b290a41 100644
--- a/jest.config.js
+++ b/jest.config.js
@@ -9,6 +9,9 @@ export default {
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(m)?ts$',
coverageDirectory: 'coverage',
+ setupFiles: [
+ "<rootDir>/test-setup.js"
+ ],
collectCoverageFrom: [
'src/**/*.ts',
'src/**/*.mts',
diff --git a/package.json b/package.json
index e310426..c590855 100644
--- a/package.json
+++ b/package.json
@@ -29,7 +29,7 @@
"build:watch": "tsc -w -p tsconfig.json",
"build:release": "npm run clean && tsc -p tsconfig.release.json",
"lint": "eslint . --ext .ts --ext .mts",
- "test": "jest --coverage",
+ "test": "node --no-warnings --experimental-vm-modules node_modules/jest/bin/jest.js --coverage",
"prettier": "prettier --config .prettierrc --write .",
"test:watch": "jest --watch"
},
diff --git a/tsconfig.json b/tsconfig.json
index 4a94f7f..9b122df 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -17,7 +17,8 @@
"noUnusedParameters": true,
"noImplicitAny": false,
"noImplicitThis": false,
- "strictNullChecks": false
+ "strictNullChecks": false,
+ "allowJs": true
},
"include": ["src/**/*", "__tests__/**/*"]
} and it will output ╰─ ➜ npm run test
> node-typescript-boilerplate@0.0.0 test
> node --no-warnings --experimental-vm-modules node_modules/jest/bin/jest.js --coverage
console.log
Hello, Bob
at __tests__/main.test.ts:44:9
PASS __tests__/main.test.ts (5.987 s)
... Lastly, because of such challenges with jest, and the overall poor DX around ESM and TypeScript, I'm going to switch this repo to vitest. Please see #56 for more details and further explanation. Your code should work without any additional modifications with vitest. |
Thanks for the quick response and resolution. |
vitest looks great. I was interested to see what would happen if I follow the latest guides for each tool:
This was the result: |
Some code works fine via
npm run build && npm start
but not bynpm test
.My assumption is that
ts-jest
is not configured to work in the same way astsconfig
.How to replicate
1) No top-level 'await' expressions
Add to bottom of main.ts
Then run:
Notice how
start
works fine, buttest
outputs this error:1) No native node.js modules
Add these lines to the bottom of main.ts
Then run:
Notice how
start
works fine, buttest
outputs this error:The text was updated successfully, but these errors were encountered: