Giter Site home page Giter Site logo

ikatyang / dts-jest Goto Github PK

View Code? Open in Web Editor NEW
120.0 120.0 4.0 1.06 MB

A preprocessor for Jest to snapshot test TypeScript declaration (.d.ts) files

License: MIT License

TypeScript 99.57% JavaScript 0.43%
jest jest-transform test typescript typescript-definitions

dts-jest's People

Contributors

dependabot[bot] avatar greenkeeper[bot] avatar ikatyang avatar karlhorky avatar mattleff avatar piotrwitek avatar renovate-bot avatar renovate[bot] 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

dts-jest's Issues

Show detailed test title

Currently

✓ Math.max(1)
  A
    ✓ Math.max(2)
    ✓ Math.max('123')
  B
    ✓ Math.max(4)

Proposed

✓ Math.max(1)
  ✓ should match snapshoted type
  ✓ should pass typescript check
  A
    ✓ Math.max(2)
      ✓ should equal to 2
    ✓ Math.max('123')
      ✓ should fail typescript check
  B
    ✓ Math.max(4)
      ✓ should pass typescript check

Support TS 3.9 @ts-expect-error comment?

Hi @ikatyang,

First of all, thanks for the awesome project, really useful!

I was wondering what your feelings are on supporting the // @ts-expect-error comments from TypeScript 3.9.

Right now, my test files have errors in them (in the editor), because the lines after // @dts-jest:fail:snap should actually have an error:

Screen Shot 2020-06-25 at 18 05 28

What I would like to be able to do would be to use // @ts-expect-error either in the place of:

// @ts-expect-error
mergeArrays([]);

...or in addition to the dts-jest annotations:

// @dts-jest:fail:snap
// @ts-expect-error
mergeArrays([]);

or...

// @ts-expect-error
// @dts-jest:fail:snap
mergeArrays([]);

or even...

// @ts-expect-error @dts-jest:fail:snap
mergeArrays([]);

...none of which currently work.

Not sure if this is a feasible ask actually, but it would be nice to clean up more errors in my project!

Is the typeRoots config supported?

I was trying to write types for an external module and test them at the same time.

tsc is able to find the types that I'm currently writing using typeRoots on tsconfig.json. Currently, it fails with Could not find a declaration file for module 'my-package'. '/node_modules/my-package/index.js' implicitly has an 'any' type.

Could you please suggest how I could achieve such a setup?

Error when incremental option is true

problem

when setting "incremental" filed (in tsconfig.json) to true, test fails and an error occurred.

reproduction

clone this repo and execute npm ci && npm test , you'll get below error message.

https://github.com/hagevvashi/dts-jest-sandbox

Test suite failed to run

Debug Failure. False expression: position cannot precede the beginnig of the file

>    import { add } from "./index";

Rewrite remap & remap-cli

  • rename bin: dts-jest-remap
  • display TS version/path
  • support glob input, e.g. **/*.ts (dts-jest test files)
  • support --typescript <path/to/typescript.js>
  • support --outDir <path/to/output-directory>
  • support --rename <template>
    • using template {{filename}}, {{basename}}, {{extname}}
  • support --check
    • do not write files
    • throw error if diff exist
  • support --listDiff
    • list different files

assert not-any

First of all, thank you for providing this tool! It was missing in the TS ecosystem.

Q: is there any approach for asserting that the type of expression is not any? Anything else would be fine. Or, more generally, is it possible to assert that the type of an expression is different than the one given?

Redefine flags

type tests

// @dts-jest[:test|:skip|:only][:pass|:fail][:snapshot][:show] [description]
  • order does not matter
  • there's no default assertion flag now
  • :snapshot
    • snapshot the result
    • expect(result).toMatchSnapshot()
    • expect(result).toThrowErrorMatchingSnapshot()
  • :show
    • console.log(result)
  • :pass
    • expect(result).not.toThrow()
  • :fail
    • expect(result).toThrow()

actual tests

  • //=> ?
    • console.log(expression)
  • //=> :error
    • expect(expression).toThrow()
  • //=> :no-error
    • expect(expression).not.toThrow()
  • //=> value
    • anything except cases above
    • expect(expression).toEqual(value)
  • support multiline comment: /*=> something */

Hard to specify typescript's position

require(typescript_id)'s path is based on the load_typescript.ts module's position, should have a <cwd> placeholder for better describing the position.

Wrong behavior for multi-`:only` flag

it('1', () => expect("1").toMatchSnapshot());
it.only('2', () => expect("2").toMatchSnapshot());
it.only('3', () => expect("3").toMatchSnapshot());
describe('a', () => {
  it.only('a-1', () => expect("a-1").toMatchSnapshot());
});
describe('b', () => {
  it('b-1', () => expect("b-1").toMatchSnapshot());
});

image

Expected

// @dts-jest:snapshot
'1';

// @dts-jest:only:snapshot -> 2
'2';

// @dts-jest:only:snapshot -> 3
'3';

// @dts-jest:group group-a

// @dts-jest:only:snapshot -> a-1
'a-1';

// @dts-jest:group group-b

// @dts-jest:snapshot
'b-1';

Actual

// @dts-jest:snapshot
'1';

// @dts-jest:only:snapshot -> 2
'2';

// @dts-jest:only:snapshot
'3';

// @dts-jest:group group-a

// @dts-jest:only:snapshot -> a-1
'a-1';

// @dts-jest:group group-b

// @dts-jest:snapshot -> b-1
'b-1';

grouping with "describe" broken in v23

Hi @ikatyang,
I have found a breaking change in dts-jest-remap tool when upgraded to dts-jest v23.

In v22 I was using code like this:

describe('Name of the group', () => {
  // @dts-jest:pass:snap
  Math.max(2); //=> 2
});

This was working fine for with dts-jest-remap in v22, but now in v23 it cannot match the snapshot name because it's prepending a describe block name string to the snapshot name:

exports[`A Name of the group Math.max(2) (type) should match snapshot 1`] = `"number"`;

@ikatyang would you be willing to update dts-jest-remap to correctly match snapshots wrapped in describe blocks?

I have reproduced the issue in my fork: piotrwitek@2e2ee97

Just run npm run remap-integration to see the error.

Thanks!

Combine type tests with actual tests

Currently they are separate, it'd be better to combine them.

transformer:

  • current
    • type tests: dts-jest/transform
    • actual tests: dts-jest/transform-actual
  • proposed
    • type tests + actual tests: dts-jest/transform

options:

  • test_type: true by default
    • show warning if there is // @dts-jest[:assertion-flag] and test_type: false
  • test_value: false by default
    • show warning if there is //=> and test_value: false
    • transform fake environment if test_value: false
    • transform actual environment if test_value: true

support inline option on file-level (in the docblock comment)

  • @dts-jest enable:test-value disable:test-type

Jest v28 support?

Hi @ikatyang ! 👋 Hope that you are well.

Would you be willing to support Jest v28? It seems like there were some changes around process / processAsync, which cause dts-jest to fail:

FAIL dts src/__tests__/dts-jest/003-literals-unions.test.ts
  ● Test suite failed to run

    ● Invalid return value:
      `process()` or/and `processAsync()` method of code transformer found at 
      "/home/runner/work/courses/courses/slide-decks/slideDecks/typescript-fundamentals/packages/typescript-fundamentals-exercises/node_modules/dts-jest/transform.js" 
      should return an object or a Promise resolving to an object. The object 
      must have `code` property with a string of processed code.
      This error may be caused by a breaking change in Jest 28:
      https://jestjs.io/docs/upgrading-to-jest28#transformer
      Code Transformation Documentation:
      https://jestjs.io/docs/code-transformation

      at ScriptTransformer._buildTransformResult (node_modules/@jest/transform/build/ScriptTransformer.js:507:15)
      at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:628:17)
      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:765:40)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:822:[19])

Test Suites: 1 failed, 4 passed, 5 total
Tests:       5 passed, 5 total
Snapshots:   0 total
Ran all test suites matching /003/i in 4 projects.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

It seems like ts-jest also did a similar upgrade here: kulshekhar/ts-jest#3490

Add support for jest@23

Hey @ikatyang,
Currently dts-jest is not supporting jest > v21 because snapshots names have different format and parser does not match.

Are there plans for updating it in the near future to support new snapshots format?

Usage with Jest 27.0.1?

Hi @ikatyang ! 👋 Hope that you are well!

I'm currently upgrading babel-jest, jest, jest-worker, and ts-jest all to version 27.0.1, and most things are working now, but [email protected] fails in my tests with the following error:

FAIL dts src/__tests__/dts-jest/003-literals-unions.test.ts
  ● Test suite failed to run

    TypeError: Cannot read property '_dts_jest_' of undefined

      at Object.exports.transform [as process] (../../../../../node_modules/dts-jest/lib/transform.js:17:84)
      at ScriptTransformer.transformSource (../../../../../node_modules/@jest/transform/build/ScriptTransformer.js:612:31)
      at ScriptTransformer._transformAndBuildScript (../../../../../node_modules/@jest/transform/build/ScriptTransformer.js:758:40)
      at ScriptTransformer.transform (../../../../../node_modules/@jest/transform/build/ScriptTransformer.js:815:19)

