#spfx-webparts SyntaxError: Unexpected token export at node_modules\@microsoft\sp-http\lib\index.js:1
Category
- [x] Question
- [ ] Typo
- [] Bug
- [ ] Additional article idea
Expected or Desired Behavior
We have written Unit Tests for our SPFX webpart components using Jest. Just before the test starts, while in the ScriptTransformer stage - we expect jest to transform "@microsoft\sp-http\lib\index.js "
Observed Behavior
jest fails to run test on react component and throws out below error
FAIL src\webparts\test\ipkKitSettingsUnit.test.ts ● Test suite failed to run
C:\Campus\Modern SharePoint\SPFx\CampusIPKit\CampusWebParts\node_modules\@microsoft\sp-http\lib\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export { default as HttpClient } from './httpClient/HttpClient';
^^^^^^
SyntaxError: Unexpected token export
66 | public Required: boolean = false;
67 | public productTaxonomyName: string = null;
> 68 | public capabilityTaxonomyName: string = null;
69 | public industryTaxonomyName: string = null;
70 | public domainsTaxonomyName: string = null;
71 | public productTaxonomyID: string = null;
at ScriptTransformer._transformAndBuildScript (node_modules/jest-cli/node_modules/jest-runtime/build/script_transformer.js:316:17)
at Object.<anonymous> (src/webparts/ipKitSettings/components/Panel/IPKitPanel.tsx:68:68)
Steps to Reproduce
We have a component which imports below import { SPHttpClient, SPHttpClientResponse, HttpClient, HttpClientResponse, IHttpClientOptions } from '@microsoft/sp-http';
now when we have a jest test case for the above component which uses ReactWrapper and Mount method. following is the jest configuration
"jest": {
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
],
"transform": {
"^.+\.(js|ts|tsx)$": "ts-jest"
},
"testMatch": [
"/src//.test.+(ts|tsx|js)"
],
"collectCoverage": true,
"coverageReporters": [
"json",
"lcov",
"text",
"cobertura"
],
"coverageDirectory": "
"moduleNameMapper": {
"\.(css|less|scss|sass)$": "identity-obj-proxy",
"office-ui-fabric-react/lib/(.
Now when we do npm test it is throwing the SyntaxError: Unexpected token export error
We have tried with "transformIgnorePatterns": [ "node_modules/?!(@microsoft/sp-http)" ], but then a new depend module throws Cannot find module '@ms/sp-client-shared' from 'OAuthTokenProvider.js' error.
before this we had a SyntaxError: Unexpected token export for "office-ui-fabric-react/lib/" which was resolved by changing the lib path to "office-ui-fabric-react/lib-commonjs/$1"
So we are getting error one after resolving one, Does jest have many compatability issues? should we expect another issue related to another module once we resolve this?
Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.
@andrewconnell @VesaJuvonen It would be really helpful if you could provide us your any inputs. cc: @sarathchamp
The problem isn't with Jest.
Many SPFx libraries expect to be running within the context of the SharePoint Framework. When testing, you aren't running the tests within SPFx. Therefore, if your tests load anything that ultimately takes a dependency on a SPFx library, you will run this risk.
My guidance is to build your components as modular as possible and have them use as little of the SPFx APIs as possible. Then, you test those components. It's a very tall order to try to test anything that takes dependencies on SPFx libraries. If you want to do that though, you need to look into Jest's support for mocking to stub out those libraries.
I'm editing your IP to remove the bug indicator... this isn't a bug in SPFx or Jest... it's an aspect of testing you'll have to address.
Closing issue as "answered". If you encounter similar issue(s), please open up a NEW issue. Thank you.
Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues