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
Getting Error Type 'typeof import("/home/quophyie/projects/helmet-issue/node_modules/helmet/index")' has no call signatures when running tests with jest, ts-jest when using ESM / ECMAScript Modules #441
Comments
Strange...does this happen with any other modules, or just Helmet? Is ts-jest using the same TypeScript configuration as ts-node?
|
Hi @EvanHahn This only happens with helmet module only. Other modules are OK jest, ts-jest and ts-node all use the same typescript config . here is the jest.config.ts with the embedded ts-jest config
import type { JestConfigWithTsJest } from 'ts-jest';
const jestConfig: JestConfigWithTsJest = {
testFailureExitCode: 1,
moduleFileExtensions: ['ts', 'js', 'json'],
extensionsToTreatAsEsm: ['.ts'],
preset: 'ts-jest/presets/default-esm',
transform: {
'^.+\\.(ts|tsx)$': [
'ts-jest',
{
useESM: true,
tsconfig: 'tsconfig.json',
},
],
},
testMatch: [
'**/*.test.(ts|js)',
'**/*.spec.(ts|js)'
],
testPathIgnorePatterns: [
'<rootDir>/build',
'<rootDir>/node_modules/'
],
testEnvironment: 'node',
};
export default jestConfig; ts-node also uses the exact same tsconfig.json Here is the ts-node start script in the package.json "start": "ts-node --esm --project tsconfig.json --transpile-only main.ts" |
Everything looks okay at a glance, but ESM + TypeScript + Jest often causes problems. What's the full error you're seeing? What happens if you add |
@EvanHahn
|
Hi @EvanHahn import {jest} from '@jest/globals'
import helmet from 'helmet'
//@ts-ignore
helmet();
describe('Helmet Issue 441', () => {
it('fails on call to helmut() function', () => {
expect(true).toBe(true);
});
}); Thanks for the help Would you know why the |
It seems like ts-jest isn't pulling in the right type declarations for some reason. I don't know why.
Is there a way to see what type declaration file ts-jest is using? Maybe some verbose logging mode or something? |
I have the same problem. The type correctly points towards |
Awhile ago, I chatted with a TypeScript team member who endorsed the way Helmet exports its types. But I concede that it's complicated and it's possible I made a mistake somewhere. Is it possible that ts-jest (or some sub-system) is getting confused, trying to treat an ES module as a CommonJS one? Or something like that? |
when you use Any other resolution method will fail. I see this issue happening in the validator.js codebase too. I have to add in another default import. When I change the
I'm just using a vanilla express js setup with the This a turbo monorepo using pnpm but the TS-Config is working fine with other packages. doing the: |
To work with Node, I expect that |
If I downgrade to 6.1.4, the error is gone. |
@tbn-mm Could you create a sample project that reproduces your issue? |
Hi
I am getting the error below when I run tests with jest, ts-jest where a module uses helmet
I am using ESM (ECMAScript modules) and node version
19.9.0
and helmetv7.0.0
The weird thing is that, the app works fine when I just run it with
ts-node
Here is my setup (The github repo is helmut-issue-441 )
node-version
v19.9.0v
package.json
tsconfig.json
main.test.ts
main.ts
The text was updated successfully, but these errors were encountered: