Coder Social home page Coder Social logo

jest-runner-tsc's Introduction

jest-runner-tsc

Travis Prettier npm semantic-release License

A Jest runner for the TypeScript compiler

install

npm install --save-dev jest-runner-tsc

usage

Jest configuration:

jest.tsc.config.js:

module.exports = {
  runner: 'jest-runner-tsc',
  displayName: 'tsc',
  moduleFileExtensions: ['js','ts', 'tsx'],
  testMatch: ['<rootDir>/**/*.ts'],
};

options

This project uses cosmiconfig, so you can provide config via:

  • a jest-runner-tsc property in your package.json
  • a jest-runner-tsc.config.js JS file
  • a .jest-runner-tscrc JSON file

Example in package.json

{
  "jest-runner-tsc": {
    "tsconfigPath": "./tsconfig.types.json"
  }
}

tsconfigPath

Default: ./tsconfig.json

A relative path to your tsconfig.json file.

run

jest -c jest.tsc.config.js

jest-runner-tsc's People

Contributors

azz avatar favna avatar kwelch avatar simenb avatar tom-sherman avatar wzoom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

jest-runner-tsc's Issues

How to support JSX?

package.json

{
 "scripts": {
    "test": "jest ./tests",
    "test:ts": "jest -c jest.tsc.config.js"
  },
 "jest-runner-tsc": {
    "tsconfigPath": "./tsconfig.json"
  }
}

jest: 27.4.7
jest-runner-tsc: 1.6.0

jest.tsc.config.js

module.exports = {
  runner: 'jest-runner-tsc',
  displayName: 'tsc',
  moduleFileExtensions: ['js', 'jsx','ts', 'tsx'],
  testMatch: ['<rootDir>/tests/*.tsx'],
};

tsconfig.json

{
  "compileOnSave": false,
  "module": "ESNext",
  "target": "ESNext",
  "compilerOptions": {
    "strict": true,
    "noEmit": true,
    "declaration": true,
      "moduleResolution": "node",
      "esModuleInterop": true
    },
  "include": ["package/*.ts", "test/*.tsx"]
}

Running yarn test is working fine!!!

However running yarn test:ts an error is fired: Cannot use JSX unless the '--jsx' flag is provided.. When do I need to add this --jsx flag?

image

Thanks

Allow passing different tsconfig file name

Currently it is hardcoded and joined.

If it's not we will be able to configure it through the jest config.

Example. I'm using Babel + TypeScript and I have

.tsconfig.typecheck.json
.tsconfig.gentypes.json

So I'll be able to define path to the .tsconfig.typecheck.json.

lib.d.ts not detected?

Project ok:

❯ npx tsc --noemit
❯ cat tsconfig.json
{
  "compilerOptions": {
    "strict": true,
    "noImplicitReturns": true,
    "esModuleInterop": true,
    "module": "commonjs",
    "target": "es2017",
    "sourceMap": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "lib": ["esnext"]
  },
  "include": ["src/**/*.ts", "lib.d.ts"]
}
❯ cat lib.d.ts
declare module 'serverless-http'

But not when checked from within jest runner:

❯ cat jest.config.js
module.exports = {
  projects: [
    {
      transform: {
        '^.+\\.ts$': 'ts-jest',
      },
      testRegex: '.*spec.ts$',
      moduleFileExtensions: ['ts', 'js'],
      collectCoverageFrom: ['**/src/**/*.ts', '!**/build/**/*'],
    },
    {
      runner: 'jest-runner-tsc',
      displayName: 'tsc',
      moduleFileExtensions: ['ts', 'tsx'],
      testMatch: ['<rootDir>/**/*.ts'],
    },
  ],
}
❯ npm test

> [email protected] test /Users/jasonkuhrt/projects/dialogue/orwell
> jest src plugins

 PASS   tsc  src/poetry.ts
 PASS   tsc  plugins/scribe.ts
 PASS   tsc  plugins/intercom.ts
 PASS   tsc  plugins/hollywood.ts (6.419s)
 FAIL   tsc  src/server.ts (8.77s)
Could not find a declaration file for module 'serverless-http'. '/Users/jasonkuhrt/projects/dialogue/orwell/node_modules/serverless-http/serverless-http.js' implicitly has an 'any' type.
  Try `npm install @types/serverless-http` if it exists or add a new declaration (.d.ts) file containing `declare module 'serverless-http';`
  3 | import express from 'express'
  4 | import QS from 'querystring'
> 5 | import serverless from 'serverless-http'
    |                        ^^^^^^^^^^^^^^^^^
  6 | import * as Plug from '../plugins'
  7 | import * as Config from './config'
  8 | import * as H from './helpers'
 PASS   tsc  src/config.ts (5.957s)
 PASS   tsc  src/helpers.spec.ts
 PASS   tsc  plugins/datadog.ts
 PASS   tsc  src/helpers.ts
 PASS   tsc  plugins/waldo.ts
 PASS   tsc  plugins/toktok.ts (5.651s)
 PASS   tsc  plugins/core.ts
 PASS   tsc  plugins/index.ts
 PASS  src/helpers.spec.ts

Test Suites: 1 failed, 13 passed, 14 total
Tests:       1 failed, 13 passed, 14 total
Snapshots:   0 total
Time:        19.859s
Ran all test suites matching /src|plugins/i in 2 projects.
npm ERR! Test failed.  See above for more details.

Doesn't respect tsconfig.json

I thought the TS Compiler API would do it by default, but looks like I need to make changes to support tsconfig options.

