|
Sharing some packages, tsconfig.json, and jest.config.ts I used in a recent project that I'm pretty happy with below (I'm not using `"type": "module"` in my package.json. Relevant npm packages:
- @jest/globals (I use these to import `describe`, `test`, `expect` and other test-related functions)
- ts-jest My tsconfig.json: {
"buildOptions": {},
"compilerOptions": {
"target": "ES6",
"module": "ESNext",
"lib": [
"ES6",
"DOM",
"ES2017",
"DOM.Iterable"
],
"moduleResolution": "Bundler",
"jsx": "react-jsx",
"declaration": true,
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./dist"
},
"include": [
"src",
"__tests__",
"*.config.ts",
"sandbox.ts",
"sandbox-ui.js"
]
}
and my jest config (jest.config.ts): import type { Config } from "jest";
export default {
preset: "ts-jest",
testEnvironment: "jsdom",
testMatch: ["\*/__tests__/\*/*.test.ts"],
globals: {
fetch: global.fetch,
},
} as Config;
I have my tests set up under the `__tests__` directory in project root as you noted, and use a `.test.ts` suffix on all relevant test files. Doing this, ts-jest handles the actual transpilation + execution of the tests (just by running jest), and you don't have to worry about including them in your built solution. I have a separate `tsconfig.build.json` for actually building my project (this... is probably inefficient... but it works well for me :P). |