Is dts-jest failing with v27?

Dependency deprecation warning: tslint-config-prettier-ext (npm)

On registry https://registry.npmjs.org/, the "latest" version (v1.5.0) of dependency tslint-config-prettier-ext has the following deprecation notice:

[DEPRECATED] This project has been merged into tslint-config-prettier

Marking the latest version of an npm package as deprecated results in the entire package being considered deprecated, so contact the package author you think this is a mistake.

Affected package file(s): package.json

If you don't care about this, you can close this issue and not be warned about tslint-config-prettier-ext's deprecation again. If you would like to completely disable all future deprecation warnings then add the following to your config:

"suppressNotifications": ["deprecationWarningIssues"]

Testing the value of types

Thanks for another great library! It really makes it easy to combine the validation of types with my current jest setup.

There's one question I have. In my tests, I need to check that my type inference is working correctly for an instance of a class I created. In my DTSLint tests, I created several something similar to the following.

// $ExpectType "extraArg" | "extraArgs" | "simple" | "withReturnValue"
type A = keyof typeof model.methods;

Is it currently possible to port over the above tests from DTSLint? Does this library provide a way of testing the exact value of types?

Value assertions when importing from different modules is breaking transformer

Hey @ikatyang,
I really love concept of this library and I'm trying to port my typescript library typesafe-actions to dts-jest, but unfortunately I have found out an issue with value testing.

Everything works great when testing only types (without transpilation involved) but I would like to test values as well in my tests and it's not working when I'm importing a function (that I try to test) from a different module.

screenshot 2018-12-25 at 10 59 21 pm

I have added an example test case so you can try to reproduce it yourself.
You can find it here in my fork. You just need to run npm run test-integration:
https://github.com/piotrwitek/dts-jest/tree/modules_not_working_demo

I think it have something to do with the transpilation process which don't translate import statements correctly.

I would appreciate any help with that issue!

TypeScript 5.0 support?

Hi @ikatyang 👋 Hope you are well.

I upgraded to TypeScript 5.0.3 recently, and noticed that the tests using dts-jest are now failing:

FAIL dts src/__tests__/dts-jest/003-literals-unions.test.ts
  ● Test suite failed to run

    TypeError: ts.getMutableClone is not a function

      at substituteNode (node_modules/dts-jest/lib/utils/get-node-one-line-text.js:8:34)
      at getPipelinePhase (node_modules/typescript/lib/typescript.js:110287:76)
      at pipelineEmit (node_modules/typescript/lib/typescript.js:110270:29)
      at print (node_modules/typescript/lib/typescript.js:110184:7)
      at writeNode (node_modules/typescript/lib/typescript.js:110027:7)
      at Object.printNode (node_modules/typescript/lib/typescript.js:109991:7)
      at get_node_one_line_text (node_modules/dts-jest/lib/utils/get-node-one-line-text.js:21:10)
      at node_modules/dts-jest/lib/utils/find-trigger-bodies.js:44:77
      at traverse_node (node_modules/dts-jest/lib/utils/traverse-node.js:6:9)
      at node_modules/dts-jest/lib/utils/traverse-node.js:8:79
      at visitNodes (node_modules/typescript/lib/typescript.js:27[58](https://github.com/upleveled/courses/actions/runs/4590799480/jobs/8106576734?pr=1621#step:18:59)8:24)
      at forEachChildInBlock (node_modules/typescript/lib/typescript.js:27[63](https://github.com/upleveled/courses/actions/runs/4590799480/jobs/8106576734?pr=1621#step:18:64)3:12)
      at Object.forEachChild (node_modules/typescript/lib/typescript.js:27673:37)
      at traverse_node (node_modules/dts-jest/lib/utils/traverse-node.js:8:8)
      at node_modules/dts-jest/lib/utils/traverse-node.js:8:79
      at visitNode2 (node_modules/typescript/lib/typescript.js:27580:20)
      at forEachChildInArrowFunction (node_modules/typescript/lib/typescript.js:28087:249)
      at Object.forEachChild (node_modules/typescript/lib/typescript.js:27673:37)
      at traverse_node (node_modules/dts-jest/lib/utils/traverse-node.js:8:8)
      at node_modules/dts-jest/lib/utils/traverse-node.js:8:79
      at visitNodes (node_modules/typescript/lib/typescript.js:27588:24)
      at forEachChildInCallOrNewExpression (node_modules/typescript/lib/typescript.js:27630:101)
      at Object.forEachChild (node_modules/typescript/lib/typescript.js:27673:37)
      at traverse_node (node_modules/dts-jest/lib/utils/traverse-node.js:8:8)
      at node_modules/dts-jest/lib/utils/traverse-node.js:8:79
      at visitNode2 (node_modules/typescript/lib/typescript.js:27580:20)
      at forEachChildInExpressionStatement (node_modules/typescript/lib/typescript.js:28217:18)
      at Object.forEachChild (node_modules/typescript/lib/typescript.js:27673:37)
      at traverse_node (node_modules/dts-jest/lib/utils/traverse-node.js:8:8)
      at node_modules/dts-jest/lib/utils/traverse-node.js:8:79
      at visitNodes (node_modules/typescript/lib/typescript.js:27588:24)
      at forEachChildInSourceFile (node_modules/typescript/lib/typescript.js:28208:18)
      at Object.forEachChild (node_modules/typescript/lib/typescript.js:27673:37)
      at traverse_node (node_modules/dts-jest/lib/utils/traverse-node.js:8:8)
      at find_trigger_bodies (node_modules/dts-jest/lib/utils/find-trigger-bodies.js:16:39)
      at find_triggers (node_modules/dts-jest/lib/utils/find-triggers.js:11:[64](https://github.com/upleveled/courses/actions/runs/4590799480/jobs/8106576734?pr=1621#step:18:65))
      at Object.transform [as process] (node_modules/dts-jest/lib/transform.js:21:54)
      at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:542:31)
      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:[67](https://github.com/upleveled/courses/actions/runs/4590799480/jobs/8106576734?pr=1621#step:18:68)1:40)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:[72](https://github.com/upleveled/courses/actions/runs/4590799480/jobs/8106576734?pr=1621#step:18:73)3:19)

What do you think about adding TypeScript 5.0 support?

Error on TypeScript 4.4

TypeScript 4.4 breaking change causes below error.

Release blog doesn't tell this breaking change.

problem

error log

    Unexpected error(s) while parsing tsconfig (./tsconfig.json):

      No inputs were found in config file 'tsconfig.json'. Specified 'include' paths were '["**/*"]' and 'exclude' paths were '[]'.

jest.config.ts

import type { Config } from "@jest/types";

const config: Config.InitialOptions = {
  clearMocks: true,
  coverageDirectory: "<rootDir>/coverage/",
  collectCoverage: true,
  collectCoverageFrom: ["<rootDir>/**/*.{tsx,ts}"],
  coveragePathIgnorePatterns: [
    "<rootDir>/node_modules/",
    "<rootDir>/jest.config.ts",
  ],
  testMatch: ["**/?(*.)+(test).ts?(x)", "**/?(*.)+(test-d).ts?(x)"],
  transform: {
    "^.+\\.test-d.tsx?$": "dts-jest/transform",
    "^.+\\.[jt]sx?$": "babel-jest",
  },
  transformIgnorePatterns: ["<rootDir>/node_modules/"],
  globals: {
    _dts_jest_: {
      compiler_options: "./tsconfig.json",
      transpile: false,
    },
  },
};

export default config;

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNEXT",
    "module": "esnext",
    "moduleResolution": "node",
    "allowJs": false,
    "noEmit": false,
    "isolatedModules": true,
    "jsx": "react-jsx",
    "declaration": true,
    "skipLibCheck": true,
    "strict": false,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "noUncheckedIndexedAccess": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true
  }
}

repo

https://github.com/hagevvashi/exp-dts-jest

cause of the problem

microsoft/TypeScript#44710 adds the call of directoryExists method.

https://github.com/microsoft/TypeScript/blob/40fa0c9f118aa1ed2b06ae0b3eed3946d5d2c612/src/compiler/utilities.ts#L6593

This PR makes ts.parseJsonConfigFileContent return above error and empty fileNames array.

workaround

The workaround is just changing the tsconfig.json path to the absolute one.

- compiler_options: "./tsconfig.json",
+ compiler_options: path.resolve(__dirname, "./tsconfig.json"), // we cannot use <rootDir> here

sample PR

hagevvashi/exp-dts-jest#1

resolution

I have no idea.

Is there anybody who has ideas for this issue?

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.