yarn add ts-jest jest @types/jest --dev
yarn remove jasmine jasmine-spec-reporter jasmine-ts ts-node proxyquire @types/jasmine @types/proxyquire
yarn ts-jest config:init
- Modify
jest.config.js
if needed.- Add
modulePathIgnorePatterns: ['.yarn-cache']
if there is a.yarn-cache
folder; otherwise you'll get a lot ofjest-haste-map
errors when running tests.
- Add
- Update the npm scripts. Give it the folder of tests you want to run (Jest matches against the pattern). Use
--verbose
to see the breakdown.- e.g.
NODE_ENV=test jest test/unit --verbose
- e.g.
NODE_ENV=test jest test/unit --verbose --watch
- e.g.
- (Experimental -- I haven't had much success with this) Try the migration tool:
npx jest-codemods
. (Runnpm i -g npx
if you don't have npx installed.) - Fix up tests
- (probable) Update mocks to use Jest mocks.
- Import what you're mocking and use
jest.mock()
on that same module. Use ts-jest'smocked()
on the object to keep TSC from complaining. - See node-template's https://github.com/Banno/node-template/blob/master/test/integration/configure-logger.spec.ts for an example.
- Import what you're mocking and use
- (optional) Replace
jasmine.*
references likejasmine.createSpy
andjasmine.any
to conform to the Jest API.
- (probable) Update mocks to use Jest mocks.
- One downside: We can't use
fdescribe
/fit
to focus tests when running against multiple tests -- Jest runs each file separately. Modify the Jest CLI call instead (e.g.jest test/unit/example.spec.ts
). - To see the compiled JS for the tests, run
jest --debug
to find the cache dir, then look in that (inside thets-jest
folder). - If you get
A "describe" callback must not return a value
and there are only statements inside adescribe
, check that an error isn't being thrown in there. In general, it's safest to put all that code inbeforeAll
/beforeEach
blocks.