Open marcioggs opened 3 years ago
It works fine if @ts-ignore
is added.
// @ts-ignore
import JasmineDOM from '@testing-library/jasmine-dom/dist';
Not sure if it can be related to the library being @testing-library/jasmine-dom/dist
having a /dist
in the end, and the associated @types
package not having it (@types/testing-library__jasmine-dom
).
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.
Not sure if it can be related to the library being @testing-library/jasmine-dom/dist having a /dist in the end, and the associated @types package not having it (@types/testing-library__jasmine-dom).
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've included a karma example that can be found at https://github.com/testing-library/angular-testing-library/blob/main/apps/example-app-karma/src/test.ts
Hi, I'm the maintainer of the Angular Testing Library. I've included a karma example that can be found at https://github.com/testing-library/angular-testing-library/blob/main/apps/example-app-karma/src/test.ts
I tried with the same tests.ts
as on this sample and got this error:
TypeError: expect(...).toHaveTextContent is not a function
Don't you have to add the matchers, as stated on the instructions below? https://github.com/testing-library/jasmine-dom#with-typescript
import JasmineDOM from '@testing-library/jasmine-dom/dist';
beforeAll(() => {
jasmine.getEnv().addMatchers(JasmineDOM);
});
I still couldn't make this work without adding // @ts-ignore
to the import statement.
Thanks.
@marcioggs somehow I'm not required to do that 🤔 Did you verify we're using the same versions? If you can reproduce it in a project, we could take a look at it.
We're using the same versions.
From my package.json
:
"@testing-library/angular": "^10.11.1",
"@testing-library/dom": "^8.11.1",
"@testing-library/jasmine-dom": "^1.2.0",
"@types/jasmine": "~3.10.2",
"jasmine-core": "~3.10.1",
"karma": "^6.3.9",
"karma-chrome-launcher": "~3.1.0",
"karma-jasmine": "~4.0.1",
"karma-jasmine-html-reporter": "~1.7.0",
Thanks for the support, @timdeschryver . I'll check for clues on the rest of your project files and will let you know if I find something.
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);
});
I have the same issue. Any updates on this? @marcioggs did you find a fix?
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.
My current solution for that is to have "noImplicitAny": true
in the main app tsconfig, and override it to false in tsconfig.spec.json.
@sdarnell I use at work these types with strict
at work and they work fine. I don't use Angular, can't check for the issue
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" ]
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
src/test.ts
file in order to load the matchers (slightly different code than the one in the README), typically this file would have been generated by Angular (in the previous versions) and we would just modify the content, but this is not the case anymore// @ts-ignore
import JasmineDOM from '@testing-library/jasmine-dom';
beforeAll(() => {
jasmine.addMatchers(JasmineDOM);
});
tsconfig.spec.json
in order to load the types and the test.ts
file{
"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"
]
}
angular.json
file overriding the include
parameters, since the test.ts
is not generated anymore by Angular it won't be automatically included. Additionally because we are overriding the include
option we also need to explicitly provide the spec or other test files. {
"$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!