ikatyang / dts-jest Goto Github PK
View Code? Open in Web Editor NEWA preprocessor for Jest to snapshot test TypeScript declaration (.d.ts) files
License: MIT License
A preprocessor for Jest to snapshot test TypeScript declaration (.d.ts) files
License: MIT License
type tests
// @dts-jest[:test|:skip|:only][:pass|:fail][:snapshot][:show] [description]
:snapshot
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
expect(expression).toEqual(value)
/*=> something */
when setting "incremental" filed (in tsconfig.json) to true, test fails and an error occurred.
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";
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.
Using TypeScript x.y.z from path/to/typescript.js
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?
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());
});
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';
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
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?
TypeScript 4.4 breaking change causes below error.
Release blog doesn't tell this breaking change.
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 '[]'.
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;
{
"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
}
}
https://github.com/hagevvashi/exp-dts-jest
microsoft/TypeScript#44710 adds the call of directoryExists
method.
This PR makes ts.parseJsonConfigFileContent
return above error and empty fileNames array.
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
I have no idea.
Is there anybody who has ideas for this issue?
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!
Hi @ikatyang !
As I mentioned in my comment, would it be possible to upgrade yargs
?
This would make it easier to upgrade mem
for the security vulnerability (memory leak).
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
(package.json)
{
"jest": {
"globals": {
"_dts_jest_": {
"typescript": "path/to/typescript.js"
}
}
}
}
Currently they are separate, it'd be better to combine them.
transformer:
dts-jest/transform
dts-jest/transform-actual
dts-jest/transform
options:
test_type
: true
by default
// @dts-jest[:assertion-flag]
and test_type: false
test_value
: false
by default
//=>
and test_value: false
test_value: false
test_value: true
support inline option on file-level (in the docblock comment)
@dts-jest enable:test-value disable:test-type
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?
Before
{
"target": 5 // ts.ScriptTarget.ESNext
}
After
{
"target": "esnext"
}
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"]
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:
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!
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?
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?
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?
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.
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!
dts-jest-remap
**/*.ts
(dts-jest test files)--typescript <path/to/typescript.js>
--outDir <path/to/output-directory>
--rename <template>
{{filename}}
, {{basename}}
, {{extname}}
--check
--listDiff
Diagnostics that are not matched with :fail
should be reported.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.