Skip to content

daggerok/jest-typescrip-tests

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jest typescript Build Status

Testing TypeScript code using jest...

jest es++ typescript..

mkdir app && cd $_
npm init -y
npm i -ED jest @jest/core @jest/globals @jest/types @types/node @babel/preset-env core-js @babel/preset-typescript typescript ts-jest @types/jest

package.json

{
  "scripts": {
    "dev": "npm run test -- --watchAll",
    "test": "jest --preset=ts-jest --testEnvironment=node ...",
    "test-js-with-ts": "jest --preset=ts-jest/presets/js-with-ts ...",
    "test-js-with-babel": "jest --preset=jest --preset=ts-jest/presets/js-with-babel ..."
  },
  "babel": {
    "presets": [
      [
        "@babel/preset-env",
        {
          "targets": {
            "node": "current"
          }
        }
      ],
      "@babel/preset-typescript"
    ]
  }
}

jest reporters

npm i -ED @jest/reporters jest-junit 

package.json

{
  "scripts": {
    "test": "jest --reporters=default --reporters=jest-junit --collect-coverage ..."
  }
}

testing

add src/dynamic-module.ts file:

export const sum = (...args): number =>
  args.reduce((prev, curr) => prev + curr, 0);

add test/dynamic-import.test.ts file:

import { expect, it } from '@jest/globals';

it('should import dynamic lazy module', async () => {
  const dynamicModule: any = await import('../src/dynamic-module');
  const total: any = await dynamicModule.sum(1, 2, 3);
  await expect(total).toBe(6);
});

finally, execute

npm t
npm run test-js-with-ts
npm run test-js-with-babel

or simply use quick one liner for github repo starter:

git clone --depth=1 --no-single-branch https://github.com/daggerok/jest-typescrip-tests app && cd $_ && npm i && npm t

resources