testcafe: Running sample test thrown an UnhandledPromiseRejectionWarning: this.moment.duration(...).format is not a function

Are you requesting a feature or reporting a bug?

  • Could be a bug.

What is the current behavior?

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

What is the expected behavior?

Finished tests and close the browser without errors (?).

How would you reproduce the current behavior (if this is a bug)?

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.

Provide the test code and the tested page URL (if applicable)

main.js
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');
});

Specify your

  • operating system: Ubuntu (as stated)
  • testcafe version: 0.17.1
  • node.js version: 8.1.0

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 8
  • Comments: 15 (7 by maintainers)

Commits related to this issue

Most upvoted comments

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 moment@2.17.1 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

@andreyluiz, It possible you need to remove the yarn.lock file to refresh dependencies.

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.

EDIT:

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? 😕

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’.