We are currently trying to implement jest as our test framework in our Aurelia application; however we have run into an issue that I am not able to solve.
When running tests we get error:
SecurityError: Could not parse url argument "blank" to replaceState against base URL "about:blank".
at HistoryImpl._sharedPushAndReplaceState (node_modules/aurelia-pal-nodejs/node_modules/jsdom/lib/jsdom/living/window/History-impl.js:73:15)
at HistoryImpl.replaceState (node_modules/aurelia-pal-nodejs/node_modules/jsdom/lib/jsdom/living/window/History-impl.js:57:10)
at History.replaceState (node_modules/aurelia-pal-nodejs/node_modules/jsdom/lib/jsdom/living/generated/History.js:129:21)
at BrowserHistory.Object.<anonymous>.BrowserHistory.setState (node_modules/aurelia-history-browser/src/browser-history.ts:214:14)
at node_modules/aurelia-router/src/app-router.ts:194:22
This is my jest-preset.js:
import āaurelia-polyfillsā;
import { Options } from āaurelia-loader-nodejsā;
import { globalize } from āaurelia-pal-nodejsā;
import path from āpathā;
Options.relativeToDir = path.join(__dirname, āunitā);
globalize();
Can you help give it a try? Basically initialize the window URL before each test to something, anything rather than the default, which is āabout:blankā.
jest uses jsdom by default, its may be a downstream dependency incompatibility with your configuration+aurelia. You can change the environment to node in your jest configuration, see https://jestjs.io/docs/en/configuration#testenvironment-string. aurelia-cli sets this to node within package.jsonās {ājestā} config.
I have tried to set the testEnvironment to ājsdomā but that just raises
TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
when trying to create to component. I am really, really lost and I canāt find any sort of documentation on how to use jest together with Aurelia. If there are any then I would love to read it.
I use Jest to test my entire library Aurelia-Slickgrid but I barely have any tests with the creation of a Custom Element lifecycle. I mean most of my tests are for Services and such, which donāt require the component.bootstrap lifecycle. I looked at Aurelia I18N lib quite a few times for references as it is tested with Jest as well.
I still donāt think your tobe is valid, youāre missing an argument and the link you sent does say that as well toBe(value) but you simply wrote that line as: expect(component).tobe;
thereās no argument at all and youāre not even calling the toBe method, I wonder how that can even work?
Another thing about JSDOM and Node, in my jest.pretest.ts I also added an import to the jsdom-global/register and also this line (global as any).navigator = { userAgent: 'node.js' };
Perhaps you can give that a try, I donāt remember what it does exactly but if you want to see my full pretest, the link is here . I might have used the navigator because of this error which has some similarities with your error (that actually came up after googling your error)
oh if you look around my lib and tests, donāt be surprised at my folder structure, my unit tests are not in the ./test folder, I prefer to put them all in their sub-sections under a __tests__ folder which is recommended by Jest (e.g. the services unit tests are under /services/__tests__), so my Jest pretest is different compare to the I18N package, since that one as all their tests under /test
Iām getting a similar error to the original poster. Error message is:
SecurityError: Could not parse url argument "blank" to replaceState against base URL
"about:blank".
at HistoryImpl._sharedPushAndReplaceState
(node_modules/jsdom/lib/jsdom/living/window/History-impl.js:73:15)
at HistoryImpl.replaceState (node_modules/jsdom/lib/jsdom/living/window/History-impl.js:57:10)
at History.replaceState (node_modules/jsdom/lib/jsdom/living/generated/History.js:129:21)
at BrowserHistory.setState (node_modules/aurelia-history-browser/dist/commonjs/aurelia-history-
browser.js:263:18)
at node_modules/aurelia-router/dist/commonjs/aurelia-router.js:1824:25
For info my jest.pretest is
import 'aurelia-polyfills';
import { Options } from 'aurelia-loader-nodejs';
import { globalize } from 'aurelia-pal-nodejs';
import * as path from 'path';
Options.relativeToDir = path.join(__dirname, 'unit');
globalize();
jest.setTimeout(30000);
This is my test file (imports, filepaths etc redacted)
The file I am testing:
import { autoinject } from āaurelia-frameworkā;
import { RouterConfiguration, Router } from āaurelia-routerā;
import { PLMA_PLATFORM } from ā./routesā;
import { ViewModelBase } from ālogic/view-models/view-model-baseā;
āI use Jest to test my entire library Aurelia-Slickgrid but I barely have any tests with the creation of a Custom Element lifecycle. I mean most of my tests are for Services and such, which donāt require the component.bootstrap lifecycle. I looked at Aurelia I18N lib quite a few times for references as it is tested with Jest as well.ā
This was what I ended up doing as well, now my unit tests for controller, services and such works. I gave up on the whole creation thing. Thanks anyway, I might have overworked my test scenarios from the beginning.