Skip to content

Commit

Permalink
Merge pull request #1140 from anthony-nhs/typescript_documentation_up…
Browse files Browse the repository at this point in the history
…date

update typescript and testing documentation
  • Loading branch information
willfarrell authored Nov 20, 2023
2 parents 6d17e15 + 8f94272 commit d80216c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
37 changes: 37 additions & 0 deletions website/docs/intro/06-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,40 @@ An example of a message generated by Jest unit tests and which signals the need
```
A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks. Active timers can also cause this, ensure that .unref() was called on them.
```

## jest and typescript
If you use middy v5+, jest and typescript, and use ts-jest as a transformer, then you need to ensure that middy modules are not transformed. Use this in your jest.config.ts file
```
const esModules = ["@middy"].join("|")
const jestConfig: JestConfigWithTsJest = {
...
transform: {
"^.+\\.ts?$": [
"ts-jest",
{
useESM: true
}
]
},
transformIgnorePatterns: [`node_modules/(?!${esModules})`],
...
}
export default jestConfig
```
You must also use the flag `--experimental-vm-modules` when running jest - eg have this in your package.json file
```
{
...
"scripts": {
...
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
...
},
...
}
```

See https://kulshekhar.github.io/ts-jest/docs/guides/esm-support/ and https://jestjs.io/docs/ecmascript-modules for more details
28 changes: 28 additions & 0 deletions website/docs/intro/06-typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,31 @@ Note that when using TypeScript, you should use what we call the _Middleware-fir
This approach makes sure that, as you attach middlewares, the type system understands how the `event` and the `context` arguments are augmented by the various middlewares and inside your handler code you can have a nice type-checking and auto-completion experience.

You can also [write custom middlewares with TypeScript](/docs/writing-middlewares/intro).

This is an example tsconfig.json file that can be used for typescript projects

```
{
"compilerOptions": {
"incremental": true,
"target": "es2020",
"module": "es2020",
"declaration": true,
"sourceMap": true,
"composite": true,
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"preserveConstEnums": true,
"resolveJsonModule": true,
"allowJs": true,
"rootDir": ".",
"outDir": "lib"
}
"include": ["src/**/*", "tests/**/*"],
"exclude": ["node_modules"]
}
```

0 comments on commit d80216c

Please sign in to comment.