6

I am currently trying to use expect to do assertions by using const { expect } = require('@playwright/test'); but every time I get Error: Cannot find module '@playwright/test'. It is a very short script but something is wrong with that.

const { chromium } = require("playwright");
const { expect } = require('@playwright/test');
const { matchers } = require('playwright-expect');
console.log("##########", expect)

// add custom matchers
expect.extend(matchers);

(async () => {
  const browser = await chromium.launch({
    headless: false,
  });
  const page = await browser.newPage();
  await page.goto("someurl");
  await page.fill("input[name='userLoginId']", 'nnn');
  await page.fill("input[name='password']", 'nnn');
  await page.click("button[type=submit]");
})();

package.json

{
  "name": "playwright",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "node ./index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "playwright": "^1.15.1",
    "playwright-expect": "^0.1.2"
  }
}

The test works fine without this:

const { expect } = require('@playwright/test');
const { matchers } = require('playwright-expect');
console.log("##########", expect)

// add custom matchers
expect.extend(matchers);

And it does what I ask it to do, but now that I want to do assertions and I add that, now it does not work.

Antonio contreras
  • 107
  • 1
  • 1
  • 5
  • in case it helps, you may already know, you are using named imports and so the name your are specifying, i.e `expect`, must exactly match the name of a value that is being exported from the module `@playwright/test`. so 1) is the file really named `@playwright/test`? and 2) is it exporting the value `expect`? – user1063287 Dec 02 '21 at 21:44
  • Hi, I went into the node modules, playwright -> test -> expect.js and found this const expect = _expect.default; exports.expect = expect; So I think they are correct? I also forgot to mention that I use an M1 Pro and there was a time where I had an issue with importing as well but Idk if the M1 Pro has anything to do – Antonio contreras Dec 02 '21 at 21:53
  • yes, sorry, I wasn’t familiar with this package, it looks like it should work, if you followed all instructions here: https://playwright.dev/docs/intro/ – user1063287 Dec 02 '21 at 21:54
  • 1
    No need to apologize, I am doing this a bit different and not using the npx command just `npm run test` and just doing everything on the main.js so that's why I was curious about why this error is happening. – Antonio contreras Dec 02 '21 at 22:01

3 Answers3

5
  1. You have to install @playwright/test library:
   npm i -D @playwright/test
  1. Do not use playwright-expect library. Playwright already includes web-first assertions. Hence, there is no reason to use an additional library to extend expect.

  2. Remove unused code:

const { matchers } = require('playwright-expect');
console.log("##########", expect)

// add custom matchers
expect.extend(matchers);
Yevhen Laichenkov
  • 7,746
  • 2
  • 27
  • 33
  • In my case, `node_modules/@playwright` remained empty when installed with `Yarn`. `npm` did the job – Jean Claveau Jun 06 '22 at 23:59
  • This seems related to https://github.com/microsoft/playwright/issues/9819 as I already have `playwright` as a dep of Storybook – Jean Claveau Jun 07 '22 at 00:14
  • I finally resolved it: there was a `test` line in my `.yarnclean` so `@playwright/test` was removed during the `cleaning modules` step of Yarn – Jean Claveau Jun 07 '22 at 06:32
0

I've created an issue about the same question here https://github.com/microsoft/playwright/issues/14971 and I'll update the result when it's answered.

Pablo LION
  • 1,294
  • 6
  • 20
0

While trying to use playwright as a library i did run into the same error

Error: Cannot find module '@playwright/test'

To fix this error in my case the following needed to be done. The pre-condition is that playwright is already installed.

Create a node js project

  1. Create a folder: mkdir HelloLibrary
  2. Inside this folder create a file from the command prompt: echo var msg = 'Hello World'; console.log(msg); > app.js
  3. Open a command prompt and run node app.js
  4. Add playwright npm i -D playwright
  5. Change content of app.js like code sample below
  6. Run it again node app.js
  7. Voila done

App.js sample

This code launches a browser and takes a screenshot

const { chromium } = require('playwright');

(async () => {
 
 const browser = await chromium.launch({ headless: false, slowMo: 50 });
 const page = await browser.newPage();
 await page.goto('http://whatsmyuseragent.org/');
 await page.screenshot({ path: `example.png` });
 await browser.close();
})();

See also

  1. Tutorial: Node.js for Beginners
  2. Use playwright as a library
surfmuggle
  • 5,527
  • 7
  • 48
  • 77