TypeError: Cannot read property 'fileName' of undefined

I attempted to use jest-runner-tsc for the first time, and am getting the exception when trying to run the test:

  ● Test suite failed to run

    TypeError: Cannot read property 'fileName' of undefined

      at node_modules/jest-runner-tsc/dist/runTsc.js:79:28
          at Array.filter (<anonymous>)
      at runTsc (node_modules/jest-runner-tsc/dist/runTsc.js:77:106)

The error happens here:

const allDiagnostics = ts
.getPreEmitDiagnostics(program)
.concat(emitResult.diagnostics)
.filter(diagnostic => diagnostic.file.fileName === testPath);

and the diagnostic at this point is:

{
  file: undefined,
  start: undefined,
  length: undefined,
  messageText:
   'Option \'--incremental\' can only be specified using tsconfig, emitting to single file or when option `--tsBuildInfoFile` is specified.',
  category: 1,
  code: 5074,
  reportsUnnecessary: undefined,
  reportsDeprecated: undefined
}

Versions:
node.js v10.18.1
jest v.26.4.2
typescript v4.0.2

LMK please if I should dig any deeper.

Config in readme is invalid

Using the config in the readme:

module.exports = {
  runner: 'jest-runner-tsc',
  displayName: 'tsc',
  moduleFileExtensions: ['ts', 'tsx'],
  testMatch: ['<rootDir>/**/*.ts'],
};

Fails when running jest with

Validation Error:

  moduleFileExtensions must include 'js':
  but instead received:
    ["ts","tsx"]
  Please change your configuration to include 'js'.

  Configuration Documentation:
  https://jestjs.io/docs/configuration.html

To fix just add js to moduleFileExtensions.

slow?

❯ npm test

> [email protected] test /Users/jasonkuhrt/projects/dialogue/orwell
> jest src plugins

 PASS   tsc  src/server.ts (9.413s)
 PASS   tsc  plugins/hollywood.ts (9.696s)
 PASS   tsc  plugins/index.ts (10.104s)
 PASS   tsc  src/config.ts (7.157s)
 PASS   tsc  plugins/waldo.ts (7.242s)
 PASS   tsc  plugins/toktok.ts (7.062s)
 PASS   tsc  src/helpers.spec.ts
 PASS   tsc  plugins/intercom.ts
 PASS   tsc  plugins/scribe.ts
 PASS   tsc  src/poetry.ts
 PASS   tsc  src/helpers.ts
 PASS   tsc  plugins/core.ts
 PASS   tsc  plugins/datadog.ts
 PASS  src/helpers.spec.ts

Test Suites: 14 passed, 14 total
Tests:       14 passed, 14 total
Snapshots:   0 total
Time:        23.425s
Ran all test suites matching /src|plugins/i in 2 projects.

25s
❯ time npx tsc --noemit
npx tsc --noemit  7.43s user 0.37s system 136% cpu 5.726 total

What is the benefit to use jest-runner-tsc?

I convert test files from flow javascript to typescript. I saw that in the application, I use jest-runner, but in the documentation of jest, there is also jest-runner-tsc. I try to understand the difference between them and what kind of benefit I will get if I use jest-runner-tsc? Additionally, I converted some test files to typescript and it works fine with jest-runner.

How can I help to get this faster?

I'm really interested in seeing the speed of this runner improve - would you be willing to share with me what you have tried in the past? Is there an interest in improving this?

From what I understand a new compiler is being created per test file right now, so probably the best improvement would be trying to reuse a single compiler instance and using TS's incremental API - I think is the strategy used by Fork TS Checker Webpack Plugin which I found to be pretty fast. Not 100% sure how this would play out with jest worker farm and how this is even utilized here, but this is something that could be explored.

Errors are showing multiple times if the erroneous module is a dependency of another module

Steps to reproduce:

  1. Create a project with the following structure
.
└── lib
    β”œβ”€β”€ bar.ts
    └── foo.ts

  1. Add code to foo.ts which contains a tsc error
export const hello = 'world'
hello * 5 // The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
  1. Import foo.js from bar.js
import { hello } from './foo'
console.log(hello)

The typescript error will be reported once from the file it occured (foo.js) and once for each file it is imported into (bar.js in this case).

tests are not run properly

When this runner is used tests are not executed as they should πŸ‘€

configs:

with transform: ts-jest

const { defaults } = require('jest-config')

const config = {
  transform: {
  '^.+\\.(ts|tsx)$': 'ts-jest'
 },
  testMatch: [
    ...defaults.testMatch,
    '**/__tests__/**/*.ts?(x)',
    '**/?(*.)+(spec|test).ts?(x)'
  ],
  moduleFileExtensions: [...defaults.moduleFileExtensions, 'ts', 'tsx'],
  globals: {
    'ts-jest': {
   skipBabel: true
}
  }
}

module.exports = config

with runner: 'jest-runner-tsc':

const { defaults } = require('jest-config')

const config = {
  runner: 'jest-runner-tsc',
  testMatch: [
    ...defaults.testMatch,
    '**/__tests__/**/*.ts?(x)',
    '**/?(*.)+(spec|test).ts?(x)'
  ],
  moduleFileExtensions: [...defaults.moduleFileExtensions, 'ts', 'tsx']
}

module.exports = config

test suite run with ts-jest as transform vs just jest-runner-tsc as runner:
image

What's the point?

This runner uses transpileModule function from Typescript compiler API which does not type check. If so, what's the point of jest-runner-tsc?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.