Open mspiess opened 2 years ago
I'm not sure if this is a problem with the office-addin-mock
library itself or just some configuration you are missing.
Searching online, I found Consider using the "jsdom" test environment, could you please try it?
TL;DR: turning off usage-data collection with the following command circumvents this issue:
npx office-addin-usage-data off
Hi @igor-ribeiiro,
I don't think it is a configuration issue. Putting the comment
/**
* @jest-environment jsdom
*/
at the top of a test file has the same effect, as configuring the testEnvironment
in jest.config.js
as suggested on the page you linked.
Besides, the error message suggests to use the node environment, so I am quite confident that the test is actually executed in the jsdom
test environment.
I created a repository with a minimal example for reproducing this issue: https://github.com/mspiess/office-add-in-min-example
I still think that the issue is the inability of the office-addin-mock
library to run in a browser-like environment, as stated in the title of this issue.
I did some further research towards the cause of this error.
I was able to get a more complete stacktrace by removing the superfluous error-wrapping in line 126 in usageData.ts
:
throw new Error(err);
I replaced it with:
throw err;
This revealed two more lines in the stacktrace:
at AutoCollectPerformance.Object.<anonymous>.AutoCollectPerformance.enable (node_modules/applicationinsights/AutoCollection/Performance.ts:81:30)
at Function.start (node_modules/applicationinsights/applicationinsights.ts:118:22)
Looking at the code, this._handle.unref
is indeed called there, and that function does not seem to exist in the jsdom
test environment.
Realizing that this code is related to the collection of usage data I tried to turn usage data collection off with:
npx office-addin-usage-data off
When running the test again afterwards, I received different errors which I was able to resolve by tuning the jest config. For anybody stumbling upon this in the future, you can see the necessary changes in the "fix" branch of my minimal-example repository.
I guess you can close this issue, but I propose following options to prevent people from running into this issue in the future:
TL;DR: turning off usage-data collection with the following command circumvents this issue:
npx office-addin-usage-data off
Hi @igor-ribeiiro,
I don't think it is a configuration issue. Putting the comment
/** * @jest-environment jsdom */
at the top of a test file has the same effect, as configuring the
testEnvironment
injest.config.js
as suggested on the page you linked. Besides, the error message suggests to use the node environment, so I am quite confident that the test is actually executed in thejsdom
test environment. I created a repository with a minimal example for reproducing this issue: https://github.com/mspiess/office-add-in-min-example I still think that the issue is the inability of theoffice-addin-mock
library to run in a browser-like environment, as stated in the title of this issue.I did some further research towards the cause of this error. I was able to get a more complete stacktrace by removing the superfluous error-wrapping in line 126 in
usageData.ts
:throw new Error(err);
I replaced it with:
throw err;
This revealed two more lines in the stacktrace:
at AutoCollectPerformance.Object.<anonymous>.AutoCollectPerformance.enable (node_modules/applicationinsights/AutoCollection/Performance.ts:81:30) at Function.start (node_modules/applicationinsights/applicationinsights.ts:118:22)
Looking at the code,
this._handle.unref
is indeed called there, and that function does not seem to exist in thejsdom
test environment. Realizing that this code is related to the collection of usage data I tried to turn usage data collection off with:npx office-addin-usage-data off
When running the test again afterwards, I received different errors which I was able to resolve by tuning the jest config. For anybody stumbling upon this in the future, you can see the necessary changes in the "fix" branch of my minimal-example repository.
I guess you can close this issue, but I propose following options to prevent people from running into this issue in the future:
- disable data collection in the mock (btw ...seriously?) by default
- somehow make it work in the js-dom test environment
We will keep the issue open until this is fixed. Thanks for providing a workaround for now, hopefully, it will work for others until fixed.
Any updates on a fix for this?
Unfortunately, @mspiess' workaround didn't work for me.
I'm able to use office-addin-mock with jest v27, but not with v28 or v29 (and those newer versions of jest are necessary for other upgrades we'd like to make).
@carlhauck it sounds like your problem is different. Can you enter a new issue with the errors you get when using v28 or v29?
I have upgraded yesterday from jest 27 to 29 with similar results:
` Details:
C:\Projects\ima-office\src\ImaOffice365.Common.Web\node_modules\.pnpm\uuid@8.3.2\node_modules\uuid\dist\esm-browser\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default as v1 } from './v1.js';
^^^^^^
SyntaxError: Unexpected token 'export'
9 | import { RoutePaths } from "../../../routing";
10 | import * as ReactRouterDom from "react-router-dom";
> 11 | import { OfficeMockObject } from "office-addin-mock";
| ^
12 | import "@testing-library/jest-dom";
13 |
14 | jest.mock("../../../services/CoordinatorService");
at Runtime.createScriptFromCode (node_modules/.pnpm/jest-runtime@29.6.4/node_modules/jest-runtime/build/index.js:1505:14)
at Object.<anonymous> (node_modules/.pnpm/office-addin-usage-data@1.6.7/node_modules/office-addin-usage-data/src/usageDataSettings.ts:7:1)
at Object.<anonymous> (node_modules/.pnpm/office-addin-usage-data@1.6.7/node_modules/office-addin-usage-data/src/usageData.ts:6:1)
at Object.<anonymous> (node_modules/.pnpm/office-addin-usage-data@1.6.7/node_modules/office-addin-usage-data/src/main.ts:6:1)
at Object.<anonymous> (node_modules/.pnpm/office-addin-manifest@1.12.7/node_modules/office-addin-manifest/src/addInTypes.ts:4:1)
at Object.<anonymous> (node_modules/.pnpm/office-addin-manifest@1.12.7/node_modules/office-addin-manifest/src/main.ts:4:1)
at Object.<anonymous> (node_modules/.pnpm/office-addin-mock@2.3.14/node_modules/office-addin-mock/src/officeMockObject.ts:1:1)
at Object.<anonymous> (node_modules/.pnpm/office-addin-mock@2.3.14/node_modules/office-addin-mock/src/main.ts:4:1)
at Object.<anonymous> (src/tests/components/imaDocument/documentTypeSelection.test.tsx:11:1)`
And I found out that latest version of Jest avtivates ESM mode by looking type property at the package.json file (Jest docs ESM ) Here it is a hint for this at stackoverflow (stackoverflow) As I understand neither uuid v8.3.2 nor v9.0.0 has this property (type: "module")
We also encountered the similar issue as above. V29 is not compatible. And npx office-addin-usage-data off
doesn't work unfortunately.
I was also able to reproduce this issue with jest v29. I was able to get a fix working (Thanks @mspiess).
I'm running a JS project and not typescript so I modified the Jest config changes a little bit and only seemed to need:
"transformIgnorePatterns": [
"/node_modules/(?!(uuid)/)"
]
}
To get it working.
Any updates on this ? I have tried the workarounds suggested in the comments however I still hit this issue of:
C:\Users\k.l\Documents\repositories\lwb360-drafting\word\add-in\node_modules\office-addin-usage-data\node_modules\uuid\dist\esm-browser\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default as v1 } from './v1.js';
^^^^^^
SyntaxError: Unexpected token 'export'
> 1 | import { OfficeMockObject } from 'office-addin-mock';
| ^
2 | import { ObjectData } from 'office-addin-mock/lib/objectData';
I am using jest v29 and "office-addin-mock": "^2.4.3",
Prerequisites
Expected behavior
The jest test runner runs the tests with jsdom environment without error while making use of "office-addin-mock".
Current behavior
The test fails to run because "office-addin-mock" seems to expect a Node environment (see Failure Logs below).
Steps to Reproduce
Run the following test file:
Context
Packages installed in addition to the office-addin-react starter:
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^12.1.5",
"@types/jest": "^28.1.1",
"jest": "^28.1.1",
"jest-environment-jsdom": "^28.1.1",
"office-addin-mock": "^2.1.2",
"ts-jest": "^28.0.5",
Operating System: Windows
Node version: v16.15.0
jest.config.js
:Test script:
jest
Failure Logs