I've set up a Typescript project using Lerna and yarn workspace
. This project includes an AWS API Gateway API that is backed by several Lambda functions that use Middy for things like CORS and handling timeouts. I prefer to have unit tests for my code, so I wrote tests for these Lambda functions using Jest, and I run them using ts-jest
. However, when I run the unit tests (the only one I've written so far) for one of my Lambdas, I get the error below.
I've tried adding "esmoduleInterop": true
(as suggested here) to the compilerOptions
section of tsconfig.json
in the package directory. I tried adding it to the root tsconfig.json
. I tried a custom tsconfig.json
for Jest and adding it there. Nothing. What am I doing wrong?
Error:
FAIL packages/announcer/test/api/list-non-compliant-rules.test.ts
● Test suite failed to run
TypeError: core_1.default is not a function
97 | }
98 |
> 99 | export const handler = middy(main, {
| ^
100 | timeoutEarlyInMillis: TIMEOUT,
101 | timeoutEarlyResponse: () => {
102 | Log.error(`Timeout of ${TIMEOUT}ms exceeded`)
at Object.<anonymous> (lambda/api/list-non-compliant-rules.ts:99:29)
at Object.<anonymous> (test/api/list-non-compliant-rules.test.ts:4:36)
I'm importing Middy like so:
import middy from "@middy/core"
And I'm using it like so:
export const handler = middy(main, {
timeoutEarlyInMillis: TIMEOUT,
timeoutEarlyResponse: () => {
Log.error(`Timeout of ${TIMEOUT}ms exceeded`)
return internalServerError(new Error("Timeout"))
},
})
.use(cors())
.use(httpErrorHandler())
.use(
httpContentNegotiation({
availableLanguages: ["en-US"],
availableMediaTypes: ["application/vnd.api+json"],
})
)
Package tsconfig.json
:
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": ["node", "jest"],
"esModuleInterop": true,
},
"exclude": ["node_modules", "cdk.out"]
}
Root tsconfig.json
:
{
"compilerOptions": {
"target": "ES2018",
"module": "commonjs",
"lib": ["es2018"],
"declaration": true,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": false,
"inlineSourceMap": true,
"inlineSources": true,
"experimentalDecorators": true,
"strictPropertyInitialization": false,
"typeRoots": ["./node_modules/@types"],
"esModuleInterop": true
},
}