DEV Community

Discussion on: Mocha/Chai with TypeScript

Collapse
 
wesleymconner profile image
WesMC

Thanks @matteobruni quite helpful.

I was able to test APIs with minor adjustments.

For context: My source is in src/ts including tests under src/ts/test. My compiles produce one js (under src/js-tmp) per source ts (under src/ts). My dists cherry-pick from js-tmp (i.e., exclude tests, ...).

=== test-whatever.ts ===
import chai, { expect } from 'chai'
import chaiHttp from 'chai-http'
chai.use(chaiHttp);
import mocha from 'mocha'
const { describe, before, beforeEach, it, after } = mocha
  :
import { ..stuff to test.. } from '../index.js'


=== package.json ===
{ ..
  "type": "module",
  "source": "src/js-tmp/index.js",
  "main": "src/js-tmp/index.js",
..}

=== tsconfigBase.json ===
{ ..
  "esModuleInterop": true,
  "module": "es2022",
  "moduleResolution": "node",
  "target": "es2022"
.. }

=== tsconfig.json ===
{
  "extends": "../../tsconfigBase.json",
  "compilerOptions": {
    "baseUrl": ".",
    "rootDir": "src/ts",
    "outDir": "src/js-tmp"
  },
  "include": [ "src/ts/**/*.ts" ]
}
Enter fullscreen mode Exit fullscreen mode


`