After a fresh installation within my project directory I followed the main example of the Getting Started doc's paragraph.
The test example runs correctly, but at the end of the test the browser hangs on a Testcafe page that said Connected with a loading spinner and an error is shown on the terminal:
UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: this.moment.duration(...).format is not a function
Finished tests and close the browser without errors (?).
Installed within my Ubuntu:
VERSION="16.04.2 LTS (Xenial Xerus)"
PRETTY_NAME="Ubuntu 16.04.2 LTS"
VERSION_ID="16.04"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
Inside a project with the command:
yarn add --dev testcafe
Running with a yarn script yarn test:testcafe that runs testcafe:
testcafe chrome test/testcafe
Within the testcafe folder I have a main.js file for my test.
import { Selector } from 'testcafe';
fixture `Getting Started`
.page `https://devexpress.github.io/testcafe/example`;
test('My first test', async t => {
await t
.typeText('#developer-name', 'John Smith')
.click('#submit-button');
});
Hello @LasaleFamine! Do you use moment package in your project? It's very likely that yarn hasn't installed dependencies correctly, we had a similar issue before: https://testcafe-discuss.devexpress.com/t/promise-rejection-warning-on-test-completion/184/16. If you use it, try to change the package version to `^2.10.3'.
Hi @AndreyBelym and thank you for your fast response.
Actually I have moment , but as Bower dependency (don't blame me for this, legacy app 馃拃) and it is at the 2.17.1 version.
I tried to add moment as yarn dependency and now testcafe works as expected.
Do you think bower is responsible for this?
NOTE: I also tried upgrading the bower's moment to 2.18.1, but I had the same (not working) result.
I have tried using npm instead of yarn to install a fresh node_modules and now the problem is disappeared... So, is yarn the problem? 馃槙
Yes, it seems it relates to a package manager issue
So I must assume that testcafe is not supporting yarn?
I've investigated the issue in more details and found the exact reason why does it fail.
For example your project has the following dependencies:
"dependencies": {
"moment": "2.17.1",
"testcafe": "^0.17.2"
}
TestCafe has the following deps:
"moment": "^2.10.3",
"moment-duration-format": "^1.3.0",
npm installs dependencies in the following way: it see that the [email protected] is ok for both dependencies (2.17.1 and ^2.10.3) and install modules in the following dirs:
/your-project-dir
-- /node_modules
-- -- /testcafe
-- -- /moment (2.17.1)
-- -- /moment-duration-format (1.3.0)
But yarn installs the latest moment version (2.18.1) as a TestCafe dependency and the 2.17.1 version as the project's dependecy. It looks like:
/your-project-dir
-- /node_modules
-- -- /testcafe
-- -- -- /node_modules
-- -- -- -- /moment (2.18.1)
-- -- /moment (2.17.1)
-- -- /moment-duration-format (1.3.0)
Then testcafe imports the moment-duration-format module and it patches the moment module from the root node_modules directory (2.17.1) and add additional API there. But TestCafe uses the moment module from it's own node_modules directory (2.18.1) that is not patched.
So to make it workable with yarn it necessary to update the moment version in your project to the latest (it possible it's necessary to clean yarn cache).
Meanwhile, we'll see how we can avoid this collision
It's definitely yarn's bug and should be reported there.
Hey,
I resolved this issue by providing resolutions to package.json according to this https://github.com/yarnpkg/yarn/issues/2763, but it also works if will specify dummy dependency which is not fulfilling semver for moment-duration-format in testcafe package.json file, like moment-duration-format: 1.2.0
It make sense to me what yarn is doing, but personally I believe that moment-duration-format requires too much. I would really like to avoid such things in package.json so is there any chance to drop this dependency in near future?
Hi @damianbaar,
Thanks for the workaround with resolutions. We will check how we can avoid this.
It make sense to me what yarn is doing, but personally I believe that moment-duration-format requires too much. I would really like to avoid such things in package.json so is there any chance to drop this dependency in near future?
Yeah if it's just for formatting dates, https://github.com/date-fns/date-fns might be a better option.
@damianbaar, the resolutions workaround did not work for me.
@andreyluiz, It possible you need to remove the yarn.lock file to refresh dependencies.
confirmed this works for me now
I just ran into this issue as well, even though my project had a newer version of moment as a dependency. I ended up having to delete an entry for the older version of moment from my yarn.lock file so that only 1 version of moment was installed.
After having looked at the documentation for moment-duration-format, I think this issue can be avoided entirely by using the package as a module in plugin-host.js, rather than letting it patch the global moment instance. Here's an example from the docs:
var moment = require("moment-timezone");
var momentDurationFormatSetup = require("moment-duration-format");
momentDurationFormatSetup(moment);
In plugin-host.js, moment-duration-format could be imported as a module and the ReporterPluginHost class constructor could set up its moment property like so:
this.moment = momentDurationFormatSetup(moment);
If it looks like this would work, and is an acceptable solution, I'd be happy to open a PR.
Hi @s-robertson! Thanks for pointing on it. It's a very important issue, I'll try your solution and provide a fix for TestCafe ASAP.
This thread has been automatically locked since it is closed and there has not been any recent activity. Please open a new issue for related bugs or feature requests. We recommend you ask TestCafe API, usage and configuration inquiries on StackOverflow.
Most helpful comment
I've investigated the issue in more details and found the exact reason why does it fail.
For example your project has the following dependencies:
TestCafe has the following deps:
npminstalls dependencies in the following way: it see that the[email protected]is ok for both dependencies (2.17.1and^2.10.3) and install modules in the following dirs:But
yarninstalls the latestmomentversion (2.18.1) as a TestCafe dependency and the2.17.1version as the project's dependecy. It looks like:Then
testcafeimports themoment-duration-formatmodule and it patches themomentmodule from the rootnode_modulesdirectory (2.17.1) and add additional API there. But TestCafe uses themomentmodule from it's ownnode_modulesdirectory (2.18.1) that is not patched.So to make it workable with yarn it necessary to update the
momentversion in your project to the latest (it possible it's necessary to clean yarn cache).Meanwhile, we'll see how we can avoid this collision