-
Notifications
You must be signed in to change notification settings - Fork 310
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
"Can't resolve all parameters" when importing ngc-compiled library with *.metadata.json #322
Comments
This is mostly a jest configuration issue. Can you share your Jest has to be configured in a specific way to work with Angular, as Angular does a lot under the hood with its own Compiler and Webpack Configuration. |
Thank you for the quick response. Here are the config files. The only diff is that I put my jest.conf in package.json. setup-jest.js import 'hammerjs';
import createGoogleMapsMock from 'jest-google-maps-mock';
import 'jest-preset-angular';
/* global mocks for jsdom */
const mock = () => {
let storage: { [key: string]: string } = {};
return {
getItem: (key: string) => (key in storage ? storage[key] : null),
setItem: (key: string, value: string) => (storage[key] = value || ''),
removeItem: (key: string) => delete storage[key],
clear: () => (storage = {})
};
};
Object.defineProperty(window, 'localStorage', { value: mock() });
Object.defineProperty(window, 'sessionStorage', { value: mock() });
Object.defineProperty(window, 'getComputedStyle', {
value: () => [ '-webkit-appearance' ],
});
const basicGoogleMock = createGoogleMapsMock('places');
(window as any).google = (window as any).google || {
maps: Object.assign(
{},
...basicGoogleMock,
{
places: {...basicGoogleMock.places, Autocomplete: () => {}},
event: {
addListener: () => {}
}
}
)
};
Object.defineProperty(document.body.style, 'transform', {
value: () => {
return {
enumerable: true,
configurable: true,
};
},
});
/* output shorter and more meaningful Zone error stack traces */
// Error.stackTraceLimit = 2; jest.config.js {
"preset": "jest-preset-angular",
"roots": [
"src"
],
"transform": {
"^.+\\.(ts|js|html)$": "ts-jest"
},
"setupFilesAfterEnv": [
"<rootDir>/src/setup-jest.ts"
],
"moduleNameMapper": {
"@influo/(.*)": "<rootDir>/src/app/$1",
"@assets/(.*)": "<rootDir>/src/assets/$1",
"@testing/(.*)": "<rootDir>/src/testing/$1",
"@core/(.*)": "<rootDir>/src/app/core/$1",
"@env": "<rootDir>/src/environments/environment",
"@src/(.*)": "<rootDir>/src/src/$1",
"@state/(.*)": "<rootDir>/src/app/state/$1"
},
"globals": {
"ts-jest": {
"tsConfig": "<rootDir>/src/tsconfig.spec.json",
"stringifyContentPathRegex": "\\.html$",
"astTransformers": [
"jest-preset-angular/InlineHtmlStripStylesTransformer.js"
]
}
}
} |
You mentioned you checked #288, but just to make sure everything is configured properly: did you set |
Yes already checked that. And it's set. Here is the full tsconfig.spec.json file. {
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/spec",
"emitDecoratorMetadata": true,
"types": [
"jasmine",
"node",
"googlemaps"
],
"module": "commonjs",
"allowJs": true
},
"files": [
"polyfills.ts"
],
"include": [
"**/*.spec.ts",
"**/*.d.ts"
]
}
|
Ok great, looks like you did everything alright! Checking If you like you can try to do some research on how it is processed, read and used, this would help a ton! I think I can have a look at it next week. Cheers! |
Hi, sorry for the late response. This is not really my area of expertise but I'm willing to probe around and maybe learn new things. Will post any progress I make here. Sorry again for the late response. |
No problem! This is Open Source work, we all invest our free time. I might find some time during the weekend to look into it. |
Some findings from my side:When compiling using The loaded metadata information from TodosTo make this work with jest as well, we have to find an appropriate moment to look for this metadata of loaded classes. Angular does this traversing the root module and all of it's imported modules and their declared components/decorators. Then we again have to add the data to components before they get loaded by Angular. I have not found out how and when the compilers do this yet, but it must be before instantiating the components on app startup. DebuggingTo debug I used this command and vscode setting: node --inspect-brk=9206 node_modules/@angular/cli/bin/ng serve ( to run a debuggable instance of angular) // in launch.json
{
"type": "node",
"request": "attach",
"name": "Attach to Node.js (9206)",
"port": 9206,
}, |
Something similar happened to running app too |
@ahnpnl this issue looks different, I think you mean #288, which is a reference issue of several classes that inject each other. This one is about using a library that was precompiled with ngc and produced a They might be related though. This is also the difficulty with
|
Do you mean we need to do some logic in the step of module resolution + AST transformation ? |
I think Angular wrote their own TS compiler host in addition to transformers to handle these scenarios (here might be the code of my guess), but I am not very knowledgeable in this area. |
it is possible to intercept module resolution of Module resolution with I will check in the link you mentioned to see, probably I can get something out of it. |
Closes #108 Closes #288 Closes #322 Closes #353 Closes #622 BREAKING CHANGE: With the new jest transformer, `jest-preset-angular` now switches to default to use this new transformer and no longer uses `ts-jest` to transform codes. Users who are currently doing in jest config ``` // jest.config.js module.exports = { // [...] transform: { '^.+\\.(ts|js|html)$': 'ts-jest', }, } ``` should change to ``` // jest.config.js module.exports = { // [...] transform: { '^.+\\.(ts|js|html)$': 'jest-preset-angular', }, } ``` `isolatedModule: true` will still use `ts-jest` to compile `ts` to `js` but you won't get full compatibility with Ivy.
Hi,
I recently changed to jest. Still trying to learn and optimize the testing process for our app. But switching to jest broke some tests. These tests keep failing and I can't figure why they fail. Maybe I'm doing something wrong or this is a bug.
I have already looked at similar issues but I think they are different from mine. #154 #288
I also used the https://github.com/briebug/jest-schematic shematic to do all the config setup. Which automatically added the emitDecoratorMetadata so I am excluding that as a possible issue.
The component I'm trying to test
This component just has one dependency GoogleChartComponent which is a part of the Ng2GoogleChartsModule.
A very basic test. I am encapsulating GoogleChartComponent.
Error
When I look at the constructor of the GoogleChartComponent I find ElementRef, GoogleChartsLoaderService. GoogleChartsLoaderService is provided by the module itself. So it's not possible that it's not provided.
When I use this module outside of a testing environment it works perfectly. So my hunch is that it has to do something with the use of jsdom but this seems wrong to assume that.
Which confuses me more why this error is even occurring.
Thanks in advance!
The text was updated successfully, but these errors were encountered: