-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Could not find a declaration file for module '@testing-library/jasmine-dom/dist' on Angular 12 project #40
Comments
It works fine if
Not sure if it can be related to the library being |
Hi Márcio! Thanks for checking out the library! To be honest it's been a while since I used the library, since I developed it for an old job that I had and never used it with typescript.
I don't really know much about how typescript declaration files work but if you're willing and able to come up with a solution for this, a PR would be more than welcome! I'm really sorry I can't help you with this 🙁 |
Hi, I'm the maintainer of the Angular Testing Library. |
I tried with the same
Don't you have to add the matchers, as stated on the instructions below?
I still couldn't make this work without adding |
@marcioggs somehow I'm not required to do that 🤔 |
We're using the same versions. From my
Thanks for the support, @timdeschryver . |
I have the same issue. Any updates on this? @marcioggs did you find a fix? |
Ok, to fix this I had to import jasmine-dom twice like the following and it fixed my error in the terminal and the Eslint error: import "zone.js/dist/long-stack-trace-zone";
import "zone.js/dist/proxy.js";
import "zone.js/dist/sync-test";
import "zone.js/dist/jasmine-patch";
import "zone.js/dist/async-test";
import "zone.js/dist/fake-async-test";
import { getTestBed } from "@angular/core/testing";
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from "@angular/platform-browser-dynamic/testing";
import "@testing-library/jasmine-dom"; // <---- Fixed the lint error: Property 'toBeInTheDocument' does not exist on type 'Matchers<HTMLElement>'.
import JasmineDOM from "@testing-library/jasmine-dom/dist"; // <---- fixed the terminal error: "TypeError: expect(...).toBeInTheDocument is not a function"
declare const require: {
context(
path: string,
deep?: boolean,
filter?: RegExp
): {
<T>(id: string): T;
keys(): string[];
};
};
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
// Then we find all the tests.
const context = require.context("./", true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
beforeAll(() => {
// On the Jasmine version I'm using the addMatches method is directly on the jasmine variable instead of having to call .getEnv()
jasmine.addMatchers(JasmineDOM);
}); |
No, I still have the workaround described in the first comment. |
For me (Angular 15, typescript 4.8.4) I needed to import the types explicitly in my tsconfig.spec.json: "types": [
"jasmine",
"node",
// "@testing-library/jasmine-dom",
"@types/testing-library__jasmine-dom"
] |
Ah, but then later I'd enabled noImplicitAny in my tsconfig and things stopped working. |
@sdarnell I use at work these types with |
To add on to this, installing the types helped and then doing a tsignore on the import in test.ts part with the extending of the matchers did it for me. Doesn't look like jasmine dom ships with types |
For me, freshly new created Angular 16.2.x with Typescript 5.1.x, this is what worked:
npm install --save-dev @testing-library/jasmine-dom
npm install --save-dev @types/testing-library__jasmine-dom
// @ts-ignore
import JasmineDOM from '@testing-library/jasmine-dom';
beforeAll(() => {
jasmine.addMatchers(JasmineDOM);
});
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"types": [
"jasmine",
"node",
// include the types
"@testing-library/jasmine-dom"
]
},
// include the test entry point
"files": ["src/test.ts"],
"include": [
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"your-project-name": {
// other stuff
"architect": {
// other stuff
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
// override the include by specifying the test.ts and all other needed files
"include": [
"src/test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
],
// other stuff
}
}
}
}
}
} Hope this helps. |
jasmine-dom
version: 1.0.3node
version: 14.15.4npm
version: 6.14.10@angular/core
version: 12.2.9typescript
version: 4.3.2jasmine-core
version: 3.8.0Relevant code or config:
tsconfig.spec.json:
test.ts:
What you did:
Tried to run the tests:
After the error happened, installed the types package as suggested in the error message, but the same error occurred:
What happened:
Problem description:
I couldn't make it work on an Angular 12 project by following the TypeScript instructions on the README file.
Please note that on
tests.ts
I had to calljasmine.addMatchers(JasmineDOM);
without.getEnv()
as it was suggested in the README. Anyway the error happens before this line.I also took a look at this other related issue, but couldn't find anything that could solve this.
Is there any additional configuration I need to make this work?
Thanks in advance for the support and for providing this helpful library!
The text was updated successfully, but these errors were encountered: